aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
sequence.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
49#ifndef GUM_SEQUENCE_H
50#define GUM_SEQUENCE_H
51
52#include <limits>
53#include <vector>
54
55#include <agrum/agrum.h>
56
57#include <agrum/base/core/set.h>
58
59#include <initializer_list>
60#include <type_traits>
61
62namespace gum {
63
64#ifndef DOXYGEN_SHOULD_SKIP_THIS
65 template < typename Key, bool >
67 template < typename Key >
68 class Sequence;
69 template < typename Key >
71 template < typename Key >
72 using SequenceIterator = SequenceIteratorSafe< Key >;
73 template < typename Key >
74 using SequenceConstIterator = SequenceIteratorSafe< Key >;
75#endif
76
77 // ===========================================================================
78 // === NON SCALAR GUM SEQUENCE IMPLEMENTATION ===
79 // ===========================================================================
108 template < typename Key, bool Gen >
112 friend class SequenceIteratorSafe< Key >;
113 friend class Sequence< Key >;
115
116 public:
119 using value_type = Key;
120 using reference = Key&;
121 using const_reference = const Key&;
122 using pointer = Key*;
123 using const_pointer = const Key*;
124 using size_type = std::size_t;
125 using difference_type = std::ptrdiff_t;
126 using iterator = SequenceIterator< Key >;
127 using const_iterator = SequenceIterator< Key >;
131
132 private:
133 // ============================================================================
135 // ============================================================================
137
142
147 SequenceImplementation(std::initializer_list< Key > list);
148
157
163
165
166 public:
167 // ============================================================================
169 // ============================================================================
171
176
178 // ============================================================================
180 // ============================================================================
182
188
194
199 const iterator_safe& endSafe() const noexcept;
200
205 const iterator_safe& rendSafe() const noexcept;
206
212
218
223 const iterator& end() const noexcept;
224
229 const iterator& rend() const noexcept;
230
232
233 private:
234 // ============================================================================
236 // ============================================================================
238
244 SequenceImplementation< Key, Gen >& operator=(const SequenceImplementation< Key, Gen >& aSeq);
245
251 SequenceImplementation< Key, Gen >& operator=(SequenceImplementation< Key, Gen >&& aSeq);
252
254
255 public:
256 // ============================================================================
258 // ============================================================================
260
267 SequenceImplementation< Key, Gen >& operator<<(const Key& k);
268
275 SequenceImplementation< Key, Gen >& operator<<(Key&& k);
276
286 SequenceImplementation< Key, Gen >& operator>>(const Key& k);
287
294 const Key& operator[](Idx i) const;
295
305 bool operator==(const SequenceImplementation< Key, Gen >& k) const;
306
316 bool operator!=(const SequenceImplementation< Key, Gen >& k) const;
317
319 // ============================================================================
321 // ============================================================================
323
327 void clear();
328
333 Size size() const noexcept;
334
339 bool empty() const noexcept;
340
349 bool exists(const Key& k) const;
350
359 void insert(const Key& k);
360
369 void insert(Key&& k);
370
381 template < typename... Args >
382 void emplace(Args&&... args);
383
393 void erase(const Key& k);
394
404 void erase(const iterator_safe& k);
405
412 const Key& atPos(Idx i) const;
413
421 Idx pos(const Key& key) const;
422
431 void setAtPos(Idx i, const Key& newKey);
432
441 void setAtPos(Idx i, Key&& newKey);
442
448 void swap(Idx i, Idx j);
449
455 const Key& front() const;
456
462 const Key& back() const;
463
468 std::string toString() const;
469
482 void resize(Size new_size);
483
485
486 private:
489
491 std::vector< Key* > _v_;
492
493 // Note that, using Key* in _v_, we actually store the Key only once in the
494 // sequence (that is, within _h_). This enables storing big objects within
495 // sequences without having memory overhead.
496
499
502
507 void _update_end_() noexcept;
508
514 void _copy_(const SequenceImplementation< Key, Gen >& aSeq);
515
521 void _insert_(HashTableBucket< Key, Idx >&& bucket);
522 };
523
524#ifndef DOXYGEN_SHOULD_SKIP_THIS
525
526 // ===========================================================================
527 // === GUM_SEQUENCE_IMPLEMENTATION OPTIMIZED FOR SCALARS ===
528 // ===========================================================================
545 template < typename Key >
546 class SequenceImplementation< Key, true > {
549 friend class SequenceIteratorSafe< Key >;
550 friend class Sequence< Key >;
552
553 public:
556 using value_type = Key;
557 using size_type = std::size_t;
558 using difference_type = std::ptrdiff_t;
559 using iterator = SequenceIterator< Key >;
560 using const_iterator = SequenceIterator< Key >;
564
565 private:
566 // ============================================================================
568 // ============================================================================
570
575
580 SequenceImplementation(std::initializer_list< Key > list);
581
590
596
598
599 public:
600 // ============================================================================
602 // ============================================================================
604
608 ~SequenceImplementation() noexcept;
609
611 // ============================================================================
613 // ============================================================================
615
620 iterator_safe beginSafe() const;
621
627
632 const iterator_safe& endSafe() const noexcept;
633
638 const iterator_safe& rendSafe() const noexcept;
639
644 iterator begin() const;
645
650 iterator rbegin() const;
651
656 const iterator& end() const noexcept;
657
662 const iterator& rend() const noexcept;
663
665
666 private:
667 // ============================================================================
669 // ============================================================================
671
678
685
687
688 public:
689 // ============================================================================
691 // ============================================================================
693
701
712
719 const Key& operator[](Idx i) const;
720
731
742
744 // ============================================================================
746 // ============================================================================
748
752 void clear();
753
758 Size size() const noexcept;
759
764 bool empty() const noexcept;
765
774 bool exists(Key k) const;
775
784 void insert(Key k);
785
796 template < typename... Args >
797 void emplace(Args&&... args);
798
808 void erase(Key k);
809
819 void erase(const iterator_safe& k);
820
827 const Key& atPos(Idx i) const;
828
836 Idx pos(Key key) const;
837
846 void setAtPos(Idx i, Key newKey);
847
853 void swap(Idx i, Idx j);
854
860 const Key& front() const;
861
867 const Key& back() const;
868
873 std::string toString() const;
874
887 void resize(Size new_size);
888
890
891 private:
894
896 std::vector< Key > _v_;
897
900
903
908 void _update_end_() noexcept;
909
916
922 void _insert_(Key k);
923 };
924
925#endif /* DOXYGEN_SHOULD_SKIP_THIS */
926
927 // ===========================================================================
928 // === GUM_SEQUENCE ===
929 // ===========================================================================
971 template < typename Key >
972 class Sequence: public SequenceImplementation< Key, std::is_scalar< Key >::value > {
973 public:
976 using value_type = Key;
977 using reference = Key&;
978 using const_reference = const Key&;
979 using pointer = Key*;
980 using const_pointer = const Key*;
981 using size_type = std::size_t;
982 using difference_type = std::ptrdiff_t;
983 using iterator = SequenceIterator< Key >;
984 using const_iterator = SequenceIterator< Key >;
988
991
992 // ============================================================================
994 // ============================================================================
996
1002
1007 Sequence(std::initializer_list< Key > list);
1008
1016 Sequence(const Sequence< Key >& aSeq);
1017
1022 Sequence(Sequence< Key >&& aSeq);
1023
1027 ~Sequence() noexcept;
1028
1030 // ============================================================================
1032 // ============================================================================
1034
1040 Sequence< Key >& operator=(const Sequence< Key >& aSeq);
1041
1047 Sequence< Key >& operator=(Sequence< Key >&& aSeq);
1048
1050 // ============================================================================
1052 // ============================================================================
1054
1061 Set< Key > diffSet(const Sequence< Key >& seq) const;
1062
1064 };
1065
1066#ifndef DOXYGEN_SHOULD_SKIP_THIS
1067
1068 // dummy classes that will enable discriminate without overhead between
1069 // scalars and non-scalars operators * and ->
1070 template < bool gen >
1071 struct SequenceIteratorGet {
1072 template < typename Key >
1073 INLINE static const Key& op_star(const Key* x) {
1074 return *x;
1075 }
1076
1077 template < typename Key >
1078 INLINE static const Key* op_arrow(const Key* x) {
1079 return x;
1080 }
1081 };
1082
1083 template <>
1084 struct SequenceIteratorGet< true > {
1085 template < typename Key >
1086 INLINE static const Key& op_star(const Key& x) {
1087 return x;
1088 }
1089
1090 template < typename Key >
1091 INLINE static const Key* op_arrow(const Key& x) {
1092 return &x;
1093 }
1094 };
1095
1096#endif /* DOXYGEN_SHOULD_SKIP_THIS */
1097
1098 // ===========================================================================
1099 // class SequenceIteratorSafe
1100 // ===========================================================================
1133 template < typename Key >
1136 template < typename K, bool >
1138
1139 public:
1142 using iterator_category = std::bidirectional_iterator_tag;
1143 using value_type = Key;
1144 using reference = Key&;
1145 using const_reference = const Key&;
1146 using pointer = Key*;
1147 using const_pointer = const Key*;
1148 using difference_type = std::ptrdiff_t;
1150
1151 private:
1153 using Getter = SequenceIteratorGet< std::is_scalar< Key >::value >;
1154
1155
1168 template < bool Gen >
1170
1171 public:
1172 // ============================================================================
1174 // ============================================================================
1176
1177
1189 SequenceIteratorSafe(const Sequence< Key >& seq, Idx pos = 0) noexcept;
1190
1195 SequenceIteratorSafe(const SequenceIteratorSafe< Key >& source) noexcept;
1196
1201 SequenceIteratorSafe(SequenceIteratorSafe< Key >&& source) noexcept;
1202
1206 ~SequenceIteratorSafe() noexcept;
1207
1209 // ============================================================================
1211 // ============================================================================
1213
1219 SequenceIteratorSafe< Key >& operator=(const SequenceIteratorSafe< Key >& source) noexcept;
1220
1226 SequenceIteratorSafe< Key >& operator=(SequenceIteratorSafe< Key >&& source) noexcept;
1227
1234 SequenceIteratorSafe< Key >& operator++() noexcept;
1235
1243 SequenceIteratorSafe< Key >& operator--() noexcept;
1244
1255 SequenceIteratorSafe< Key >& operator+=(Size nb) noexcept;
1256
1267 SequenceIteratorSafe< Key >& operator-=(Size nb) noexcept;
1268
1278 SequenceIteratorSafe< Key > operator+(Size nb) noexcept;
1279
1289 SequenceIteratorSafe< Key > operator-(Size nb) noexcept;
1290
1297 bool operator!=(const SequenceIteratorSafe< Key >& source) const noexcept;
1298
1305 bool operator==(const SequenceIteratorSafe< Key >& source) const noexcept;
1306
1312 const Key& operator*() const;
1313
1320 const Key* operator->() const;
1321
1323 // ============================================================================
1325 // ============================================================================
1327
1333 Idx pos() const;
1334
1336
1337 private:
1340
1342 const SequenceImplementation< Key, std::is_scalar< Key >::value >* _seq_;
1343
1346 void _setPos_(Idx pos) noexcept;
1347
1349 void _setAtRend_() noexcept;
1350
1352 void _setAtEnd_() noexcept;
1353 };
1354
1356 template < typename Key >
1357 std::ostream& operator<<(std::ostream& stream, const Sequence< Key >& s);
1358
1359} /* namespace gum */
1360
1361
1362#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1363extern template class gum::Sequence< int >;
1364extern template class gum::Sequence< long >;
1365extern template class gum::Sequence< double >;
1366extern template class gum::Sequence< std::string >;
1367#endif
1368
1369
1370// always include the implementation of the templates
1372
1373#endif // GUM_SEQUENCE_H
The class for generic Hash Tables.
Definition hashTable.h:637
The internal class for storing (ordered) sequences of objects.
Definition sequence.h:109
const Key & const_reference
Types for STL compliance.
Definition sequence.h:121
void _copy_(const SequenceImplementation< Key, Gen > &aSeq)
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition sequence.h:128
SequenceImplementation< Key, Gen > & operator=(const SequenceImplementation< Key, Gen > &aSeq)
Copy operator.
SequenceImplementation< Key, Gen > & operator<<(const Key &k)
Insert k at the end of the sequence (synonym for insert).
SequenceImplementation(const SequenceImplementation< Key, Gen > &aSeq)
Copy constructor.
void _insert_(HashTableBucket< Key, Idx > &&bucket)
Key value_type
Types for STL compliance.
Definition sequence.h:119
SequenceImplementation(SequenceImplementation< Key, Gen > &&aSeq)
Move constructor.
bool operator==(const SequenceImplementation< Key, Gen > &k) const
Returns true if the content of k equals that of *this.
Key & reference
Types for STL compliance.
Definition sequence.h:120
const Key & operator[](Idx i) const
Returns the element at position i (synonym for atPos).
bool operator!=(const SequenceImplementation< Key, Gen > &k) const
Returns true if the content of k is different from that of *this.
SequenceImplementation< Key, Gen > & operator>>(const Key &k)
Remove k in the sequence (synonym for erase).
SequenceImplementation(std::initializer_list< Key > list)
Initializer list constructor.
Key * pointer
Types for STL compliance.
Definition sequence.h:122
~SequenceImplementation() noexcept
Class destructor.
SequenceIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition sequence.h:129
SequenceIterator< Key > const_iterator
Types for STL compliance.
Definition sequence.h:127
std::size_t size_type
Types for STL compliance.
Definition sequence.h:124
std::ptrdiff_t difference_type
Types for STL compliance.
Definition sequence.h:125
SequenceImplementation(Size size_param=HashTableConst::default_size)
Default constructor.
friend class SequenceIteratorSafe< Key >
Friends to speed up access.
Definition sequence.h:112
SequenceIterator< Key > iterator
Types for STL compliance.
Definition sequence.h:126
const Key * const_pointer
Types for STL compliance.
Definition sequence.h:123
Safe iterators for Sequence.
Definition sequence.h:1134
void _setAtEnd_() noexcept
The iterator points to the end (which is pos size()-1).
Key * pointer
types for STL compliance
Definition sequence.h:1146
SequenceIteratorGet< std::is_scalar< Key >::value > Getter
The Getter used by this iterator.
Definition sequence.h:1153
const Key * const_pointer
types for STL compliance
Definition sequence.h:1147
const Key & const_reference
types for STL compliance
Definition sequence.h:1145
const SequenceImplementation< Key, std::is_scalar< Key >::value > * _seq_
The sequence pointed to by the iterator (by default, key is a scalar).
Definition sequence.h:1342
void _setAtRend_() noexcept
The iterator points to rend.
Idx _iterator_
The index in the sequence's vector where the iterator is pointing.
Definition sequence.h:1339
std::ptrdiff_t difference_type
types for STL compliance
Definition sequence.h:1148
Idx pos() const
Returns the position of the iterator in the sequence.
SequenceIteratorSafe(const SequenceImplementation< Key, Gen > &seq, Idx pos=0) noexcept
Constructor, always give a valid iterator (even if pos too large).
void _setPos_(Idx pos) noexcept
The iterator points to the posth element (0 = beginning of the sequence).
Key value_type
types for STL compliance
Definition sequence.h:1143
friend class SequenceImplementation
Friend to speed up access.
Definition sequence.h:1137
std::bidirectional_iterator_tag iterator_category
types for STL compliance
Definition sequence.h:1142
Key & reference
types for STL compliance
Definition sequence.h:1144
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:972
Key value_type
Types for STL compliance.
Definition sequence.h:976
SequenceIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition sequence.h:986
const Key * const_pointer
Types for STL compliance.
Definition sequence.h:980
SequenceIterator< Key > const_iterator
Types for STL compliance.
Definition sequence.h:984
Key & reference
Types for STL compliance.
Definition sequence.h:977
std::ptrdiff_t difference_type
Types for STL compliance.
Definition sequence.h:982
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition sequence.h:985
Sequence(Size size_param=HashTableConst::default_size)
Default constructor.
const Key & const_reference
Types for STL compliance.
Definition sequence.h:978
std::size_t size_type
Types for STL compliance.
Definition sequence.h:981
SequenceImplementation< Key, std::is_scalar< Key >::value > Implementation
The gum::Sequence implementation.
Definition sequence.h:990
Key * pointer
Types for STL compliance.
Definition sequence.h:979
SequenceIterator< Key > iterator
Types for STL compliance.
Definition sequence.h:983
Set< Key > diffSet(const Sequence< Key > &seq) const
Difference between two sequences as a Set<Key> = this \ seq.
Representation of a set.
Definition set.h:131
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
Template implementation file of gum::Sequence, a class for storing (ordered) sequences of objects.
Sets of elements (i.e.
A recipient for a pair of key value in a gum::HashTableList.
Definition hashTable.h:215
static constexpr Size default_size
The default number of slots in hashtables.
Definition hashTable.h:101