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