Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynarray_set.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
39# ifndef TPL_DYNARRAY_SET_H
40# define TPL_DYNARRAY_SET_H
41
42# include <stdexcept>
43# include <utility>
44
45# include <tpl_dynArray.H>
46# include <tpl_sort_utils.H>
47 # include <ah-errors.H>
48
49namespace Aleph
50{
51
74 template <typename T, class Equal = Aleph::equal_to<T>>
75 class DynArray_Set : public DynArray<T>
76 {
78
79 Equal eq;
80
81 public:
82 using Item_Type = T;
83 using Key_Type = T;
84
85 using Base::Base;
87
92 explicit DynArray_Set(Equal __eq) : Base(), eq(std::move(__eq))
93 {
94 // empty
95 }
96
103 DynArray_Set(Equal __eq, const size_t dim) : Base(dim), eq(std::move(__eq))
104 {
105 // empty
106 }
107
116 DynArray_Set(Equal __eq, size_t _pow_dir, size_t _pow_seg, size_t _pow_block)
117 : Base(_pow_dir, _pow_seg, _pow_block), eq(std::move(__eq))
118 {
119 // empty
120 }
121
123 const Equal & get_equal() const noexcept { return eq; }
124
130 void set_equal(Equal __eq) { eq = std::move(__eq); }
131
137 T * insert(const T & item)
138 {
139 return &this->append(item);
140 }
141
143 T * insert(T && item)
144 {
145 return &this->append(std::forward<T>(item));
146 }
147
152 T * search(const T & key) noexcept
153 {
154 for (size_t i = 0; i < this->size(); ++i)
155 if (eq(this->access(i), key))
156 return &this->access(i);
157 return nullptr;
158 }
159
161 const T * search(const T & key) const noexcept
162 {
163 return const_cast<DynArray_Set *>(this)->search(key);
164 }
165
170 bool contains(const T & key) const noexcept
171 {
172 return search(key) != nullptr;
173 }
174
179 T & find(const T & key)
180 {
181 auto * p = search(key);
182 ah_domain_error_if(p == nullptr) << "key not found";
183 return *p;
184 }
185
187 const T & find(const T & key) const
188 {
189 return const_cast<DynArray_Set *>(this)->find(key);
190 }
191
196 size_t count(const T & key) const noexcept
197 {
198 size_t n = 0;
199 for (size_t i = 0; i < this->size(); ++i)
200 if (eq(this->access(i), key))
201 ++n;
202 return n;
203 }
204
211 bool remove_one(const T & key)
212 {
213 auto * p = search(key);
214 if (p == nullptr)
215 return false;
216 this->remove(*p);
217 return true;
218 }
219
224 size_t remove_all(const T & key)
225 {
226 size_t removed = 0;
227 for (size_t i = 0; i < this->size();)
228 {
229 if (eq(this->access(i), key))
230 {
231 this->remove(this->access(i));
232 ++removed;
233 }
234 else
235 ++i;
236 }
237 return removed;
238 }
239 };
240
241}
242
243# endif // TPL_DYNARRAY_SET_H
244
#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
Set-like container backed by a dynamic array.
T & find(const T &key)
Find and return a reference to the first matching element.
T * insert(T &&item)
See insert(const T &).
const T & find(const T &key) const
See find(const T &).
DynArray_Set(Equal __eq, size_t _pow_dir, size_t _pow_seg, size_t _pow_block)
Construct a set using custom DynArray layout parameters and a custom equality predicate.
T * search(const T &key) noexcept
Search for the first element equal to key.
size_t count(const T &key) const noexcept
Count how many occurrences of key exist.
DynArray_Set(Equal __eq, const size_t dim)
Construct a set with a given initial dimension using a custom equality predicate.
bool remove_one(const T &key)
Remove a single occurrence of key.
size_t remove_all(const T &key)
Remove all occurrences of key.
DynArray_Set(Equal __eq)
Construct an empty set using a custom equality predicate.
bool contains(const T &key) const noexcept
Check whether key is present.
const Equal & get_equal() const noexcept
Return the equality predicate used by this container.
const T * search(const T &key) const noexcept
See search(const T &).
void set_equal(Equal __eq)
Replace the equality predicate used by this container.
T * insert(const T &item)
Insert item (duplicates are allowed).
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4130
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
STL namespace.
Lazy and scalable dynamic array implementation.
Comprehensive sorting algorithms and search utilities for Aleph-w.