Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_union.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
51# ifndef TPL_UNION_H
52# define TPL_UNION_H
53
54# include <tpl_dynArray.H>
55# include <tpl_dynSetTree.H>
56
57# include <limits>
58# include <stdexcept>
59
60# include <ah-errors.H>
61
62
82{
83protected:
86 size_t num_blocks;
87
88 void verify_index(const size_t i) const
89 {
90 ah_out_of_range_error_if(i >= id.size()) << "index out of range";
91 }
92
93 virtual size_t root(size_t i)
94 {
95 verify_index(i);
96 while (i != id(i))
97 {
98 id(i) = id(id(i));
99 i = id(i);
100 }
101
102 return i;
103 }
104
105 size_t depth(size_t i) const
106 {
107 verify_index(i);
108 size_t d = 0;
109 while (true)
110 {
111 ++d;
112 const size_t parent = id(i);
113 verify_index(parent);
114 if (parent == i)
115 return d;
116 i = parent;
117 if (d > id.size())
118 {
119 ah_logic_error_if(d > id.size())
120 << "cycle detected in union-find structure";
121 }
122 }
123 }
124
125public:
126 virtual ~Fixed_Relation() = default;
127
129 Fixed_Relation(const size_t n = 0) : num_blocks(n)
130 {
131 if (num_blocks == 0)
132 return;
133
134 id.reserve(num_blocks);
136 for (size_t i = 0; i < num_blocks; ++i)
137 {
138 id(i) = i;
139 sz(i) = 1;
140 }
141 }
142
153 void set_n(size_t n)
154 {
155 id.empty();
156 sz.empty();
157 num_blocks = n;
158 if (num_blocks == 0)
159 return;
160
161 id.reserve(num_blocks);
163 for (size_t i = 0; i < num_blocks; ++i)
164 {
165 id(i) = i;
166 sz(i) = 1;
167 }
168 }
169
171 size_t size() const noexcept { return id.size(); }
172
175 [[nodiscard]] constexpr size_t get_num_blocks() const noexcept { return num_blocks; }
176
189 bool are_connected(size_t i, size_t j)
190 {
191 return root(i) == root(j);
192 }
193
200 void join(size_t i, size_t j)
201 {
202 i = root(i);
203 j = root(j);
204 if (i == j)
205 return;
206
207 if (sz(i) < sz(j))
208 {
209 id(i) = j;
210 sz(j) += sz(i);
211 }
212 else
213 {
214 id(j) = i;
215 sz(i) += sz(j);
216 }
217 --num_blocks;
218 }
219};
220
221
241{
242 void verify_if_add_new_points(const size_t n)
243 {
244 const size_t l = size();
245 if (n < l)
246 return;
247
248 id.reserve(l, n);
249 sz.reserve(l, n);
250 for (size_t i = l; i <= n; ++i)
251 {
252 id(i) = i;
253 sz(i) = 1;
254 }
255 num_blocks += n - l + 1;
256 }
257
258 size_t root(size_t i) override
259 {
261 return Fixed_Relation::root(i);
262 }
263
264public:
266 Relation(const size_t n = 0) : Fixed_Relation(n) {}
267};
268
269
286template <typename T, class Compare = Aleph::less<T>>
287class Relation_T : public Relation
288{
289 struct Pair
290 {
292 size_t i;
293
294 Pair() noexcept {}
295
296 Pair(const T & __item, size_t __i)
297 : item(__item), i(__i)
298 { /* empty */
299 }
300 };
301
302 struct Cmp
303 {
304 bool operator ()(const Pair & p1, const Pair & p2) const noexcept
305 {
306 return Compare()(p1.item, p2.item);
307 }
308 };
309
311
312 // returns the item ID; either because it is found or because it is inserted
313 size_t test_and_insert_new_item(const T & item)
314 {
315 Pair p(item, std::numeric_limits<size_t>::max());
316 Pair *ptr = items_tree.search_or_insert(p);
317 if (ptr->i == std::numeric_limits<size_t>::max())
318 {
319 const size_t new_id = size();
320 ptr->i = new_id;
321 Relation::are_connected(new_id, new_id);
322 }
323 return ptr->i;
324 }
325
326public:
328 bool are_connected(const T & p, const T & q)
329 {
330 const size_t i = test_and_insert_new_item(p);
331 const size_t j = test_and_insert_new_item(q);
332
333 return Relation::are_connected(i, j);
334 }
335
337 void join(const T & p, const T & q)
338 {
339 const size_t i = test_and_insert_new_item(p);
340 const size_t j = test_and_insert_new_item(q);
341 Relation::join(i, j);
342 }
343};
344
345# endif // TPL_UNION_H
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_logic_error_if(C)
Throws std::logic_error if condition holds.
Definition ah-errors.H:474
void empty() noexcept
Empty the array.
void reserve(const size_t l, const size_t r)
Allocate a range of entries.
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Binary relation between a set of integers.
Definition tpl_union.H:82
Fixed_Relation(const size_t n=0)
Initialize an empty binary relation of integers between 0 and n - 1.
Definition tpl_union.H:129
size_t size() const noexcept
Return the number of items of the set (not of relation)
Definition tpl_union.H:171
virtual size_t root(size_t i)
Definition tpl_union.H:93
DynArray< size_t > id
Definition tpl_union.H:84
void verify_index(const size_t i) const
Definition tpl_union.H:88
constexpr size_t get_num_blocks() const noexcept
Return the number of connected blocks, which is the number of equivalence classes.
Definition tpl_union.H:175
DynArray< size_t > sz
Definition tpl_union.H:85
void join(size_t i, size_t j)
Insert the pair '(i,j)' in the relation.
Definition tpl_union.H:200
bool are_connected(size_t i, size_t j)
Return true if item i is related to item j.
Definition tpl_union.H:189
size_t num_blocks
Definition tpl_union.H:86
void set_n(size_t n)
Set the number of items of the relation.
Definition tpl_union.H:153
virtual ~Fixed_Relation()=default
size_t depth(size_t i) const
Definition tpl_union.H:105
Binary relation between a set of items.
Definition tpl_union.H:288
bool are_connected(const T &p, const T &q)
Return true if p and q are connected; inserts missing items into the relation.
Definition tpl_union.H:328
size_t test_and_insert_new_item(const T &item)
Definition tpl_union.H:313
void join(const T &p, const T &q)
Join p with q; inserts missing items into the relation.
Definition tpl_union.H:337
DynSetAvlTree< Pair, Cmp > items_tree
Definition tpl_union.H:310
Binary relation between a set of integers.
Definition tpl_union.H:241
void verify_if_add_new_points(const size_t n)
Definition tpl_union.H:242
size_t root(size_t i) override
Definition tpl_union.H:258
Relation(const size_t n=0)
Initialize an empty relation of n elements between [0..n)
Definition tpl_union.H:266
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
bool operator()(const Pair &p1, const Pair &p2) const noexcept
Definition tpl_union.H:304
Pair() noexcept
Definition tpl_union.H:294
Pair(const T &__item, size_t __i)
Definition tpl_union.H:296
Lazy and scalable dynamic array implementation.
Dynamic set implementations based on balanced binary search trees.
DynList< int > l