29 #ifndef GUM_SEQUENCE_H 30 #define GUM_SEQUENCE_H 32 #include <initializer_list> 34 #include <type_traits> 37 #include <agrum/agrum.h> 38 #include <agrum/tools/core/hashTable.h> 39 #include <agrum/tools/core/set.h> 43 #ifndef DOXYGEN_SHOULD_SKIP_THIS 44 template <
typename Key,
typename Alloc,
bool >
46 template <
typename Key,
typename Alloc >
48 template <
typename Key >
50 template <
typename Key >
52 template <
typename Key >
88 template <
typename Key,
typename Alloc,
bool Gen >
92 template <
typename K,
typename A,
bool >
101 using value_type = Key;
102 using reference = Key&;
103 using const_reference =
const Key&;
104 using pointer = Key*;
105 using const_pointer =
const Key*;
106 using size_type = std::size_t;
107 using difference_type = std::ptrdiff_t;
108 using allocator_type = Alloc;
149 template <
typename OtherAlloc >
193 const iterator_safe&
endSafe()
const noexcept;
199 const iterator_safe&
rendSafe()
const noexcept;
247 template <
typename OtherAlloc >
312 template <
typename OtherAlloc >
325 template <
typename OtherAlloc >
349 bool empty()
const noexcept;
359 bool exists(
const Key& k)
const;
369 void insert(
const Key& k);
391 template <
typename... Args >
403 void erase(
const Key& k);
414 void erase(
const iterator_safe& k);
422 const Key&
atPos(Idx i)
const;
441 void setAtPos(Idx i,
const Key& newKey);
458 void swap(Idx i, Idx j);
465 const Key&
front()
const;
472 const Key&
back()
const;
492 void resize(Size new_size);
525 template <
typename OtherAlloc >
533 void _insert_(HashTableBucket< Key, Idx >&& bucket);
536 #ifndef DOXYGEN_SHOULD_SKIP_THIS 558 template <
typename Key,
typename Alloc >
562 template <
typename K,
typename A,
bool >
565 friend class Sequence< Key, Alloc >;
571 using value_type = Key;
572 using size_type = std::size_t;
573 using difference_type = std::ptrdiff_t;
574 using allocator_type = Alloc;
590 SequenceImplementation(Size size_param = HashTableConst::default_size);
596 SequenceImplementation(std::initializer_list< Key > list);
615 template <
typename OtherAlloc >
634 ~SequenceImplementation()
noexcept;
646 iterator_safe beginSafe()
const;
652 iterator_safe rbeginSafe()
const;
658 const iterator_safe& endSafe()
const noexcept;
664 const iterator_safe& rendSafe()
const noexcept;
670 iterator begin()
const;
676 iterator rbegin()
const;
682 const iterator& end()
const noexcept;
688 const iterator& rend()
const noexcept;
712 template <
typename OtherAlloc >
757 const Key& operator[](Idx i)
const;
769 template <
typename OtherAlloc >
782 template <
typename OtherAlloc >
800 Size size()
const noexcept;
806 bool empty()
const noexcept;
816 bool exists(Key k)
const;
838 template <
typename... Args >
839 void emplace(Args&&... args);
861 void erase(
const iterator_safe& k);
869 const Key& atPos(Idx i)
const;
878 Idx pos(Key key)
const;
888 void setAtPos(Idx i, Key newKey);
895 void swap(Idx i, Idx j);
902 const Key& front()
const;
909 const Key& back()
const;
915 std::string toString()
const;
929 void resize(Size new_size);
935 HashTable< Key, Idx, Alloc > _h_;
938 std::vector< Key, Alloc > _v_;
950 void _update_end_()
noexcept;
958 template <
typename OtherAlloc >
966 void _insert_(Key k);
1016 template <
typename Key,
typename Alloc = std::allocator< Key > >
1021 using value_type = Key;
1022 using reference = Key&;
1023 using const_reference =
const Key&;
1024 using pointer = Key*;
1025 using const_pointer =
const Key*;
1026 using size_type = std::size_t;
1027 using difference_type = std::ptrdiff_t;
1028 using allocator_type = Alloc;
1053 Sequence(std::initializer_list< Key > list);
1072 template <
typename OtherAlloc >
1105 template <
typename OtherAlloc >
1127 template <
typename OtherAlloc >
1133 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1137 template <
bool gen >
1138 struct SequenceIteratorGet {
1139 template <
typename Key >
1140 INLINE
static const Key& op_star(
const Key* x) {
1143 template <
typename Key >
1144 INLINE
static const Key* op_arrow(
const Key* x) {
1150 struct SequenceIteratorGet<
true > {
1151 template <
typename Key >
1152 INLINE
static const Key& op_star(
const Key& x) {
1155 template <
typename Key >
1156 INLINE
static const Key* op_arrow(
const Key& x) {
1198 template <
typename Key >
1201 template <
typename K,
typename A,
bool >
1207 using iterator_category = std::bidirectional_iterator_tag;
1208 using value_type = Key;
1209 using reference = Key&;
1210 using const_reference =
const Key&;
1211 using pointer = Key*;
1212 using const_pointer =
const Key*;
1213 using difference_type = std::ptrdiff_t;
1218 using Getter = SequenceIteratorGet< std::is_scalar< Key >::value >;
1234 template <
typename Alloc,
bool Gen >
1236 Idx pos = 0)
noexcept;
1257 template <
typename Alloc >
1425 template <
typename Key,
typename Alloc >
1431 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS 1432 extern template class gum::Sequence<
int >;
1433 extern template class gum::Sequence<
long >;
1434 extern template class gum::Sequence<
double >;
1435 extern template class gum::Sequence< std::string >;
1440 #include <agrum/tools/core/sequence_tpl.h> HashTable< Key, Idx, Alloc > _h_
Keep track of the position of the element in v (for fast retrieval).
bool operator!=(const SequenceImplementation< Key, OtherAlloc, Gen > &k) const
Returns true if the content of k is different from that of *this.
void _setAtRend_() noexcept
The iterator points to rend.
Sequence(std::initializer_list< Key > list)
Initializer list constructor.
Safe iterators for Sequence.
SequenceIteratorSafe< Key > operator-(Size nb) noexcept
Returns a new iterator.
bool operator!=(const SequenceIteratorSafe< Key > &source) const noexcept
Checks whether two iterators are pointing to different elements.
SequenceIteratorSafe< Key > & operator=(const SequenceIteratorSafe< Key > &source) noexcept
Copy operator.
SequenceImplementation< Key, Alloc, Gen > & operator=(const SequenceImplementation< Key, OtherAlloc, Gen > &aSeq)
Generalized opy operator.
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
iterator begin() const
Returns an unsafe begin iterator.
const SequenceImplementation< Key, std::allocator< Key >, std::is_scalar< Key >::value > * _seq_
The sequence pointed to by the iterator (by default, key is a scalar).
Sequence(Size size_param=HashTableConst::default_size)
Default constructor.
iterator_safe rbeginSafe() const
Returns a safe rbegin iterator.
std::vector< Key *, typename Alloc::template rebind< Key *>::other > _v_
The set of the elements stored into the sequence.
void erase(const iterator_safe &k)
Remove from the sequence the element pointed to by the iterator.
void clear()
Clear the sequence.
const iterator_safe & rendSafe() const noexcept
Returns the safe rend iterator.
const iterator_safe & endSafe() const noexcept
Returns the safe end iterator.
Sequence(Sequence< Key, Alloc > &&aSeq)
Move constructor.
SequenceIteratorSafe< Key > & operator=(SequenceIteratorSafe< Key > &&source) noexcept
Move operator.
Sequence< Key, Alloc > & operator=(Sequence< Key, Alloc > &&aSeq)
Move operator.
void resize(Size new_size)
Modifies the size of the internal structures of the sequence.
Size size() const noexcept
Returns the size of the sequence.
void _setAtEnd_() noexcept
The iterator points to the end (which is pos size()-1).
INLINE void emplace(Args &&... args)
SequenceImplementation(Size size_param=HashTableConst::default_size)
Default constructor.
void _update_end_() noexcept
A method to update the end iterator after changes in the sequence.
Idx pos() const
Returns the position of the iterator in the sequence.
Idx _iterator_
The index in the sequence's vector where the iterator is pointing.
The generic class for storing (ordered) sequences of objects.
Sequence(const Sequence< Key, OtherAlloc > &aSeq)
Generalised copy constructor.
Sequence(const Sequence< Key, Alloc > &aSeq)
Copy constructor.
~SequenceIteratorSafe() noexcept
Class destructor.
SequenceIteratorSafe< Key > & operator++() noexcept
Point the iterator to the next value in the sequence.
SequenceIteratorSafe< Key > & operator--() noexcept
Point the iterator to the preceding value in the sequence.
~Sequence() noexcept
Class destructor.
bool empty() const noexcept
Return true if empty.
const Key * operator->() const
Returns the value pointed to by the iterator (works only for non-scalars).
std::string toString() const
Displays the content of the sequence.
SequenceIteratorSafe(const SequenceIteratorSafe< Key > &source) noexcept
Copy constructor.
void _setPos_(Idx pos) noexcept
The iterator points to the posth element (0 = beginning of the sequence).
iterator_safe beginSafe() const
Returns a safe begin iterator.
const Key & operator[](Idx i) const
Returns the element at position i (synonym for atPos).
SequenceImplementation< Key, Alloc, Gen > & operator=(const SequenceImplementation< Key, Alloc, Gen > &aSeq)
Copy operator.
SequenceImplementation(std::initializer_list< Key > list)
Initializer list constructor.
SequenceImplementation(const SequenceImplementation< Key, Alloc, Gen > &aSeq)
Copy constructor.
SequenceImplementation< Key, Alloc, Gen > & operator>>(const Key &k)
Remove k in the sequence (synonym for erase).
void _insert_(HashTableBucket< Key, Idx > &&bucket)
Insert an element at the end of the sequence.
void setAtPos(Idx i, Key &&newKey)
Change the value.
friend class SequenceImplementation
Friend to speed up access.
Set< Key, Alloc > diffSet(const Sequence< Key, OtherAlloc > &seq) const
Difference between two sequences as a Set<Key> = this \ seq.
bool operator==(const SequenceIteratorSafe< Key > &source) const noexcept
Checks whether two iterators are pointing to the same elements.
bool exists(const Key &k) const
Check the existence of k in the sequence.
void _copy_(const SequenceImplementation< Key, OtherAlloc, Gen > &aSeq)
Clears the current sequence and fill it with copies the element of aSeq.
SequenceIteratorSafe< Key > & operator-=(Size nb) noexcept
Makes the iterator point to i elements further in the sequence.
SequenceIteratorSafe< Key > _end_safe_
Stores the end iterator for fast access.
SequenceIteratorSafe(SequenceIteratorSafe< Key > &&source) noexcept
Move constructor.
SequenceImplementation(SequenceImplementation< Key, Alloc, Gen > &&aSeq)
Move constructor.
std::ostream & operator<<(std::ostream &stream, const Sequence< Key, Alloc > &s)
A << operator for displaying the content of the Sequence.
void erase(const Key &k)
Remove an element from the sequence.
SequenceIteratorSafe(const Sequence< Key, Alloc > &seq, Idx pos=0) noexcept
Constructor, always give a valid iterator (even if pos too large).
void swap(Idx i, Idx j)
Swap index.
Sequence< Key, Alloc > & operator=(const Sequence< Key, Alloc > &aSeq)
Copy operator.
const Key & operator*() const
Returns the value pointed to by the iterator.
SequenceImplementation< Key, Alloc, Gen > & operator=(SequenceImplementation< Key, Alloc, Gen > &&aSeq)
Move operator.
void emplace(Args &&... args)
Emplace a new element in the sequence.
const Key & front() const
Returns the first element of the element.
Sequence< Key, Alloc > & operator=(const Sequence< Key, OtherAlloc > &aSeq)
Generalized opy operator.
const iterator & rend() const noexcept
Returns the unsafe rend iterator.
void insert(Key &&k)
Move an element at the end of the sequence.
SequenceIteratorSafe(const SequenceImplementation< Key, Alloc, Gen > &seq, Idx pos=0) noexcept
Constructor, always give a valid iterator (even if pos too large).
SequenceImplementation(const SequenceImplementation< Key, OtherAlloc, Gen > &aSeq)
Generalised copy constructor.
const iterator & end() const noexcept
Returns the unsafe end iterator.
SequenceIteratorSafe< Key > operator+(Size nb) noexcept
Returns a new iterator.
~SequenceImplementation() noexcept
Class destructor.
SequenceIteratorSafe< Key > & operator+=(Size nb) noexcept
Makes the iterator point to i elements further in the sequence.
iterator rbegin() const
Returns an unsafe rbegin iterator.
SequenceIteratorSafe< Key > _rend_safe_
Stores the rend iterator for fast access.
void setAtPos(Idx i, const Key &newKey)
Change the value.
bool operator==(const SequenceImplementation< Key, OtherAlloc, Gen > &k) const
Returns true if the content of k equals that of *this.
const Key & back() const
Returns the last element of the sequence.
const Key & atPos(Idx i) const
Returns the object at the pos i.
void insert(const Key &k)
Insert an element at the end of the sequence.