Aleph-w 3.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;
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
94 AHMapping(AHMapping && other) noexcept = default;
95
105 template <typename... Pairs>
106 AHMapping(const Key & key, const ValueType & value, Pairs... pairs)
107 : AHMapping(pairs...)
108 {
109 tbl.insert(key, value);
110 }
111
117 AHMapping &operator=(const AHMapping & other) = default;
118
124 AHMapping &operator=(AHMapping && other) noexcept = default;
125
131 void insert(const Key & key, const ValueType & value)
132 {
133 tbl.insert(key, value);
134 }
135
142 const ValueType &operator[](const Key & key) const
143 {
144 auto ptr = tbl.search(key);
145 ah_domain_error_if(ptr == nullptr) << "Key not found: " << key;
146 return ptr->second;
147 }
148
155 ValueType &operator[](const Key & key)
156 {
157 return tbl[key];
158 }
159
167 {
169 for (auto it = tbl.get_it(); it.has_curr(); it.next())
170 {
171 auto curr = it.get_curr();
172 ret.insert(curr.second, curr.first);
173 }
174 return ret;
175 }
176
182 bool remove(const Key & key) noexcept
183 {
184 try
185 {
186 tbl.remove(key);
187 return true;
188 }
189 catch (...)
190 {
191 return false;
192 }
193 }
194
200 bool valid_key(const Key & key) const noexcept
201 {
202 return tbl.has(key);
203 }
204
210 {
211 return tbl.keys();
212 }
213
219 {
220 DynList<ValueType> result;
221 for (auto it = tbl.get_it(); it.has_curr(); it.next())
222 result.append(it.get_curr().second);
223 return result;
224 }
225
231 {
232 return tbl.size();
233 }
234
240 {
241 return size() == 0;
242 }
243
248 {
249 tbl.empty();
250 }
251
257 bool contains_value(const ValueType & value) const
258 {
259 for (auto it = tbl.get_it(); it.has_curr(); it.next())
260 if (it.get_curr().second == value)
261 return true;
262 return false;
263 }
264
270 template <typename F>
271 void for_each(F f) const
272 {
273 static_assert(std::is_invocable_v<F, const Key &, const ValueType &>,
274 "Function must take (const Key&, const ValueType&) parameters");
275
276 for (auto it = tbl.get_it(); it.has_curr(); it.next())
277 {
278 const auto & pair = it.get_curr();
279 f(pair.first, pair.second);
280 }
281 }
282 };
283} // end namespace Aleph
284
285#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:522
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:182
DynList< ValueType > values() const
Gets all values in the mapping.
Definition ah-mapping.H:218
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:106
AHMapping()=default
Default constructor.
size_t size() const noexcept
Gets the number of key-value pairs in the mapping.
Definition ah-mapping.H:230
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:200
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:166
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:142
ValueType mapped_type
Type of the mapped values.
Definition ah-mapping.H:75
AHMapping(AHMapping &&other) noexcept=default
Move constructor.
bool contains_value(const ValueType &value) const
Checks if the mapping contains a specific value.
Definition ah-mapping.H:257
ValueType & operator[](const Key &key)
Accesses the value associated with the given key.
Definition ah-mapping.H:155
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:131
void for_each(F f) const
Applies a function to each key-value pair.
Definition ah-mapping.H:271
void clear() noexcept
Removes all key-value pairs from the mapping.
Definition ah-mapping.H:247
AHMapping & operator=(const AHMapping &other)=default
Copy assignment operator.
bool empty() const noexcept
Checks if the mapping is empty.
Definition ah-mapping.H:239
DynList< Key > keys() const
Gets all keys in the mapping.
Definition ah-mapping.H:209
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
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) const noexcept
Collect all keys.
bool has(const Key &key) const noexcept
DynList< Key > keys() const
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
const size_t & size() const
Returns the cardinality of the set.
void empty()
remove all elements from the set
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Dynamic key-value map based on balanced binary search trees.