Aleph-w 3.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
81# include <ahFunctional.H>
82# include <ah-errors.H>
83
84namespace Aleph
85{
86
87template<typename T> class DynList;
88
113# define Special_Ctors(Set_Type, Type) \
114 template <template <typename> class List> \
115 Set_Type(const List<Type> & l) : Set_Type() \
116 { \
117 l.for_each([this] (const Type & item) { (void)this->append(item); }); \
118 } \
119 \
120 template <class It> \
121 Set_Type(It b, It e) : Set_Type() \
122 { \
123 for (It it = b; it != e; ++it) \
124 (void)this->append(*it); \
125 } \
126 \
127 Set_Type(std::initializer_list<Type> l) : Set_Type() \
128 { \
129 for (const auto & item : l) \
130 (void)this->append(item); \
131 }
132
139# define Generic_Items(Type) \
140 template <template <typename> class Container = DynList> \
141 Container<Type> items() const \
142 { \
143 return this->template maps<Type, Container> ([] (const Type & key) \
144 { return key; }); \
145 }
146
151# define Generate_Proxy_Operator(Class_Name) \
152 const Data & operator [] (const Key & key) const \
153 { \
154 return find(key); \
155 } \
156 \
157 Data & operator [] (const Key & key) \
158 { \
159 return find(key); \
160 }
161
162
163// ============================================================================
164// Utility functions and classes
165// ============================================================================
166
171template<typename Type> inline
172std::string to_str(const Type &d)
173{
174 std::ostringstream os;
175 os << d;
176 return os.str();
177}
178
188template<typename Key, typename Data, class Cmp = std::equal_to<Key>>
190{
191 Cmp cmp;
192 Dft_Pair_Cmp(Cmp __cmp = Cmp()) : cmp(__cmp) {}
193
195 bool operator()(const std::pair<Key, Data> &p1,
196 const std::pair<Key, Data> &p2) const noexcept
197 {
198 return cmp(p1.first, p2.first);
199 }
200
202 bool operator()(const std::pair<Key, Data> &p,
203 const Key &k) const noexcept
204 {
205 return cmp(p.first, k);
206 }
207
209 bool operator()(const Key &k,
210 const std::pair<Key, Data> &p) const noexcept
211 {
212 return cmp(k, p.first);
213 }
214};
215
220template<typename Key, typename Data>
221std::pair<Key, Data> *key_to_pair(Key *ptr)
222{
223 using PairType = std::pair<Key, Data>;
224 return reinterpret_cast<PairType *>(
225 reinterpret_cast<char *>(ptr) - offsetof(PairType, first));
226}
227
229template<typename Key, typename Data>
230const std::pair<Key, Data> *key_to_pair(const Key *ptr)
231{
232 using PairType = std::pair<Key, Data>;
233 return reinterpret_cast<const PairType *>(
234 reinterpret_cast<const char *>(ptr) - offsetof(PairType, first));
235}
236
241template<typename Key, typename Data>
242std::pair<Key, Data> *data_to_pair(Data *ptr)
243{
244 using PairType = std::pair<Key, Data>;
245 return reinterpret_cast<PairType *>(
246 reinterpret_cast<char *>(ptr) - offsetof(PairType, second));
247}
248
250template<typename Key, typename Data>
251const std::pair<Key, Data> *data_to_pair(const Data *ptr)
252{
253 using PairType = std::pair<Key, Data>;
254 return reinterpret_cast<const PairType *>(
255 reinterpret_cast<const char *>(ptr) - offsetof(PairType, second));
256}
257
258} // end namespace Aleph
259
260# 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.
Definition ah-arena.H:89
std::pair< Key, Data > * key_to_pair(Key *ptr)
Converts a pointer to Key to a pointer to the containing pair.
Definition ahDry.H:221
std::pair< Key, Data > * data_to_pair(Data *ptr)
Converts a pointer to Data to a pointer to the containing pair.
Definition ahDry.H:242
std::string to_str(const double d)
Convert double to a std::string with maximum round-trip precision.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Default comparator for pair types in hash maps.
Definition ahDry.H:190
bool operator()(const Key &k, const std::pair< Key, Data > &p) const noexcept
Heterogeneous: Key vs Pair.
Definition ahDry.H:209
Dft_Pair_Cmp(Cmp __cmp=Cmp())
Definition ahDry.H:192
bool operator()(const std::pair< Key, Data > &p, const Key &k) const noexcept
Heterogeneous: Pair vs Key.
Definition ahDry.H:202
bool operator()(const std::pair< Key, Data > &p1, const std::pair< Key, Data > &p2) const noexcept
Pair vs Pair comparison.
Definition ahDry.H:195