Aleph-w 3.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 <tpl_hash.H>
68# include <ah-errors.H>
69
70namespace Aleph
71{
72
83 template <typename T = int>
84class AlDomain : public Aleph::HashSet<T, SetODhash>
85{
86 mutable bool keys_build = false;
88
89public:
90
92 using Base::Base;
93
101 const DynList<T> & keys() const
102 {
103 if (not keys_build)
104 {
105 domain = sort(this->Base::keys());
106 keys_build = true;
107 }
108 return domain;
109 }
110
115 DynList<T> to_list() const { return this->keys(); }
116
121 std::string to_str() const
122 {
123 return sort(to_list()).template foldl<std::string>
124 ("", [] (const std::string & s, const T & item)
125 {
126 return s + " " + Aleph::to_str(item);
127 });
128 }
129 };
130
139 template <typename T> inline
140 std::ostream & operator << (std::ostream & s, const AlDomain<T> & dom)
141 {
142 return s << dom.to_str();
143 }
144
161 struct IntRange : public AlDomain<int>
162 {
164 IntRange() = delete;
165
180 IntRange(const int start, const int end, const int step = 1)
181 {
182 ah_domain_error_if(step < 0) << "negative step";
183
184 for (long i = start; i <= end; i += step)
185 (void)insert(i);
186 }
187
198 IntRange(const size_t n)
199 {
200 for (size_t i = 0; i < n; ++i)
201 (void)insert(i);
202 }
203
204 };
205
206} // end namespace Aleph
207
208# endif // AL_DOMAIN_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
Generic domain class based on hash set.
Definition al-domain.H:85
const DynList< T > & keys() const
Get sorted list of all elements in the domain.
Definition al-domain.H:101
DynList< T > domain
Cached sorted keys.
Definition al-domain.H:87
DynList< T > to_list() const
Convert domain to sorted list.
Definition al-domain.H:115
bool keys_build
Lazy cache flag.
Definition al-domain.H:86
std::string to_str() const
Convert domain to string representation.
Definition al-domain.H:121
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:107
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:227
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
DynList< T > maps(const C &c, Op op)
Classic map operation.
HashSetTable< Key, Cmp > Base
Definition tpl_hash.H:64
Integer range domain [start, end] with optional step.
Definition al-domain.H:162
IntRange(const int start, const int end, const int step=1)
Construct range [start, end] with step.
Definition al-domain.H:180
IntRange()=delete
Deleted default constructor (range required)
IntRange(const size_t n)
Construct range [0, n-1].
Definition al-domain.H:198
Unified hash table interface.