aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
set_tpl.h
Go to the documentation of this file.
1 
30 #include <agrum/tools/core/set.h>
31 
32 namespace gum {
33 
34  // ===========================================================================
35  // === SAFE SET ITERATORS ===
36  // ===========================================================================
37 
38  // default constructor: the iterator points toward nothing
39  template < typename Key >
41  GUM_CONSTRUCTOR(SetIteratorSafe);
42  }
43 
44  // creates an iterator for a given set
45  template < typename Key >
46  template < typename Alloc >
48  Position pos) :
49  __ht_iter{pos == SetIteratorSafe< Key >::END ? set.__inside.cendSafe()
50  : set.__inside.cbeginSafe()} {
51  GUM_CONSTRUCTOR(SetIteratorSafe);
52  }
53 
54  // copy constructor
55  template < typename Key >
56  INLINE
58  __ht_iter{iter.__ht_iter} {
59  GUM_CONS_CPY(SetIteratorSafe);
60  }
61 
62  // copy constructor
63  template < typename Key >
65  __ht_iter{iter.__ht_iter} {
66  GUM_CONS_CPY(SetIteratorSafe);
67  }
68 
69  // move constructor
70  template < typename Key >
72  __ht_iter{std::move(from.__ht_iter)} {
73  GUM_CONS_MOV(SetIteratorSafe);
74  }
75 
76  // destructor
77  template < typename Key >
79  GUM_DESTRUCTOR(SetIteratorSafe);
80  }
81 
82  // assignment operator
83  template < typename Key >
86  __ht_iter = from.__ht_iter;
87  return *this;
88  }
89 
90  // assignment operator
91  template < typename Key >
94  __ht_iter = from.__ht_iter;
95  return *this;
96  }
97 
98  // move operator
99  template < typename Key >
100  INLINE SetIteratorSafe< Key >&
102  __ht_iter = std::move(from.__ht_iter);
103  return *this;
104  }
105 
106  // increments the iterator
107  template < typename Key >
109  // note that, if the hashtable's iterator points toward nothing, the
110  // hashtable's iterator incrementation will do nothing. In particular, it
111  // will not segfault.
112  ++__ht_iter;
113  return *this;
114  }
115 
116  // makes the iterator point to i elements further in the set
117  template < typename Key >
118  INLINE SetIteratorSafe< Key >&
120  __ht_iter += nb;
121  return *this;
122  }
123 
124  // returns a new iterator
125  template < typename Key >
127  return SetIteratorSafe< Key >{*this} += nb;
128  }
129 
130  // indicates whether two iterators point to different elements or sets
131  template < typename Key >
132  INLINE bool
134  noexcept {
135  return __ht_iter != from.__ht_iter;
136  }
137 
138  // indicates whether two iterators point toward the same element of a same
139  // set
140  template < typename Key >
141  INLINE bool
143  noexcept {
144  return __ht_iter == from.__ht_iter;
145  }
146 
147  // returns the element pointed to by the iterator
148  template < typename Key >
149  INLINE const Key& SetIteratorSafe< Key >::operator*() const {
150  // note that, if the hashtable's iterator points toward nothing, it will
151  // raise an UndefinedIteratorValue exception
152  return __ht_iter.key();
153  }
154 
155  // returns aointer to the element pointed to by the iterator
156  template < typename Key >
157  INLINE const Key* SetIteratorSafe< Key >::operator->() const {
158  // note that, if the hashtable's iterator points toward nothing, it will
159  // raise an UndefinedIteratorValue exception
160  return &(__ht_iter.key());
161  }
162 
163  // @brief makes the iterator point toward nothing (in particular, it is not
164  // related anymore to its current set) */
165  template < typename Key >
166  INLINE void SetIteratorSafe< Key >::clear() noexcept {
167  __ht_iter.clear();
168  }
169 
170  // ===========================================================================
171  // === UNSAFE SET ITERATORS ===
172  // ===========================================================================
173 
174  // default constructor: the iterator points toward nothing
175  template < typename Key >
177  GUM_CONSTRUCTOR(SetIterator);
178  }
179 
180  // creates an iterator for a given set
181  template < typename Key >
182  template < typename Alloc >
184  Position pos) :
185  __ht_iter{pos == SetIterator< Key >::END ? set.__inside.cend()
186  : set.__inside.cbegin()} {
187  GUM_CONSTRUCTOR(SetIterator);
188  }
189 
190  // copy constructor
191  template < typename Key >
193  __ht_iter{iter.__ht_iter} {
194  GUM_CONS_CPY(SetIterator);
195  }
196 
197  // move constructor
198  template < typename Key >
200  __ht_iter{std::move(from.__ht_iter)} {
201  GUM_CONS_MOV(SetIterator);
202  }
203 
204  // destructor
205  template < typename Key >
207  GUM_DESTRUCTOR(SetIterator);
208  }
209 
210  // assignment operator
211  template < typename Key >
212  INLINE SetIterator< Key >&
214  __ht_iter = from.__ht_iter;
215  return *this;
216  }
217 
218  // move operator
219  template < typename Key >
220  INLINE SetIterator< Key >&
222  __ht_iter = std::move(from.__ht_iter);
223  return *this;
224  }
225 
226  // increments the iterator
227  template < typename Key >
229  // note that, if the hashtable's iterator points toward nothing, the
230  // hashtable's iterator incrementation will do nothing. In particular, it
231  // will not segfault.
232  ++__ht_iter;
233  return *this;
234  }
235 
236  // makes the iterator point to i elements further in the set
237  template < typename Key >
239  __ht_iter += nb;
240  return *this;
241  }
242 
243  // returns a new iterator
244  template < typename Key >
246  return SetIterator< Key >{*this} += nb;
247  }
248 
249  // indicates whether two iterators point to different elements or sets
250  template < typename Key >
251  INLINE bool SetIterator< Key >::operator!=(const SetIterator< Key >& from) const
252  noexcept {
253  return __ht_iter != from.__ht_iter;
254  }
255 
256  // indicates whether two iterators point toward the same element of a same
257  // set
258  template < typename Key >
259  INLINE bool SetIterator< Key >::operator==(const SetIterator< Key >& from) const
260  noexcept {
261  return __ht_iter == from.__ht_iter;
262  }
263 
264  // returns the element pointed to by the iterator
265  template < typename Key >
266  INLINE const Key& SetIterator< Key >::operator*() const {
267  // note that, if the hashtable's iterator points toward nothing, it will
268  // raise an UndefinedIteratorValue exception
269  return __ht_iter.key();
270  }
271 
272  // returns aointer to the element pointed to by the iterator
273  template < typename Key >
274  INLINE const Key* SetIterator< Key >::operator->() const {
275  // note that, if the hashtable's iterator points toward nothing, it will
276  // raise an UndefinedIteratorValue exception
277  return &(__ht_iter.key());
278  }
279 
280  // @brief makes the iterator point toward nothing (in particular, it is not
281  // related anymore to its current set) */
282  template < typename Key >
283  INLINE void SetIterator< Key >::clear() noexcept {
284  __ht_iter.clear();
285  }
286 
287  // ===========================================================================
288  // === SETS ===
289  // ===========================================================================
290 
291  // returns the end iterator for other classes' statics
292  template < typename Key, typename Alloc >
294  return *(reinterpret_cast< const SetIteratorSafe< Key >* >(
295  SetIteratorStaticEnd::endSafe4Statics()));
296  }
297 
298  // returns the end iterator for other classes' statics
299  template < typename Key, typename Alloc >
301  return *(reinterpret_cast< const SetIteratorSafe< Key >* >(
302  SetIteratorStaticEnd::constEndSafe4Statics()));
303  }
304 
305  // returns the end iterator for other classes' statics
306  template < typename Key, typename Alloc >
308  return *(reinterpret_cast< const SetIterator< Key >* >(
309  SetIteratorStaticEnd::end4Statics()));
310  }
311 
312  // returns the end iterator for other classes' statics
313  template < typename Key, typename Alloc >
315  return *(reinterpret_cast< const SetIterator< Key >* >(
316  SetIteratorStaticEnd::constEnd4Statics()));
317  }
318 
319  // default constructor
320  template < typename Key, typename Alloc >
321  INLINE Set< Key, Alloc >::Set(Size capacity, bool resize_policy) :
322  // create the hash table without key uniqueness policy (as we will
323  // check
324  // ourselves the uniqueness of Keys before inserting new elements)
325  __inside(capacity, resize_policy, false) {
326  GUM_CONSTRUCTOR(Set);
327 
328  // make sure the end() iterator is constructed properly
329  endSafe4Statics();
330  end4Statics();
331  }
332 
333  // initializer list constructor
334  template < typename Key, typename Alloc >
335  INLINE Set< Key, Alloc >::Set(std::initializer_list< Key > list) :
336  __inside(Size(list.size()) / 2, true, false) {
337  GUM_CONSTRUCTOR(Set);
338  for (const auto& elt: list) {
339  insert(elt);
340  }
341 
342  // make sure the end() iterator is constructed properly
343  endSafe4Statics();
344  end4Statics();
345  }
346 
347  // copy constructor
348  template < typename Key, typename Alloc >
350  __inside(s.__inside) {
351  GUM_CONS_CPY(Set);
352  }
353 
354  // generalized copy constructor
355  template < typename Key, typename Alloc >
356  template < typename OtherAlloc >
358  __inside(s.__inside) {
359  GUM_CONS_CPY(Set);
360  }
361 
362  // move constructor
363  template < typename Key, typename Alloc >
365  __inside(std::move(s.__inside)) {
366  GUM_CONS_MOV(Set);
367  }
368 
369  // destructor
370  template < typename Key, typename Alloc >
372  GUM_DESTRUCTOR(Set);
373  }
374 
375  // removes all the elements, if any, from the set
376  template < typename Key, typename Alloc >
377  INLINE void Set< Key, Alloc >::clear() {
378  // first we remove all the elements from the hashtable actually containing
379  // the elements of the set. Note that, doing so, all the hashtable iterators
380  // will be updated as well. In turn, this will imply that, whenever an
381  // operation will be performed on a SetIteratorSafe, this will raise an
382  // exception.
383  __inside.clear();
384 
385  // Note that actually there is no need to update the end iterator as this
386  // one
387  // is not affected by changes within hashtables (adding/deleting elements).
388  // Hence, for speedup, we do not update the end iterator
389  }
390 
391  // copy operator
392  template < typename Key, typename Alloc >
394  // avoid self assignment
395  if (&s != this) {
396  // remove the old content of the set. Actually, we remove all the elements
397  // from the underlying hashtable. Note that, doing so, all the hashtable
398  // iterators will be updated as well. In turn, this will imply that,
399  // whenever
400  // an operation will be performed on a SetIteratorSafe, this will raise an
401  // exception.
402  clear();
403 
404  // prepare the set for its new data
405  resize(s.capacity());
406  setResizePolicy(s.resizePolicy());
407 
408  // copy the set
409  __inside = s.__inside;
410 
411  // Note that actually there is no need to update the end iterator as this
412  // one
413  // is not affected by changes within hashtables (adding/deleting
414  // elements).
415  // Hence, for speedup, we do not update the end iterator
416  }
417 
418  return *this;
419  }
420 
421  // generalized copy operator
422  template < typename Key, typename Alloc >
423  template < typename OtherAlloc >
426  // avoid self assignment
427  if (this != reinterpret_cast< const Set< Key, Alloc >* >(&s)) {
428  // remove the old content of the set. Actually, we remove all the elements
429  // from the underlying hashtable. Note that, doing so, all the hashtable
430  // iterators will be updated as well. In turn, this will imply that,
431  // whenever
432  // an operation will be performed on a SetIteratorSafe, this will raise an
433  // exception.
434  clear();
435 
436  // prepare the set for its new data
437  resize(s.capacity());
438  setResizePolicy(s.resizePolicy());
439 
440  // copy the set
441  __inside = s.__inside;
442 
443  // Note that actually there is no need to update the end iterator as this
444  // one
445  // is not affected by changes within hashtables (adding/deleting
446  // elements).
447  // Hence, for speedup, we do not update the end iterator
448  }
449 
450  return *this;
451  }
452 
453  // move operator
454  template < typename Key, typename Alloc >
456  __inside = std::move(from.__inside);
457  return *this;
458  }
459 
460  // mathematical equality between two sets
461  template < typename Key, typename Alloc >
462  template < typename OtherAlloc >
465 
466  // check whether both sets have the same number of elements
467  if (size() != h2.size()) return false;
468 
469  // check the content of the sets
470  for (HashTableConstIterator< Key, bool > iter = __inside.cbegin();
471  iter != __inside.cend();
472  ++iter) {
473  if (!h2.exists(iter.key())) return false;
474  }
475 
476  return true;
477  }
478 
479  // mathematical inequality between two sets
480  template < typename Key, typename Alloc >
481  template < typename OtherAlloc >
482  INLINE bool
484  return !(operator==(s2));
485  }
486 
487  // the usual begin iterator to parse the set
488  template < typename Key, typename Alloc >
489  INLINE typename Set< Key, Alloc >::iterator_safe
491  return SetIteratorSafe< Key >{*this};
492  }
493 
494  // the usual begin iterator to parse the set
495  template < typename Key, typename Alloc >
498  return SetIteratorSafe< Key >{*this};
499  }
500 
501  // the usual end iterator to parse the set
502  template < typename Key, typename Alloc >
503  INLINE const typename Set< Key, Alloc >::iterator_safe&
504  Set< Key, Alloc >::endSafe() const noexcept {
505  return *(reinterpret_cast< const SetIteratorSafe< Key >* >(
506  SetIteratorStaticEnd::__SetIterEndSafe));
507  }
508 
509  // the usual end iterator to parse the set
510  template < typename Key, typename Alloc >
511  INLINE const typename Set< Key, Alloc >::const_iterator_safe&
512  Set< Key, Alloc >::cendSafe() const noexcept {
513  return *(reinterpret_cast< const SetIteratorSafe< Key >* >(
514  SetIteratorStaticEnd::__SetIterEndSafe));
515  }
516 
517  // the usual begin iterator to parse the set
518  template < typename Key, typename Alloc >
520  return SetIterator< Key >{*this};
521  }
522 
523  // the usual begin iterator to parse the set
524  template < typename Key, typename Alloc >
525  INLINE typename Set< Key, Alloc >::const_iterator
527  return SetIterator< Key >{*this};
528  }
529 
530  // the usual end iterator to parse the set
531  template < typename Key, typename Alloc >
533  noexcept {
534  return *(reinterpret_cast< const SetIterator< Key >* >(
535  SetIteratorStaticEnd::__SetIterEnd));
536  }
537 
538  // the usual end iterator to parse the set
539  template < typename Key, typename Alloc >
540  INLINE const typename Set< Key, Alloc >::const_iterator&
541  Set< Key, Alloc >::cend() const noexcept {
542  return *(reinterpret_cast< const SetIterator< Key >* >(
543  SetIteratorStaticEnd::__SetIterEnd));
544  }
545 
546  // returns the size of the underlying hashtable containing the set
547  template < typename Key, typename Alloc >
549  return __inside.capacity();
550  }
551 
552  // changes the size of the underlying hashtable
553  template < typename Key, typename Alloc >
554  INLINE void Set< Key, Alloc >::resize(Size new_size) {
555  __inside.resize(new_size);
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  // enables the user to change dynamically the resizing policy of the
564  // underlying hashtable
565  template < typename Key, typename Alloc >
566  INLINE void Set< Key, Alloc >::setResizePolicy(const bool new_policy) {
567  __inside.setResizePolicy(new_policy);
568 
569  // Note that actually there is no need to update the end iterator as this
570  // one
571  // is not affected by changes within hashtables (adding/deleting elements).
572  // Hence, for speedup, we do not update the end iterator
573  }
574 
575  // returns the current resizing policy of the underlying hashtable
576  template < typename Key, typename Alloc >
577  INLINE bool Set< Key, Alloc >::resizePolicy() const {
578  return __inside.resizePolicy();
579  }
580 
581  // indicates whether a given elements belong to the set
582  template < typename Key, typename Alloc >
583  INLINE bool Set< Key, Alloc >::contains(const Key& k) const {
584  return __inside.exists(k);
585  }
586 
587 
588  template < typename Key, typename Alloc >
589  template < typename OtherAlloc >
590  INLINE bool
592  if (this->size() >= s.size()) { return false; }
593 
594  for (const auto& elt: *this) {
595  if (!s.contains(elt)) { return false; }
596  }
597  return true;
598  }
599 
600  template < typename Key, typename Alloc >
601  template < typename OtherAlloc >
602  INLINE bool
604  return s.isSubsetOf(*this);
605  }
606 
607  // indicates whether a given elements belong to the set
608  template < typename Key, typename Alloc >
609  INLINE bool Set< Key, Alloc >::exists(const Key& k) const {
610  return __inside.exists(k);
611  }
612 
613  // inserts a new element in the set
614  template < typename Key, typename Alloc >
615  INLINE void Set< Key, Alloc >::insert(const Key& k) {
616  // WARNING: we shall always test whether k already belongs to the set before
617  // trying to insert it because we set __inside's key uniqueness policy to
618  // false
619  if (!contains(k)) {
620  // insert the element
621  __inside.insert(k, true);
622 
623  // Note that actually there is no need to update the end iterator as this
624  // one
625  // is not affected by changes within hashtables (adding/deleting
626  // elements).
627  // Hence, for speedup, we do not update the end iterator
628  }
629  }
630 
631  // inserts a new element in the set
632  template < typename Key, typename Alloc >
633  INLINE void Set< Key, Alloc >::insert(Key&& k) {
634  // WARNING: we shall always test whether k already belongs to the set before
635  // trying to insert it because we set __inside's key uniqueness policy to
636  // false
637  if (!contains(k)) {
638  // insert the element
639  __inside.insert(std::move(k), true);
640 
641  // Note that actually there is no need to update the end iterator as this
642  // one
643  // is not affected by changes within hashtables (adding/deleting
644  // elements).
645  // Hence, for speedup, we do not update the end iterator
646  }
647  }
648 
649  // emplace a new element in the set
650  template < typename Key, typename Alloc >
651  template < typename... Args >
652  INLINE void Set< Key, Alloc >::emplace(Args&&... args) {
653  insert(std::move(Key(std::forward< Args >(args)...)));
654  }
655 
656  // erases an element from the set
657  template < typename Key, typename Alloc >
658  INLINE void Set< Key, Alloc >::erase(const Key& k) {
659  // erase the element (if it exists)
660  __inside.erase(k);
661 
662  // Note that actually there is no need to update the end iterator as this
663  // one
664  // is not affected by changes within hashtables (adding/deleting elements).
665  // Hence, for speedup, we do not update the end iterator
666  }
667 
668  // erases an element from the set
669  template < typename Key, typename Alloc >
671  // erase the element
672  __inside.erase(iter.__ht_iter);
673 
674  // Note that actually there is no need to update the end iterator as this
675  // one
676  // is not affected by changes within hashtables (adding/deleting elements).
677  // Hence, for speedup, we do not update the end iterator
678  }
679 
680  // adds a new element to the set
681  template < typename Key, typename Alloc >
683  insert(k);
684  return *this;
685  }
686 
687  // adds a new element to the set
688  template < typename Key, typename Alloc >
690  insert(std::move(k));
691  return *this;
692  }
693 
694  // removes an element from the set
695  template < typename Key, typename Alloc >
697  erase(k);
698  return *this;
699  }
700 
701  // returns the number of elements in the set
702  template < typename Key, typename Alloc >
703  INLINE Size Set< Key, Alloc >::size() const noexcept {
704  return __inside.size();
705  }
706 
707  // indicates whether the set is the empty set
708  template < typename Key, typename Alloc >
709  INLINE bool Set< Key, Alloc >::empty() const noexcept {
710  return __inside.empty();
711  }
712 
713  // Intersection operator
714  template < typename Key, typename Alloc >
715  template < typename OtherAlloc >
718  Set< Key, Alloc > res;
721 
722  if (size() < h2.size()) {
723  for (HashTableConstIterator< Key, bool > iter = __inside.cbegin();
724  iter != __inside.cend();
725  ++iter) {
726  if (h2.exists(iter.key())) h_r.insert(iter.key(), true);
727  }
728  } else {
730  iter != h2.cend();
731  ++iter) {
732  if (__inside.exists(iter.key())) h_r.insert(iter.key(), true);
733  }
734  }
735 
736  return res;
737  }
738 
739 
740  // Intersection update operator
741  template < typename Key, typename Alloc >
742  template < typename OtherAlloc >
743  const Set< Key, Alloc >&
745  if (&s2 != this) {
747  for (auto iter = __inside.beginSafe(); iter != __inside.endSafe(); ++iter) {
748  if (!h2.exists(iter.key())) __inside.erase(iter);
749  }
750  }
751 
752  return *this;
753  }
754 
755 
756  // Union update operator
757  template < typename Key, typename Alloc >
758  template < typename OtherAlloc >
759  const Set< Key, Alloc >&
761  if (&s2 != this) {
762  for (auto pair: s2.__inside) {
763  if (!__inside.exists(pair.first)) __inside.insert(pair.first, true);
764  }
765  }
766 
767  return *this;
768  }
769 
770 
771  // Union operator
772  template < typename Key, typename Alloc >
773  template < typename OtherAlloc >
776  Set< Key, Alloc > res = *this;
779 
780  for (HashTableConstIterator< Key, bool > iter = h2.cbegin(); iter != h2.cend();
781  ++iter) {
782  if (!h_r.exists(iter.key())) h_r.insert(iter.key(), true);
783  }
784 
785  return res;
786  }
787 
788 
789  // Disjunction operator
790  template < typename Key, typename Alloc >
791  template < typename OtherAlloc >
794  Set< Key, Alloc > res;
797 
798  for (HashTableConstIterator< Key, bool > iter = __inside.cbegin();
799  iter != __inside.cend();
800  ++iter)
801  if (!h2.exists(iter.key())) h_r.insert(iter.key(), true);
802 
803  return res;
804  }
805 
806  // to display the content of the set
807  template < typename Key, typename Alloc >
808  INLINE std::string Set< Key, Alloc >::toString() const {
809  std::stringstream out;
810  bool first = true;
811  out << "{";
812 
813  for (iterator iter = begin(); iter != end(); ++iter) {
814  if (first) {
815  out << *iter;
816  first = false;
817  } else {
818  out << "," << *iter;
819  }
820  }
821 
822  out << "}";
823 
824  std::string res;
825  out >> res;
826  return res;
827  }
828 
829  // to friendly display the content of the set
830  template < typename Key, typename Alloc >
831  std::ostream& operator<<(std::ostream& stream, const Set< Key, Alloc >& set) {
832  stream << set.toString();
833  return stream;
834  }
835 
836  // creates a hashtable of NewKey from the set
837  template < typename Key, typename Alloc >
838  template < typename NewKey, typename NewAlloc >
840  Set< Key, Alloc >::hashMap(NewKey (*f)(const Key&), Size size) const {
841  // determine the proper size of the hashtable
842  // by default, the size of the table is set so that the table does not take
843  // too much space while allowing to add a few elements without resizing
844  if (size == 0) size = std::max(Size(2), __inside.size() / 2);
845 
846  // create a new table
848 
849  // fill the new hash table
850  for (HashTableConstIterator< Key, bool > iter = __inside.cbegin();
851  iter != __inside.cend();
852  ++iter) {
853  table.insert(iter.key(), f(iter.key()));
854  }
855 
856  return table;
857  }
858 
859  // creates a hashtable of NewKey from the set
860  template < typename Key, typename Alloc >
861  template < typename NewKey, typename NewAlloc >
863  Size size) const {
864  // determine the proper size of the hashtable
865  // by default, the size of the table is set so that the table does not take
866  // too much space while allowing to add a few elements without resizing
867  if (size == 0) size = std::max(Size(2), __inside.size() / 2);
868 
869  // create a new table
871 
872  // fill the new hash table
873  for (HashTableConstIterator< Key, bool > iter = __inside.cbegin();
874  iter != __inside.cend();
875  ++iter) {
876  table.insert(iter.key(), val);
877  }
878 
879  return table;
880  }
881 
882  // a method to create a list of NewKey from the set
883  template < typename Key, typename Alloc >
884  template < typename NewKey, typename NewAlloc >
886  Set< Key, Alloc >::listMap(NewKey (*f)(const Key&)) const {
887  // create a new list
889 
890  // fill the new list
891  for (HashTableConstIterator< Key, bool > iter = __inside.cbegin();
892  iter != __inside.cend();
893  ++iter) {
894  list.pushBack(f(iter.key()));
895  }
896 
897  return list;
898  }
899 
900 
901  // Returns the value of a key as a Size
902  template < typename T, typename Alloc >
903  INLINE Size HashFunc< Set< T, Alloc > >::castToSize(const Set< T, Alloc >& key) {
904  Size h = Size(0);
905  Size i = Size(0);
906  for (const auto& k: key) {
907  h += ++i * HashFunc< T >::castToSize(k);
908  }
909 
910  return h;
911  }
912 
913 
914  // Returns the hashed value of a key.
915  template < typename T, typename Alloc >
916  INLINE Size
917  HashFunc< Set< T, Alloc > >::operator()(const Set< T, Alloc >& key) const {
918  return (castToSize(key) * HashFuncConst::gold) & this->_hash_mask;
919  }
920 
921 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:583
const key_type & key() const
Returns the key corresponding to the element pointed to by the iterator.
bool empty() const noexcept
Indicates whether the set is the empty set.
Definition: set_tpl.h:709
SetIterator< Key > operator+(Size i) const noexcept
Returns a new iterator.
Definition: set_tpl.h:245
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
static const const_iterator_safe & constEndSafe4Statics()
Returns the end iterator for other classes&#39; statics (read the detailed description of this method)...
Definition: set_tpl.h:300
bool operator!=(const SetIterator< Key > &from) const noexcept
Indicates whether two iterators point to different elements or sets.
Definition: set_tpl.h:251
Safe iterators for the Set classDevelopers may consider using Set<x>::iterator_safe instead of SetIte...
Definition: set.h:811
Size capacity() const
Returns the capacity of the underlying hash table containing the set.
Definition: set_tpl.h:548
Size size() const noexcept
Returns the number of elements stored into the hashtable.
bool isSubsetOf(const Set< Key, OtherAlloc > &s) const
STL namespace.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
Class template representing hashing function of LpCol.
Definition: hashFunc.h:471
void erase(const Key &k)
Erases an element from the set.
Definition: set_tpl.h:658
void clear() noexcept
Makes the iterator point toward nothing (in particular, it is not related anymore to its current hash...
Generic doubly linked lists.
Definition: list.h:372
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
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:259
The class for generic Hash Tables.
Definition: hashTable.h:679
SetIterator() noexcept
Default constructor: the iterator points toward nothing.
Definition: set_tpl.h:176
std::istream & operator>>(std::istream &in, TiXmlNode &base)
Definition: tinyxml.cpp:1505
Representation of a setA Set is a structure that contains arbitrary elements.
Definition: set.h:165
static const iterator & end4Statics()
Returns the end iterator for other classes&#39; statics (read the detailed description of this method)...
Definition: set_tpl.h:307
HashTableConstIteratorSafe< Key, bool > __ht_iter
The underlying iterator for the set&#39;s hash table containing the data.
Definition: set.h:981
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:609
Position
An enumeration to position the iterator at the beginning or the end of the set.
Definition: set.h:1042
std::ostream & operator<<(std::ostream &output, const BayesNet< GUM_SCALAR > &bn)
Prints map&#39;s DAG in output using the Graphviz-dot format.
Definition: BayesNet_tpl.h:626
void resize(Size new_capacity)
Changes the size of the underlying hash table containing the set.
Definition: set_tpl.h:554
HashTableConstIterator< Key, bool > __ht_iter
The underlying iterator for the set&#39;s hash table containing the data.
Definition: set.h:1183
bool resizePolicy() const
Returns the current resizing policy of the underlying hash table.
Definition: set_tpl.h:577
Position
An enumeration to position the iterator at the beginning or the end of the set.
Definition: set.h:828
const const_iterator & cend() const noexcept
Returns the unsafe const_iterator pointing to the end of the hashtable.
LpExpr operator+(LpExpr &&lhs, const T2 &rhs)
Overload of operator + between anything ( a scalar, a variable or an expression ) and anything except...
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1592
bool operator==(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:243
void setResizePolicy(const bool new_policy)
Enables the user to change dynamically the resizing policy of the underlying hash table...
Definition: set_tpl.h:566
static const iterator_safe & endSafe4Statics()
Returns the end iterator for other classes&#39; statics (read the detailed description of this method)...
Definition: set_tpl.h:293
SetIterator< Key > & operator+=(Size i) noexcept
Makes the iterator point to i elements further in the set.
Definition: set_tpl.h:238
void clear() noexcept
makes the iterator point toward nothing (in particular, it is not related anymore to its current set)...
Definition: set_tpl.h:166
friend class Set
Friends to speed up access.
Definition: set.h:763
const_iterator cbegin() const
Returns an unsafe const_iterator pointing to the beginning of the hashtable.
const Key * operator->() const
Returns a pointer to the element pointed to by the iterator.
Definition: set_tpl.h:274
Unsafe iterators for the Set class.
Definition: set.h:1025
LpExpr operator*(const SCALAR &lhs, const LpCol &rhs)
Overload of operator * between a scalar and a variable.
SetIterator< Key > & operator++() noexcept
Increments the iterator.
Definition: set_tpl.h:228
bool operator!=(const TiXmlString &a, const TiXmlString &b)
Definition: tinystr.h:251
void clear() noexcept
makes the iterator point toward nothing (in particular, it is not related anymore to its current set)...
Definition: set_tpl.h:283
HashTable< Key, bool, Alloc > __inside
A set of X&#39;s is actually a hash table whose keys are the X&#39;s.
Definition: set.h:767
~SetIterator() noexcept
Class destructor.
Definition: set_tpl.h:206
void clear()
Removes all the elements, if any, from the set.
Definition: set_tpl.h:377
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
LpExpr operator-(LpExpr &&lhs, const T2 &rhs)
Overload of operator - between anything ( a scalar, a variable or an expression ) and anything except...
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:703
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
SetIteratorSafe()
Default constructor: the iterator points toward nothing.
Definition: set_tpl.h:40
static const const_iterator & constEnd4Statics()
Returns the end iterator for other classes&#39; statics (read the detailed description of this method)...
Definition: set_tpl.h:314
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:615
const Key & operator*() const
Returns the element pointed to by the iterator.
Definition: set_tpl.h:266
SetIterator< Key > & operator=(const SetIterator< Key > &from) noexcept
Assignment operator.
Definition: set_tpl.h:213