Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
al-domain.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
64# ifndef AL_DOMAIN_H
65# define AL_DOMAIN_H
66
67# include <concepts>
68# include <limits>
69# include <ostream>
70# include <tpl_hash.H>
71# include <ahSort.H>
72# include <ah-errors.H>
73
74namespace Aleph
75{
76
88template <typename T>
89concept AlDomainKey = std::default_initializable<T> and std::copyable<T>
90 and std::equality_comparable<T>;
91
97template <typename T>
98concept AlOrderedDomainKey = AlDomainKey<T> and requires (const T & a, const T & b)
99{
100 { a < b } -> std::convertible_to<bool>;
101};
102
108template <typename T>
109concept AlStreamable = requires (std::ostream & s, const T & x)
110{
111 s << x;
112};
113
129 template <typename T = int>
130 requires AlDomainKey<T>
131class AlDomain : public Aleph::HashSet<T, SetODhash>
132{
133 mutable bool keys_build = false;
135
136public:
137
139 using Base::Base;
141
151 const DynList<T> & keys() const requires AlOrderedDomainKey<T>
152 {
153 if (not keys_build or domain.size() != this->size())
154 {
155 domain = sort(this->Base::keys());
156 keys_build = true;
157 }
158 return domain;
159 }
160
162 [[nodiscard]] T * insert(const T & key)
163 {
164 keys_build = false;
165 return Base::insert(key);
166 }
167
169 [[nodiscard]] T * insert(T && key)
170 {
171 keys_build = false;
172 return Base::insert(std::move(key));
173 }
174
177 [[nodiscard]] T * search_or_insert(const T & key)
178 {
179 keys_build = false;
180 return Base::search_or_insert(key);
181 }
182
184 [[nodiscard]] T * search_or_insert(T && key)
185 {
186 keys_build = false;
187 return Base::search_or_insert(std::move(key));
188 }
189
192 [[nodiscard]] std::pair<T *, bool> contains_or_insert(const T & key)
193 {
194 keys_build = false;
195 return Base::contains_or_insert(key);
196 }
197
199 [[nodiscard]] std::pair<T *, bool> contains_or_insert(T && key)
200 {
201 keys_build = false;
202 return Base::contains_or_insert(std::move(key));
203 }
204
206 [[nodiscard]] T * append(const T & key)
207 {
208 keys_build = false;
209 return Base::append(key);
210 }
211
213 [[nodiscard]] T * append(T && key)
214 {
215 keys_build = false;
216 return Base::append(std::move(key));
217 }
218
220 void add(const DynList<T> & l)
221 {
222 keys_build = false;
223 Base::add(l);
224 }
225
227 void remove(const T & key)
228 {
229 keys_build = false;
230 Base::remove(key);
231 }
232
235 void remove_ptr(T * key)
236 {
237 keys_build = false;
238 Base::remove_ptr(key);
239 }
240
242 void empty()
243 {
244 keys_build = false;
245 Base::empty();
246 }
247
249 void clear() { empty(); }
250
252 void swap(AlDomain & other) noexcept
253 {
254 Base::swap(other);
255 std::swap(keys_build, other.keys_build);
256 domain.swap(other.domain);
257 }
258
263 DynList<T> to_list() const requires AlOrderedDomainKey<T> { return this->keys(); }
264
269 std::string to_str() const requires AlOrderedDomainKey<T> and AlStreamable<T>
270 {
271 return sort(to_list()).template foldl<std::string>
272 ("", [] (const std::string & s, const T & item)
273 {
274 return s + " " + Aleph::to_str(item);
275 });
276 }
277 };
278
287 template <typename T>
288 requires AlOrderedDomainKey<T> and AlStreamable<T>
289 inline std::ostream & operator << (std::ostream & s, const AlDomain<T> & dom)
290 {
291 return s << dom.to_str();
292 }
293
310 struct IntRange : public AlDomain<int>
311 {
313 IntRange() = delete;
314
329 IntRange(const int start, const int end, const int step = 1)
330 {
331 ah_domain_error_if(step <= 0) << "the step must be positive";
332
333 // long long, so that i + step does not overflow where long has 32
334 // bits, as on Windows, when end is near the maximum of int
335 for (long long i = start; i <= end; i += step)
336 (void)insert(static_cast<int>(i));
337 }
338
352 IntRange(const size_t n)
353 {
354 ah_domain_error_if(n > static_cast<size_t>(std::numeric_limits<int>::max()) + 1)
355 << "IntRange(n): n - 1 is above the maximum of int";
356
357 for (size_t i = 0; i < n; ++i)
358 (void)insert(static_cast<int>(i));
359 }
360
361 };
362
363} // end namespace Aleph
364
365# endif // AL_DOMAIN_H
#define Derived_Build(Name)
Declare build() in a class derived from a container.
Definition ah-dry.H:2881
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
High-level sorting functions for Aleph containers.
Generic domain class based on hash set.
Definition al-domain.H:132
std::pair< T *, bool > contains_or_insert(T &&key)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition al-domain.H:199
T * insert(const T &key)
Insert key, as HashSet::insert(), and forget the sorted keys.
Definition al-domain.H:162
void swap(AlDomain &other) noexcept
Swap the keys, and the sorted keys, with other.
Definition al-domain.H:252
T * search_or_insert(const T &key)
Search or insert key, as HashSet::search_or_insert(), and forget the sorted keys.
Definition al-domain.H:177
T * append(T &&key)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition al-domain.H:213
DynList< T > domain
Cached sorted keys.
Definition al-domain.H:134
T * insert(T &&key)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition al-domain.H:169
void add(const DynList< T > &l)
Insert the keys of l, as HashSet::add(), and forget the sorted keys.
Definition al-domain.H:220
DynList< T > to_list() const
Convert domain to sorted list.
Definition al-domain.H:263
T * append(const T &key)
Insert key, as HashSet::append(), and forget the sorted keys.
Definition al-domain.H:206
std::string to_str() const and AlStreamable< T >
Convert domain to string representation.
Definition al-domain.H:269
bool keys_build
Lazy cache flag.
Definition al-domain.H:133
void clear()
Remove every key, as HashSet::clear(), and forget the sorted keys.
Definition al-domain.H:249
const DynList< T > & keys() const
Get sorted list of all elements in the domain.
Definition al-domain.H:151
void remove(const T &key)
Remove key, as HashSet::remove(), and forget the sorted keys.
Definition al-domain.H:227
void remove_ptr(T *key)
Remove the key that key points to, as HashSet::remove_ptr(), and forget the sorted keys.
Definition al-domain.H:235
T * search_or_insert(T &&key)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition al-domain.H:184
std::pair< T *, bool > contains_or_insert(const T &key)
Search or insert key, as HashSet::contains_or_insert(), and forget the sorted keys.
Definition al-domain.H:192
void empty()
Remove every key, as HashSet::empty(), and forget the sorted keys.
Definition al-domain.H:242
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
DynList & swap(DynList &l) noexcept
Definition htlist.H:1253
A key of a domain: of an AlDomain, and so an index of a Vector or of a Matrix.
Definition al-domain.H:89
A key of a domain that < orders: what AlDomain::keys(), and the members that take the keys in their o...
Definition al-domain.H:98
A value that << writes to a stream: what the members that print keys or numbers need.
Definition al-domain.H:109
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
and
Check uniqueness with explicit hash + equality functors.
std::string to_str(const double d)
Convert double to a std::string with maximum round-trip precision.
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
Definition ahField.H:121
void add(const DynList< Key > &l)
Definition tpl_hash.H:68
HashSetTable< Key, Cmp > Base
Definition tpl_hash.H:64
Integer range domain [start, end] with optional step.
Definition al-domain.H:311
IntRange(const int start, const int end, const int step=1)
Construct range [start, end] with step.
Definition al-domain.H:329
IntRange()=delete
Deleted default constructor (range required)
IntRange(const size_t n)
Construct range [0, n-1].
Definition al-domain.H:352
Unified hash table interface.
DynList< int > l