aGrUM 2.3.2
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-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
41
48#ifndef GUM_SET_H
49#define GUM_SET_H
50
51#include <iostream>
52#include <sstream>
53#include <string>
54
58
59#include <initializer_list>
60
61namespace gum {
62
63#ifndef DOXYGEN_SHOULD_SKIP_THIS
64
65 template < typename Key >
66 class SetIteratorSafe;
67 template < typename Key >
68 class SetIterator;
69 template < typename Key >
70 class Set;
71
72 template < typename Key >
73 using SetConstIterator = SetIterator< Key >;
74 template < typename Key >
75 using SetConstIteratorSafe = SetIteratorSafe< Key >;
76
77#endif /* DOXYGEN_SHOULD_SKIP_THIS */
78
79
80 // ===========================================================================
81 // === GUM_SET ===
82 // ===========================================================================
83
130 template < typename Key >
131 class Set {
132 public:
135 using value_type = Key;
136 using reference = Key&;
137 using const_reference = const Key&;
138 using pointer = Key*;
139 using const_pointer = const Key*;
140 using size_type = std::size_t;
141 using difference_type = std::ptrdiff_t;
147
148 // ============================================================================
150 // ============================================================================
152
165 explicit Set(Size capacity = HashTableConst::default_size, bool resize_policy = true);
166
171 Set(std::initializer_list< Key > list);
172
177 Set(const Set< Key >& aHT);
178
184
189
191 // ============================================================================
193 // ============================================================================
195
202
209
215 bool operator==(const Set< Key >& s2) const;
216
222 bool operator!=(const Set< Key >& s2) const;
223
231
239
247
255
256
264
270 Set< Key >& operator<<(const Key& k);
271
277 Set< Key >& operator<<(Key&& k);
278
284 Set< Key >& operator>>(const Key& k);
285
287 // ============================================================================
289 // ============================================================================
291
298 void insert(const Key& k);
299
306 void insert(Key&& k);
307
318 template < typename... Args >
319 void emplace(Args&&... args);
320
328 void erase(const Key& k);
329
335 Key popFirst();
336
343 void erase(const iterator_safe& k);
344
348 void clear();
349
354 Size size() const noexcept;
355
360 bool contains(const Key& k) const;
361
365 bool isStrictSubsetOf(const Set< Key >& s) const;
366
370 bool isStrictSupersetOf(const Set< Key >& s) const;
371
375 bool isSubsetOrEqual(const Set< Key >& s) const;
376
380 bool isSupersetOrEqual(const Set< Key >& s) const;
381
386 bool exists(const Key& k) const;
387
392 bool empty() const noexcept;
393
398 std::string toString() const;
399
401 // ============================================================================
403 // ============================================================================
405
411
417
422 const iterator_safe& endSafe() const noexcept;
423
428 const const_iterator_safe& cendSafe() const noexcept;
429
435
441
446 const iterator& end() const noexcept;
447
452 const const_iterator& cend() const noexcept;
453
455
456 // ============================================================================
458 // ============================================================================
460
470 Size capacity() const;
471
479 void resize(Size new_capacity);
480
491 void setResizePolicy(const bool new_policy);
492
498 bool resizePolicy() const;
499
501 // ============================================================================
503 // ============================================================================
505
518 template < typename NewKey >
519 HashTable< Key, NewKey > hashMap(NewKey (*f)(const Key&), Size capacity = 0) const;
520
534 template < typename NewKey >
535 HashTable< Key, NewKey > hashMap(const NewKey& val, Size size = 0) const;
536
545 template < typename NewKey >
546 List< NewKey > listMap(NewKey (*f)(const Key&)) const;
547
549
550 private:
553 friend class SetIterator< Key >;
554 friend class SetIteratorSafe< Key >;
556
558 HashTable< Key, bool > _inside_;
559
561 Set(const HashTable< Key, bool >& h);
562 };
563
564 // ===========================================================================
565 // === SAFE SET ITERATORS ===
566 // ===========================================================================
567
600 template < typename Key >
602 public:
605 using iterator_category = std::forward_iterator_tag;
606 using value_type = Key;
610 using const_pointer = const value_type*;
611 using difference_type = std::ptrdiff_t;
612
614
619 enum Position { BEGIN, END };
620
621 // ============================================================================
623 // ============================================================================
625
629 explicit SetIteratorSafe();
630
631#ifndef DOXYGEN_SHOULD_SKIP_THIS
632 // constructor for the static endSafe iterator
633 // only set.cpp should use this constructor
634 explicit consteval SetIteratorSafe(StaticInitializer init) noexcept : _ht_iter_(init) {}
635#endif // DOXYGEN_SHOULD_SKIP_THIS
636
647
653
658 explicit SetIteratorSafe(const SetIterator< Key >& from);
659
665
670
672 // ============================================================================
674 // ============================================================================
676
682 SetIteratorSafe< Key >& operator=(const SetIteratorSafe< Key >& from);
683
689 SetIteratorSafe< Key >& operator=(const SetIterator< Key >& from);
690
696 SetIteratorSafe< Key >& operator=(SetIteratorSafe< Key >&& from) noexcept;
697
702 SetIteratorSafe< Key >& operator++() noexcept;
703
709 SetIteratorSafe< Key >& operator+=(Size i) noexcept;
710
716 SetIteratorSafe< Key > operator+(Size i) const;
717
724 bool operator!=(const SetIteratorSafe< Key >& from) const noexcept;
725
732 bool operator==(const SetIteratorSafe< Key >& from) const noexcept;
733
743 const Key& operator*() const;
744
754 const Key* operator->() const;
755
757 // ============================================================================
759 // ============================================================================
761
766 void clear() noexcept;
767
769
770 private:
772 friend class Set< Key >;
773
776 };
777
778 // ===========================================================================
779 // === UNSAFE SET ITERATORS ===
780 // ===========================================================================
781
818 template < typename Key >
820 public:
823 using iterator_category = std::forward_iterator_tag;
824 using value_type = Key;
828 using const_pointer = const value_type*;
829 using difference_type = std::ptrdiff_t;
830
832
837 enum Position { BEGIN, END };
838
839 // ============================================================================
841 // ============================================================================
843
847 explicit SetIterator() noexcept;
848
849#ifndef DOXYGEN_SHOULD_SKIP_THIS
850 // constructor for the static end iterator
851 // only set.cpp should use this constructor
852 explicit consteval SetIterator(StaticInitializer init) noexcept : _ht_iter_(init) {}
853#endif // DOXYGEN_SHOULD_SKIP_THIS
854
864 SetIterator(const Set< Key >& from, Position pos = BEGIN);
865
870 SetIterator(const SetIterator< Key >& from) noexcept;
871
876 SetIterator(SetIterator< Key >&& from) noexcept;
877
881 ~SetIterator() noexcept;
882
884 // ============================================================================
886 // ============================================================================
888
894 SetIterator< Key >& operator=(const SetIterator< Key >& from) noexcept;
895
901 SetIterator< Key >& operator=(SetIterator< Key >&& from) noexcept;
902
907 SetIterator< Key >& operator++() noexcept;
908
914 SetIterator< Key >& operator+=(Size i) noexcept;
915
921 SetIterator< Key > operator+(Size i) const noexcept;
922
929 bool operator!=(const SetIterator< Key >& from) const noexcept;
930
937 bool operator==(const SetIterator< Key >& from) const noexcept;
938
948 const Key& operator*() const;
949
959 const Key* operator->() const;
960
962 // ============================================================================
964 // ============================================================================
966
971 void clear() noexcept;
972
974
975 private:
977 friend class Set< Key >;
978 friend class SetIteratorSafe< Key >;
979
982 };
983
985 template < typename Key >
986 std::ostream& operator<<(std::ostream&, const Set< Key >&);
987
989 template < typename T >
990 class HashFunc< Set< T > >: public HashFuncBase< Set< T > > {
991 public:
997 static Size castToSize(const Set< T >& key);
998
1000 virtual Size operator()(const Set< T >& key) const override final;
1001 };
1002
1003#ifndef DOXYGEN_SHOULD_SKIP_THIS
1004 // _static_Set_end_ is a 'constant' iterator initialized at compile time
1005 // that represents the end iterators for all sets (whatever their
1006 // type). This global variable avoids creating the same iterators within every
1007 // Set instance (this would be quite inefficient as end is precisely
1008 // identical for all sets). The same hold for safe end iterators.
1009 // The type of _Set_end_ is a pointer to void because C++ allows
1010 // pointers to void to be cast into pointers to other types (and conversely).
1011 // This avoids the painful strict-aliasing rule warning
1012 extern const SetIterator< int > _static_Set_end_;
1013 extern const SetIteratorSafe< int > _static_Set_end_safe_;
1014
1015 inline constexpr void* const _Set_end_ = (void* const)&_static_Set_end_;
1016 inline constexpr void* const _Set_end_safe_ = (void* const)&_static_Set_end_safe_;
1017#endif // DOXYGEN_SHOULD_SKIP_THIS
1018
1019} /* namespace gum */
1020
1021
1022#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1023extern template class gum::Set< int >;
1024extern template class gum::Set< long >;
1025extern template class gum::Set< unsigned int >;
1026extern template class gum::Set< unsigned long >;
1027extern template class gum::Set< double >;
1028extern template class gum::Set< std::string >;
1029#endif
1030
1031
1032// always include the implementation of the templates
1034
1035#endif // GUM_SET_H
Unsafe Const Iterators for hashtables.
Definition hashTable.h:2146
All hash functions should inherit from this class.
Definition hashFunc.h:169
virtual Size operator()(const Set< T > &key) const override final
computes the hashed value of a key
Definition set_tpl.h:822
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
Safe Const Iterators for hashtables.
Definition hashTable.h:1602
The class for generic Hash Tables.
Definition hashTable.h:637
Generic doubly linked lists.
Definition list.h:379
Safe iterators for the Set class.
Definition set.h:601
SetIteratorSafe(const Set< Key > &from, Position pos=BEGIN)
Creates an iterator for a given set.
Definition set_tpl.h:66
const value_type * const_pointer
Types for STL compliance.
Definition set.h:610
SetIteratorSafe()
Default constructor: the iterator points toward nothing.
Definition set_tpl.h:60
~SetIteratorSafe() noexcept
Class destructor.
Definition set_tpl.h:95
SetIteratorSafe(const SetIterator< Key > &from)
Copy constructor.
Definition set_tpl.h:81
Key value_type
Types for STL compliance.
Definition set.h:606
value_type * pointer
Types for STL compliance.
Definition set.h:609
HashTableConstIteratorSafe< const Tensor< GUM_SCALAR > *, bool > _ht_iter_
Definition set.h:775
const value_type & const_reference
Types for STL compliance.
Definition set.h:608
SetIteratorSafe(const SetIteratorSafe< Key > &from)
Copy constructor.
Definition set_tpl.h:74
SetIteratorSafe(SetIteratorSafe< Key > &&from)
Move constructor.
Definition set_tpl.h:88
std::ptrdiff_t difference_type
Types for STL compliance.
Definition set.h:611
std::forward_iterator_tag iterator_category
Types for STL compliance.
Definition set.h:605
value_type & reference
Types for STL compliance.
Definition set.h:607
Unsafe iterators for the Set class.
Definition set.h:819
const value_type * const_pointer
Types for STL compliance.
Definition set.h:828
std::forward_iterator_tag iterator_category
Types for STL compliance.
Definition set.h:823
std::ptrdiff_t difference_type
Types for STL compliance.
Definition set.h:829
value_type * pointer
Types for STL compliance.
Definition set.h:827
SetIterator() noexcept
Default constructor: the iterator points toward nothing.
Definition set_tpl.h:189
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
HashTableConstIterator< Key, bool > _ht_iter_
The underlying iterator for the set's hash table containing the data.
Definition set.h:981
value_type & reference
Types for STL compliance.
Definition set.h:825
Position
An enumeration to position the iterator at the beginning or the end of the set.
Definition set.h:837
const value_type & const_reference
Types for STL compliance.
Definition set.h:826
Key value_type
Types for STL compliance.
Definition set.h:824
Representation of a set.
Definition set.h:131
Set(std::initializer_list< Key > list)
Initializer list constructor.
Definition set_tpl.h:310
Set(const Set< Key > &aHT)
Copy constructor.
Definition set_tpl.h:320
iterator begin() const
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
const Set< Key > & operator*=(const Set< Key > &s2)
Intersection update operator.
Definition set_tpl.h:669
HashTable< Edge, bool > _inside_
Definition set.h:558
Key & reference
Types for STL compliance.
Definition set.h:136
const Key & const_reference
Types for STL compliance.
Definition set.h:137
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:636
iterator_safe beginSafe() const
List< NewKey > listMap(NewKey(*f)(const Edge &)) const
Key popFirst()
Removes and returns an arbitrary element from the set.
Definition set_tpl.h:593
~Set()
Class destructor.
Key * pointer
Types for STL compliance.
Definition set.h:138
const iterator_safe & endSafe() const noexcept
Set< Key > operator+(const Set< Key > &s2) const
Union operator.
Definition set_tpl.h:694
Size capacity() const
Key value_type
Types for STL compliance.
Definition set.h:135
bool isSubsetOrEqual(const Set< Edge > &s) const
bool isSupersetOrEqual(const Set< Edge > &s) const
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
std::string toString() const
Set(Size capacity=HashTableConst::default_size, bool resize_policy=true)
Default constructor.
Definition set_tpl.h:300
bool resizePolicy() const
SetIterator< Key > iterator
Types for STL compliance.
Definition set.h:142
bool isStrictSubsetOf(const Set< Edge > &s) const
Set< Key > & operator=(Set< Key > &&from)
Move operator.
Definition set_tpl.h:384
SetIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition set.h:144
const const_iterator & cend() const noexcept
std::size_t size_type
Types for STL compliance.
Definition set.h:140
void emplace(Args &&... args)
Emplace a new element in the set.
void erase(const iterator_safe &k)
Erases an element from the set.
Definition set_tpl.h:603
const Set< Key > & operator+=(const Set< Key > &s2)
Union update operator.
Definition set_tpl.h:682
bool exists(const Edge &k) const
bool operator!=(const Set< Key > &s2) const
Mathematical inequality between two sets.
Definition set_tpl.h:408
void setResizePolicy(const bool new_policy)
HashTable< Edge, NewKey > hashMap(NewKey(*f)(const Edge &), Size capacity=0) const
bool operator==(const Set< Key > &s2) const
Mathematical equality between two sets.
Definition set_tpl.h:391
friend class SetIterator< Key >
Friends to speed up access.
Definition set.h:553
bool isStrictSupersetOf(const Set< Edge > &s) const
bool contains(const Edge &k) const
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)
const_iterator cbegin() const
Set(Set< Key > &&aHT)
Move constructor.
Definition set_tpl.h:326
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:539
const const_iterator_safe & cendSafe() const noexcept
const Key * const_pointer
Types for STL compliance.
Definition set.h:139
bool empty() const noexcept
Set< Key > & operator>>(const Key &k)
Removes an element from the set (alias for erase).
Definition set_tpl.h:629
void insert(Key &&k)
Inserts a new element into the set.
Definition set_tpl.h:557
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:582
std::ptrdiff_t difference_type
Types for STL compliance.
Definition set.h:141
void clear()
Removes all the elements, if any, from the set.
Definition set_tpl.h:338
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:101