aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
sequence.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Header file of gum::Sequence, a class for storing (ordered) sequences
25  * of objects.
26  *
27  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
28  */
29 #ifndef GUM_SEQUENCE_H
30 #define GUM_SEQUENCE_H
31 
32 #include <initializer_list>
33 #include <limits>
34 #include <type_traits>
35 #include <vector>
36 
37 #include <agrum/agrum.h>
38 #include <agrum/tools/core/hashTable.h>
39 #include <agrum/tools/core/set.h>
40 
41 namespace gum {
42 
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44  template < typename Key, typename Alloc, bool >
46  template < typename Key, typename Alloc >
47  class Sequence;
48  template < typename Key >
50  template < typename Key >
51  using SequenceIterator = SequenceIteratorSafe< Key >;
52  template < typename Key >
53  using SequenceConstIterator = SequenceIteratorSafe< Key >;
54 #endif
55 
56  // ===========================================================================
57  // === NON SCALAR GUM SEQUENCE IMPLEMENTATION ===
58  // ===========================================================================
59  /**
60  * @class SequenceImplementation
61  * @headerfile sequence.h <agrum/tools/core/sequence.h>
62  * @brief The internal class for storing (ordered) sequences of objects.
63  * @ingroup sequence_group
64  *
65  * A SequenceImplementation<Key,Alloc,bool Gen> is a specialized version of
66  * of a Sequence<Key,Alloc>. It shall not be used by itself but rather
67  * through the Sequence class. A SequenceImplementation is quite similar to a
68  * vector<Key> in that it stores an ordered set of elements. The main
69  * difference between these two data structures lies in the fact that, given
70  * a key, it is possible to retrieve from a SequenceImplementation the index
71  * in the vector where the key lies in O(1). As a result, it is not possible
72  * to insert a given element twice in the sequence, that is, all the Keys
73  * must be different.
74  *
75  * When the Boolean template parameter gen is false, SequenceImplementation
76  * implements a very generic sequence. This allows having Sequences
77  * containing elements of virtually any class or type. When the Boolean gen
78  * is equal to true, the SequenceImplementation shall contain only scalar
79  * types (integers, floats, pointers, etc). As such, knowning that the
80  * element is a scalar enables to optimize the code of the sequences.
81  * Determining whether gen should be set to true or false is not left to the
82  * developper but is determined by the compiler itself at compile time.
83  *
84  * @tparam Key The elements type stored in the sequence.
85  * @tparam Alloc The values allocator.
86  * @tparam Gen Used for meta-programation.
87  */
88  template < typename Key, typename Alloc, bool Gen >
90  /// Friends to speed up access
91  /// @{
92  template < typename K, typename A, bool >
93  friend class SequenceImplementation;
94  friend class SequenceIteratorSafe< Key >;
95  friend class Sequence< Key, Alloc >;
96  /// @}
97 
98  public:
99  /// Types for STL compliance.
100  /// @{
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;
111  using iterator_safe = SequenceIteratorSafe< Key >;
112  using const_iterator_safe = SequenceIteratorSafe< Key >;
113  /// @}
114 
115  private:
116  // ============================================================================
117  /// @name Constructors
118  // ============================================================================
119  /// @{
120  /**
121  * @brief Default constructor.
122  * @param size_param The intial size of the gum::SequenceImplementation.
123  */
125 
126  /**
127  * @brief Initializer list constructor.
128  * @param list The initializer list.
129  */
130  SequenceImplementation(std::initializer_list< Key > list);
131 
132  /**
133  * @brief Copy constructor.
134  *
135  * @param aSeq The sequence the elements of which will be copied.
136  * @warning The elements of the newly constructed sequence are copies of
137  * those in aSeq.
138  */
139  SequenceImplementation(const SequenceImplementation< Key, Alloc, Gen >& aSeq);
140 
141  /**
142  * @brief Generalised copy constructor.
143  *
144  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
145  * @param aSeq The sequence the elements of which will be copied.
146  * @warning The elements of the newly constructed sequence are copies of
147  * those in aSeq.
148  */
149  template < typename OtherAlloc >
150  SequenceImplementation(const SequenceImplementation< Key, OtherAlloc, Gen >& aSeq);
151 
152  /**
153  * @brief Move constructor.
154  * @param aSeq The gum::SequenceImplementation to move/
155  */
156  SequenceImplementation(SequenceImplementation< Key, Alloc, Gen >&& aSeq);
157 
158  /// @}
159 
160  public:
161  // ============================================================================
162  /// @name Destructor
163  // ============================================================================
164  /// @{
165 
166  /**
167  * @brief Class destructor.
168  */
169  ~SequenceImplementation() noexcept;
170 
171  /// @}
172  // ============================================================================
173  /// @name Iterators
174  // ============================================================================
175  /// @{
176 
177  /**
178  * @brief Returns a safe begin iterator.
179  * @return Returns a safe begin iterator.
180  */
181  iterator_safe beginSafe() const;
182 
183  /**
184  * @brief Returns a safe rbegin iterator.
185  * @return Returns a safe rbegin iterator.
186  */
187  iterator_safe rbeginSafe() const;
188 
189  /**
190  * @brief Returns the safe end iterator.
191  * @return Returns the safe end iterator.
192  */
193  const iterator_safe& endSafe() const noexcept;
194 
195  /**
196  * @brief Returns the safe rend iterator.
197  * @return Returns the safe rend iterator.
198  */
199  const iterator_safe& rendSafe() const noexcept;
200 
201  /**
202  * @brief Returns an unsafe begin iterator.
203  * @return Returns an unsafe begin iterator.
204  */
205  iterator begin() const;
206 
207  /**
208  * @brief Returns an unsafe rbegin iterator.
209  * @return Returns an unsafe rbegin iterator.
210  */
211  iterator rbegin() const;
212 
213  /**
214  * @brief Returns the unsafe end iterator.
215  * @return Returns the unsafe end iterator.
216  */
217  const iterator& end() const noexcept;
218 
219  /**
220  * @brief Returns the unsafe rend iterator.
221  * @return Returns the unsafe rend iterator.
222  */
223  const iterator& rend() const noexcept;
224 
225  /// @}
226 
227  private:
228  // ============================================================================
229  /// @name Private Operators
230  // ============================================================================
231  /// @{
232 
233  /**
234  * @brief Copy operator.
235  * @param aSeq The sequence to copy.
236  * @return Returns a ref to this.
237  */
238  SequenceImplementation< Key, Alloc, Gen >&
239  operator=(const SequenceImplementation< Key, Alloc, Gen >& aSeq);
240 
241  /**
242  * @brief Generalized opy operator.
243  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
244  * @param aSeq The sequence to copy.
245  * @return Returns a ref to this.
246  */
247  template < typename OtherAlloc >
248  SequenceImplementation< Key, Alloc, Gen >&
249  operator=(const SequenceImplementation< Key, OtherAlloc, Gen >& aSeq);
250 
251  /**
252  * @brief Move operator.
253  * @param aSeq The sequence to move.
254  * @return Returns a ref to this.
255  */
256  SequenceImplementation< Key, Alloc, Gen >&
257  operator=(SequenceImplementation< Key, Alloc, Gen >&& aSeq);
258 
259  /// @}
260 
261  public:
262  // ============================================================================
263  /// @name Operators
264  // ============================================================================
265  /// @{
266 
267  /**
268  * @brief Insert k at the end of the sequence (synonym for insert).
269  * @param k The key we wish to insert in the sequence.
270  * @return Returns this gum::SequenceImplementation.
271  * @throw DuplicateElement Raised if the sequence contains already k.
272  */
273  SequenceImplementation< Key, Alloc, Gen >& operator<<(const Key& k);
274 
275  /**
276  * @brief Insert k at the end of the sequence (synonym for insert).
277  * @param k The key we wish to insert in the sequence.
278  * @return Returns this gum::SequenceImplementation.
279  * @throw DuplicateElement Raised if the sequence contains already k.
280  */
281  SequenceImplementation< Key, Alloc, Gen >& operator<<(Key&& k);
282 
283  /**
284  * @brief Remove k in the sequence (synonym for erase).
285  *
286  * If the element cannot be found, the function does nothing. In
287  * particular, it throws no exception.
288  *
289  * @param k The key we wish to remove.
290  * @return Returns this gum::SequenceImplementation.
291  */
292  SequenceImplementation< Key, Alloc, Gen >& operator>>(const Key& k);
293 
294  /**
295  * @brief Returns the element at position i (synonym for atPos).
296  * @param i The position of the element to return.
297  * @return Returns the element at position i.
298  * @throw OutOfBounds Raised if the element does not exist.
299  */
300  const Key& operator[](Idx i) const;
301 
302  /**
303  * @brief Returns true if the content of k equals that of *this.
304  *
305  * Note that two sequences are equal if and only if they contain the same
306  * Keys (using Key::operator==) in the same order.
307  *
308  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
309  * @param k The other gum::SequenceImplementation.
310  * @brief Returns true if both gum::SequenceImplementation are equal.
311  */
312  template < typename OtherAlloc >
313  bool operator==(const SequenceImplementation< Key, OtherAlloc, Gen >& k) const;
314 
315  /**
316  * @brief Returns true if the content of k is different from that of *this.
317  *
318  * Note that two sequences are equal if and only if they contain the same
319  * variables (using Key::operator==) in the same order.
320  *
321  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
322  * @param k The other gum::SequenceImplementation.
323  * @brief Returns true if both gum::SequenceImplementation are not equal.
324  */
325  template < typename OtherAlloc >
326  bool operator!=(const SequenceImplementation< Key, OtherAlloc, Gen >& k) const;
327 
328  /// @}
329  // ============================================================================
330  /// @name Accessors / Modifiers
331  // ============================================================================
332  /// @{
333 
334  /**
335  * @brief Clear the sequence.
336  */
337  void clear();
338 
339  /**
340  * @brief Returns the size of the sequence.
341  * @return Returns the size of the sequence.
342  */
343  Size size() const noexcept;
344 
345  /**
346  * @brief Return true if empty.
347  * @return Return true if empty.
348  */
349  bool empty() const noexcept;
350 
351  /**
352  * @brief Check the existence of k in the sequence.
353  *
354  * The complexity is \f$o(1)\f$.
355  *
356  * @param k The key to check for existence.
357  * @return Returns true if k is in the gum::SequenceImplementation.
358  */
359  bool exists(const Key& k) const;
360 
361  /**
362  * @brief Insert an element at the end of the sequence.
363  *
364  * The complexity is \f$o(1)\f$.
365  *
366  * @param k The element to insert.
367  * @throw DuplicateElement Raised if the sequence contains already k.
368  */
369  void insert(const Key& k);
370 
371  /**
372  * @brief Move an element at the end of the sequence.
373  *
374  * The complexity is \f$o(1)\f$.
375  *
376  * @param k The element to insert.
377  * @throw DuplicateElement Raised if the sequence contains already k.
378  */
379  void insert(Key&& k);
380 
381  /**
382  * @brief Emplace a new element in the sequence.
383  *
384  * The emplace is a method that allows to construct directly an element of
385  * type Key by passing to its constructor all the arguments it needs.
386  *
387  * @tparam Args The arguments types passed to the constructor.
388  * @param args The arguments passed to the constructor.
389  * @throw DuplicateElement Raised if the sequence contains already k.
390  */
391  template < typename... Args >
392  void emplace(Args&&... args);
393 
394  /**
395  * @brief Remove an element from the sequence.
396  *
397  * If the element cannot be found, the function does nothing. In
398  * particular, it throws no exception. Complexity \f$o(n)\f$ (need to
399  * change the position of at most the n elements).
400  *
401  * @param k The element to remove.
402  */
403  void erase(const Key& k);
404 
405  /**
406  * @brief Remove from the sequence the element pointed to by the iterator.
407  *
408  * If the element cannot be found, the function does nothing. In
409  * particular, it throws no exception. Complexity \f$o(n)\f$ (need to
410  * change the position of at most the n elements)
411  *
412  * @param k The iterator poiting to the element to remove.
413  */
414  void erase(const iterator_safe& k);
415 
416  /**
417  * @brief Returns the object at the pos i.
418  * @param i The position of the element to return.
419  * @return Returns the object at the pos i.
420  * @throw NotFound Raised if the element does not exist.
421  */
422  const Key& atPos(Idx i) const;
423 
424  /**
425  * @brief Returns the position of the object passed in argument (if it
426  * exists).
427  * @param key The element for which the positon is returned.
428  * @return Returns the position of the object passed in argument.
429  * @throw NotFound Raised if the element does not exist.
430  */
431  Idx pos(const Key& key) const;
432 
433  /**
434  * @brief Change the value.
435  *
436  * @param i The element's position.
437  * @param newKey The element's new value.
438  * @throw NotFound Raised if the element does not exist.
439  * @throw DuplicateElement Raised if newKey alreay exists.
440  */
441  void setAtPos(Idx i, const Key& newKey);
442 
443  /**
444  * @brief Change the value.
445  *
446  * @param i The element's position.
447  * @param newKey The element's new value.
448  * @throw NotFound Raised if the element does not exist.
449  * @throw DuplicateElement Raised if newKey alreay exists.
450  */
451  void setAtPos(Idx i, Key&& newKey);
452 
453  /**
454  * @brief Swap index.
455  * @param i The index of the first elt to swap.
456  * @param j The index of the other elt to swap.
457  */
458  void swap(Idx i, Idx j);
459 
460  /**
461  * @brief Returns the first element of the element.
462  * @return Returns the first element of the element.
463  * @throw NotFound Raised if the sequence is empty.
464  */
465  const Key& front() const;
466 
467  /**
468  * @brief Returns the last element of the sequence.
469  * @return Returns the last element of the sequence.
470  * @throw NotFound Raised if the sequence is empty.
471  */
472  const Key& back() const;
473 
474  /**
475  * @brief Displays the content of the sequence.
476  * @return The content of the sequence.
477  */
478  std::string toString() const;
479 
480  /**
481  * @brief Modifies the size of the internal structures of the sequence.
482  *
483  * This function is provided for optimization issues. When you know you
484  * will have to insert elements into the sequence, it may be faster to use
485  * this function prior to the additions because it will change once and for
486  * all the sizes of all the internal containers. Note that if you provide a
487  * size that is smaller than the number of elements of the sequence, the
488  * function will not modify anything.
489  *
490  * @param new_size The internal structure new size.
491  */
492  void resize(Size new_size);
493 
494  /// @}
495 
496  private:
497  /// Keep track of the position of the element in v (for fast retrieval).
499 
500  /// The set of the elements stored into the sequence.
501  std::vector< Key*, typename Alloc::template rebind< Key* >::other > _v_;
502 
503  // Note that, using Key* in _v_, we actually store the Key only once in the
504  // sequence (that is, within _h_). This enables storing big objects within
505  // sequences without having memory overhead.
506 
507  /// Stores the end iterator for fast access.
509 
510  /// Stores the rend iterator for fast access.
512 
513  /**
514  * @brief A method to update the end iterator after changes in the
515  * sequence.
516  */
517  void _update_end_() noexcept;
518 
519  /**
520  * @brief Clears the current sequence and fill it with copies the element
521  * of aSeq.
522  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
523  * @param aSeq The gum::SequenceImplementation to copy.
524  */
525  template < typename OtherAlloc >
526  void _copy_(const SequenceImplementation< Key, OtherAlloc, Gen >& aSeq);
527 
528  /**
529  * @brief Insert an element at the end of the sequence.
530  *
531  * @param bucket The bucket holing the store to insert.
532  */
533  void _insert_(HashTableBucket< Key, Idx >&& bucket);
534  };
535 
536 #ifndef DOXYGEN_SHOULD_SKIP_THIS
537 
538  // ===========================================================================
539  // === GUM_SEQUENCE_IMPLEMENTATION OPTIMIZED FOR SCALARS ===
540  // ===========================================================================
541  /**
542  * @class SequenceImplementation
543  * @headerfile sequence.h <agrum/tools/core/sequence.h>
544  * @brief The class for storing (ordered) sequences of scalar objects
545  * @ingroup sequence_group
546  *
547  * A Sequence<Key> is quite similar to a vector<Key> in that it stores an
548  * ordered set of elements. The main difference between these two data
549  * structures lies in the fact that, given a key, it is possible to retrieve
550  * from a Sequence<Key> the index in the vector where the key lies in O(1).
551  * As a result, it is not possible to insert a given element twice in the
552  * sequence, that is, all the Keys must be different.
553  *
554  * @tparam Key The elements type stored in the sequence.
555  * @tparam Alloc The values allocator.
556  * @tapram Gen Used for meta-programation.
557  */
558  template < typename Key, typename Alloc >
559  class SequenceImplementation< Key, Alloc, true > {
560  /// Friends to speed up access
561  /// @{
562  template < typename K, typename A, bool >
563  friend class SequenceImplementation;
564  friend class SequenceIteratorSafe< Key >;
565  friend class Sequence< Key, Alloc >;
566  /// @}
567 
568  public:
569  /// Types for STL compliance.
570  /// @{
571  using value_type = Key;
572  using size_type = std::size_t;
573  using difference_type = std::ptrdiff_t;
574  using allocator_type = Alloc;
575  using iterator = SequenceIterator< Key >;
577  using iterator_safe = SequenceIteratorSafe< Key >;
578  using const_iterator_safe = SequenceIteratorSafe< Key >;
579  /// @}
580 
581  private:
582  // ============================================================================
583  /// @name Constructors
584  // ============================================================================
585  /// @{
586  /**
587  * @brief Default constructor.
588  * @param size_param The intial size of the gum::SequenceImplementation.
589  */
590  SequenceImplementation(Size size_param = HashTableConst::default_size);
591 
592  /**
593  * @brief Initializer list constructor.
594  * @param list The initializer list.
595  */
596  SequenceImplementation(std::initializer_list< Key > list);
597 
598  /**
599  * @brief Copy constructor.
600  *
601  * @param aSeq The sequence the elements of which will be copied.
602  * @warning The elements of the newly constructed sequence are copies of
603  * those in aSeq.
604  */
605  SequenceImplementation(const SequenceImplementation< Key, Alloc, true >& aSeq);
606 
607  /**
608  * @brief Generalised copy constructor.
609  *
610  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
611  * @param aSeq The sequence the elements of which will be copied.
612  * @warning The elements of the newly constructed sequence are copies of
613  * those in aSeq.
614  */
615  template < typename OtherAlloc >
616  SequenceImplementation(const SequenceImplementation< Key, OtherAlloc, true >& aSeq);
617 
618  /**
619  * @brief Move constructor.
620  * @param aSeq The gum::SequenceImplementation to move/
621  */
622  SequenceImplementation(SequenceImplementation< Key, Alloc, true >&& aSeq);
623 
624  /// @}
625  public:
626  // ============================================================================
627  /// @name Destructor
628  // ============================================================================
629  /// @{
630 
631  /**
632  * @brief Class destructor.
633  */
634  ~SequenceImplementation() noexcept;
635 
636  /// @}
637  // ============================================================================
638  /// @name Iterators
639  // ============================================================================
640  /// @{
641 
642  /**
643  * @brief Returns a safe begin iterator.
644  * @return Returns a safe begin iterator.
645  */
646  iterator_safe beginSafe() const;
647 
648  /**
649  * @brief Returns a safe rbegin iterator.
650  * @return Returns a safe rbegin iterator.
651  */
652  iterator_safe rbeginSafe() const;
653 
654  /**
655  * @brief Returns the safe end iterator.
656  * @return Returns the safe end iterator.
657  */
658  const iterator_safe& endSafe() const noexcept;
659 
660  /**
661  * @brief Returns the safe rend iterator.
662  * @return Returns the safe rend iterator.
663  */
664  const iterator_safe& rendSafe() const noexcept;
665 
666  /**
667  * @brief Returns an unsafe begin iterator.
668  * @return Returns an unsafe begin iterator.
669  */
670  iterator begin() const;
671 
672  /**
673  * @brief Returns an unsafe rbegin iterator.
674  * @return Returns an unsafe rbegin iterator.
675  */
676  iterator rbegin() const;
677 
678  /**
679  * @brief Returns the unsafe end iterator.
680  * @return Returns the unsafe end iterator.
681  */
682  const iterator& end() const noexcept;
683 
684  /**
685  * @brief Returns the unsafe rend iterator.
686  * @return Returns the unsafe rend iterator.
687  */
688  const iterator& rend() const noexcept;
689 
690  /// @}
691 
692  private:
693  // ============================================================================
694  /// @name Private Operators
695  // ============================================================================
696  /// @{
697 
698  /**
699  * @brief Copy operator.
700  * @param aSeq The sequence to copy.
701  * @return Returns a ref to this.
702  */
703  SequenceImplementation< Key, Alloc, true >&
704  operator=(const SequenceImplementation< Key, Alloc, true >& aSeq);
705 
706  /**
707  * @brief generalized opy operator.
708  * @tparam otheralloc the other gum::sequenceimplementation allocator.
709  * @param aseq the sequence to copy.
710  * @return returns a ref to this.
711  */
712  template < typename OtherAlloc >
713  SequenceImplementation< Key, Alloc, true >&
714  operator=(const SequenceImplementation< Key, OtherAlloc, true >& aSeq);
715 
716  /**
717  * @brief Move operator.
718  * @param aSeq The sequence to move.
719  * @return Returns a ref to this.
720  */
721  SequenceImplementation< Key, Alloc, true >&
722  operator=(SequenceImplementation< Key, Alloc, true >&& aSeq);
723 
724  /// @}
725 
726  public:
727  // ============================================================================
728  /// @name Operators
729  // ============================================================================
730  /// @{
731 
732  /**
733  * @brief Insert k at the end of the sequence (synonym for insert).
734  * @param k The key we wish to insert in the sequence.
735  * @return Returns this gum::SequenceImplementation.
736  * @throw DuplicateElement Raised if the sequence contains already k.
737  */
738  SequenceImplementation< Key, Alloc, true >& operator<<(Key k);
739 
740  /**
741  * @brief Remove k in the sequence (synonym for erase).
742  *
743  * If the element cannot be found, the function does nothing. In
744  * particular, it throws no exception.
745  *
746  * @param k The key we wish to remove.
747  * @return Returns this gum::SequenceImplementation.
748  */
749  SequenceImplementation< Key, Alloc, true >& operator>>(Key k);
750 
751  /**
752  * @brief Returns the element at position i (synonym for atPos).
753  * @param i The position of the element to return.
754  * @return Returns the element at position i.
755  * @throw OutOfBounds Raised if the element does not exist.
756  */
757  const Key& operator[](Idx i) const;
758 
759  /**
760  * @brief Returns true if the content of k equals that of *this.
761  *
762  * Note that two sequences are equal if and only if they contain the same
763  * Keys (using Key::operator==) in the same order.
764  *
765  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
766  * @param k The other gum::SequenceImplementation.
767  * @brief Returns true if both gum::SequenceImplementation are equal.
768  */
769  template < typename OtherAlloc >
770  bool operator==(const SequenceImplementation< Key, OtherAlloc, true >& k) const;
771 
772  /**
773  * @brief Returns true if the content of k is different from that of *this.
774  *
775  * Note that two sequences are equal if and only if they contain the same
776  * variables (using Key::operator==) in the same order.
777  *
778  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
779  * @param k The other gum::SequenceImplementation.
780  * @brief Returns true if both gum::SequenceImplementation are not equal.
781  */
782  template < typename OtherAlloc >
783  bool operator!=(const SequenceImplementation< Key, OtherAlloc, true >& k) const;
784 
785  /// @}
786  // ============================================================================
787  /// @name Accessors / Modifiers
788  // ============================================================================
789  /// @{
790 
791  /**
792  * @brief Clear the sequence.
793  */
794  void clear();
795 
796  /**
797  * @brief Returns the size of the sequence.
798  * @return Returns the size of the sequence.
799  */
800  Size size() const noexcept;
801 
802  /**
803  * @brief Return true if empty.
804  * @return Return true if empty.
805  */
806  bool empty() const noexcept;
807 
808  /**
809  * @brief Check the existence of k in the sequence.
810  *
811  * The complexity is \f$o(1)\f$.
812  *
813  * @param k The key to check for existence.
814  * @return Returns true if k is in the gum::SequenceImplementation.
815  */
816  bool exists(Key k) const;
817 
818  /**
819  * @brief Insert an element at the end of the sequence.
820  *
821  * The complexity is \f$o(1)\f$.
822  *
823  * @param k The element to insert.
824  * @throw DuplicateElement Raised if the sequence contains already k.
825  */
826  void insert(Key k);
827 
828  /**
829  * @brief Emplace a new element in the sequence.
830  *
831  * The emplace is a method that allows to construct directly an element of
832  * type Key by passing to its constructor all the arguments it needs.
833  *
834  * @tparam Args The arguments types passed to the constructor.
835  * @param args The arguments passed to the constructor.
836  * @throw DuplicateElement Raised if the sequence contains already k.
837  */
838  template < typename... Args >
839  void emplace(Args&&... args);
840 
841  /**
842  * @brief Remove an element from the sequence.
843  *
844  * If the element cannot be found, the function does nothing. In
845  * particular, it throws no exception. Complexity \f$o(n)\f$ (need to
846  * change the position of at most the n elements).
847  *
848  * @param k The element to remove.
849  */
850  void erase(Key k);
851 
852  /**
853  * @brief Remove from the sequence the element pointed to by the iterator.
854  *
855  * If the element cannot be found, the function does nothing. In
856  * particular, it throws no exception. Complexity \f$o(n)\f$ (need to
857  * change the position of at most the n elements)
858  *
859  * @param k The iterator poiting to the element to remove.
860  */
861  void erase(const iterator_safe& k);
862 
863  /**
864  * @brief Returns the object at the pos i.
865  * @param i The position of the element to return.
866  * @return Returns the object at the pos i.
867  * @throw NotFound Raised if the element does not exist.
868  */
869  const Key& atPos(Idx i) const;
870 
871  /**
872  * @brief Returns the position of the object passed in argument (if it
873  * exists).
874  * @param key The element for which the positon is returned.
875  * @rerurn Returns the position of the object passed in argument.
876  * @throw NotFound Raised if the element does not exist.
877  */
878  Idx pos(Key key) const;
879 
880  /**
881  * @brief Change the value.
882  *
883  * @param i The element's position.
884  * @param newKey The element's new value.
885  * @throw NotFound Raised if the element does not exist.
886  * @throw DuplicateElement Raised if newKey alreay exists.
887  */
888  void setAtPos(Idx i, Key newKey);
889 
890  /**
891  * @brief Swap index.
892  * @param i The index of the first elt to swap.
893  * @param j The index of the other elt to swap.
894  */
895  void swap(Idx i, Idx j);
896 
897  /**
898  * @brief Returns the first element of the element.
899  * @return Returns the first element of the element.
900  * @throw NotFound Raised if the sequence is empty.
901  */
902  const Key& front() const;
903 
904  /**
905  * @brief Returns the last element of the sequence.
906  * @return Returns the last element of the sequence.
907  * @throw NotFound Raised if the sequence is empty.
908  */
909  const Key& back() const;
910 
911  /**
912  * @brief Displays the content of the sequence.
913  * @return The content of the sequence.
914  */
915  std::string toString() const;
916 
917  /**
918  * @brief Modifies the size of the internal structures of the sequence.
919  *
920  * This function is provided for optimization issues. When you know you
921  * will have to insert elements into the sequence, it may be faster to use
922  * this function prior to the additions because it will change once and for
923  * all the sizes of all the internal containers. Note that if you provide a
924  * size that is smaller than the number of elements of the sequence, the
925  * function will not modify anything.
926  *
927  * @param new_size The internal structure new size.
928  */
929  void resize(Size new_size);
930 
931  /// @}
932 
933  private:
934  /// Keep track of the position of the element in v (for fast retrieval).
935  HashTable< Key, Idx, Alloc > _h_;
936 
937  /// The set of the elements stored into the sequence.
938  std::vector< Key, Alloc > _v_;
939 
940  /// Stores the end iterator for fast access.
941  SequenceIteratorSafe< Key > _end_safe_;
942 
943  /// Stores the rend iterator for fast access.
944  SequenceIteratorSafe< Key > _rend_safe_;
945 
946  /**
947  * @brief A method to update the end iterator after changes in the
948  * sequence.
949  */
950  void _update_end_() noexcept;
951 
952  /**
953  * @brief Clears the current sequence and fill it with copies the element
954  * of aSeq.
955  * @tparam OtherAlloc The other gum::SequenceImplementation allocator.
956  * @para aSeq The gum::SequenceImplementation to copy.
957  */
958  template < typename OtherAlloc >
959  void _copy_(const SequenceImplementation< Key, OtherAlloc, true >& aSeq);
960 
961  /**
962  * @brief Insert an element at the end of the sequence.
963  *
964  * @param bucket The bucket holing the store to insert.
965  */
966  void _insert_(Key k);
967  };
968 
969 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
970 
971  // ===========================================================================
972  // === GUM_SEQUENCE ===
973  // ===========================================================================
974  /**
975  * @class Sequence
976  * @headerfile sequence.h <agrum/tools/core/sequence.h>
977  * @brief The generic class for storing (ordered) sequences of objects.
978  * @ingroup basicstruct_group
979  * @ingroup sequence_group
980  *
981  * A gum::Sequence<Key> is quite similar to a std::vector<Key> in that it
982  * stores an ordered set of elements. The main difference between these two
983  * data structures lies in the fact that, given a key, it is possible to
984  * retrieve from a gum::Sequence<Key> the index in the vector where the key
985  * lies in O(1). As a result, it is not possible to insert a given element
986  * twice in the sequence, that is, all the Keys must be different.
987  *
988  * @par Usage example:
989  * @code
990  * // creation of a sequence
991  * Sequence<int> seq { 1, 2, 3, 4 };
992  * Sequence<string> seq2;
993  *
994  * // creation of safe iterators
995  * SequenceIteratorSafe<int> iter1 = seq.beginSafe (); // points to 1
996  * SequenceIteratorSafe<int> iter2 = iter1;
997  * SequenceIteratorSafe<int> iter3 = std::move ( iter1 );
998  *
999  * // creation of unsafe iterators
1000  * SequenceIterator<int> xiter1 = seq.begin (); // points to 1
1001  * SequenceIterator<int> xiter2 = xiter1;
1002  * SequenceIterator<int> xiter3 = std::move ( xiter1 );
1003  *
1004  * // parsing the sequence
1005  * for ( auto iter = seq.begin (); iter != seq.end (); ++iter )
1006  * std::cout << *iter << std::endl;
1007  * for ( auto iter = seq.rbegin (); iter != seq.rend (); --iter )
1008  * std::cout << *iter << std::endl;
1009  * for ( auto iter = seq.begin (); iter != seq.end (); ++iter )
1010  * std::cout << iter->size () << std::endl;
1011  * @endcode
1012  *
1013  * @tparam Key The elements type stored in the sequence.
1014  * @tparam Alloc The values allocator.
1015  */
1016  template < typename Key, typename Alloc = std::allocator< Key > >
1017  class Sequence: public SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value > {
1018  public:
1019  /// Types for STL compliance.
1020  /// @{
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;
1031  using iterator_safe = SequenceIteratorSafe< Key >;
1032  using const_iterator_safe = SequenceIteratorSafe< Key >;
1033  /// @}
1034 
1035  /// The gum::Sequence implementation.
1036  using Implementation = SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >;
1037 
1038  // ============================================================================
1039  /// @name Constructors / Destructors
1040  // ============================================================================
1041  ///@{
1042 
1043  /**
1044  * @brief Default constructor.
1045  * @param size_param The intial size of the gum::SequenceImplementation.
1046  */
1047  Sequence(Size size_param = HashTableConst::default_size);
1048 
1049  /**
1050  * @brief Initializer list constructor.
1051  * @param list The initializer list.
1052  */
1053  Sequence(std::initializer_list< Key > list);
1054 
1055  /**
1056  * @brief Copy constructor.
1057  *
1058  * @param aSeq The sequence the elements of which will be copied.
1059  * @warning The elements of the newly constructed sequence are copies of
1060  * those in aSeq.
1061  */
1062  Sequence(const Sequence< Key, Alloc >& aSeq);
1063 
1064  /**
1065  * @brief Generalised copy constructor.
1066  *
1067  * @tparam OtherAlloc The other gum::Sequence allocator.
1068  * @param aSeq The sequence the elements of which will be copied.
1069  * @warning The elements of the newly constructed sequence are copies of
1070  * those in aSeq.
1071  */
1072  template < typename OtherAlloc >
1073  Sequence(const Sequence< Key, OtherAlloc >& aSeq);
1074 
1075  /**
1076  * @brief Move constructor.
1077  * @param aSeq The gum::Sequence to move/
1078  */
1079  Sequence(Sequence< Key, Alloc >&& aSeq);
1080 
1081  /**
1082  * @brief Class destructor.
1083  */
1084  ~Sequence() noexcept;
1085 
1086  /// @}
1087  // ============================================================================
1088  /// @name Operators
1089  // ============================================================================
1090  /// @{
1091 
1092  /**
1093  * @brief Copy operator.
1094  * @param aSeq The sequence to copy.
1095  * @return Returns a ref to this.
1096  */
1097  Sequence< Key, Alloc >& operator=(const Sequence< Key, Alloc >& aSeq);
1098 
1099  /**
1100  * @brief Generalized opy operator.
1101  * @tparam otheralloc The other gum::sequenceimplementation allocator.
1102  * @param aSeq The sequence to copy.
1103  * @return Returns a ref to this.
1104  */
1105  template < typename OtherAlloc >
1106  Sequence< Key, Alloc >& operator=(const Sequence< Key, OtherAlloc >& aSeq);
1107 
1108  /**
1109  * @brief Move operator.
1110  * @param aSeq The sequence to move.
1111  * @return Returns a ref to this.
1112  */
1113  Sequence< Key, Alloc >& operator=(Sequence< Key, Alloc >&& aSeq);
1114 
1115  /// @}
1116  // ============================================================================
1117  /// @name Accessors / Modifiers
1118  // ============================================================================
1119  /// @{
1120 
1121  /**
1122  * @brief Difference between two sequences as a Set<Key> = this \ seq.
1123  *
1124  * @param seq The gum::Sequence to substract of this.
1125  * @return Returns the set difference : *this \ seq.
1126  */
1127  template < typename OtherAlloc >
1128  Set< Key, Alloc > diffSet(const Sequence< Key, OtherAlloc >& seq) const;
1129 
1130  /// @}
1131  };
1132 
1133 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1134 
1135  // dummy classes that will enable discriminate without overhead between
1136  // scalars and non-scalars operators * and ->
1137  template < bool gen >
1138  struct SequenceIteratorGet {
1139  template < typename Key >
1140  INLINE static const Key& op_star(const Key* x) {
1141  return *x;
1142  }
1143  template < typename Key >
1144  INLINE static const Key* op_arrow(const Key* x) {
1145  return x;
1146  }
1147  };
1148 
1149  template <>
1150  struct SequenceIteratorGet< true > {
1151  template < typename Key >
1152  INLINE static const Key& op_star(const Key& x) {
1153  return x;
1154  }
1155  template < typename Key >
1156  INLINE static const Key* op_arrow(const Key& x) {
1157  return &x;
1158  }
1159  };
1160 
1161 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
1162 
1163  // ===========================================================================
1164  // class SequenceIteratorSafe
1165  // ===========================================================================
1166  /**
1167  * @class SequenceIteratorSafe
1168  * @headerfile sequence.h <agrum/tools/core/sequence.h>
1169  * @brief Safe iterators for Sequence.
1170  * @ingroup sequence_group
1171  *
1172  * This iterator enables to parse the elements in the sequence. Note that
1173  * this is similar to a const safe iterator because the elements that can be
1174  * accessed in the sequence are constant.
1175  *
1176  * @par Usage example:
1177  * @code
1178  * // creation of a sequence
1179  * Sequence<int> seq { 1, 2, 3, 4 };
1180  * Sequence<string> seq2;
1181  *
1182  * // creation of iterators
1183  * auto iter1 = seq.beginSafe (); // points to 1
1184  * auto iiter2 = iter1;
1185  * auto iiter3 = std::move ( iter1 );
1186  *
1187  * // parsing the sequence
1188  * for ( auto iter = seq.beginSafe (); iter != seq.endSafe (); ++iter )
1189  * std::cout << *iter << std::endl;
1190  * for ( auto iter = seq.rbeginSafe (); iter != seq.rend (); --iter )
1191  * std::cout << *iter << std::endl;
1192  * for ( auto iter = seq.beginSafe (); iter != seq.end (); ++iter )
1193  * std::cout << iter->size () << std::endl;
1194  * @endcode
1195  *
1196  * @tparam Key The type of elements stored in the gum::Sequence.
1197  */
1198  template < typename Key >
1200  /// Friend to speed up access.
1201  template < typename K, typename A, bool >
1203 
1204  public:
1205  /// types for STL compliance
1206  /// @{
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;
1214  /// @}
1215 
1216  private:
1217  /// The Getter used by this iterator.
1218  using Getter = SequenceIteratorGet< std::is_scalar< Key >::value >;
1219 
1220 
1221  /**
1222  * @brief Constructor, always give a valid iterator (even if pos too
1223  * large).
1224  *
1225  * @warning if pos is greater than the size of the sequence, the iterator
1226  * is made pointing to end().
1227  *
1228  * @tparam Alloc The sequence allocator.
1229  * @tparam Gen Used for meta-programation.
1230  * @param seq The sequence.
1231  * @param pos Indicates to which position of the sequence the iterator
1232  * should be pointing. By default, the iterator points to begin().
1233  */
1234  template < typename Alloc, bool Gen >
1235  SequenceIteratorSafe(const SequenceImplementation< Key, Alloc, Gen >& seq,
1236  Idx pos = 0) noexcept;
1237 
1238  public:
1239  // ============================================================================
1240  /// @name Constructors / Destructors
1241  // ============================================================================
1242  ///@{
1243 
1244 
1245  /**
1246  * @brief Constructor, always give a valid iterator (even if pos too
1247  * large).
1248  *
1249  * @warning if pos is greater than the size of the sequence, the iterator
1250  * is made pointing to end().
1251  *
1252  * @param seq the sequence
1253  * @param pos indicates to which position of the sequence the iterator
1254  * should be pointing. By default, the iterator points to begin()
1255  * @tparam Alloc The sequence allocator.
1256  */
1257  template < typename Alloc >
1258  SequenceIteratorSafe(const Sequence< Key, Alloc >& seq, Idx pos = 0) noexcept;
1259 
1260  /**
1261  * @brief Copy constructor.
1262  * @param source The iterator to copy.
1263  */
1264  SequenceIteratorSafe(const SequenceIteratorSafe< Key >& source) noexcept;
1265 
1266  /**
1267  * @brief Move constructor.
1268  * @param source The iterator to move.
1269  */
1270  SequenceIteratorSafe(SequenceIteratorSafe< Key >&& source) noexcept;
1271 
1272  /**
1273  * @brief Class destructor.
1274  */
1275  ~SequenceIteratorSafe() noexcept;
1276 
1277  ///@}
1278  // ============================================================================
1279  /// @name Operators
1280  // ============================================================================
1281  ///@{
1282 
1283  /**
1284  * @brief Copy operator.
1285  * @param source The iterator to copy.
1286  * @return Returns this iterator.
1287  */
1288  SequenceIteratorSafe< Key >& operator=(const SequenceIteratorSafe< Key >& source) noexcept;
1289 
1290  /**
1291  * @brief Move operator.
1292  * @param source The iterator to move.
1293  * @return Returns this iterator.
1294  */
1295  SequenceIteratorSafe< Key >& operator=(SequenceIteratorSafe< Key >&& source) noexcept;
1296 
1297  /**
1298  * @brief Point the iterator to the next value in the sequence.
1299  *
1300  * @warning if the iterator already points to end(), it is unaltered.
1301  * @return Returns this iterator.
1302  */
1303  SequenceIteratorSafe< Key >& operator++() noexcept;
1304 
1305  /**
1306  * @brief Point the iterator to the preceding value in the sequence.
1307  *
1308  * @warning If the iterator already points to rend(), it is unaltered.
1309  *
1310  * @return Returns this iterator.
1311  */
1312  SequenceIteratorSafe< Key >& operator--() noexcept;
1313 
1314  /**
1315  * @brief Makes the iterator point to i elements further in the sequence.
1316  *
1317  * @warning If moving the iterator nb would make it point outside the
1318  * Sequence, then iterator is moved to end or rend, depending on the sign
1319  * of nb.
1320  *
1321  * @param nb The number of steps to move the iterator.
1322  * @return Returns this iterator.
1323  */
1324  SequenceIteratorSafe< Key >& operator+=(Size nb) noexcept;
1325 
1326  /**
1327  * @brief Makes the iterator point to i elements further in the sequence.
1328  *
1329  * @warning If moving the iterator nb would make it point outside the
1330  * Sequence, then iterator is moved to end or rend, depending on the
1331  * sign of nb.
1332  *
1333  * @param nb The number of steps to move the iterator.
1334  * @return Returns this iterator.
1335  */
1336  SequenceIteratorSafe< Key >& operator-=(Size nb) noexcept;
1337 
1338  /**
1339  * @brief Returns a new iterator.
1340  *
1341  * @warning The created iterator should point outside the Sequence, then it
1342  * is set either to end or rend, depending on the sign of nb.
1343  *
1344  * @param nb The number of steps the created iterator is ahead of this.
1345  * @return Returns a new iterator.
1346  */
1347  SequenceIteratorSafe< Key > operator+(Size nb) noexcept;
1348 
1349  /**
1350  * @brief Returns a new iterator.
1351  *
1352  * @warning The created iterator should point outside the Sequence, then it
1353  * is set either to end or rend, depending on the sign of nb.
1354  *
1355  * @param nb The number of steps the created iterator is behind of this.
1356  * @brief Returns a new iterator.
1357  */
1358  SequenceIteratorSafe< Key > operator-(Size nb) noexcept;
1359 
1360  /**
1361  * @brief Checks whether two iterators are pointing to different elements.
1362  *
1363  * @param source The iterator to test for inequality.
1364  * @return Returns true if both iterators are not equal.
1365  */
1366  bool operator!=(const SequenceIteratorSafe< Key >& source) const noexcept;
1367 
1368  /**
1369  * @brief Checks whether two iterators are pointing to the same elements.
1370  *
1371  * @param source The iterator to test for equality.
1372  * @return Returns true if both iterators are equal.
1373  */
1374  bool operator==(const SequenceIteratorSafe< Key >& source) const noexcept;
1375 
1376  /**
1377  * @brief Returns the value pointed to by the iterator.
1378  * @return Returns the value pointed to by the iterator.
1379  * @throw UndefinedIteratorValue Raised on end() or rend().
1380  */
1381  const Key& operator*() const;
1382 
1383  /**
1384  * @brief Returns the value pointed to by the iterator (works only for
1385  * non-scalars).
1386  * @return Returns the value pointed to by the iterator (works only for
1387  * non-scalars).
1388  */
1389  const Key* operator->() const;
1390 
1391  /// @}
1392  // ============================================================================
1393  /// @name Accessors / Modifiers
1394  // ============================================================================
1395  /// @{
1396 
1397  /**
1398  * @brief Returns the position of the iterator in the sequence.
1399  * @return Returns the position of the iterator in the sequence.
1400  * @throw UndefinedIteratorValue Raised on end() or rend().
1401  */
1402  Idx pos() const;
1403 
1404  /// @}
1405 
1406  private:
1407  /// The index in the sequence's vector where the iterator is pointing.
1409 
1410  /// The sequence pointed to by the iterator (by default, key is a scalar).
1411  const SequenceImplementation< Key, std::allocator< Key >, std::is_scalar< Key >::value >* _seq_;
1412 
1413  /// The iterator points to the posth element (0 = beginning of the
1414  /// sequence).
1415  void _setPos_(Idx pos) noexcept;
1416 
1417  /// The iterator points to rend.
1418  void _setAtRend_() noexcept;
1419 
1420  /// The iterator points to the end (which is pos size()-1).
1421  void _setAtEnd_() noexcept;
1422  };
1423 
1424  /// @brief A << operator for displaying the content of the Sequence.
1425  template < typename Key, typename Alloc >
1426  std::ostream& operator<<(std::ostream& stream, const Sequence< Key, Alloc >& s);
1427 
1428 } /* namespace gum */
1429 
1430 
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 >;
1436 #endif
1437 
1438 
1439 // always include the implementation of the templates
1440 #include <agrum/tools/core/sequence_tpl.h>
1441 
1442 #endif // GUM_SEQUENCE_H
HashTable< Key, Idx, Alloc > _h_
Keep track of the position of the element in v (for fast retrieval).
Definition: sequence.h:498
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.
Definition: sequence_tpl.h:230
Sequence(std::initializer_list< Key > list)
Initializer list constructor.
Safe iterators for Sequence.
Definition: sequence.h:1199
SequenceIteratorSafe< Key > operator-(Size nb) noexcept
Returns a new iterator.
Definition: sequence_tpl.h:187
bool operator!=(const SequenceIteratorSafe< Key > &source) const noexcept
Checks whether two iterators are pointing to different elements.
Definition: sequence_tpl.h:204
SequenceIteratorSafe< Key > & operator=(const SequenceIteratorSafe< Key > &source) noexcept
Copy operator.
Definition: sequence_tpl.h:125
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).
Definition: sequence_tpl.h:499
iterator begin() const
Returns an unsafe begin iterator.
Definition: sequence_tpl.h:631
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).
Definition: sequence.h:1411
Sequence(Size size_param=HashTableConst::default_size)
Default constructor.
iterator_safe rbeginSafe() const
Returns a safe rbegin iterator.
Definition: sequence_tpl.h:616
std::vector< Key *, typename Alloc::template rebind< Key *>::other > _v_
The set of the elements stored into the sequence.
Definition: sequence.h:501
void erase(const iterator_safe &k)
Remove from the sequence the element pointed to by the iterator.
Definition: sequence_tpl.h:457
void clear()
Clear the sequence.
Definition: sequence_tpl.h:264
const iterator_safe & rendSafe() const noexcept
Returns the safe rend iterator.
Definition: sequence_tpl.h:625
const iterator_safe & endSafe() const noexcept
Returns the safe end iterator.
Definition: sequence_tpl.h:610
Sequence(Sequence< Key, Alloc > &&aSeq)
Move constructor.
SequenceIteratorSafe< Key > & operator=(SequenceIteratorSafe< Key > &&source) noexcept
Move operator.
Definition: sequence_tpl.h:134
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.
Definition: sequence_tpl.h:659
Size size() const noexcept
Returns the size of the sequence.
Definition: sequence_tpl.h:37
void _setAtEnd_() noexcept
The iterator points to the end (which is pos size()-1).
Definition: sequence_tpl.h:236
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
SequenceImplementation(Size size_param=HashTableConst::default_size)
Default constructor.
Definition: sequence_tpl.h:287
void _update_end_() noexcept
A method to update the end iterator after changes in the sequence.
Definition: sequence_tpl.h:258
Idx pos() const
Returns the position of the iterator in the sequence.
Definition: sequence_tpl.h:211
Idx _iterator_
The index in the sequence&#39;s vector where the iterator is pointing.
Definition: sequence.h:1408
The generic class for storing (ordered) sequences of objects.
Definition: sequence.h:1017
Sequence(const Sequence< Key, OtherAlloc > &aSeq)
Generalised copy constructor.
Sequence(const Sequence< Key, Alloc > &aSeq)
Copy constructor.
~SequenceIteratorSafe() noexcept
Class destructor.
Definition: sequence_tpl.h:118
SequenceIteratorSafe< Key > & operator++() noexcept
Point the iterator to the next value in the sequence.
Definition: sequence_tpl.h:142
SequenceIteratorSafe< Key > & operator--() noexcept
Point the iterator to the preceding value in the sequence.
Definition: sequence_tpl.h:153
~Sequence() noexcept
Class destructor.
bool empty() const noexcept
Return true if empty.
Definition: sequence_tpl.h:43
const Key * operator->() const
Returns the value pointed to by the iterator (works only for non-scalars).
Definition: sequence_tpl.h:248
std::string toString() const
Displays the content of the sequence.
Definition: sequence_tpl.h:552
SequenceIteratorSafe(const SequenceIteratorSafe< Key > &source) noexcept
Copy constructor.
Definition: sequence_tpl.h:100
void _setPos_(Idx pos) noexcept
The iterator points to the posth element (0 = beginning of the sequence).
Definition: sequence_tpl.h:221
iterator_safe beginSafe() const
Returns a safe begin iterator.
Definition: sequence_tpl.h:603
const Key & operator[](Idx i) const
Returns the element at position i (synonym for atPos).
Definition: sequence_tpl.h:493
SequenceImplementation< Key, Alloc, Gen > & operator=(const SequenceImplementation< Key, Alloc, Gen > &aSeq)
Copy operator.
Definition: sequence_tpl.h:350
SequenceImplementation(std::initializer_list< Key > list)
Initializer list constructor.
Definition: sequence_tpl.h:296
SequenceImplementation(const SequenceImplementation< Key, Alloc, Gen > &aSeq)
Copy constructor.
Definition: sequence_tpl.h:309
SequenceImplementation< Key, Alloc, Gen > & operator>>(const Key &k)
Remove k in the sequence (synonym for erase).
Definition: sequence_tpl.h:476
void _insert_(HashTableBucket< Key, Idx > &&bucket)
Insert an element at the end of the sequence.
void setAtPos(Idx i, Key &&newKey)
Change the value.
Definition: sequence_tpl.h:515
friend class SequenceImplementation
Friend to speed up access.
Definition: sequence.h:1202
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.
Definition: sequence_tpl.h:193
bool exists(const Key &k) const
Check the existence of k in the sequence.
Definition: sequence_tpl.h:387
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.
Definition: sequence_tpl.h:171
SequenceIteratorSafe< Key > _end_safe_
Stores the end iterator for fast access.
Definition: sequence.h:508
SequenceIteratorSafe(SequenceIteratorSafe< Key > &&source) noexcept
Move constructor.
Definition: sequence_tpl.h:109
SequenceImplementation(SequenceImplementation< Key, Alloc, Gen > &&aSeq)
Move constructor.
Definition: sequence_tpl.h:332
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.
Definition: sequence_tpl.h:437
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.
Definition: sequence_tpl.h:525
Sequence< Key, Alloc > & operator=(const Sequence< Key, Alloc > &aSeq)
Copy operator.
const Key & operator*() const
Returns the value pointed to by the iterator.
Definition: sequence_tpl.h:242
SequenceImplementation< Key, Alloc, Gen > & operator=(SequenceImplementation< Key, Alloc, Gen > &&aSeq)
Move operator.
Definition: sequence_tpl.h:373
void emplace(Args &&... args)
Emplace a new element in the sequence.
const Key & front() const
Returns the first element of the element.
Definition: sequence_tpl.h:540
Sequence< Key, Alloc > & operator=(const Sequence< Key, OtherAlloc > &aSeq)
Generalized opy operator.
const iterator & rend() const noexcept
Returns the unsafe rend iterator.
Definition: sequence_tpl.h:653
void insert(Key &&k)
Move an element at the end of the sequence.
Definition: sequence_tpl.h:402
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.
Definition: sequence_tpl.h:638
SequenceIteratorSafe< Key > operator+(Size nb) noexcept
Returns a new iterator.
Definition: sequence_tpl.h:181
~SequenceImplementation() noexcept
Class destructor.
Definition: sequence_tpl.h:343
SequenceIteratorSafe< Key > & operator+=(Size nb) noexcept
Makes the iterator point to i elements further in the sequence.
Definition: sequence_tpl.h:161
iterator rbegin() const
Returns an unsafe rbegin iterator.
Definition: sequence_tpl.h:644
SequenceIteratorSafe< Key > _rend_safe_
Stores the rend iterator for fast access.
Definition: sequence.h:511
void setAtPos(Idx i, const Key &newKey)
Change the value.
Definition: sequence_tpl.h:505
bool operator==(const SequenceImplementation< Key, OtherAlloc, Gen > &k) const
Returns true if the content of k equals that of *this.
Definition: sequence_tpl.h:573
const Key & back() const
Returns the last element of the sequence.
Definition: sequence_tpl.h:546
const Key & atPos(Idx i) const
Returns the object at the pos i.
Definition: sequence_tpl.h:483
void insert(const Key &k)
Insert an element at the end of the sequence.
Definition: sequence_tpl.h:393