Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_scope.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
46#ifndef TPL_SCOPE_H
47#define TPL_SCOPE_H
48
49#include <concepts>
50#include <cstddef>
51#include <utility>
52
53#include <ah-errors.H>
54#include <tpl_dynArray.H>
55
56namespace Aleph {
85template <typename Key, typename Value>
86 requires std::equality_comparable<Key>
87class Scope
88{
89public:
94 struct Binding
95 {
96 Key key;
98 };
99
100private:
105
107
108 static Value *find_value(DynArray<Binding> &bindings, const Key &key) noexcept
109 {
110 for (size_t i = bindings.size(); i > 0; --i)
111 if (bindings.access(i - 1).key == key)
112 return &bindings.access(i - 1).value;
113 return nullptr;
114 }
115
116 static const Value *find_value(const DynArray<Binding> &bindings, const Key &key) noexcept
117 {
118 for (size_t i = bindings.size(); i > 0; --i)
119 if (bindings.access(i - 1).key == key)
120 return &bindings.access(i - 1).value;
121 return nullptr;
122 }
123
125 {
126 ah_runtime_error_unless(frames.size() > 0) << "Scope::current_frame(): no active scope";
127 return frames.top();
128 }
129
130 const Frame &current_frame() const
131 {
132 ah_runtime_error_unless(frames.size() > 0) << "Scope::current_frame(): no active scope";
133 return frames.top();
134 }
135
136public:
141 Scope() = default;
142
150 {
151 return frames.size();
152 }
153
159 {
160 return frames.is_empty();
161 }
162
168 {
169 frames.clear();
170 }
171
179 size_t enter_scope()
180 {
181 // Reinitialize reused slots explicitly because DynArray::pop() moves the
182 // last frame out before shrinking, and a later append may reuse that
183 // moved-from storage.
184 frames.append(Frame());
185 return frames.size();
186 }
187
196 {
197 ah_runtime_error_unless(frames.size() > 0) << "Scope::leave_scope(): no active scope";
198 (void) frames.pop();
199 }
200
215 bool insert(const Key &key, const Value &value)
216 {
217 auto &frame = current_frame();
218 if (find_value(frame.bindings, key) != nullptr)
219 return false;
220 frame.bindings.append({key, value});
221 return true;
222 }
223
229 bool insert(Key &&key, Value &&value)
230 {
231 auto &frame = current_frame();
232 if (find_value(frame.bindings, key) != nullptr)
233 return false;
234 frame.bindings.append({std::move(key), std::move(value)});
235 return true;
236 }
237
246 Value *lookup_local(const Key &key) noexcept
247 {
248 if (frames.is_empty())
249 return nullptr;
250 return find_value(frames.top().bindings, key);
251 }
252
254 const Value *lookup_local(const Key &key) const noexcept
255 {
256 if (frames.is_empty())
257 return nullptr;
258 return find_value(frames.top().bindings, key);
259 }
260
271 Value *lookup(const Key &key) noexcept
272 {
273 for (size_t i = frames.size(); i > 0; --i)
274 if (auto *value = find_value(frames.access(i - 1).bindings, key); value != nullptr)
275 return value;
276 return nullptr;
277 }
278
280 const Value *lookup(const Key &key) const noexcept
281 {
282 for (size_t i = frames.size(); i > 0; --i)
283 if (const auto *value = find_value(frames.access(i - 1).bindings, key); value != nullptr)
284 return value;
285 return nullptr;
286 }
287};
288} // namespace Aleph
289
290#endif // TPL_SCOPE_H
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:255
size_t size_t int32_t value
Definition ca-c-api.h:116
Lexical scope stack for Key to Value associations.
Definition tpl_scope.H:88
void clear() noexcept
Resets the structure by removing all frames and bindings.
Definition tpl_scope.H:167
size_t enter_scope()
Creates a new visibility level.
Definition tpl_scope.H:179
static const Value * find_value(const DynArray< Binding > &bindings, const Key &key) noexcept
Definition tpl_scope.H:116
Value * lookup(const Key &key) noexcept
Searches for a key traversing all active scopes (from inside out).
Definition tpl_scope.H:271
bool empty() const noexcept
Indicates if the structure is empty of frames.
Definition tpl_scope.H:158
bool insert(const Key &key, const Value &value)
Registers a new association in the current scope.
Definition tpl_scope.H:215
const Value * lookup_local(const Key &key) const noexcept
Constant version of lookup_local().
Definition tpl_scope.H:254
Value * lookup_local(const Key &key) noexcept
Searches for a key only in the innermost (current) scope.
Definition tpl_scope.H:246
DynArray< Frame > frames
Definition tpl_scope.H:106
void leave_scope()
Destroys the current visibility level and all its associations.
Definition tpl_scope.H:195
Scope()=default
Default constructor.
bool insert(Key &&key, Value &&value)
Move version of insert().
Definition tpl_scope.H:229
const Frame & current_frame() const
Definition tpl_scope.H:130
Frame & current_frame()
Definition tpl_scope.H:124
const Value * lookup(const Key &key) const noexcept
Constant version of lookup().
Definition tpl_scope.H:280
size_t depth() const noexcept
Queries the current depth of the scope stack.
Definition tpl_scope.H:149
static Value * find_value(DynArray< Binding > &bindings, const Key &key) noexcept
Definition tpl_scope.H:108
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
@ Value
Regular runtime payload stored as Interpreter_Value.
Individual entry in a lexical frame.
Definition tpl_scope.H:95
Key key
Search key (e.g., variable name).
Definition tpl_scope.H:96
Value value
Associated value (e.g., type or address).
Definition tpl_scope.H:97
DynArray< Binding > bindings
Definition tpl_scope.H:103
Lazy and scalable dynamic array implementation.