aGrUM 3.1.1
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-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
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
360 optional_ref< const Idx > tryPos(const Key& k) const;
361
370 void insert(const Key& k);
371
380 void insert(Key&& k);
381
392 template < typename... Args >
393 void emplace(Args&&... args);
394
404 void erase(const Key& k);
405
415 void erase(const iterator_safe& k);
416
423 const Key& atPos(Idx i) const;
424
432 Idx pos(const Key& key) const;
433
442 void setAtPos(Idx i, const Key& newKey);
443
452 void setAtPos(Idx i, Key&& newKey);
453
459 void swap(Idx i, Idx j);
460
466 const Key& front() const;
467
473 const Key& back() const;
474
479 std::string toString() const;
480
493 void resize(Size new_size);
494
496
497 private:
500
502 std::vector< Key* > _v_;
503
504 // Note that, using Key* in _v_, we actually store the Key only once in the
505 // sequence (that is, within _h_). This enables storing big objects within
506 // sequences without having memory overhead.
507
510
513
518 void _update_end_() noexcept;
519
525 void _copy_(const SequenceImplementation< Key, Gen >& aSeq);
526
532 void _insert_(HashTableBucket< Key, Idx >&& bucket);
533 };
534
535#ifndef DOXYGEN_SHOULD_SKIP_THIS
536
537 // ===========================================================================
538 // === GUM_SEQUENCE_IMPLEMENTATION OPTIMIZED FOR SCALARS ===
539 // ===========================================================================
556 template < typename Key >
557 class SequenceImplementation< Key, true > {
560 friend class SequenceIteratorSafe< Key >;
561 friend class Sequence< Key >;
563
564 public:
567 using value_type = Key;
568 using size_type = std::size_t;
569 using difference_type = std::ptrdiff_t;
570 using iterator = SequenceIterator< Key >;
571 using const_iterator = SequenceIterator< Key >;
575
576 private:
577 // ============================================================================
579 // ============================================================================
581
586
591 SequenceImplementation(std::initializer_list< Key > list);
592
601
607
609
610 public:
611 // ============================================================================
613 // ============================================================================
615
619 ~SequenceImplementation() noexcept;
620
622 // ============================================================================
624 // ============================================================================
626
631 iterator_safe beginSafe() const;
632
638
643 const iterator_safe& endSafe() const noexcept;
644
649 const iterator_safe& rendSafe() const noexcept;
650
655 iterator begin() const;
656
661 iterator rbegin() const;
662
667 const iterator& end() const noexcept;
668
673 const iterator& rend() const noexcept;
674
676
677 private:
678 // ============================================================================
680 // ============================================================================
682
689
696
698
699 public:
700 // ============================================================================
702 // ============================================================================
704
712
723
730 const Key& operator[](Idx i) const;
731
742
753
755 // ============================================================================
757 // ============================================================================
759
763 void clear();
764
769 Size size() const noexcept;
770
775 bool empty() const noexcept;
776
785 bool exists(Key k) const;
786
797
806 void insert(Key k);
807
818 template < typename... Args >
819 void emplace(Args&&... args);
820
830 void erase(Key k);
831
841 void erase(const iterator_safe& k);
842
849 const Key& atPos(Idx i) const;
850
858 Idx pos(Key key) const;
859
868 void setAtPos(Idx i, Key newKey);
869
875 void swap(Idx i, Idx j);
876
882 const Key& front() const;
883
889 const Key& back() const;
890
895 std::string toString() const;
896
909 void resize(Size new_size);
910
912
913 private:
916
918 std::vector< Key > _v_;
919
922
925
930 void _update_end_() noexcept;
931
938
944 void _insert_(Key k);
945 };
946
947#endif /* DOXYGEN_SHOULD_SKIP_THIS */
948
949 // ===========================================================================
950 // === GUM_SEQUENCE ===
951 // ===========================================================================
993 template < typename Key >
994 class Sequence: public SequenceImplementation< Key, std::is_scalar< Key >::value > {
995 public:
998 using value_type = Key;
999 using reference = Key&;
1000 using const_reference = const Key&;
1001 using pointer = Key*;
1002 using const_pointer = const Key*;
1003 using size_type = std::size_t;
1004 using difference_type = std::ptrdiff_t;
1005 using iterator = SequenceIterator< Key >;
1006 using const_iterator = SequenceIterator< Key >;
1010
1013
1014 // ============================================================================
1016 // ============================================================================
1018
1024
1029 Sequence(std::initializer_list< Key > list);
1030
1038 Sequence(const Sequence< Key >& aSeq);
1039
1044 Sequence(Sequence< Key >&& aSeq);
1045
1049 ~Sequence() noexcept;
1050
1052 // ============================================================================
1054 // ============================================================================
1056
1062 Sequence< Key >& operator=(const Sequence< Key >& aSeq);
1063
1069 Sequence< Key >& operator=(Sequence< Key >&& aSeq);
1070
1072 // ============================================================================
1074 // ============================================================================
1076
1083 Set< Key > diffSet(const Sequence< Key >& seq) const;
1084
1086 };
1087
1088#ifndef DOXYGEN_SHOULD_SKIP_THIS
1089
1090 // dummy classes that will enable discriminate without overhead between
1091 // scalars and non-scalars operators * and ->
1092 template < bool gen >
1093 struct SequenceIteratorGet {
1094 template < typename Key >
1095 static const Key& op_star(const Key* x);
1096
1097 template < typename Key >
1098 static const Key* op_arrow(const Key* x);
1099 };
1100
1101 template <>
1102 struct SequenceIteratorGet< true > {
1103 template < typename Key >
1104 static const Key& op_star(const Key& x);
1105
1106 template < typename Key >
1107 static const Key* op_arrow(const Key& x);
1108 };
1109
1110#endif /* DOXYGEN_SHOULD_SKIP_THIS */
1111
1112 // ===========================================================================
1113 // class SequenceIteratorSafe
1114 // ===========================================================================
1147 template < typename Key >
1150 template < typename K, bool >
1152
1153 public:
1156 using iterator_category = std::bidirectional_iterator_tag;
1157 using value_type = Key;
1158 using reference = Key&;
1159 using const_reference = const Key&;
1160 using pointer = Key*;
1161 using const_pointer = const Key*;
1162 using difference_type = std::ptrdiff_t;
1164
1165 private:
1167 using Getter = SequenceIteratorGet< std::is_scalar_v< Key > >;
1168
1169
1182 template < bool Gen >
1184
1185 public:
1186 // ============================================================================
1188 // ============================================================================
1190
1192
1204 SequenceIteratorSafe(const Sequence< Key >& seq, Idx pos = 0) noexcept;
1205
1210 SequenceIteratorSafe(const SequenceIteratorSafe< Key >& source) noexcept;
1211
1216 SequenceIteratorSafe(SequenceIteratorSafe< Key >&& source) noexcept;
1217
1221 ~SequenceIteratorSafe() noexcept;
1222
1224 // ============================================================================
1226 // ============================================================================
1228
1234 SequenceIteratorSafe< Key >& operator=(const SequenceIteratorSafe< Key >& source) noexcept;
1235
1241 SequenceIteratorSafe< Key >& operator=(SequenceIteratorSafe< Key >&& source) noexcept;
1242
1249 SequenceIteratorSafe< Key >& operator++() noexcept;
1250
1258 SequenceIteratorSafe< Key >& operator--() noexcept;
1259
1270 SequenceIteratorSafe< Key >& operator+=(Size nb) noexcept;
1271
1282 SequenceIteratorSafe< Key >& operator-=(Size nb) noexcept;
1283
1293 SequenceIteratorSafe< Key > operator+(Size nb) noexcept;
1294
1304 SequenceIteratorSafe< Key > operator-(Size nb) noexcept;
1305
1312 bool operator!=(const SequenceIteratorSafe< Key >& source) const noexcept;
1313
1320 bool operator==(const SequenceIteratorSafe< Key >& source) const noexcept;
1321
1327 const Key& operator*() const;
1328
1335 const Key* operator->() const;
1336
1338 // ============================================================================
1340 // ============================================================================
1342
1348 Idx pos() const;
1349
1351
1352 private:
1355
1357 const SequenceImplementation< Key, std::is_scalar_v< Key > >* _seq_{nullptr};
1358
1361 void _setPos_(Idx pos) noexcept;
1362
1364 void _setAtRend_() noexcept;
1365
1367 void _setAtEnd_() noexcept;
1368 };
1369
1371 template < typename Key >
1372 std::ostream& operator<<(std::ostream& stream, const Sequence< Key >& s);
1373
1374} /* namespace gum */
1375
1376
1377#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
1378extern template class gum::Sequence< int >;
1379extern template class gum::Sequence< long >;
1380extern template class gum::Sequence< double >;
1381extern template class gum::Sequence< std::string >;
1382#endif
1383
1384
1385// always include the implementation of the templates
1387
1388#endif // GUM_SEQUENCE_H
The class for generic Hash Tables.
Definition hashTable.h:640
The internal class for storing (ordered) sequences of objects.
Definition sequence.h:109
SequenceImplementation(SequenceImplementation< Key, Gen > &&aSeq)
Move constructor.
const Key & const_reference
Types for STL compliance.
Definition sequence.h:121
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition sequence.h:128
optional_ref< const Idx > tryPos(const Key &k) const
SequenceImplementation< Key, Gen > & operator<<(const Key &k)
Insert k at the end of the sequence (synonym for insert).
void _insert_(HashTableBucket< Key, Idx > &&bucket)
Key value_type
Types for STL compliance.
Definition sequence.h:119
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
SequenceImplementation(const SequenceImplementation< Key, Gen > &aSeq)
Copy constructor.
SequenceImplementation< Key, Gen > & operator>>(const Key &k)
Remove k in the sequence (synonym for erase).
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< Key, Gen > & operator=(const SequenceImplementation< Key, Gen > &aSeq)
Copy operator.
SequenceImplementation(std::initializer_list< Key > list)
Initializer list constructor.
void _copy_(const SequenceImplementation< Key, Gen > &aSeq)
const Key & operator[](Idx i) const
Returns the element at position i (synonym for atPos).
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
SequenceImplementation(Size size_param=HashTableConst::default_size)
Default constructor.
bool operator!=(const SequenceImplementation< Key, Gen > &k) const
Returns true if the content of k is different from that of *this.
Safe iterators for Sequence.
Definition sequence.h:1148
SequenceIteratorGet< std::is_scalar_v< Key > > Getter
The Getter used by this iterator.
Definition sequence.h:1167
Key * pointer
types for STL compliance
Definition sequence.h:1160
const Key * const_pointer
types for STL compliance
Definition sequence.h:1161
const Key & const_reference
types for STL compliance
Definition sequence.h:1159
Idx _iterator_
The index in the sequence's vector where the iterator is pointing.
Definition sequence.h:1354
std::ptrdiff_t difference_type
types for STL compliance
Definition sequence.h:1162
SequenceIteratorSafe(const SequenceImplementation< Key, Gen > &seq, Idx pos=0) noexcept
Constructor, always give a valid iterator (even if pos too large).
Key value_type
types for STL compliance
Definition sequence.h:1157
const SequenceImplementation< Key, std::is_scalar_v< Key > > * _seq_
The sequence pointed to by the iterator (by default, key is a scalar).
Definition sequence.h:1357
Idx pos() const
Returns the position of the iterator in the sequence.
friend class SequenceImplementation
Friend to speed up access.
Definition sequence.h:1151
std::bidirectional_iterator_tag iterator_category
types for STL compliance
Definition sequence.h:1156
Key & reference
types for STL compliance
Definition sequence.h:1158
The generic class for storing (ordered) sequences of objects.
Definition sequence.h:994
Key value_type
Types for STL compliance.
Definition sequence.h:998
SequenceIteratorSafe< Key > const_iterator_safe
Types for STL compliance.
Definition sequence.h:1008
Sequence(Size size_param=HashTableConst::default_size)
Default constructor.
const Key * const_pointer
Types for STL compliance.
Definition sequence.h:1002
SequenceIterator< Key > const_iterator
Types for STL compliance.
Definition sequence.h:1006
Key & reference
Types for STL compliance.
Definition sequence.h:999
std::ptrdiff_t difference_type
Types for STL compliance.
Definition sequence.h:1004
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
Definition sequence.h:1007
const Key & const_reference
Types for STL compliance.
Definition sequence.h:1000
std::size_t size_type
Types for STL compliance.
Definition sequence.h:1003
SequenceImplementation< Key, std::is_scalar< Key >::value > Implementation
The gum::Sequence implementation.
Definition sequence.h:1012
Key * pointer
Types for STL compliance.
Definition sequence.h:1001
SequenceIterator< Key > iterator
Types for STL compliance.
Definition sequence.h:1005
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:129
A lightweight wrapper around a pointer providing an optional-like API for references (not supported b...
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:216
static constexpr Size default_size
The default number of slots in hashtables.
Definition hashTable.h:102