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
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;
86
91 explicit DynArray_Set(Equal __eq) : Base(), eq(std::move(__eq))
92 {
93 // empty
94 }
95
102 DynArray_Set(Equal __eq, const size_t dim) : Base(dim), eq(std::move(__eq))
103 {
104 // empty
105 }
106
115 DynArray_Set(Equal __eq, size_t _pow_dir, size_t _pow_seg, size_t _pow_block)
117 {
118 // empty
119 }
120
122 const Equal & get_equal() const noexcept { return eq; }
123
129 void set_equal(Equal __eq) { eq = std::move(__eq); }
130
136 T * insert(const T & item)
137 {
138 return &this->append(item);
139 }
140
142 T * insert(T && item)
143 {
144 return &this->append(std::forward<T>(item));
145 }
146
151 T * search(const T & key) noexcept
152 {
153 for (size_t i = 0; i < this->size(); ++i)
154 if (eq(this->access(i), key))
155 return &this->access(i);
156 return nullptr;
157 }
158
160 const T * search(const T & key) const noexcept
161 {
162 return const_cast<DynArray_Set *>(this)->search(key);
163 }
164
169 bool contains(const T & key) const noexcept
170 {
171 return search(key) != nullptr;
172 }
173
178 T & find(const T & key)
179 {
180 auto * p = search(key);
181 ah_domain_error_if(p == nullptr) << "key not found";
182 return *p;
183 }
184
186 const T & find(const T & key) const
187 {
188 return const_cast<DynArray_Set *>(this)->find(key);
189 }
190
195 size_t count(const T & key) const noexcept
196 {
197 size_t n = 0;
198 for (size_t i = 0; i < this->size(); ++i)
199 if (eq(this->access(i), key))
200 ++n;
201 return n;
202 }
203
210 bool remove_one(const T & key)
211 {
212 auto * p = search(key);
213 if (p == nullptr)
214 return false;
215 this->remove(*p);
216 return true;
217 }
218
223 size_t remove_all(const T & key)
224 {
225 size_t removed = 0;
226 for (size_t i = 0; i < this->size();)
227 {
228 if (eq(this->access(i), key))
229 {
230 this->remove(this->access(i));
231 ++removed;
232 }
233 else
234 ++i;
235 }
236 return removed;
237 }
238 };
239
240}
241
242# endif // TPL_DYNARRAY_SET_H
243
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:527
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:4063
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
STL namespace.
Lazy and scalable dynamic array implementation.
Comprehensive sorting algorithms and search utilities for Aleph-w.