Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-mapping.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
44#ifndef AH_MAPPING_H
45#define AH_MAPPING_H
46
47#include <ah-errors.H>
48#include <tpl_dynMapTree.H>
49
50namespace Aleph
51{
68 template <typename Key, typename ValueType>
70 {
72
73 public:
74 using key_type = Key;
75 using mapped_type = ValueType;
76 using value_type = std::pair<const Key, ValueType>;
77 using size_type = size_t;
78
82 AHMapping() = default;
83
88 AHMapping(const AHMapping & other) = default;
89
99 AHMapping(AHMapping && other) = default;
100
110 template <typename... Pairs>
111 AHMapping(const Key & key, const ValueType & value, Pairs... pairs)
112 : AHMapping(pairs...)
113 {
114 tbl.insert(key, value);
115 }
116
122 AHMapping &operator=(const AHMapping & other) = default;
123
129 AHMapping &operator=(AHMapping && other) noexcept = default;
130
136 void insert(const Key & key, const ValueType & value)
137 {
138 tbl.insert(key, value);
139 }
140
147 const ValueType &operator[](const Key & key) const
148 {
149 auto ptr = tbl.search(key);
150 ah_domain_error_if(ptr == nullptr) << "Key not found: " << key;
151 return ptr->second;
152 }
153
160 ValueType &operator[](const Key & key)
161 {
162 return tbl[key];
163 }
164
172 {
174 for (auto it = tbl.get_it(); it.has_curr(); it.next())
175 {
176 auto curr = it.get_curr();
177 ret.insert(curr.second, curr.first);
178 }
179 return ret;
180 }
181
187 bool remove(const Key & key) noexcept
188 {
189 try
190 {
191 tbl.remove(key);
192 return true;
193 }
194 catch (...)
195 {
196 return false;
197 }
198 }
199
205 bool valid_key(const Key & key) const noexcept
206 {
207 return tbl.has(key);
208 }
209
215 {
216 return tbl.keys();
217 }
218
224 {
225 DynList<ValueType> result;
226 for (auto it = tbl.get_it(); it.has_curr(); it.next())
227 result.append(it.get_curr().second);
228 return result;
229 }
230
235 size_t size() const noexcept
236 {
237 return tbl.size();
238 }
239
244 bool empty() const noexcept
245 {
246 return size() == 0;
247 }
248
252 void clear() noexcept
253 {
254 tbl.empty();
255 }
256
262 bool contains_value(const ValueType & value) const
263 {
264 for (auto it = tbl.get_it(); it.has_curr(); it.next())
265 if (it.get_curr().second == value)
266 return true;
267 return false;
268 }
269
275 template <typename F>
276 void for_each(F f) const
277 {
278 static_assert(std::is_invocable_v<F, const Key &, const ValueType &>,
279 "Function must take (const Key&, const ValueType&) parameters");
280
281 for (auto it = tbl.get_it(); it.has_curr(); it.next())
282 {
283 const auto & pair = it.get_curr();
284 f(pair.first, pair.second);
285 }
286 }
287 };
288} // end namespace Aleph
289
290#endif // AH_MAPPING_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
size_t size_t int32_t value
Definition ca-c-api.h:116
A generic key-value mapping container with inverse operation support.
Definition ah-mapping.H:70
bool remove(const Key &key) noexcept
Removes the key-value pair with the given key.
Definition ah-mapping.H:187
DynList< ValueType > values() const
Gets all values in the mapping.
Definition ah-mapping.H:223
size_t size_type
Type for size-related operations.
Definition ah-mapping.H:77
AHMapping(const Key &key, const ValueType &value, Pairs... pairs)
Constructs a mapping from a list of key-value pairs.
Definition ah-mapping.H:111
AHMapping()=default
Default constructor.
AHMapping(AHMapping &&other)=default
Move constructor.
size_t size() const noexcept
Gets the number of key-value pairs in the mapping.
Definition ah-mapping.H:235
AHMapping(const AHMapping &other)=default
Copy constructor.
bool valid_key(const Key &key) const noexcept
Checks if a key exists in the mapping.
Definition ah-mapping.H:205
Key key_type
Type of the keys.
Definition ah-mapping.H:74
AHMapping< ValueType, Key > inverse() const
Creates a new mapping with keys and values swapped.
Definition ah-mapping.H:171
AHMapping & operator=(AHMapping &&other) noexcept=default
Move assignment operator.
DynMapTree< Key, ValueType > tbl
Definition ah-mapping.H:71
const ValueType & operator[](const Key &key) const
Accesses the value associated with the given key.
Definition ah-mapping.H:147
ValueType mapped_type
Type of the mapped values.
Definition ah-mapping.H:75
bool contains_value(const ValueType &value) const
Checks if the mapping contains a specific value.
Definition ah-mapping.H:262
ValueType & operator[](const Key &key)
Accesses the value associated with the given key.
Definition ah-mapping.H:160
std::pair< const Key, ValueType > value_type
Type of the key-value pairs.
Definition ah-mapping.H:76
void insert(const Key &key, const ValueType &value)
Inserts or updates a key-value pair in the mapping.
Definition ah-mapping.H:136
void for_each(F f) const
Applies a function to each key-value pair.
Definition ah-mapping.H:276
void clear() noexcept
Removes all key-value pairs from the mapping.
Definition ah-mapping.H:252
AHMapping & operator=(const AHMapping &other)=default
Copy assignment operator.
bool empty() const noexcept
Checks if the mapping is empty.
Definition ah-mapping.H:244
DynList< Key > keys() const
Gets all keys in the mapping.
Definition ah-mapping.H:214
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Generic key-value map implemented on top of a binary search tree.
Data remove(const Key &key)
Deletes the pair key,data
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
DynList< Key > keys() const
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
bool has(const Key &key) const noexcept(nothrow_search)
Return whether key is in the map.
const size_t & size() const
Returns the cardinality of the set.
void empty()
Remove all the elements of the set.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Main namespace for Aleph-w library functions.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
Dynamic key-value map based on balanced binary search trees.