aGrUM 2.3.2
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-2025 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-2025 *
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#pragma once
41
42
49
50#include <agrum/base/core/set.h>
51
52namespace gum {
53
54 // ===========================================================================
55 // === SAFE SET ITERATORS ===
56 // ===========================================================================
57
58 // default constructor: the iterator points toward nothing
59 template < typename Key >
61 GUM_CONSTRUCTOR(SetIteratorSafe);
62 }
63
64 // creates an iterator for a given set
65 template < typename Key >
67 _ht_iter_{pos == SetIteratorSafe< Key >::END ? set._inside_.cendSafe()
68 : set._inside_.cbeginSafe()} {
69 GUM_CONSTRUCTOR(SetIteratorSafe);
70 }
71
72 // copy constructor
73 template < typename Key >
78
79 // copy constructor
80 template < typename Key >
82 _ht_iter_{iter._ht_iter_} {
83 GUM_CONS_CPY(SetIteratorSafe);
84 }
85
86 // move constructor
87 template < typename Key >
92
93 // destructor
94 template < typename Key >
96 GUM_DESTRUCTOR(SetIteratorSafe);
97 }
98
99 // assignment operator
100 template < typename Key >
103 _ht_iter_ = from._ht_iter_;
104 return *this;
105 }
106
107 // assignment operator
108 template < typename Key >
110 _ht_iter_ = from._ht_iter_;
111 return *this;
112 }
113
114 // move operator
115 template < typename Key >
118 _ht_iter_ = std::move(from._ht_iter_);
119 return *this;
120 }
121
122 // increments the iterator
123 template < typename Key >
125 // note that, if the hashtable's iterator points toward nothing, the
126 // hashtable's iterator incrementation will do nothing. In particular, it
127 // will not segfault.
128 ++_ht_iter_;
129 return *this;
130 }
131
132 // makes the iterator point to i elements further in the set
133 template < typename Key >
135 _ht_iter_ += nb;
136 return *this;
137 }
138
139 // returns a new iterator
140 template < typename Key >
144
145 // indicates whether two iterators point to different elements or sets
146 template < typename Key >
147 INLINE bool
149 return _ht_iter_ != from._ht_iter_;
150 }
151
152 // indicates whether two iterators point toward the same element of a same
153 // set
154 template < typename Key >
155 INLINE bool
157 return _ht_iter_ == from._ht_iter_;
158 }
159
160 // returns the element pointed to by the iterator
161 template < typename Key >
162 INLINE const Key& SetIteratorSafe< Key >::operator*() const {
163 // note that, if the hashtable's iterator points toward nothing, it will
164 // raise an UndefinedIteratorValue exception
165 return _ht_iter_.key();
166 }
167
168 // returns aointer to the element pointed to by the iterator
169 template < typename Key >
170 INLINE const Key* SetIteratorSafe< Key >::operator->() const {
171 // note that, if the hashtable's iterator points toward nothing, it will
172 // raise an UndefinedIteratorValue exception
173 return &(_ht_iter_.key());
174 }
175
176 // @brief makes the iterator point toward nothing (in particular, it is not
177 // related anymore to its current set) */
178 template < typename Key >
179 INLINE void SetIteratorSafe< Key >::clear() noexcept {
180 _ht_iter_.clear();
181 }
182
183 // ===========================================================================
184 // === UNSAFE SET ITERATORS ===
185 // ===========================================================================
186
187 // default constructor: the iterator points toward nothing
188 template < typename Key >
190 GUM_CONSTRUCTOR(SetIterator);
191 }
192
193 // creates an iterator for a given set
194 template < typename Key >
196 _ht_iter_{pos == SetIterator< Key >::END ? set._inside_.cend() : set._inside_.cbegin()} {
197 GUM_CONSTRUCTOR(SetIterator);
198 }
199
200 // copy constructor
201 template < typename Key >
203 _ht_iter_{iter._ht_iter_} {
204 GUM_CONS_CPY(SetIterator);
205 }
206
207 // move constructor
208 template < typename Key >
210 _ht_iter_{std::move(from._ht_iter_)} {
211 GUM_CONS_MOV(SetIterator);
212 }
213
214 // destructor
215 template < typename Key >
217 GUM_DESTRUCTOR(SetIterator);
218 }
219
220 // assignment operator
221 template < typename Key >
224 _ht_iter_ = from._ht_iter_;
225 return *this;
226 }
227
228 // move operator
229 template < typename Key >
231 _ht_iter_ = std::move(from._ht_iter_);
232 return *this;
233 }
234
235 // increments the iterator
236 template < typename Key >
238 // note that, if the hashtable's iterator points toward nothing, the
239 // hashtable's iterator incrementation will do nothing. In particular, it
240 // will not segfault.
241 ++_ht_iter_;
242 return *this;
243 }
244
245 // makes the iterator point to i elements further in the set
246 template < typename Key >
248 _ht_iter_ += nb;
249 return *this;
250 }
251
252 // returns a new iterator
253 template < typename Key >
255 return SetIterator< Key >{*this} += nb;
256 }
257
258 // indicates whether two iterators point to different elements or sets
259 template < typename Key >
260 INLINE bool SetIterator< Key >::operator!=(const SetIterator< Key >& from) const noexcept {
261 return _ht_iter_ != from._ht_iter_;
262 }
264 // indicates whether two iterators point toward the same element of a same
265 // set
266 template < typename Key >
267 INLINE bool SetIterator< Key >::operator==(const SetIterator< Key >& from) const noexcept {
268 return _ht_iter_ == from._ht_iter_;
269 }
270
271 // returns the element pointed to by the iterator
272 template < typename Key >
273 INLINE const Key& SetIterator< Key >::operator*() const {
274 // note that, if the hashtable's iterator points toward nothing, it will
275 // raise an UndefinedIteratorValue exception
276 return _ht_iter_.key();
277 }
278
279 // returns aointer to the element pointed to by the iterator
280 template < typename Key >
281 INLINE const Key* SetIterator< Key >::operator->() const {
282 // note that, if the hashtable's iterator points toward nothing, it will
283 // raise an UndefinedIteratorValue exception
284 return &(_ht_iter_.key());
285 }
286
287 // @brief makes the iterator point toward nothing (in particular, it is not
288 // related anymore to its current set) */
289 template < typename Key >
290 INLINE void SetIterator< Key >::clear() noexcept {
291 _ht_iter_.clear();
292 }
293
294 // ===========================================================================
295 // === SETS ===
296 // ===========================================================================
297
298 // default constructor
299 template < typename Key >
300 INLINE Set< Key >::Set(Size capacity, bool resize_policy) :
301 // create the hash table without key uniqueness policy (as we will
302 // check
303 // ourselves the uniqueness of Keys before inserting new elements)
304 _inside_(capacity, resize_policy, false) {
305 GUM_CONSTRUCTOR(Set);
306 }
307
308 // initializer list constructor
309 template < typename Key >
310 INLINE Set< Key >::Set(std::initializer_list< Key > list) :
311 _inside_(Size(list.size()) / 2, true, false) {
312 GUM_CONSTRUCTOR(Set);
313 for (const auto& elt: list) {
314 insert(elt);
315 }
316 }
317
318 // copy constructor
319 template < typename Key >
321 GUM_CONS_CPY(Set);
322 }
323
324 // move constructor
325 template < typename Key >
327 GUM_CONS_MOV(Set);
329
330 // destructor
331 template < typename Key >
332 INLINE Set< Key >::Set::~Set() {
333 GUM_DESTRUCTOR(Set);
334 }
336 // removes all the elements, if any, from the set
337 template < typename Key >
338 INLINE void Set< Key >::clear() {
339 // first we remove all the elements from the hashtable actually containing
340 // the elements of the set. Note that, doing so, all the hashtable iterators
341 // will be updated as well. In turn, this will imply that, whenever an
342 // operation will be performed on a SetIteratorSafe, this will raise an
343 // exception.
344 _inside_.clear();
345
346 // Note that actually there is no need to update the end iterator as this
347 // one
348 // is not affected by changes within hashtables (adding/deleting elements).
349 // Hence, for speedup, we do not update the end iterator
350 }
351
352 // copy operator
353 template < typename Key >
355 // avoid self assignment
356 if (&s != this) {
357 // remove the old content of the set. Actually, we remove all the elements
358 // from the underlying hashtable. Note that, doing so, all the hashtable
359 // iterators will be updated as well. In turn, this will imply that,
360 // whenever
361 // an operation will be performed on a SetIteratorSafe, this will raise an
362 // exception.
363 clear();
364
365 // prepare the set for its new data
366 resize(s.capacity());
368
369 // copy the set
371
372 // Note that actually there is no need to update the end iterator as this
373 // one
374 // is not affected by changes within hashtables (adding/deleting
375 // elements).
376 // Hence, for speedup, we do not update the end iterator
377 }
378
379 return *this;
381
382 // move operator
383 template < typename Key >
385 _inside_ = std::move(from._inside_);
386 return *this;
387 }
388
389 // mathematical equality between two sets
390 template < typename Key >
391 bool Set< Key >::operator==(const Set< Key >& s2) const {
393
394 // check whether both sets have the same number of elements
395 if (size() != h2.size()) return false;
396
397 // check the content of the sets
398 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
399 ++iter) {
400 if (!h2.exists(iter.key())) return false;
401 }
402
403 return true;
404 }
405
406 // mathematical inequality between two sets
407 template < typename Key >
408 INLINE bool Set< Key >::operator!=(const Set< Key >& s2) const {
409 return !(operator==(s2));
411
412 // the usual begin iterator to parse the set
413 template < typename Key >
415 return SetIteratorSafe< Key >{*this};
417
418 // the usual begin iterator to parse the set
419 template < typename Key >
423
424 // the usual end iterator to parse the set
425 template < typename Key >
426 INLINE const typename Set< Key >::iterator_safe& Set< Key >::endSafe() const noexcept {
427 return *(reinterpret_cast< const SetIteratorSafe< Key >* >(_Set_end_safe_));
429
430 // the usual end iterator to parse the set
431 template < typename Key >
432 INLINE const typename Set< Key >::const_iterator_safe& Set< Key >::cendSafe() const noexcept {
433 return *(reinterpret_cast< const SetIteratorSafe< Key >* >(_Set_end_safe_));
435
436 // the usual begin iterator to parse the set
437 template < typename Key >
438 INLINE typename Set< Key >::iterator Set< Key >::begin() const {
439 return SetIterator< Key >{*this};
441
442 // the usual begin iterator to parse the set
443 template < typename Key >
445 return SetIterator< Key >{*this};
447
448 // the usual end iterator to parse the set
449 template < typename Key >
450 INLINE const typename Set< Key >::iterator& Set< Key >::end() const noexcept {
451 return *(reinterpret_cast< const SetIterator< Key >* >(_Set_end_));
453
454 // the usual end iterator to parse the set
455 template < typename Key >
456 INLINE const typename Set< Key >::const_iterator& Set< Key >::cend() const noexcept {
457 return *(reinterpret_cast< const SetIterator< Key >* >(_Set_end_));
458 }
459
460 // returns the size of the underlying hashtable containing the set
461 template < typename Key >
462 INLINE Size Set< Key >::capacity() const {
463 return _inside_.capacity();
464 }
465
466 // changes the size of the underlying hashtable
467 template < typename Key >
468 INLINE void Set< Key >::resize(Size new_size) {
469 _inside_.resize(new_size);
471 // Note that actually there is no need to update the end iterator as this
472 // one
473 // is not affected by changes within hashtables (adding/deleting elements).
474 // Hence, for speedup, we do not update the end iterator
475 }
476
477 // enables the user to change dynamically the resizing policy of the
478 // underlying hashtable
479 template < typename Key >
480 INLINE void Set< Key >::setResizePolicy(const bool new_policy) {
481 _inside_.setResizePolicy(new_policy);
482
483 // Note that actually there is no need to update the end iterator as this
484 // one
485 // is not affected by changes within hashtables (adding/deleting elements).
486 // Hence, for speedup, we do not update the end iterator
487 }
488
489 // returns the current resizing policy of the underlying hashtable
490 template < typename Key >
491 INLINE bool Set< Key >::resizePolicy() const {
492 return _inside_.resizePolicy();
493 }
494
495 // indicates whether a given elements belong to the set
496 template < typename Key >
497 INLINE bool Set< Key >::contains(const Key& k) const {
498 return _inside_.exists(k);
499 }
500
501 template < typename Key >
502 INLINE bool Set< Key >::isStrictSubsetOf(const Set< Key >& s) const {
503 if (this->size() >= s.size()) { return false; }
504
505 for (const auto& elt: *this) {
506 if (!s.contains(elt)) { return false; }
507 }
508 return true;
509 }
510
511 template < typename Key >
512 INLINE bool Set< Key >::isStrictSupersetOf(const Set< Key >& s) const {
513 return s.isStrictSubsetOf(*this);
514 }
515
516 template < typename Key >
517 INLINE bool Set< Key >::isSubsetOrEqual(const Set< Key >& s) const {
518 if (this->size() > s.size()) { return false; }
520 for (const auto& elt: *this) {
521 if (!s.contains(elt)) { return false; }
522 }
523 return true;
524 }
525
526 template < typename Key >
527 INLINE bool Set< Key >::isSupersetOrEqual(const Set< Key >& s) const {
528 return s.isSubsetOrEqual(*this);
529 }
530
531 // indicates whether a given elements belong to the set
532 template < typename Key >
533 INLINE bool Set< Key >::exists(const Key& k) const {
534 return _inside_.exists(k);
535 }
536
537 // inserts a new element in the set
538 template < typename Key >
539 INLINE void Set< Key >::insert(const Key& k) {
540 // WARNING: we shall always test whether k already belongs to the set before
541 // trying to insert it because we set _inside_'s key uniqueness policy to
542 // false
543 if (!contains(k)) {
544 // insert the element
545 _inside_.insert(k, true);
547 // Note that actually there is no need to update the end iterator as this
548 // one
549 // is not affected by changes within hashtables (adding/deleting
550 // elements).
551 // Hence, for speedup, we do not update the end iterator
552 }
553 }
554
555 // inserts a new element in the set
556 template < typename Key >
557 INLINE void Set< Key >::insert(Key&& k) {
558 // WARNING: we shall always test whether k already belongs to the set before
559 // trying to insert it because we set _inside_'s key uniqueness policy to
560 // false
561 if (!contains(k)) {
562 // insert the element
563 _inside_.insert(std::move(k), true);
564
565 // Note that actually there is no need to update the end iterator as this
566 // one
567 // is not affected by changes within hashtables (adding/deleting
568 // elements).
569 // Hence, for speedup, we do not update the end iterator
570 }
571 }
572
573 // emplace a new element in the set
574 template < typename Key >
575 template < typename... Args >
576 INLINE void Set< Key >::emplace(Args&&... args) {
577 insert(std::move(Key(std::forward< Args >(args)...)));
578 }
579
580 // erases an element from the set
581 template < typename Key >
582 INLINE void Set< Key >::erase(const Key& k) {
583 // erase the element (if it exists)
584 _inside_.erase(k);
585
586 // Note that actually there is no need to update the end iterator as this
587 // one
588 // is not affected by changes within hashtables (adding/deleting elements).
589 // Hence, for speedup, we do not update the end iterator
590 }
591
592 template < typename Key >
594 if (this->empty()) { GUM_ERROR(NotFound, "Cannot popFirst from an empty set"); }
595
596 auto key = *this->begin();
597 this->erase(key);
598 return key;
599 }
600
601 // erases an element from the set
602 template < typename Key >
603 INLINE void Set< Key >::erase(const SetIteratorSafe< Key >& iter) {
604 // erase the element
605 _inside_.erase(iter._ht_iter_);
606
607 // Note that actually there is no need to update the end iterator as this
608 // one
609 // is not affected by changes within hashtables (adding/deleting elements).
610 // Hence, for speedup, we do not update the end iterator
611 }
612
613 // adds a new element to the set
614 template < typename Key >
615 INLINE Set< Key >& Set< Key >::operator<<(const Key& k) {
616 insert(k);
617 return *this;
618 }
619
620 // adds a new element to the set
621 template < typename Key >
623 insert(std::move(k));
624 return *this;
625 }
626
627 // removes an element from the set
628 template < typename Key >
629 INLINE Set< Key >& Set< Key >::operator>>(const Key& k) {
630 erase(k);
631 return *this;
632 }
633
634 // returns the number of elements in the set
635 template < typename Key >
636 INLINE Size Set< Key >::size() const noexcept {
637 return _inside_.size();
638 }
639
640 // indicates whether the set is the empty set
641 template < typename Key >
642 INLINE bool Set< Key >::empty() const noexcept {
643 return _inside_.empty();
644 }
645
646 // Intersection operator
647 template < typename Key >
649 Set< Key > res;
650 const HashTable< Key, bool >& h2 = s2._inside_;
652
653 if (size() < h2.size()) {
654 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
655 ++iter) {
656 if (h2.exists(iter.key())) h_r.insert(iter.key(), true);
657 }
658 } else {
659 for (HashTableConstIterator< Key, bool > iter = h2.cbegin(); iter != h2.cend(); ++iter) {
660 if (_inside_.exists(iter.key())) h_r.insert(iter.key(), true);
661 }
662 }
663
664 return res;
665 }
666
667 // Intersection update operator
668 template < typename Key >
670 if (&s2 != this) {
671 const HashTable< Key, bool >& h2 = s2._inside_;
672 for (auto iter = _inside_.beginSafe(); iter != _inside_.endSafe(); ++iter) {
673 if (!h2.exists(iter.key())) _inside_.erase(iter);
674 }
675 }
676
677 return *this;
678 }
679
680 // Union update operator
681 template < typename Key >
683 if (&s2 != this) {
684 for (auto pair: s2._inside_) {
685 if (!_inside_.exists(pair.first)) _inside_.insert(pair.first, true);
686 }
687 }
688
689 return *this;
690 }
691
692 // Union operator
693 template < typename Key >
695 Set< Key > res = *this;
696 const HashTable< Key, bool >& h2 = s2._inside_;
698
699 for (HashTableConstIterator< Key, bool > iter = h2.cbegin(); iter != h2.cend(); ++iter) {
700 if (!h_r.exists(iter.key())) h_r.insert(iter.key(), true);
701 }
703 return res;
704 }
705
706 // Disjunction operator
707 template < typename Key >
710 const HashTable< Key, bool >& h2 = s2._inside_;
712
713 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
714 ++iter)
715 if (!h2.exists(iter.key())) h_r.insert(iter.key(), true);
717 return res;
718 }
719
720 // to display the content of the set
721 template < typename Key >
722 INLINE std::string Set< Key >::toString() const {
723 std::stringstream out;
724 bool first = true;
725 out << "{";
726
727 for (iterator iter = begin(); iter != end(); ++iter) {
728 if (first) {
729 out << *iter;
730 first = false;
731 } else {
732 out << "," << *iter;
733 }
734 }
735
736 out << "}";
737
738 std::string res;
739 out >> res;
740 return res;
741 }
742
743 // to friendly display the content of the set
744 template < typename Key >
745 std::ostream& operator<<(std::ostream& stream, const Set< Key >& set) {
746 stream << set.toString();
747 return stream;
748 }
749
750 // creates a hashtable of NewKey from the set
751 template < typename Key >
752 template < typename NewKey >
753 HashTable< Key, NewKey > Set< Key >::hashMap(NewKey (*f)(const Key&), Size size) const {
754 // determine the proper size of the hashtable
755 // by default, the size of the table is set so that the table does not take
756 // too much space while allowing to add a few elements without resizing
757 if (size == 0) size = std::max(Size(2), _inside_.size() / 2);
758
759 // create a new table
760 HashTable< Key, NewKey > table(size);
761
762 // fill the new hash table
763 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
764 ++iter) {
765 table.insert(iter.key(), f(iter.key()));
767
768 return table;
769 }
770
771 // creates a hashtable of NewKey from the set
772 template < typename Key >
773 template < typename NewKey >
775 // determine the proper size of the hashtable
776 // by default, the size of the table is set so that the table does not take
777 // too much space while allowing to add a few elements without resizing
778 if (size == 0) size = std::max(Size(2), _inside_.size() / 2);
779
780 // create a new table
782
783 // fill the new hash table
784 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
785 ++iter) {
786 table.insert(iter.key(), val);
787 }
788
789 return table;
790 }
791
792 // a method to create a list of NewKey from the set
793 template < typename Key >
794 template < typename NewKey >
795 List< NewKey > Set< Key >::listMap(NewKey (*f)(const Key&)) const {
796 // create a new list
797 List< NewKey > list;
798
799 // fill the new list
800 for (HashTableConstIterator< Key, bool > iter = _inside_.cbegin(); iter != _inside_.cend();
801 ++iter) {
802 list.pushBack(f(iter.key()));
803 }
804
805 return list;
806 }
807
808 // Returns the value of a key as a Size
809 template < typename T >
811 Size h = Size(0);
812 for (const auto& k: key) {
813 const auto hs = HashFunc< T >::castToSize(k);
814 h += hs * (hs ^ HashFuncConst::gold);
815 }
816
817 return h;
818 }
819
820 // Returns the hashed value of a key.
821 template < typename T >
822 INLINE Size HashFunc< Set< T > >::operator()(const Set< T >& key) const {
823 return (castToSize(key) * HashFuncConst::gold) & this->hash_mask_;
824 }
825
826} /* namespace gum */
static Size castToSize(const Set< T > &key)
Returns the value of a key as a Size.
Definition set_tpl.h:810
This class should be useless as only its specializations should be used.
Definition hashFunc.h:487
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
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.
Size size() const noexcept
Returns the number of elements stored into the hashtable.
Generic doubly linked lists.
Definition list.h:379
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition list_tpl.h:1488
Exception : the element we looked for cannot be found.
Safe iterators for the Set class.
Definition set.h:601
const Key * operator->() const
Returns a pointer to the element pointed to by the iterator.
Definition set_tpl.h:170
Position
An enumeration to position the iterator at the beginning or the end of the set.
Definition set.h:619
bool operator==(const SetIteratorSafe< Key > &from) const noexcept
Indicates whether two iterators point toward the same element of a same set.
Definition set_tpl.h:156
SetIteratorSafe< Key > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the set.
Definition set_tpl.h:134
SetIteratorSafe()
Default constructor: the iterator points toward nothing.
Definition set_tpl.h:60
~SetIteratorSafe() noexcept
Class destructor.
Definition set_tpl.h:95
SetIteratorSafe< Key > & operator++() noexcept
Increments the iterator.
Definition set_tpl.h:124
HashTableConstIteratorSafe< Key, bool > _ht_iter_
The underlying iterator for the set's hash table containing the data.
Definition set.h:775
void clear() noexcept
makes the iterator point toward nothing (in particular, it is not related anymore to its current set)...
Definition set_tpl.h:179
bool operator!=(const SetIteratorSafe< Key > &from) const noexcept
Indicates whether two iterators point to different elements or sets.
Definition set_tpl.h:148
SetIteratorSafe< Key > & operator=(const SetIteratorSafe< Key > &from)
Assignment operator.
Definition set_tpl.h:102
const Key & operator*() const
Returns the element pointed to by the iterator.
Definition set_tpl.h:162
friend class Set< Key >
For efficiency, Set should be able to modify the hash table iterator.
Definition set.h:772
SetIteratorSafe< Key > operator+(Size i) const
Returns a new iterator.
Definition set_tpl.h:141
Unsafe iterators for the Set class.
Definition set.h:819
SetIterator< Key > operator+(Size i) const noexcept
Returns a new iterator.
Definition set_tpl.h:254
const Key * operator->() const
Returns a pointer to the element pointed to by the iterator.
Definition set_tpl.h:281
SetIterator() noexcept
Default constructor: the iterator points toward nothing.
Definition set_tpl.h:189
bool operator==(const SetIterator< Key > &from) const noexcept
Indicates whether two iterators point toward the same element of a same set.
Definition set_tpl.h:267
void clear() noexcept
makes the iterator point toward nothing (in particular, it is not related anymore to its current set)...
Definition set_tpl.h:290
~SetIterator() noexcept
Class destructor.
Definition set_tpl.h:216
HashTableConstIterator< Key, bool > _ht_iter_
The underlying iterator for the set's hash table containing the data.
Definition set.h:981
friend class Set< Key >
For efficiency, Set should be able to modify the hash table iterator.
Definition set.h:977
Position
An enumeration to position the iterator at the beginning or the end of the set.
Definition set.h:837
SetIterator< Key > & operator++() noexcept
Increments the iterator.
Definition set_tpl.h:237
SetIterator< Key > & operator=(const SetIterator< Key > &from) noexcept
Assignment operator.
Definition set_tpl.h:223
SetIterator< Key > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the set.
Definition set_tpl.h:247
bool operator!=(const SetIterator< Key > &from) const noexcept
Indicates whether two iterators point to different elements or sets.
Definition set_tpl.h:260
const Key & operator*() const
Returns the element pointed to by the iterator.
Definition set_tpl.h:273
Representation of a set.
Definition set.h:131
iterator begin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:438
SetIterator< Key > const_iterator
Types for STL compliance.
Definition set.h:143
SetIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition set.h:145
const iterator & end() const noexcept
The usual unsafe end iterator to parse the set.
Definition set_tpl.h:450
const Set< Key > & operator*=(const Set< Key > &s2)
Intersection update operator.
Definition set_tpl.h:669
HashTable< Key, bool > _inside_
A set of X's is actually a hash table whose keys are the X's.
Definition set.h:558
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:636
iterator_safe beginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:414
List< NewKey > listMap(NewKey(*f)(const Key &)) const
A method to create a List of NewKey from the set.
Definition set_tpl.h:795
Key popFirst()
Removes and returns an arbitrary element from the set.
Definition set_tpl.h:593
const iterator_safe & endSafe() const noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:426
Set< Key > operator+(const Set< Key > &s2) const
Union operator.
Definition set_tpl.h:694
Size capacity() const
Returns the capacity of the underlying hash table containing the set.
Definition set_tpl.h:462
bool isSubsetOrEqual(const Set< Key > &s) const
Definition set_tpl.h:517
bool isSupersetOrEqual(const Set< Key > &s) const
Definition set_tpl.h:527
Set< Key > & operator=(const Set< Key > &from)
Copy operator.
Definition set_tpl.h:354
friend class SetIteratorSafe< Key >
Friends to speed up access.
Definition set.h:554
Set< Key > operator-(const Set< Key > &s2) const
Disjunction operator.
Definition set_tpl.h:708
Set< Key > operator*(const Set< Key > &s2) const
Intersection operator.
Definition set_tpl.h:648
const_iterator_safe cbeginSafe() const
The usual safe begin iterator to parse the set.
Definition set_tpl.h:420
std::string toString() const
Prints the content of the set.
Definition set_tpl.h:722
Set(Size capacity=HashTableConst::default_size, bool resize_policy=true)
Default constructor.
Definition set_tpl.h:300
bool resizePolicy() const
Returns the current resizing policy of the underlying hash table.
Definition set_tpl.h:491
SetIterator< Key > iterator
Types for STL compliance.
Definition set.h:142
bool isStrictSubsetOf(const Set< Key > &s) const
Definition set_tpl.h:502
SetIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition set.h:144
const const_iterator & cend() const noexcept
The usual unsafe end iterator to parse the set.
Definition set_tpl.h:456
void emplace(Args &&... args)
Emplace a new element in the set.
const Set< Key > & operator+=(const Set< Key > &s2)
Union update operator.
Definition set_tpl.h:682
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:533
bool operator!=(const Set< Key > &s2) const
Mathematical inequality between two sets.
Definition set_tpl.h:408
void setResizePolicy(const bool new_policy)
Definition set_tpl.h:480
HashTable< Key, NewKey > hashMap(NewKey(*f)(const Key &), Size capacity=0) const
Creates a hashtable of NewKey from the set.
Definition set_tpl.h:753
bool operator==(const Set< Key > &s2) const
Mathematical equality between two sets.
Definition set_tpl.h:391
bool isStrictSupersetOf(const Set< Key > &s) const
Definition set_tpl.h:512
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:497
Set< Key > & operator<<(const Key &k)
Adds a new element to the set (alias for insert).
Definition set_tpl.h:615
void resize(Size new_capacity)
Definition set_tpl.h:468
const_iterator cbegin() const
The usual unsafe begin iterator to parse the set.
Definition set_tpl.h:444
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
const const_iterator_safe & cendSafe() const noexcept
The usual safe end iterator to parse the set.
Definition set_tpl.h:432
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition set_tpl.h:642
Set< Key > & operator>>(const Key &k)
Removes an element from the set (alias for erase).
Definition set_tpl.h:629
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:582
void clear()
Removes all the elements, if any, from the set.
Definition set_tpl.h:338
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
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
Definition AVLTree.h:913
bool operator==(const HashTableIteratorSafe< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.
STL namespace.
Sets of elements (i.e.
static constexpr Size gold
Definition hashFunc.h:98