Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahDry.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
72# ifndef AHDRY_H
73# define AHDRY_H
74
75# include <cstddef>
76# include <tuple>
77# include <functional>
78# include <sstream>
79# include <initializer_list>
80# include <type_traits>
81
82# include <ahFunctional.H>
83# include <ah-errors.H>
84
85namespace Aleph
86{
87
88template<typename T> class DynList;
89
123# define Special_Ctors(Set_Type, Type) \
124 template <template <typename> class List> \
125 Set_Type(const List<Type> & l) : Set_Type() \
126 { \
127 l.for_each([this] (const Type & item) { (void)this->append(item); }); \
128 } \
129 \
130 template <class It> \
131 requires (not std::is_arithmetic_v<It> \
132 and requires (It it) { *it; ++it; it != it; }) \
133 Set_Type(It b, It e) : Set_Type() \
134 { \
135 for (It it = b; it != e; ++it) \
136 (void)this->append(*it); \
137 } \
138 \
139 Set_Type(std::initializer_list<Type> l) : Set_Type() \
140 { \
141 for (const auto & item : l) \
142 (void)this->append(item); \
143 }
144
151# define Generic_Items(Type) \
152 template <template <typename> class Container = DynList> \
153 Container<Type> items() const \
154 { \
155 return this->template maps<Type, Container> ([] (const Type & key) \
156 { return key; }); \
157 }
158
163# define Generate_Proxy_Operator(Class_Name) \
164 const Data & operator [] (const Key & key) const \
165 { \
166 return find(key); \
167 } \
168 \
169 Data & operator [] (const Key & key) \
170 { \
171 return find(key); \
172 }
173
174
175// ============================================================================
176// Utility functions and classes
177// ============================================================================
178
183template<typename Type> inline
184std::string to_str(const Type &d)
185{
186 std::ostringstream os;
187 os << d;
188 return os.str();
189}
190
204template<typename Key, typename Data, class Cmp = std::equal_to<Key>>
206{
208
221 Dft_Pair_Cmp() noexcept(std::is_nothrow_default_constructible_v<Cmp>)
222 : cmp() {}
223
231 Dft_Pair_Cmp(Cmp __cmp) noexcept(std::is_nothrow_constructible_v<Cmp, Cmp &>)
232 : cmp(__cmp) {}
233
235 bool operator()(const std::pair<Key, Data> &p1,
236 const std::pair<Key, Data> &p2) const
237 noexcept(std::is_nothrow_invocable_r_v<bool, const Cmp &,
238 const Key &, const Key &>)
239 {
240 return cmp(p1.first, p2.first);
241 }
242
244 bool operator()(const std::pair<Key, Data> &p,
245 const Key &k) const
246 noexcept(std::is_nothrow_invocable_r_v<bool, const Cmp &,
247 const Key &, const Key &>)
248 {
249 return cmp(p.first, k);
250 }
251
253 bool operator()(const Key &k,
254 const std::pair<Key, Data> &p) const
255 noexcept(std::is_nothrow_invocable_r_v<bool, const Cmp &,
256 const Key &, const Key &>)
257 {
258 return cmp(k, p.first);
259 }
260};
261
266template<typename Key, typename Data>
267std::pair<Key, Data> *key_to_pair(Key *ptr)
268{
269 using PairType = std::pair<Key, Data>;
270 return reinterpret_cast<PairType *>(
271 reinterpret_cast<char *>(ptr) - offsetof(PairType, first));
272}
273
275template<typename Key, typename Data>
276const std::pair<Key, Data> *key_to_pair(const Key *ptr)
277{
278 using PairType = std::pair<Key, Data>;
279 return reinterpret_cast<const PairType *>(
280 reinterpret_cast<const char *>(ptr) - offsetof(PairType, first));
281}
282
287template<typename Key, typename Data>
288std::pair<Key, Data> *data_to_pair(Data *ptr)
289{
290 using PairType = std::pair<Key, Data>;
291 return reinterpret_cast<PairType *>(
292 reinterpret_cast<char *>(ptr) - offsetof(PairType, second));
293}
294
296template<typename Key, typename Data>
297const std::pair<Key, Data> *data_to_pair(const Data *ptr)
298{
299 using PairType = std::pair<Key, Data>;
300 return reinterpret_cast<const PairType *>(
301 reinterpret_cast<const char *>(ptr) - offsetof(PairType, second));
302}
303
304} // end namespace Aleph
305
306# endif // AHDRY_H
Exception handling system with formatted messages for Aleph-w.
Functional programming utilities for Aleph-w containers.
Main namespace for Aleph-w library functions.
std::pair< Key, Data > * key_to_pair(Key *ptr)
Converts a pointer to Key to a pointer to the containing pair.
Definition ahDry.H:267
std::pair< Key, Data > * data_to_pair(Data *ptr)
Converts a pointer to Data to a pointer to the containing pair.
Definition ahDry.H:288
std::string to_str(const double d)
Convert double to a std::string with maximum round-trip precision.
STL namespace.
Default comparator for pair types in hash maps.
Definition ahDry.H:206
bool operator()(const Key &k, const std::pair< Key, Data > &p) const noexcept(std::is_nothrow_invocable_r_v< bool, const Cmp &, const Key &, const Key & >)
Heterogeneous: Key vs Pair; it throws what cmp throws.
Definition ahDry.H:253
bool operator()(const std::pair< Key, Data > &p, const Key &k) const noexcept(std::is_nothrow_invocable_r_v< bool, const Cmp &, const Key &, const Key & >)
Heterogeneous: Pair vs Key; it throws what cmp throws.
Definition ahDry.H:244
Dft_Pair_Cmp() noexcept(std::is_nothrow_default_constructible_v< Cmp >)
Builds the comparator of the keys by default.
Definition ahDry.H:221
bool operator()(const std::pair< Key, Data > &p1, const std::pair< Key, Data > &p2) const noexcept(std::is_nothrow_invocable_r_v< bool, const Cmp &, const Key &, const Key & >)
Pair vs Pair comparison; it throws what cmp throws.
Definition ahDry.H:235
Dft_Pair_Cmp(Cmp __cmp) noexcept(std::is_nothrow_constructible_v< Cmp, Cmp & >)
Wraps the comparator __cmp of the keys.
Definition ahDry.H:231
static int * k