aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
set.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
48#ifndef GUM_SET_H
49#define GUM_SET_H
50
51#include <string>
52
56
57#include <initializer_list>
58
59namespace gum {
60
61#ifndef DOXYGEN_SHOULD_SKIP_THIS
62
63 template < typename Key >
64 class SetIteratorSafe;
65 template < typename Key >
66 class SetIterator;
67 template < typename Key >
68 class Set;
69
70 template < typename Key >
71 using SetConstIterator = SetIterator< Key >;
72 template < typename Key >
73 using SetConstIteratorSafe = SetIteratorSafe< Key >;
74
75#endif /* DOXYGEN_SHOULD_SKIP_THIS */
76
77
78 // ===========================================================================
79 // === GUM_SET ===
80 // ===========================================================================
81
128 template < typename Key >
129 class Set {
130 public:
133 using value_type = Key;
134 using reference = Key&;
135 using const_reference = const Key&;
136 using pointer = Key*;
137 using const_pointer = const Key*;
138 using size_type = std::size_t;
139 using difference_type = std::ptrdiff_t;
145
146 // ============================================================================
148 // ============================================================================
150
163 explicit Set(Size capacity = HashTableConst::default_size, bool resize_policy = true);
164
169 Set(std::initializer_list< Key > list);
170
175 Set(const Set< Key >& aHT);
176
181 Set(Set< Key >&& aHT) noexcept;
182
187
189 // ============================================================================
191 // ============================================================================
193
200
206 Set< Key >& operator=(Set< Key >&& from) noexcept;
207
213 bool operator==(const Set< Key >& s2) const;
214
222
230
238
246
247
255
261 Set< Key >& operator<<(const Key& k);
262
268 Set< Key >& operator<<(Key&& k);
269
275 Set< Key >& operator>>(const Key& k);
276
278 // ============================================================================
280 // ============================================================================
282
289 void insert(const Key& k);
290
297 void insert(Key&& k);
298
309 template < typename... Args >
310 void emplace(Args&&... args);
311
319 void erase(const Key& k);
320
326 Key popFirst();
327
334 void erase(const iterator_safe& k);
335
339 void clear();
340
345 Size size() const noexcept;
346
351 bool contains(const Key& k) const;
352
356 bool isStrictSubsetOf(const Set< Key >& s) const;
357
361 bool isStrictSupersetOf(const Set< Key >& s) const;
362
366 bool isSubsetOrEqual(const Set< Key >& s) const;
367
371 bool isSupersetOrEqual(const Set< Key >& s) const;
372
377 bool exists(const Key& k) const;
378
383 bool empty() const noexcept;
384
389 std::string toString() const;
390
392 // ============================================================================
394 // ============================================================================
396
402
408
413 static const iterator_safe& endSafe() noexcept;
414
419 static const const_iterator_safe& cendSafe() noexcept;
420
426
432
437 static const iterator& end() noexcept;
438
443 static const const_iterator& cend() noexcept;
444
446
447 // ============================================================================
449 // ============================================================================
451
461 Size capacity() const;
462
470 void resize(Size new_capacity);
471
482 void setResizePolicy(const bool new_policy);
483
489 bool resizePolicy() const;
490
492 // ============================================================================
494 // ============================================================================
496
509 template < typename NewKey >
510 HashTable< Key, NewKey > hashMap(NewKey (*f)(const Key&), Size capacity = 0) const;
511
525 template < typename NewKey >
526 HashTable< Key, NewKey > hashMap(const NewKey& val, Size size = 0) const;
527
536 template < typename NewKey >
537 List< NewKey > listMap(NewKey (*f)(const Key&)) const;
538
540
541 private:
544 friend class SetIterator< Key >;
545 friend class SetIteratorSafe< Key >;
547
549 HashTable< Key, bool > _inside_;
550
552 explicit Set(const HashTable< Key, bool >& h);
553 };
554
555 // ===========================================================================
556 // === SAFE SET ITERATORS ===
557 // ===========================================================================
558
591 template < typename Key >
593 public:
596 using iterator_category = std::forward_iterator_tag;
597 using value_type = Key;
601 using const_pointer = const value_type*;
602 using difference_type = std::ptrdiff_t;
603
605
610 enum class Position : unsigned char { BEGIN, END };
611
612 // ============================================================================
614 // ============================================================================
616
620 explicit SetIteratorSafe();
621
622#ifndef DOXYGEN_SHOULD_SKIP_THIS
623# ifndef SWIG
624 // constructor for the static endSafe iterator
625 // only set.cpp should use this constructor
626 // hidden from SWIG: it does not understand consteval and mistakes this
627 // overload for a redefinition of the default constructor above (warning 302)
628 explicit consteval SetIteratorSafe(StaticInitializer init) noexcept : _ht_iter_(init) {}
629# endif // SWIG
630#endif // DOXYGEN_SHOULD_SKIP_THIS
631
641 explicit SetIteratorSafe(const Set< Key >& from, Position pos = Position::BEGIN);
642
648
653 explicit SetIteratorSafe(const SetIterator< Key >& iter);
654
660
665
667 // ============================================================================
669 // ============================================================================
671
677 SetIteratorSafe< Key >& operator=(const SetIteratorSafe< Key >& from);
678
684 SetIteratorSafe< Key >& operator=(const SetIterator< Key >& iter);
685
691 SetIteratorSafe< Key >& operator=(SetIteratorSafe< Key >&& from) noexcept;
692
697 SetIteratorSafe< Key >& operator++() noexcept;
698
704 SetIteratorSafe< Key >& operator+=(Size i) noexcept;
705
711 SetIteratorSafe< Key > operator+(Size i) const;
712
719 bool operator==(const SetIteratorSafe< Key >& from) const noexcept;
720
730 const Key& operator*() const;
731
741 const Key* operator->() const;
742
744 // ============================================================================
746 // ============================================================================
748
753 void clear() noexcept;
754
756
757 private:
759 friend class Set< Key >;
760
763 };
764
765 // ===========================================================================
766 // === UNSAFE SET ITERATORS ===
767 // ===========================================================================
768
805 template < typename Key >
807 public:
810 using iterator_category = std::forward_iterator_tag;
811 using value_type = Key;
815 using const_pointer = const value_type*;
816 using difference_type = std::ptrdiff_t;
817
819
824 enum class Position : unsigned char { BEGIN, END };
825
826 // ============================================================================
828 // ============================================================================
830
834 explicit SetIterator() noexcept;
835
836#ifndef DOXYGEN_SHOULD_SKIP_THIS
837# ifndef SWIG
838 // constructor for the static end iterator
839 // only set.cpp should use this constructor
840 // hidden from SWIG: it does not understand consteval and mistakes this
841 // overload for a redefinition of the default constructor above (warning 302)
842 explicit consteval SetIterator(StaticInitializer init) noexcept : _ht_iter_(init) {}
843# endif // SWIG
844#endif // DOXYGEN_SHOULD_SKIP_THIS
845
855 explicit SetIterator(const Set< Key >& from, Position pos = Position::BEGIN);
856
861 SetIterator(const SetIterator< Key >& iter) noexcept;
862
867 SetIterator(SetIterator< Key >&& from) noexcept;
868
872 ~SetIterator() noexcept;
873
875 // ============================================================================
877 // ============================================================================
879
885 SetIterator< Key >& operator=(const SetIterator< Key >& iter) noexcept;
886
892 SetIterator< Key >& operator=(SetIterator< Key >&& from) noexcept;
893
898 SetIterator< Key >& operator++() noexcept;
899
905 SetIterator< Key >& operator+=(Size nb) noexcept;
906
912 SetIterator< Key > operator+(Size nb) const noexcept;
913
920 bool operator==(const SetIterator< Key >& iter) const noexcept;
921
931 const Key& operator*() const;
932
942 const Key* operator->() const;
943
945 // ============================================================================
947 // ============================================================================
949
954 void clear() noexcept;
955
957
958 private:
960 friend class Set< Key >;
961 friend class SetIteratorSafe< Key >;
962
965 };
966
968 template < typename Key >
969 std::ostream& operator<<(std::ostream&, const Set< Key >&);
970
972 template < typename T >
973 class HashFunc< Set< T > >: public HashFuncBase< Set< T > > {
974 public:
980 static Size castToSize(const Set< T >& key);
981
983 Size operator()(const Set< T >& key) const final;
984 };
985
986#ifndef DOXYGEN_SHOULD_SKIP_THIS
987 // _static_Set_end_ is a 'constant' iterator initialized at compile time
988 // that represents the end iterators for all sets (whatever their
989 // type). This global variable avoids creating the same iterators within every
990 // Set instance (this would be quite inefficient as end is precisely
991 // identical for all sets). The same hold for safe end iterators.
992 // The type of _Set_end_ is a pointer to void because C++ allows
993 // pointers to void to be cast into pointers to other types (and conversely).
994 // This avoids the painful strict-aliasing rule warning
995 extern const SetIterator< int > _static_Set_end_;
996 extern const SetIteratorSafe< int > _static_Set_end_safe_;
997
998 inline constexpr const void* const _Set_end_ = &_static_Set_end_;
999 inline constexpr const void* const _Set_end_safe_ = &_static_Set_end_safe_;
1000#endif // DOXYGEN_SHOULD_SKIP_THIS
1001
1002} /* namespace gum */
1003
1004
1005#if !defined(GUM_NO_EXTERN_TEMPLATE_CLASS) && !defined(SWIG)
1006extern template class gum::Set< int >;
1007extern template class gum::Set< long >;
1008extern template class gum::Set< unsigned int >;
1009extern template class gum::Set< unsigned long >;
1010extern template class gum::Set< double >;
1011extern template class gum::Set< std::string >;
1012#endif
1013
1014
1015// always include the implementation of the templates
1017
1018#endif // GUM_SET_H
Unsafe Const Iterators for hashtables.
Definition hashTable.h:2191
All hash functions should inherit from this class.
Definition hashFunc.h:174
static Size castToSize(const Set< T > &key)
Returns the value of a key as a Size.
Definition set_tpl.h:781
Size operator()(const Set< T > &key) const final
computes the hashed value of a key
Definition set_tpl.h:793
This class should be useless as only its specializations should be used.
Definition hashFunc.h:492
Safe Const Iterators for hashtables.
Definition hashTable.h:1662
The class for generic Hash Tables.
Definition hashTable.h:640
Generic doubly linked lists.
Definition list.h:378
Safe iterators for the Set class.
Definition set.h:592
SetIteratorSafe(const Set< Key > &from, Position pos=Position::BEGIN)
Creates an iterator for a given set.
Definition set_tpl.h:67
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
const value_type * const_pointer
Types for STL compliance.
Definition set.h:601
SetIteratorSafe(const SetIterator< Key > &iter)
Copy constructor.
Definition set_tpl.h:81
Key value_type
Types for STL compliance.
Definition set.h:597
value_type * pointer
Types for STL compliance.
Definition set.h:600
HashTableConstIteratorSafe< const Tensor< GUM_SCALAR > *, bool > _ht_iter_
Definition set.h:762
SetIteratorSafe(const SetIteratorSafe< Key > &from)
Copy constructor.
Definition set_tpl.h:74
const value_type & const_reference
Types for STL compliance.
Definition set.h:599
std::ptrdiff_t difference_type
Types for STL compliance.
Definition set.h:602
std::forward_iterator_tag iterator_category
Types for STL compliance.
Definition set.h:596
value_type & reference
Types for STL compliance.
Definition set.h:598
SetIteratorSafe(SetIteratorSafe< Key > &&from) noexcept
Move constructor.
Definition set_tpl.h:88
~SetIteratorSafe() noexcept
Class destructor.
Definition set_tpl.h:95
Unsafe iterators for the Set class.
Definition set.h:806
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
const value_type * const_pointer
Types for STL compliance.
Definition set.h:815
std::forward_iterator_tag iterator_category
Types for STL compliance.
Definition set.h:810
std::ptrdiff_t difference_type
Types for STL compliance.
Definition set.h:816
value_type * pointer
Types for STL compliance.
Definition set.h:814
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
value_type & reference
Types for STL compliance.
Definition set.h:812
Position
An enumeration to position the iterator at the beginning or the end of the set.
Definition set.h:824
const value_type & const_reference
Types for STL compliance.
Definition set.h:813
Key value_type
Types for STL compliance.
Definition set.h:811
Representation of a set.
Definition set.h:129
Set< Key > & operator=(Set< Key > &&from) noexcept
Move operator.
Definition set_tpl.h:361
Set(Size capacity=HashTableConst::default_size, bool resize_policy=true)
Default constructor.
Definition set_tpl.h:277
bool isSupersetOrEqual(const Set< Edge > &s) const
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< Edge, bool > _inside_
Definition set.h:549
void erase(const iterator_safe &k)
Erases an element from the set.
Definition set_tpl.h:574
bool contains(const Edge &k) const
static const const_iterator_safe & cendSafe() noexcept
Key & reference
Types for STL compliance.
Definition set.h:134
void insert(Key &&k)
Inserts a new element into the set.
Definition set_tpl.h:528
const Key & const_reference
Types for STL compliance.
Definition set.h:135
static const iterator & end() noexcept
List< NewKey > listMap(NewKey(*f)(const Edge &)) const
Set(std::initializer_list< Key > list)
Initializer list constructor.
Definition set_tpl.h:287
Key popFirst()
Removes and returns an arbitrary element from the set.
Definition set_tpl.h:564
~Set()
Class destructor.
bool isSubsetOrEqual(const Set< Edge > &s) const
const_iterator cbegin() const
Key * pointer
Types for STL compliance.
Definition set.h:136
Set(const Set< Key > &aHT)
Copy constructor.
Definition set_tpl.h:297
Set< Key > operator+(const Set< Key > &s2) const
Union operator.
Definition set_tpl.h:665
void setResizePolicy(const bool new_policy)
void resize(Size new_capacity)
bool exists(const Edge &k) const
Key value_type
Types for STL compliance.
Definition set.h:133
void clear()
Removes all the elements, if any, from the set.
Definition set_tpl.h:315
bool resizePolicy() const
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
Size capacity() const
bool isStrictSupersetOf(const Set< Edge > &s) const
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
SetIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition set.h:142
iterator begin() const
std::size_t size_type
Types for STL compliance.
Definition set.h:138
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< Edge > &s) const
iterator_safe beginSafe() const
HashTable< Edge, NewKey > hashMap(NewKey(*f)(const Edge &), Size capacity=0) const
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
friend class SetIterator< Key >
Friends to speed up access.
Definition set.h:544
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
Set(Set< Key > &&aHT) noexcept
Move constructor.
Definition set_tpl.h:303
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:607
const Key * const_pointer
Types for STL compliance.
Definition set.h:137
static const iterator_safe & endSafe() noexcept
std::ptrdiff_t difference_type
Types for STL compliance.
Definition set.h:139
std::string toString() const
static const const_iterator & cend() noexcept
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Class hash tables iterators.
Generic class for manipulating lists.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
Implementation of the Set.
static constexpr Size default_size
The default number of slots in hashtables.
Definition hashTable.h:102