57 template <
typename Key,
bool Gen >
63 template <
typename Key,
bool Gen >
69 template <
typename Key >
75 template <
typename Key >
85 template <
typename Key >
94 if (pos > _seq_->size()) _iterator_ = _seq_->size();
95 else _iterator_ = pos;
99 template <
typename Key >
106 if (pos > _seq_->size()) _iterator_ = _seq_->size();
107 else _iterator_ = pos;
111 template <
typename Key >
113 const SequenceIteratorSafe< Key >& source) noexcept :
114 _iterator_{source._iterator_}, _seq_{source._seq_} {
115 GUM_CONS_CPY(SequenceIteratorSafe);
119 template <
typename Key >
121 SequenceIteratorSafe< Key >&& source) noexcept :
122 _iterator_{source._iterator_}, _seq_{source._seq_} {
123 GUM_CONS_MOV(SequenceIteratorSafe);
127 template <
typename Key >
133 template <
typename Key >
134 INLINE SequenceIteratorSafe< Key >&
137 _seq_ = source._seq_;
142 template <
typename Key >
146 _seq_ = source._seq_;
151 template <
typename Key >
160 template <
typename Key >
168 template <
typename Key >
170 if (
_iterator_ == std::numeric_limits< Idx >::max())
return *
this;
178 template <
typename Key >
180 if (
_iterator_ == std::numeric_limits< Idx >::max())
return *
this;
188 template <
typename Key >
190 return SequenceIteratorSafe< Key >{*
this} += nb;
194 template <
typename Key >
196 return SequenceIteratorSafe< Key >{*
this} -= nb;
200 template <
typename Key >
202 const SequenceIteratorSafe< Key >& source)
const noexcept {
203 if (
_seq_->empty())
return true;
205 if ((_iterator_ != source._iterator_) || (_seq_ != source._seq_))
return false;
211 template <
typename Key >
213 const SequenceIteratorSafe< Key >& source)
const noexcept {
218 template <
typename Key >
228 template <
typename Key >
235 template <
typename Key >
237 _iterator_ = std::numeric_limits< Idx >::max();
241 template <
typename Key >
247 template <
typename Key >
249 return Getter::op_star(
_seq_->_v_[
pos()]);
253 template <
typename Key >
255 return Getter::op_arrow(
_seq_->_v_[
pos()]);
263 template <
typename Key,
bool Gen >
269 template <
typename Key,
bool Gen >
277 template <
typename Key,
bool Gen >
282 for (
Size i = 0; i < aSeq.
size(); ++i) {
283 Key& new_key =
const_cast< Key&
>(
_h_.insert(*(aSeq.
_v_[i]), i).first);
284 _v_.push_back(&new_key);
291 template <
typename Key,
bool Gen >
300 template <
typename Key,
bool Gen >
305 for (
const auto& elt: list) {
311 template <
typename Key,
bool Gen >
320 template <
typename Key,
bool Gen >
330 template <
typename Key,
bool Gen >
336 template <
typename Key,
bool Gen >
348 template <
typename Key,
bool Gen >
353 _h_ = std::move(aSeq.
_h_);
354 _v_ = std::move(aSeq.
_v_);
362 template <
typename Key,
bool Gen >
364 return _h_.exists(k);
368 template <
typename Key,
bool Gen >
371 Key& new_key =
const_cast< Key&
>(
_h_.insert(k,
_h_.size()).first);
372 _v_.push_back(&new_key);
377 template <
typename Key,
bool Gen >
380 Key& new_key =
const_cast< Key&
>(
_h_.insert(std::move(k),
_h_.size()).first);
381 _v_.push_back(&new_key);
386 template <
typename Key,
bool Gen >
387 template <
typename... Args >
389 Key key(std::forward< Args >(args)...);
390 Key& new_key =
const_cast< Key&
>(
_h_.insert(std::move(key),
_h_.size()).first);
391 _v_.push_back(&new_key);
396 template <
typename Key,
bool Gen >
404 template <
typename Key,
bool Gen >
412 template <
typename Key,
bool Gen >
419 }
catch (
NotFound const&) {
return; }
423 for (
Idx i =
pos, nb_elts =
_h_.size() - 1; i < nb_elts; ++i) {
432 template <
typename Key,
bool Gen >
434 if (iter.pos() >=
size())
return;
441 for (
Idx i =
pos, nb_elts =
_h_.size() - 1; i < nb_elts; ++i) {
450 template <
typename Key,
bool Gen >
458 template <
typename Key,
bool Gen >
460 if (i >=
_h_.size()) {
468 template <
typename Key,
bool Gen >
474 template <
typename Key,
bool Gen >
480 template <
typename Key,
bool Gen >
484 Key& new_key =
const_cast< Key&
>(_h_.insert(newKey, i).first);
485 _h_.erase(*(_v_[i]));
490 template <
typename Key,
bool Gen >
494 Key& new_key =
const_cast< Key&
>(
_h_.insert(std::move(newKey), i).first);
500 template <
typename Key,
bool Gen >
504 Key& ki =
const_cast< Key&
>(
atPos(i));
505 Key& kj =
const_cast< Key&
>(
atPos(j));
515 template <
typename Key,
bool Gen >
521 template <
typename Key,
bool Gen >
527 template <
typename Key,
bool Gen >
529 std::stringstream stream;
533 stream << 0 <<
":" << *
_v_[0];
535 for (
Idx i = 1; i <
_h_.size(); ++i) {
536 stream <<
" - " << i <<
":" << *
_v_[i];
542 std::string res = stream.str();
547 template <
typename Key,
bool Gen >
550 if (
size() != k.
size())
return false;
552 for (
Idx i = 0; i <
size(); ++i)
553 if (*
_v_[i] != *(k.
_v_[i]))
return false;
560 template <
typename Key,
bool Gen >
567 template <
typename Key,
bool Gen >
575 template <
typename Key,
bool Gen >
581 template <
typename Key,
bool Gen >
582 INLINE
const SequenceIteratorSafe< Key >&
588 template <
typename Key,
bool Gen >
591 it._setPos_(
size() - 1);
596 template <
typename Key,
bool Gen >
597 INLINE
const SequenceIteratorSafe< Key >&
603 template <
typename Key,
bool Gen >
605 return SequenceIterator< Key >{*
this};
609 template <
typename Key,
bool Gen >
615 template <
typename Key,
bool Gen >
617 SequenceIterator< Key > it{*
this};
618 it._setPos_(
size() - 1);
623 template <
typename Key,
bool Gen >
629 template <
typename Key,
bool Gen >
631 if (new_size <
_h_.size())
return;
633 _h_.resize(new_size);
634 _v_.reserve(new_size);
642 template <
typename Key >
644 _end_safe_._setAtEnd_();
648 template <
typename Key >
656 template <
typename Key >
661 for (
Size i = 0; i < aSeq.size(); ++i) {
662 _h_.insert(aSeq._v_[i], i);
663 _v_.push_back(aSeq._v_[i]);
670 template <
typename Key >
672 _h_(size_param), _end_safe_{*this}, _rend_safe_{*this} {
679 template <
typename Key >
681 std::initializer_list< Key > list) : _end_safe_{*this}, _rend_safe_{*this} {
684 for (
const auto& elt: list) {
690 template <
typename Key >
693 _h_(aSeq._h_), _v_(aSeq._v_), _end_safe_{*this}, _rend_safe_{*this} {
700 template <
typename Key >
703 _h_(std::move(aSeq._h_)), _v_(std::move(aSeq._v_)), _end_safe_{*this}, _rend_safe_{*this} {
710 template <
typename Key >
716 template <
typename Key >
720 if (&aSeq !=
this) { _copy_(aSeq); }
726 template <
typename Key >
731 _h_ = std::move(aSeq._h_);
732 _v_ = std::move(aSeq._v_);
740 template <
typename Key >
742 return _h_.exists(k);
746 template <
typename Key >
749 _h_.insert(k, _h_.size());
755 template <
typename Key >
756 template <
typename... Args >
758 Key new_key(std::forward< Args >(args)...);
759 _h_.insert(new_key, _h_.size());
760 _v_.push_back(new_key);
765 template <
typename Key >
773 template <
typename Key >
780 }
catch (NotFound
const&) {
return; }
783 _v_.erase(_v_.begin() + pos);
784 for (
Idx i = pos, nb_elts = _h_.size() - 1; i < nb_elts; ++i) {
793 template <
typename Key >
795 if (iter.pos() >= size())
return;
798 Idx pos = iter.pos();
800 _v_.erase(_v_.begin() + pos);
802 for (
Idx i = pos, nb_elts = _h_.size() - 1; i < nb_elts; ++i) {
811 template <
typename Key >
819 template <
typename Key >
821 if (i >= _h_.size()) {
GUM_ERROR(NotFound,
"not enough elements in the sequence") }
827 template <
typename Key >
833 template <
typename Key >
839 template <
typename Key >
841 if (i >= _h_.size()) {
GUM_ERROR(NotFound,
"index too large") }
843 _h_.insert(newKey, i);
849 template <
typename Key >
864 template <
typename Key >
870 template <
typename Key >
872 return atPos(size() - 1);
876 template <
typename Key >
878 std::stringstream stream;
882 stream << 0 <<
":" << _v_[0];
884 for (
Idx i = 1; i < _h_.size(); ++i) {
885 stream <<
" - " << i <<
":" << _v_[i];
891 std::string res = stream.str();
896 template <
typename Key >
899 if (size() != k.size())
return false;
901 for (
Idx i = 0; i < size(); ++i)
902 if (_v_[i] != k._v_[i])
return false;
909 template <
typename Key >
916 template <
typename Key >
924 template <
typename Key >
926 return SequenceIteratorSafe< Key >{*
this};
930 template <
typename Key >
937 template <
typename Key >
940 it._setPos_(size() - 1);
945 template <
typename Key >
952 template <
typename Key >
954 return SequenceIterator< Key >{*
this};
958 template <
typename Key >
964 template <
typename Key >
966 SequenceIterator< Key > it{*
this};
967 it._setPos_(size() - 1);
972 template <
typename Key >
978 template <
typename Key >
980 if (new_size < _h_.size())
return;
982 _h_.resize(new_size);
983 _v_.reserve(new_size);
991 template <
typename Key >
998 template <
typename Key >
1006 template <
typename Key >
1014 template <
typename Key >
1022 template <
typename Key >
1029 template <
typename Key >
1036 template <
typename Key >
1043 template <
typename Key >
1048 if (!seq.
exists(*iter)) res << *iter;
1055 template <
typename Key >
Exception : the element we looked for cannot be found.
Exception : out of bound.
The internal class for storing (ordered) sequences of objects.
const iterator & end() const noexcept
Returns the unsafe end iterator.
std::string toString() const
Displays the content of the sequence.
void _copy_(const SequenceImplementation< Key, Gen > &aSeq)
Clears the current sequence and fill it with copies the element of aSeq.
SequenceIteratorSafe< Key > iterator_safe
Types for STL compliance.
void swap(Idx i, Idx j)
Swap index.
iterator_safe beginSafe() const
Returns a safe begin iterator.
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).
const Key & front() const
Returns the first element of the element.
std::vector< Key * > _v_
The set of the elements stored into the sequence.
void emplace(Args &&... args)
Emplace a new element in the sequence.
void _update_end_() noexcept
A method to update the end iterator after changes in the sequence.
iterator begin() const
Returns an unsafe begin iterator.
iterator rbegin() const
Returns an unsafe rbegin iterator.
SequenceIteratorSafe< Key > _rend_safe_
Stores the rend iterator for fast access.
bool operator==(const SequenceImplementation< Key, Gen > &k) const
Returns true if the content of k equals that of *this.
const iterator_safe & endSafe() const noexcept
Returns the safe end iterator.
void erase(const Key &k)
Remove an element from the sequence.
void clear()
Clear the sequence.
Size size() const noexcept
Returns the size of the sequence.
bool exists(const Key &k) const
Check the existence of k in the sequence.
const iterator & rend() const noexcept
Returns the unsafe rend iterator.
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).
const iterator_safe & rendSafe() const noexcept
Returns the safe rend iterator.
~SequenceImplementation() noexcept
Class destructor.
const Key & back() const
Returns the last element of the sequence.
const Key & atPos(Idx i) const
Returns the object at the pos i.
bool empty() const noexcept
Return true if empty.
SequenceImplementation(Size size_param=HashTableConst::default_size)
Default constructor.
void setAtPos(Idx i, const Key &newKey)
Change the value.
SequenceIteratorSafe< Key > _end_safe_
Stores the end iterator for fast access.
friend class SequenceIteratorSafe< Key >
friend class Sequence< Key >
void resize(Size new_size)
Modifies the size of the internal structures of the sequence.
iterator_safe rbeginSafe() const
Returns a safe rbegin iterator.
HashTable< Key, Idx > _h_
Keep track of the position of the element in v (for fast retrieval).
void insert(const Key &k)
Insert an element at the end of the sequence.
Idx pos(const Key &key) const
Returns the position of the object passed in argument (if it exists).
Safe iterators for Sequence.
SequenceIteratorSafe< Key > & operator--() noexcept
Point the iterator to the preceding value in the sequence.
SequenceIteratorSafe< Key > & operator=(const SequenceIteratorSafe< Key > &source) noexcept
Copy operator.
void _setAtEnd_() noexcept
The iterator points to the end (which is pos size()-1).
const SequenceImplementation< const gum::DiscreteVariable *, std::is_scalar< const gum::DiscreteVariable * >::value > * _seq_
SequenceIteratorSafe< Key > operator+(Size nb) noexcept
Returns a new iterator.
SequenceIteratorSafe< Key > & operator++() noexcept
Point the iterator to the next value in the 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.
Idx _iterator_
The index in the sequence's vector where the iterator is pointing.
const Key * operator->() const
Returns the value pointed to by the iterator (works only for non-scalars).
SequenceIteratorSafe(const SequenceImplementation< Key, Gen > &seq, Idx pos=0) noexcept
Constructor, always give a valid iterator (even if pos too large).
SequenceIteratorSafe< Key > & operator-=(Size nb) noexcept
Makes the iterator point to i elements further in the 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 the same elements.
~SequenceIteratorSafe() noexcept
Class destructor.
void _setPos_(Idx pos) noexcept
The iterator points to the posth element (0 = beginning of the sequence).
SequenceIteratorSafe< Key > & operator+=(Size nb) noexcept
Makes the iterator point to i elements further in the sequence.
const Key & operator*() const
Returns the value pointed to by the iterator.
friend class SequenceImplementation
The generic class for storing (ordered) sequences of objects.
Sequence(Size size_param=HashTableConst::default_size)
Default constructor.
~Sequence() noexcept
Class destructor.
Sequence< Key > & operator=(const Sequence< Key > &aSeq)
Copy operator.
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.
#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.