Aleph-w 3.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
70 template <typename T, class Equal = Aleph::equal_to<T>>
71 class DynArray_Set : public DynArray<T>
72 {
74
75 Equal eq;
76
77 public:
78 using Item_Type = T;
79 using Key_Type = T;
80
81 using Base::Base;
82
87 explicit DynArray_Set(Equal __eq) : Base(), eq(std::move(__eq))
88 {
89 // empty
90 }
91
98 DynArray_Set(Equal __eq, const size_t dim) : Base(dim), eq(std::move(__eq))
99 {
100 // empty
101 }
102
111 DynArray_Set(Equal __eq, size_t _pow_dir, size_t _pow_seg, size_t _pow_block)
113 {
114 // empty
115 }
116
118 const Equal & get_equal() const noexcept { return eq; }
119
125 void set_equal(Equal __eq) { eq = std::move(__eq); }
126
132 T * insert(const T & item)
133 {
134 return &this->append(item);
135 }
136
138 T * insert(T && item)
139 {
140 return &this->append(std::forward<T>(item));
141 }
142
147 T * search(const T & key) noexcept
148 {
149 for (size_t i = 0; i < this->size(); ++i)
150 if (eq(this->access(i), key))
151 return &this->access(i);
152 return nullptr;
153 }
154
156 const T * search(const T & key) const noexcept
157 {
158 return const_cast<DynArray_Set *>(this)->search(key);
159 }
160
165 bool contains(const T & key) const noexcept
166 {
167 return search(key) != nullptr;
168 }
169
174 T & find(const T & key)
175 {
176 auto * p = search(key);
177 ah_domain_error_if(p == nullptr) << "key not found";
178 return *p;
179 }
180
182 const T & find(const T & key) const
183 {
184 return const_cast<DynArray_Set *>(this)->find(key);
185 }
186
191 size_t count(const T & key) const noexcept
192 {
193 size_t n = 0;
194 for (size_t i = 0; i < this->size(); ++i)
195 if (eq(this->access(i), key))
196 ++n;
197 return n;
198 }
199
206 bool remove_one(const T & key)
207 {
208 auto * p = search(key);
209 if (p == nullptr)
210 return false;
211 this->remove(*p);
212 return true;
213 }
214
219 size_t remove_all(const T & key)
220 {
221 size_t removed = 0;
222 for (size_t i = 0; i < this->size();)
223 {
224 if (eq(this->access(i), key))
225 {
226 this->remove(this->access(i));
227 ++removed;
228 }
229 else
230 ++i;
231 }
232 return removed;
233 }
234 };
235
236}
237
238# endif // TPL_DYNARRAY_SET_H
239
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
Set-like container backed by a dynamic array.
const T & find(const T &key) const
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.
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.
T & find(const T &key)
Find and return a reference to the first matching element.
T * insert(const T &item)
Insert item (duplicates are allowed).
bool remove_one(const T &key)
Remove a single occurrence of key.
size_t remove_all(const T &key)
Remove all occurrences of key.
T * search(const T &key) noexcept
Search for the first element equal to 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
void set_equal(Equal __eq)
Replace the equality predicate used by this container.
void remove(T &item)
Given a valid reference to an item in the array, it removes it and decrease the dimension.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
__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:4052
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
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Lazy and scalable dynamic array implementation.
Comprehensive sorting algorithms and search utilities for Aleph-w.