aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
set_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41#pragma once
42
43
50
51#include <agrum/base/core/set.h>
52
53namespace gum {
54
55 // ===========================================================================
56 // === SAFE SET ITERATORS ===
57 // ===========================================================================
58
59 // default constructor: the iterator points toward nothing
60 template < typename Key >
64
65 // creates an iterator for a given set
66 template < typename Key >
68 _ht_iter_{pos == Position::END ? from._inside_.cendSafe() : from._inside_.cbeginSafe()} {
69 GUM_CONSTRUCTOR(SetIteratorSafe);
70 }
71
72 // copy constructor
73 template < typename Key >
78
79 // copy constructor
80 template < typename Key >
85
86 // move constructor
87 template < typename Key >
89 _ht_iter_{std::move(from._ht_iter_)} {
90 GUM_CONS_MOV(SetIteratorSafe);
91 }
92
93 // destructor
94 template < typename Key >
96 GUM_DESTRUCTOR(SetIteratorSafe);
97 }
98
99 // assignment operator
100 template < typename Key >
102 = default;
103
104 // assignment operator
105 template < typename Key >
110
111 // move operator
112 template < typename Key >
115 _ht_iter_ = std::move(from._ht_iter_);
116 return *this;
117 }
118
119 // increments the iterator
120 template < typename Key >
122 // note that, if the hashtable's iterator points toward nothing, the
123 // hashtable's iterator incrementation will do nothing. In particular, it
124 // will not segfault.
125 ++_ht_iter_;
126 return *this;
127 }
128
129 // makes the iterator point to i elements further in the set
130 template < typename Key >
132 _ht_iter_ += nb;
133 return *this;
134 }
135
136 // returns a new iterator
137 template < typename Key >
141
142 // indicates whether two iterators point toward the same element of a same
143 // set
144 template < typename Key >
145 bool SetIteratorSafe< Key >::operator==(const SetIteratorSafe< Key >& from) const noexcept
146 = default;
147
148 // returns the element pointed to by the iterator
149 template < typename Key >
151 // note that, if the hashtable's iterator points toward nothing, it will
152 // raise an UndefinedIteratorValue exception
153 return _ht_iter_.key();
154 }
155
156 // returns aointer to the element pointed to by the iterator
157 template < typename Key >
159 // note that, if the hashtable's iterator points toward nothing, it will
160 // raise an UndefinedIteratorValue exception
161 return &(_ht_iter_.key());
162 }
164 // @brief makes the iterator point toward nothing (in particular, it is not
165 // related anymore to its current set) */
166 template < typename Key >
168 _ht_iter_.clear();
169 }
170
171 // ===========================================================================
172 // === UNSAFE SET ITERATORS ===
173 // ===========================================================================
174
175 // default constructor: the iterator points toward nothing
176 template < typename Key >
178 GUM_CONSTRUCTOR(SetIterator);
179 }
180
181 // creates an iterator for a given set
182 template < typename Key >
184 _ht_iter_{pos == Position::END ? from._inside_.cend() : from._inside_.cbegin()} {
185 GUM_CONSTRUCTOR(SetIterator);
186 }
187
188 // copy constructor
189 template < typename Key >
191 _ht_iter_{iter._ht_iter_} {
192 GUM_CONS_CPY(SetIterator);
193 }
194
195 // move constructor
196 template < typename Key >
198 _ht_iter_{std::move(from._ht_iter_)} {
199 GUM_CONS_MOV(SetIterator);
200 }
201
202 // destructor
203 template < typename Key >
205 GUM_DESTRUCTOR(SetIterator);
206 }
207
208 // assignment operator
209 template < typename Key >
211 = default;
212
213 // move operator
214 template < typename Key >
216 _ht_iter_ = std::move(from._ht_iter_);
217 return *this;
218 }
219
220 // increments the iterator
221 template < typename Key >
223 // note that, if the hashtable's iterator points toward nothing, the
224 // hashtable's iterator incrementation will do nothing. In particular, it
225 // will not segfault.
226 ++_ht_iter_;
227 return *this;
228 }
230 // makes the iterator point to i elements further in the set
231 template < typename Key >
233 _ht_iter_ += nb;
234 return *this;
235 }
236
237 // returns a new iterator
238 template < typename Key >
240 return SetIterator< Key >{*this} += nb;
241 }
242
243 // indicates whether two iterators point toward the same element of a same
244 // set
245 template < typename Key >
246 bool SetIterator< Key >::operator==(const SetIterator< Key >& from) const noexcept = default;
247
248 // returns the element pointed to by the iterator
249 template < typename Key >
250 const Key& SetIterator< Key >::operator*() const {
251 // note that, if the hashtable's iterator points toward nothing, it will
252 // raise an UndefinedIteratorValue exception
253 return _ht_iter_.key();
255
256 // returns aointer to the element pointed to by the iterator
257 template < typename Key >
259 // note that, if the hashtable's iterator points toward nothing, it will
260 // raise an UndefinedIteratorValue exception
261 return &(_ht_iter_.key());
262 }
263
264 // @brief makes the iterator point toward nothing (in particular, it is not
265 // related anymore to its current set) */
266 template < typename Key >
268 _ht_iter_.clear();
269 }
270
271 // ===========================================================================
272 // === SETS ===
273 // ===========================================================================
274
275 // default constructor
276 template < typename Key >
277 Set< Key >::Set(Size capacity, bool resize_policy) :
278 // create the hash table without key uniqueness policy (as we will
279 // check
280 // ourselves the uniqueness of Keys before inserting new elements)
281 _inside_(capacity, resize_policy, false) {
282 GUM_CONSTRUCTOR(Set);
283 }
284
285 // initializer list constructor
286 template < typename Key >
287 Set< Key >::Set(std::initializer_list< Key > list) :
288 _inside_(Size(list.size()) / 2, true, false) {
289 GUM_CONSTRUCTOR(Set);
290 for (const auto& elt: list) {
291 insert(elt);
292 }
293 }
294
295 // copy constructor
296 template < typename Key >
298 GUM_CONS_CPY(Set);
299 }
300
301 // move constructor
302 template < typename Key >
303 Set< Key >::Set(Set< Key >&& s) noexcept : _inside_(std::move(s._inside_)) {
304 GUM_CONS_MOV(Set);
305 }
306
307 // destructor
308 template < typename Key >
310 GUM_DESTRUCTOR(Set);
311 }
312
313 // removes all the elements, if any, from the set
314 template < typename Key >
316 // first we remove all the elements from the hashtable actually containing
317 // the elements of the set. Note that, doing so, all the hashtable iterators
318 // will be updated as well. In turn, this will imply that, whenever an
319 // operation will be performed on a SetIteratorSafe, this will raise an
320 // exception.
321 _inside_.clear();
322
323 // Note that actually there is no need to update the end iterator as this
324 // one
325 // is not affected by changes within hashtables (adding/deleting elements).
326 // Hence, for speedup, we do not update the end iterator
327 }
328
329 // copy operator
330 template < typename Key >
332 // avoid self assignment
333 if (&s != this) {
334 // remove the old content of the set. Actually, we remove all the elements
335 // from the underlying hashtable. Note that, doing so, all the hashtable
336 // iterators will be updated as well. In turn, this will imply that,
337 // whenever
338 // an operation will be performed on a SetIteratorSafe, this will raise an
339 // exception.
340 clear();
341
342 // prepare the set for its new data
343 resize(s.capacity());
346 // copy the set
347 _inside_ = s._inside_;
348
349 // Note that actually there is no need to update the end iterator as this
350 // one
351 // is not affected by changes within hashtables (adding/deleting
352 // elements).
353 // Hence, for speedup, we do not update the end iterator
354 }
355
356 return *this;
357 }
358
359 // move operator
360 template < typename Key >
362 if (this != &from) { _inside_ = std::move(from._inside_); }
363 return *this;
364 }
365
366 // mathematical equality between two sets
367 template < typename Key >
368 bool Set< Key >::operator==(const Set< Key >& s2) const {
369 const HashTable< Key, bool >& h2 = s2._inside_;
370
371 // check whether both sets have the same number of elements
372 if (size() != h2.size()) { return false; }
373
374 // check the content of the sets
375 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
376 ++iter) {
377 if (!h2.exists(iter.key())) { return false; }
378 }
379
380 return true;
381 }
382
383 // the usual begin iterator to parse the set
384 template < typename Key >
386 return SetIteratorSafe< Key >{*this};
387 }
388
389 // the usual begin iterator to parse the set
390 template < typename Key >
394
395 // the usual end iterator to parse the set
396 template < typename Key >
398 return *(static_cast< const SetIteratorSafe< Key >* >(_Set_end_safe_));
399 }
400
401 // the usual end iterator to parse the set
402 template < typename Key >
404 return *(static_cast< const SetIteratorSafe< Key >* >(_Set_end_safe_));
405 }
406
407 // the usual begin iterator to parse the set
408 template < typename Key >
410 return SetIterator< Key >{*this};
411 }
412
413 // the usual begin iterator to parse the set
414 template < typename Key >
416 return SetIterator< Key >{*this};
417 }
418
419 // the usual end iterator to parse the set
420 template < typename Key >
421 const typename Set< Key >::iterator& Set< Key >::end() noexcept {
422 return *(static_cast< const SetIterator< Key >* >(_Set_end_));
423 }
424
425 // the usual end iterator to parse the set
426 template < typename Key >
427 const typename Set< Key >::const_iterator& Set< Key >::cend() noexcept {
428 return *(static_cast< const SetIterator< Key >* >(_Set_end_));
429 }
430
431 // returns the size of the underlying hashtable containing the set
432 template < typename Key >
434 return _inside_.capacity();
435 }
436
437 // changes the size of the underlying hashtable
438 template < typename Key >
439 void Set< Key >::resize(Size new_size) {
440 _inside_.resize(new_size);
441
442 // Note that actually there is no need to update the end iterator as this
443 // one
444 // is not affected by changes within hashtables (adding/deleting elements).
445 // Hence, for speedup, we do not update the end iterator
446 }
447
448 // enables the user to change dynamically the resizing policy of the
449 // underlying hashtable
450 template < typename Key >
451 void Set< Key >::setResizePolicy(const bool new_policy) {
452 _inside_.setResizePolicy(new_policy);
453
454 // Note that actually there is no need to update the end iterator as this
455 // one
456 // is not affected by changes within hashtables (adding/deleting elements).
457 // Hence, for speedup, we do not update the end iterator
458 }
459
460 // returns the current resizing policy of the underlying hashtable
461 template < typename Key >
463 return _inside_.resizePolicy();
464 }
465
466 // indicates whether a given elements belong to the set
467 template < typename Key >
468 bool Set< Key >::contains(const Key& k) const {
469 return _inside_.exists(k);
471
472 template < typename Key >
474 if (this->size() >= s.size()) { return false; }
475
476 for (const auto& elt: *this) {
477 if (!s.contains(elt)) { return false; }
478 }
479 return true;
480 }
481
482 template < typename Key >
484 return s.isStrictSubsetOf(*this);
485 }
486
487 template < typename Key >
489 if (this->size() > s.size()) { return false; }
490
491 for (const auto& elt: *this) {
492 if (!s.contains(elt)) { return false; }
493 }
494 return true;
495 }
496
497 template < typename Key >
499 return s.isSubsetOrEqual(*this);
500 }
501
502 // indicates whether a given elements belong to the set
503 template < typename Key >
504 bool Set< Key >::exists(const Key& k) const {
505 return _inside_.exists(k);
506 }
507
508 // inserts a new element in the set
509 template < typename Key >
510 void Set< Key >::insert(const Key& k) {
511 // WARNING: we shall always test whether k already belongs to the set before
512 // trying to insert it because we set _inside_'s key uniqueness policy to
513 // false
514 if (!contains(k)) {
515 // insert the element
516 _inside_.insert(k, true);
517
518 // Note that actually there is no need to update the end iterator as this
519 // one
520 // is not affected by changes within hashtables (adding/deleting
521 // elements).
522 // Hence, for speedup, we do not update the end iterator
523 }
524 }
525
526 // inserts a new element in the set
527 template < typename Key >
528 void Set< Key >::insert(Key&& k) {
529 // WARNING: we shall always test whether k already belongs to the set before
530 // trying to insert it because we set _inside_'s key uniqueness policy to
531 // false
532 if (!contains(k)) {
533 // insert the element
534 _inside_.insert(std::move(k), true);
535
536 // Note that actually there is no need to update the end iterator as this
537 // one
538 // is not affected by changes within hashtables (adding/deleting
539 // elements).
540 // Hence, for speedup, we do not update the end iterator
541 }
542 }
543
544 // emplace a new element in the set
545 template < typename Key >
546 template < typename... Args >
547 void Set< Key >::emplace(Args&&... args) {
548 insert(std::move(Key(std::forward< Args >(args)...)));
549 }
550
551 // erases an element from the set
552 template < typename Key >
553 void Set< Key >::erase(const Key& k) {
554 // erase the element (if it exists)
555 _inside_.erase(k);
556
557 // Note that actually there is no need to update the end iterator as this
558 // one
559 // is not affected by changes within hashtables (adding/deleting elements).
560 // Hence, for speedup, we do not update the end iterator
561 }
562
563 template < typename Key >
565 if (this->empty()) { GUM_ERROR(NotFound, "Cannot popFirst from an empty set"); }
566
567 auto key = *this->begin();
568 this->erase(key);
569 return key;
570 }
571
572 // erases an element from the set
573 template < typename Key >
575 // erase the element
576 _inside_.erase(iter._ht_iter_);
577
578 // Note that actually there is no need to update the end iterator as this
579 // one
580 // is not affected by changes within hashtables (adding/deleting elements).
581 // Hence, for speedup, we do not update the end iterator
582 }
583
584 // adds a new element to the set
585 template < typename Key >
586 Set< Key >& Set< Key >::operator<<(const Key& k) {
587 insert(k);
588 return *this;
589 }
590
591 // adds a new element to the set
592 template < typename Key >
593 Set< Key >& Set< Key >::operator<<(Key&& k) {
594 insert(std::move(k));
595 return *this;
596 }
597
598 // removes an element from the set
599 template < typename Key >
601 erase(k);
602 return *this;
603 }
604
605 // returns the number of elements in the set
606 template < typename Key >
607 Size Set< Key >::size() const noexcept {
608 return _inside_.size();
609 }
610
611 // indicates whether the set is the empty set
612 template < typename Key >
613 bool Set< Key >::empty() const noexcept {
614 return _inside_.empty();
615 }
616
617 // Intersection operator
618 template < typename Key >
621 const HashTable< Key, bool >& h2 = s2._inside_;
623
624 if (size() < h2.size()) {
625 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
626 ++iter) {
627 if (h2.exists(iter.key())) h_r.insert(iter.key(), true);
628 }
629 } else {
630 for (HashTableConstIterator< Key, bool > iter = h2.cbegin(); iter != h2.cend(); ++iter) {
631 if (_inside_.exists(iter.key())) h_r.insert(iter.key(), true);
632 }
633 }
634
635 return res;
636 }
637
638 // Intersection update operator
639 template < typename Key >
641 if (&s2 != this) {
642 const HashTable< Key, bool >& h2 = s2._inside_;
643 for (auto iter = _inside_.beginSafe(); iter != _inside_.endSafe(); ++iter) {
644 if (!h2.exists(iter.key())) _inside_.erase(iter);
645 }
646 }
647
648 return *this;
649 }
650
651 // Union update operator
652 template < typename Key >
654 if (&s2 != this) {
655 for (auto pair: s2._inside_) {
656 if (!_inside_.exists(pair.first)) { _inside_.insert(pair.first, true); }
657 }
658 }
659
660 return *this;
661 }
662
663 // Union operator
664 template < typename Key >
666 Set< Key > res = *this;
667 const HashTable< Key, bool >& h2 = s2._inside_;
669
670 for (HashTableConstIterator< Key, bool > iter = h2.cbegin(); iter != h2.cend(); ++iter) {
671 if (!h_r.exists(iter.key())) h_r.insert(iter.key(), true);
672 }
673
674 return res;
675 }
676
677 // Disjunction operator
678 template < typename Key >
680 Set< Key > res;
681 const HashTable< Key, bool >& h2 = s2._inside_;
683
684 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
685 ++iter)
686 if (!h2.exists(iter.key())) h_r.insert(iter.key(), true);
687
688 return res;
689 }
690
691 // to display the content of the set
692 template < typename Key >
693 std::string Set< Key >::toString() const {
694 std::stringstream out;
695 bool first = true;
696 out << "{";
698 for (iterator iter = begin(); iter != end(); ++iter) {
699 if (first) {
700 out << *iter;
701 first = false;
702 } else {
703 out << "," << *iter;
705 }
706
707 out << "}";
708
709 std::string res;
710 out >> res;
711 return res;
712 }
713
714 // to friendly display the content of the set
715 template < typename Key >
716 std::ostream& operator<<(std::ostream& stream, const Set< Key >& set) {
717 stream << set.toString();
718 return stream;
719 }
720
721 // creates a hashtable of NewKey from the set
722 template < typename Key >
723 template < typename NewKey >
724 HashTable< Key, NewKey > Set< Key >::hashMap(NewKey (*f)(const Key&), Size size) const {
725 // determine the proper size of the hashtable
726 // by default, the size of the table is set so that the table does not take
727 // too much space while allowing to add a few elements without resizing
728 if (size == 0) size = std::max(Size(2), _inside_.size() / 2);
729
730 // create a new table
731 HashTable< Key, NewKey > table(size);
732
733 // fill the new hash table
734 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
735 ++iter) {
736 table.insert(iter.key(), f(iter.key()));
737 }
738
739 return table;
740 }
742 // creates a hashtable of NewKey from the set
743 template < typename Key >
744 template < typename NewKey >
746 // determine the proper size of the hashtable
747 // by default, the size of the table is set so that the table does not take
748 // too much space while allowing to add a few elements without resizing
749 if (size == 0) size = std::max(Size(2), _inside_.size() / 2);
750
751 // create a new table
754 // fill the new hash table
755 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
756 ++iter) {
757 table.insert(iter.key(), val);
758 }
759
760 return table;
761 }
762
763 // a method to create a list of NewKey from the set
764 template < typename Key >
765 template < typename NewKey >
766 List< NewKey > Set< Key >::listMap(NewKey (*f)(const Key&)) const {
767 // create a new list
768 List< NewKey > list;
769
770 // fill the new list
771 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
772 ++iter) {
773 list.pushBack(f(iter.key()));
774 }
775
776 return list;
777 }
778
779 // Returns the value of a key as a Size
780 template < typename T >
782 auto h = static_cast< Size >(0);
783 for (const auto& k: key) {
784 const auto hs = HashFunc< T >::castToSize(k);
785 h += hs * (hs ^ HashFuncConst::gold);
786 }
787
788 return h;
789 }
790
791 // Returns the hashed value of a key.
792 template < typename T >
793 Size HashFunc< Set< T > >::operator()(const Set< T >& key) const {
794 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
795 }
796
797} /* namespace gum */
static Size castToSize(const Set< T > &key)
Returns the value of a key as a Size.
Definition set_tpl.h:781
This class should be useless as only its specializations should be used.
Definition hashFunc.h:492
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
Generic doubly linked lists.
Definition list.h:378
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition list_tpl.h:1481
Exception : the element we looked for cannot be found.
Safe iterators for the Set class.
Definition set.h:592
SetIteratorSafe()
Default constructor: the iterator points toward nothing.
Definition set_tpl.h:61
Position
An enumeration to position the iterator at the beginning or the end of the set.
Definition set.h:610
void clear() noexcept
makes the iterator point toward nothing (in particular, it is not related anymore to its current set)...
Definition set_tpl.h:167
const Key * operator->() const
Returns a pointer to the element pointed to by the iterator.
Definition set_tpl.h:158
SetIteratorSafe< Key > operator+(Size i) const
Returns a new iterator.
Definition set_tpl.h:138
SetIteratorSafe< Key > & operator=(const SetIteratorSafe< Key > &from)
Assignment operator.
SetIteratorSafe< Key > & operator++() noexcept
Increments the iterator.
Definition set_tpl.h:121
bool operator==(const SetIteratorSafe< Key > &from) const noexcept
Indicates whether two iterators point toward the same element of a same set.
HashTableConstIteratorSafe< Key, bool > _ht_iter_
The underlying iterator for the set's hash table containing the data.
Definition set.h:762
SetIteratorSafe< Key > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the set.
Definition set_tpl.h:131
friend class Set< Key >
For efficiency, Set should be able to modify the hash table iterator.
Definition set.h:759
const Key & operator*() const
Returns the element pointed to by the iterator.
Definition set_tpl.h:150
~SetIteratorSafe() noexcept
Class destructor.
Definition set_tpl.h:95
Unsafe iterators for the Set class.
Definition set.h:806
SetIterator< Key > & operator++() noexcept
Increments the iterator.
Definition set_tpl.h:222
const Key * operator->() const
Returns a pointer to the element pointed to by the iterator.
Definition set_tpl.h:258
bool operator==(const SetIterator< Key > &iter) const noexcept
Indicates whether two iterators point toward the same element of a same set.
void clear() noexcept
makes the iterator point toward nothing (in particular, it is not related anymore to its current set)...
Definition set_tpl.h:267
SetIterator< Key > operator+(Size nb) const noexcept
Returns a new iterator.
Definition set_tpl.h:239
SetIterator< Key > & operator+=(Size nb) noexcept
Makes the iterator point to i elements further in the set.
Definition set_tpl.h:232
~SetIterator() noexcept
Class destructor.
Definition set_tpl.h:204
HashTableConstIterator< Key, bool > _ht_iter_
The underlying iterator for the set's hash table containing the data.
Definition set.h:964
SetIterator() noexcept
Default constructor: the iterator points toward nothing.
Definition set_tpl.h:177
SetIterator< Key > & operator=(const SetIterator< Key > &iter) noexcept
Assignment operator.
const Key & operator*() const
Returns the element pointed to by the iterator.
Definition set_tpl.h:250
Position
An enumeration to position the iterator at the beginning or the end of the set.
Definition set.h:824
friend class Set< Key >
For efficiency, Set should be able to modify the hash table iterator.
Definition set.h:960
Representation of a set.
Definition set.h:129
Set(Size capacity=HashTableConst::default_size, bool resize_policy=true)
Default constructor.
Definition set_tpl.h:277
bool isSupersetOrEqual(const Set< Key > &s) const
Definition set_tpl.h:498
SetIterator< Key > const_iterator
Types for STL compliance.
Definition set.h:141
SetIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition set.h:143
const Set< Key > & operator*=(const Set< Key > &s2)
Intersection update operator.
Definition set_tpl.h:640
HashTable< Key, bool > _inside_
A set of X's is actually a hash table whose keys are the X's.
Definition set.h:549
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:468
static const const_iterator_safe & cendSafe() noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:403
static const iterator & end() noexcept
The usual unsafe end iterator to parse the set.
Definition set_tpl.h:421
List< NewKey > listMap(NewKey(*f)(const Key &)) const
A method to create a List of NewKey from the set.
Definition set_tpl.h:766
Key popFirst()
Removes and returns an arbitrary element from the set.
Definition set_tpl.h:564
bool isSubsetOrEqual(const Set< Key > &s) const
Definition set_tpl.h:488
const_iterator cbegin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:415
Set< Key > operator+(const Set< Key > &s2) const
Union operator.
Definition set_tpl.h:665
void setResizePolicy(const bool new_policy)
Definition set_tpl.h:451
void resize(Size new_capacity)
Definition set_tpl.h:439
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:504
void clear()
Removes all the elements, if any, from the set.
Definition set_tpl.h:315
bool resizePolicy() const
Returns the current resizing policy of the underlying hash table.
Definition set_tpl.h:462
Set< Key > & operator<<(const Key &k)
Adds a new element to the set (alias for insert).
Definition set_tpl.h:586
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:613
Size capacity() const
Returns the capacity of the underlying hash table containing the set.
Definition set_tpl.h:433
bool isStrictSupersetOf(const Set< Key > &s) const
Definition set_tpl.h:483
Set< Key > & operator=(const Set< Key > &from)
Copy operator.
Definition set_tpl.h:331
friend class SetIteratorSafe< Key >
Friends to speed up access.
Definition set.h:545
Set< Key > operator-(const Set< Key > &s2) const
Disjunction operator.
Definition set_tpl.h:679
Set< Key > operator*(const Set< Key > &s2) const
Intersection operator.
Definition set_tpl.h:619
SetIterator< Key > iterator
Types for STL compliance.
Definition set.h:140
const_iterator_safe cbeginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:391
SetIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition set.h:142
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:409
void emplace(Args &&... args)
Emplace a new element in the set.
Definition set_tpl.h:547
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
const Set< Key > & operator+=(const Set< Key > &s2)
Union update operator.
Definition set_tpl.h:653
bool isStrictSubsetOf(const Set< Key > &s) const
Definition set_tpl.h:473
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:385
HashTable< Key, NewKey > hashMap(NewKey(*f)(const Key &), Size capacity=0) const
Creates a hashtable of NewKey from the set.
Definition set_tpl.h:724
Set< Key > & operator>>(const Key &k)
Removes an element from the set (alias for erase).
Definition set_tpl.h:600
bool operator==(const Set< Key > &s2) const
Mathematical equality between two sets.
Definition set_tpl.h:368
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:607
static const iterator_safe & endSafe() noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:397
std::string toString() const
Prints the content of the set.
Definition set_tpl.h:693
static const const_iterator & cend() noexcept
The usual unsafe end iterator to parse the set.
Definition set_tpl.h:427
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
bool contains(std::string_view s, std::string_view needle)
true if needle in s
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
Sets of elements (i.e.
static constexpr Size gold
Definition hashFunc.h:101