58 template <
typename Key,
bool Gen >
64 template <
typename Key,
bool Gen >
70 template <
typename Key >
76 template <
typename Key >
86 template <
typename Key >
94 if (pos > _seq_->size()) _iterator_ = _seq_->size();
95 else _iterator_ = pos;
99 template <
typename Key >
105 if (pos > _seq_->size()) _iterator_ = _seq_->size();
106 else _iterator_ = pos;
110 template <
typename Key >
112 const SequenceIteratorSafe< Key >& source) noexcept :
113 _iterator_{source._iterator_}, _seq_{source._seq_} {
114 GUM_CONS_CPY(SequenceIteratorSafe);
118 template <
typename Key >
120 _iterator_{source._iterator_}, _seq_{source._seq_} {
121 GUM_CONS_MOV(SequenceIteratorSafe);
125 template <
typename Key >
131 template <
typename Key >
132 SequenceIteratorSafe< Key >&
137 template <
typename Key >
138 SequenceIteratorSafe< Key >&
141 _seq_ = source._seq_;
146 template <
typename Key >
155 template <
typename Key >
163 template <
typename Key >
165 if (
_iterator_ == std::numeric_limits< Idx >::max())
return *
this;
173 template <
typename Key >
175 if (_iterator_ == std::numeric_limits< Idx >::max())
return *
this;
177 if (_iterator_ > _seq_->size()) _iterator_ = std::numeric_limits< Idx >::max();
183 template <
typename Key >
185 return SequenceIteratorSafe< Key >{*
this} += nb;
189 template <
typename Key >
191 return SequenceIteratorSafe< Key >{*
this} -= nb;
195 template <
typename Key >
197 const SequenceIteratorSafe< Key >& source)
const noexcept {
198 if (
_seq_->empty())
return true;
200 if ((_iterator_ != source._iterator_) || (_seq_ != source._seq_))
return false;
206 template <
typename Key >
208 const SequenceIteratorSafe< Key >& source)
const noexcept {
213 template <
typename Key >
223 template <
typename Key >
230 template <
typename Key >
232 _iterator_ = std::numeric_limits< Idx >::max();
236 template <
typename Key >
242 template <
typename Key >
244 return Getter::op_star(_seq_->_v_[
pos()]);
248 template <
typename Key >
250 return Getter::op_arrow(
_seq_->_v_[
pos()]);
258 template <
typename Key,
bool Gen >
264 template <
typename Key,
bool Gen >
272 template <
typename Key,
bool Gen >
276 for (
Size i = 0; i < aSeq.
size(); ++i) {
277 Key& new_key =
const_cast< Key&
>(
_h_.insert(*(aSeq.
_v_[i]), i).first);
278 _v_.push_back(&new_key);
285 template <
typename Key,
bool Gen >
294 template <
typename Key,
bool Gen >
299 for (
const auto& elt: list) {
305 template <
typename Key,
bool Gen >
314 template <
typename Key,
bool Gen >
324 template <
typename Key,
bool Gen >
330 template <
typename Key,
bool Gen >
342 template <
typename Key,
bool Gen >
347 _h_ = std::move(aSeq.
_h_);
348 _v_ = std::move(aSeq.
_v_);
356 template <
typename Key,
bool Gen >
358 return _h_.exists(k);
362 template <
typename Key,
bool Gen >
364 return _h_.tryGet(k);
368 template <
typename Key,
bool Gen >
371 Key& new_key =
const_cast< Key&
>(
_h_.insert(k,
_h_.size()).first);
373 _v_.push_back(&new_key);
382 template <
typename Key,
bool Gen >
385 Key& new_key =
const_cast< Key&
>(
_h_.insert(std::move(k),
_h_.size()).first);
387 _v_.push_back(&new_key);
396 template <
typename Key,
bool Gen >
397 template <
typename... Args >
399 Key key(std::forward< Args >(args)...);
400 Key& new_key =
const_cast< Key&
>(
_h_.insert(std::move(key),
_h_.size()).first);
402 _v_.push_back(&new_key);
411 template <
typename Key,
bool Gen >
418 template <
typename Key,
bool Gen >
420 insert(std::move(k));
425 template <
typename Key,
bool Gen >
428 auto p =
_h_.tryGet(k);
434 for (
Idx i =
pos, nb_elts =
_h_.size() - 1; i < nb_elts; ++i) {
443 template <
typename Key,
bool Gen >
445 if (iter.pos() >=
size())
return;
452 for (
Idx i =
pos, nb_elts =
_h_.size() - 1; i < nb_elts; ++i) {
461 template <
typename Key,
bool Gen >
468 template <
typename Key,
bool Gen >
470 if (i >=
_h_.size()) {
478 template <
typename Key,
bool Gen >
484 template <
typename Key,
bool Gen >
490 template <
typename Key,
bool Gen >
494 Key& new_key =
const_cast< Key&
>(
_h_.insert(newKey, i).first);
500 template <
typename Key,
bool Gen >
504 Key& new_key =
const_cast< Key&
>(
_h_.insert(std::move(newKey), i).first);
510 template <
typename Key,
bool Gen >
514 Key& ki =
const_cast< Key&
>(
atPos(i));
515 Key& kj =
const_cast< Key&
>(
atPos(j));
525 template <
typename Key,
bool Gen >
531 template <
typename Key,
bool Gen >
537 template <
typename Key,
bool Gen >
539 std::stringstream stream;
543 stream <<
"0:" << *
_v_[0];
545 for (
Idx i = 1; i <
_h_.size(); ++i) {
546 stream << std::format(
" - {}:", i) << *
_v_[i];
556 template <
typename Key,
bool Gen >
559 if (
size() != k.
size())
return false;
561 for (
Idx i = 0; i <
size(); ++i)
562 if (*
_v_[i] != *(k.
_v_[i]))
return false;
569 template <
typename Key,
bool Gen >
576 template <
typename Key,
bool Gen >
583 template <
typename Key,
bool Gen >
589 template <
typename Key,
bool Gen >
595 template <
typename Key,
bool Gen >
598 it._setPos_(
size() - 1);
603 template <
typename Key,
bool Gen >
609 template <
typename Key,
bool Gen >
611 return SequenceIterator< Key >{*
this};
615 template <
typename Key,
bool Gen >
621 template <
typename Key,
bool Gen >
623 SequenceIterator< Key > it{*
this};
624 it._setPos_(
size() - 1);
629 template <
typename Key,
bool Gen >
635 template <
typename Key,
bool Gen >
637 if (new_size <
_h_.size())
return;
639 _h_.resize(new_size);
640 _v_.reserve(new_size);
648 template <
typename Key >
650 _end_safe_._setAtEnd_();
654 template <
typename Key >
662 template <
typename Key >
667 for (
Size i = 0; i < aSeq.size(); ++i) {
668 _h_.insert(aSeq._v_[i], i);
669 _v_.push_back(aSeq._v_[i]);
676 template <
typename Key >
678 _h_(size_param), _end_safe_{*this}, _rend_safe_{*this} {
685 template <
typename Key >
687 _end_safe_{*this}, _rend_safe_{*this} {
690 for (
const auto& elt: list) {
696 template <
typename Key >
699 _h_(aSeq._h_), _v_(aSeq._v_), _end_safe_{*this}, _rend_safe_{*this} {
706 template <
typename Key >
709 _h_(std::move(aSeq._h_)), _v_(std::move(aSeq._v_)), _end_safe_{*this}, _rend_safe_{*this} {
716 template <
typename Key >
722 template <
typename Key >
726 if (&aSeq !=
this) { _copy_(aSeq); }
732 template <
typename Key >
737 _h_ = std::move(aSeq._h_);
738 _v_ = std::move(aSeq._v_);
746 template <
typename Key >
748 return _h_.exists(k);
752 template <
typename Key >
754 return _h_.tryGet(k);
758 template <
typename Key >
761 Key& new_key =
const_cast< Key&
>(_h_.insert(k, _h_.size()).first);
763 _v_.push_back(new_key);
772 template <
typename Key >
773 template <
typename... Args >
775 Key key(std::forward< Args >(args)...);
776 Key& new_key =
const_cast< Key&
>(_h_.insert(std::move(key), _h_.size()).first);
778 _v_.push_back(new_key);
779 }
catch (...) { _h_.erase(new_key); }
784 template <
typename Key >
791 template <
typename Key >
794 auto p = _h_.tryGet(k);
799 _v_.erase(_v_.begin() + pos);
800 for (
Idx i = pos, nb_elts = _h_.size() - 1; i < nb_elts; ++i) {
809 template <
typename Key >
811 if (iter.pos() >= size())
return;
814 Idx pos = iter.pos();
816 _v_.erase(_v_.begin() + pos);
818 for (
Idx i = pos, nb_elts = _h_.size() - 1; i < nb_elts; ++i) {
827 template <
typename Key >
834 template <
typename Key >
836 if (i >= _h_.size()) {
GUM_ERROR(NotFound,
"not enough elements in the sequence") }
842 template <
typename Key >
848 template <
typename Key >
854 template <
typename Key >
856 if (i >= _h_.size()) {
GUM_ERROR(NotFound,
"index too large") }
858 _h_.insert(newKey, i);
864 template <
typename Key >
879 template <
typename Key >
885 template <
typename Key >
887 return atPos(size() - 1);
891 template <
typename Key >
893 std::stringstream stream;
897 stream <<
"0:" << _v_[0];
899 for (
Idx i = 1; i < _h_.size(); ++i) {
900 stream << std::format(
" - {}:", i) << _v_[i];
910 template <
typename Key >
913 if (size() != k.size())
return false;
915 for (
Idx i = 0; i < size(); ++i)
916 if (_v_[i] != k._v_[i])
return false;
923 template <
typename Key >
930 template <
typename Key >
937 template <
typename Key >
939 return SequenceIteratorSafe< Key >{*
this};
943 template <
typename Key >
949 template <
typename Key >
952 it._setPos_(size() - 1);
957 template <
typename Key >
964 template <
typename Key >
966 return SequenceIterator< Key >{*
this};
970 template <
typename Key >
976 template <
typename Key >
978 SequenceIterator< Key > it{*
this};
979 it._setPos_(size() - 1);
984 template <
typename Key >
990 template <
typename Key >
992 if (new_size < _h_.size())
return;
994 _h_.resize(new_size);
995 _v_.reserve(new_size);
1003 template <
typename Key >
1010 template <
typename Key >
1018 template <
typename Key >
1026 template <
typename Key >
1034 template <
typename Key >
1041 template <
typename Key >
1048 template <
typename Key >
1055 template <
typename Key >
1060 if (!seq.
exists(*iter)) res << *iter;
1067 template <
typename Key >
1073 template <
bool gen >
1074 template <
typename Key >
1075 const Key& SequenceIteratorGet< gen >::op_star(
const Key* x) {
1079 template <
bool gen >
1080 template <
typename Key >
1081 const Key* SequenceIteratorGet< gen >::op_arrow(
const Key* x) {
1085 template <
typename Key >
1086 const Key& SequenceIteratorGet< true >::op_star(
const Key& x) {
1090 template <
typename Key >
1091 const Key* SequenceIteratorGet< true >::op_arrow(
const Key& x) {
Exception : the element we looked for cannot be found.
Exception : out of bound.
The internal class for storing (ordered) sequences of objects.
std::string toString() const
Displays the content of the sequence.
const Key & atPos(Idx i) const
Returns the object at the pos i.
Idx pos(const const gum::DiscreteVariable *&key) const
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
optional_ref< const Idx > tryPos(const Key &k) const
Returns a pointer to the position of k in the sequence, or nullptr if k is not found.
iterator begin() const
Returns an unsafe begin iterator.
std::vector< Key * > _v_
The set of the elements stored into the sequence.
SequenceImplementation< Key, Gen > & operator<<(const Key &k)
Insert k at the end of the sequence (synonym for insert).
const iterator & end() const noexcept
Returns the unsafe end iterator.
SequenceIteratorSafe< const gum::DiscreteVariable * > _rend_safe_
iterator_safe beginSafe() const
Returns a safe begin iterator.
const iterator & rend() const noexcept
Returns the unsafe rend iterator.
const Key & front() const
Returns the first element of the element.
void insert(const Key &k)
Insert an element at the end of the sequence.
bool operator==(const SequenceImplementation< Key, Gen > &k) const
Returns true if the content of k equals that of *this.
void erase(const Key &k)
Remove an element from the sequence.
void resize(Size new_size)
Modifies the size of the internal structures of the sequence.
void emplace(Args &&... args)
Emplace a new element in the sequence.
const Key & back() const
Returns the last element of the sequence.
bool empty() const noexcept
Return true if empty.
SequenceImplementation< Key, Gen > & operator>>(const Key &k)
Remove k in the sequence (synonym for erase).
iterator rbegin() const
Returns an unsafe rbegin iterator.
bool exists(const Key &k) const
Check the existence of k in the sequence.
~SequenceImplementation() noexcept
Class destructor.
const iterator_safe & rendSafe() const noexcept
Returns the safe rend iterator.
void setAtPos(Idx i, const Key &newKey)
Change the value.
SequenceImplementation< Key, Gen > & operator=(const SequenceImplementation< Key, Gen > &aSeq)
Copy operator.
SequenceIteratorSafe< Key > _end_safe_
Stores the end iterator for fast access.
void _update_end_() noexcept
A method to update the end iterator after changes in the sequence.
void _copy_(const SequenceImplementation< Key, Gen > &aSeq)
Clears the current sequence and fill it with copies the element of aSeq.
iterator_safe rbeginSafe() const
Returns a safe rbegin iterator.
const Key & operator[](Idx i) const
Returns the element at position i (synonym for atPos).
friend class SequenceIteratorSafe< Key >
Friends to speed up access.
void swap(Idx i, Idx j)
Swap index.
const iterator_safe & endSafe() const noexcept
Returns the safe end iterator.
friend class Sequence< Key >
void clear()
Clear the sequence.
Size size() const noexcept
Returns the size of the sequence.
HashTable< Key, Idx > _h_
Keep track of the position of the element in v (for fast retrieval).
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.
void _setAtRend_() noexcept
The iterator points to rend.
bool operator!=(const SequenceIteratorSafe< Key > &source) const noexcept
Checks whether two iterators are pointing to different elements.
SequenceIteratorSafe()=delete
Constructor, always give a valid iterator (even if pos too large).
SequenceIteratorSafe< Key > & operator++() noexcept
Point the iterator to the next value in the sequence.
void _setPos_(Idx pos) noexcept
The iterator points to the posth element (0 = beginning of the sequence).
const Key * operator->() const
Returns the value pointed to by the iterator (works only for non-scalars).
Idx _iterator_
The index in the sequence's vector where the iterator is pointing.
SequenceIteratorSafe< Key > operator+(Size nb) noexcept
Returns a new iterator.
SequenceIteratorSafe(const SequenceImplementation< Key, Gen > &seq, Idx pos=0) noexcept
Constructor, always give a valid iterator (even if pos too large).
SequenceIteratorSafe< Key > & operator--() noexcept
Point the iterator to the preceding value in the sequence.
void _setAtEnd_() noexcept
The iterator points to the end (which is pos size()-1).
SequenceIteratorSafe< Key > & operator-=(Size nb) noexcept
Makes the iterator point to i elements further in the sequence.
bool operator==(const SequenceIteratorSafe< Key > &source) const noexcept
Checks whether two iterators are pointing to the same elements.
~SequenceIteratorSafe() noexcept
Class destructor.
SequenceIteratorSafe< Key > & operator=(const SequenceIteratorSafe< Key > &source) noexcept
Copy operator.
const Key & operator*() const
Returns the value pointed to by the iterator.
SequenceIteratorSafe< Key > operator-(Size nb) noexcept
Returns a new iterator.
const SequenceImplementation< Key, std::is_scalar_v< Key > > * _seq_
The sequence pointed to by the iterator (by default, key is a scalar).
SequenceIteratorSafe< Key > & operator+=(Size nb) noexcept
Makes the iterator point to i elements further in the sequence.
Idx pos() const
Returns the position of the iterator in the sequence.
friend class SequenceImplementation
Friend to speed up access.
The generic class for storing (ordered) sequences of objects.
Sequence(Size size_param=HashTableConst::default_size)
Default constructor.
Sequence< Key > & operator=(const Sequence< Key > &aSeq)
Copy operator.
~Sequence() noexcept
Class destructor.
SequenceIterator< Key > iterator
Types for STL compliance.
Set< Key > diffSet(const Sequence< Key > &seq) const
Difference between two sequences as a Set<Key> = this \ seq.
Exception : generic error on iterator.
A lightweight wrapper around a pointer providing an optional-like API for references (not supported b...
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
gum is the global namespace for all aGrUM entities
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
bool operator==(const HashTableIteratorSafe< Key, Val > &from) const noexcept
Checks whether two iterators are pointing toward equal elements.
Header file of gum::Sequence, a class for storing (ordered) sequences of objects.