ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::ArraySequence< T > Class Template Reference

a SEQUENCE realized with a vector that doubles when it is full More...

#include <ADS/sequence/ArraySequence.h>

Public types

using Item = T
 the type of the items
using Pos = long
 a position is an index, from -1 to \( n \)
using value_type = T
 the type of the items
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances
using reference = T &
 a reference to an item
using const_reference = const T &
 a constant reference to an item
using pointer = T *
 a pointer to an item
using const_pointer = const T *
 a constant pointer to an item
using iterator = T *
 the iterator on the items
using const_iterator = const T *
 the iterator on the constant items
using reverse_iterator = std::reverse_iterator< iterator >
 the iterator on the items, backwards
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 the iterator on the constant items, backwards

Public member functions

 ArraySequence (void)
 the empty sequence, with no vector
 ArraySequence (std::initializer_list< T > items)
 the sequence of the given items, in their order
template<std::input_iterator I, std::sentinel_for< I > S>
 ArraySequence (I first, S last)
 the sequence of the items of [ first , last ), in their order
 ArraySequence (const ArraySequence &other)
 a copy of other
 ArraySequence (ArraySequence &&other) noexcept
 the items of other, which is left empty
ArraySequence & operator= (ArraySequence other) noexcept
 the items of other, by copy and swap
bool empty (void) const
 true if there is no item, i.e., \( n = 0 \)
bool finished (Pos p) const
 true if \( p \) is -1 or \( n \)
Pos head (void) const
 the position of the first item, 0
Pos tail (void) const
 the position of the last item, \( n - 1 \)
Pos next (Pos p) const
 the position \( p + 1 \)
Pos prev (Pos p) const
 the position \( p - 1 \)
Pos insert (Pos p, const T &v)
 puts \( v \) at the index \( p \), the items from the index \( p \) moving one place on, which takes \( O( n - p ) \) time, \( O( 1 ) \) amortized at the end
template<std::input_iterator I, std::sentinel_for< I > S>
Pos insert (Pos p, I first, S last)
 puts the \( k \) items of [ first , last ) at the indices \( p , \ldots , p + k - 1 \), in their order, the items from the index \( p \) moving \( k \) places on once, in \( O( n - p + k ) \) time (exercise 2.1 of Morin's book), where \( k \) calls of insert( p , v ) move them \( k \) times, in \( O( k ( n - p ) ) \); the range may hold items of this sequence
Pos remove (Pos p)
 removes the item at the index \( p \), the following ones moving one place back; a vector left a quarter full is halved
const T & read (Pos p) const
 the item at the index \( p \)
void write (Pos p, const T &v)
 writes \( v \) at the index \( p \)
Pos size (void) const
 the number of items, \( n \)
Pos capacity (void) const
 the size \( c \) of the vector, which the analysis of the doubling is about
bool operator== (const ArraySequence &o) const
 the same items in the same order
iterator begin (void)
 the iterator on the first item
iterator end (void)
 the iterator past the last item
const_iterator begin (void) const
 the constant iterator on the first item
const_iterator end (void) const
 the constant iterator past the last item
const_iterator cbegin (void) const
 the constant iterator on the first item
const_iterator cend (void) const
 the constant iterator past the last item
reverse_iterator rbegin (void)
 the iterator on the last item, backwards
reverse_iterator rend (void)
 the iterator before the first item, backwards
const_reverse_iterator rbegin (void) const
 the constant iterator on the last item, backwards
const_reverse_iterator rend (void) const
 the constant iterator before the first item, backwards
T * data (void)
 the vector of the items
const T * data (void) const
 the vector of the items, constant
T & operator[] (Pos i)
 the item at the index \( i \)
const T & operator[] (Pos i) const
 the item at the index \( i \), constant
void push_back (const T &v)
 \( v \) at the end, for std::back_inserter

Detailed description

template<class T>
class ADS::ArraySequence< T >

a SEQUENCE realized with a vector that doubles when it is full

Member typedef documentation

◆ const_iterator

template<class T>
using ADS::ArraySequence< T >::const_iterator = const T *

the iterator on the constant items

◆ const_pointer

template<class T>
using ADS::ArraySequence< T >::const_pointer = const T *

a constant pointer to an item

◆ const_reference

template<class T>
using ADS::ArraySequence< T >::const_reference = const T &

a constant reference to an item

◆ const_reverse_iterator

template<class T>
using ADS::ArraySequence< T >::const_reverse_iterator = std::reverse_iterator< const_iterator >

the iterator on the constant items, backwards

◆ difference_type

template<class T>
using ADS::ArraySequence< T >::difference_type = std::ptrdiff_t

the type of the distances

◆ Item

template<class T>
using ADS::ArraySequence< T >::Item = T

the type of the items

◆ iterator

template<class T>
using ADS::ArraySequence< T >::iterator = T *

the iterator on the items

◆ pointer

template<class T>
using ADS::ArraySequence< T >::pointer = T *

a pointer to an item

◆ Pos

template<class T>
using ADS::ArraySequence< T >::Pos = long

a position is an index, from -1 to \( n \)

◆ reference

template<class T>
using ADS::ArraySequence< T >::reference = T &

a reference to an item

◆ reverse_iterator

template<class T>
using ADS::ArraySequence< T >::reverse_iterator = std::reverse_iterator< iterator >

the iterator on the items, backwards

◆ size_type

template<class T>
using ADS::ArraySequence< T >::size_type = std::size_t

the type of the sizes

◆ value_type

template<class T>
using ADS::ArraySequence< T >::value_type = T

the type of the items

Constructor and destructor documentation

◆ ArraySequence() [1/5]

template<class T>
ADS::ArraySequence< T >::ArraySequence ( void )

the empty sequence, with no vector

◆ ArraySequence() [2/5]

template<class T>
ADS::ArraySequence< T >::ArraySequence ( std::initializer_list< T > items)

the sequence of the given items, in their order

◆ ArraySequence() [3/5]

template<class T>
template<std::input_iterator I, std::sentinel_for< I > S>
ADS::ArraySequence< T >::ArraySequence ( I first,
S last )

the sequence of the items of [ first , last ), in their order

◆ ArraySequence() [4/5]

template<class T>
ADS::ArraySequence< T >::ArraySequence ( const ArraySequence< T > & other)

a copy of other

◆ ArraySequence() [5/5]

template<class T>
ADS::ArraySequence< T >::ArraySequence ( ArraySequence< T > && other)
noexcept

the items of other, which is left empty

Member function documentation

◆ begin() [1/2]

template<class T>
iterator ADS::ArraySequence< T >::begin ( void )
inlinenodiscard

the iterator on the first item

◆ begin() [2/2]

template<class T>
const_iterator ADS::ArraySequence< T >::begin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ capacity()

template<class T>
Pos ADS::ArraySequence< T >::capacity ( void ) const
nodiscard

the size \( c \) of the vector, which the analysis of the doubling is about

◆ cbegin()

template<class T>
const_iterator ADS::ArraySequence< T >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<class T>
const_iterator ADS::ArraySequence< T >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ data() [1/2]

template<class T>
T * ADS::ArraySequence< T >::data ( void )
inlinenodiscard

the vector of the items

◆ data() [2/2]

template<class T>
const T * ADS::ArraySequence< T >::data ( void ) const
inlinenodiscard

the vector of the items, constant

◆ empty()

template<class T>
bool ADS::ArraySequence< T >::empty ( void ) const
nodiscard

true if there is no item, i.e., \( n = 0 \)

◆ end() [1/2]

template<class T>
iterator ADS::ArraySequence< T >::end ( void )
inlinenodiscard

the iterator past the last item

◆ end() [2/2]

template<class T>
const_iterator ADS::ArraySequence< T >::end ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ finished()

template<class T>
bool ADS::ArraySequence< T >::finished ( Pos p) const
nodiscard

true if \( p \) is -1 or \( n \)

◆ head()

template<class T>
Pos ADS::ArraySequence< T >::head ( void ) const
nodiscard

the position of the first item, 0

◆ insert() [1/2]

template<class T>
Pos ADS::ArraySequence< T >::insert ( Pos p,
const T & v )

puts \( v \) at the index \( p \), the items from the index \( p \) moving one place on, which takes \( O( n - p ) \) time, \( O( 1 ) \) amortized at the end

In pseudocode (sections 4.2 and 4.2.1), \( A[ 0 .. c - 1 ] \) the vector of capacity \( c \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( p , v ) \cr 2 & \quad \textbf{if}\ n = c\ \textbf{then}\ \mathrm{resize}( \max( 2 c , 1 ) )\quad \triangleright\ \text{no room: the vector doubles} \cr 3 & \quad \text{shift } A[ p .. n - 1 ] \text{ one place right, from the end} \cr 4 & \quad A[ p ] \gets v;\ n \gets n + 1;\ \textbf{return}\ p \end{array}} \]

Precondition
\( 0 \le p \le n \)
Postcondition
one item more, \( v \) at the index \( p \), the others where they were before \( p \) and one place on from \( p \) (an audit)

◆ insert() [2/2]

template<class T>
template<std::input_iterator I, std::sentinel_for< I > S>
Pos ADS::ArraySequence< T >::insert ( Pos p,
I first,
S last )

puts the \( k \) items of [ first , last ) at the indices \( p , \ldots , p + k - 1 \), in their order, the items from the index \( p \) moving \( k \) places on once, in \( O( n - p + k ) \) time (exercise 2.1 of Morin's book), where \( k \) calls of insert( p , v ) move them \( k \) times, in \( O( k ( n - p ) ) \); the range may hold items of this sequence

In pseudocode (Morin, exercise 2.1), \( C[ 0 .. k - 1 ] \) the items:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insertAll}( p , C ) \cr 2 & \quad \textbf{if}\ n + k > c\ \textbf{then}\ \mathrm{resize}( \text{the least } 2^j c \ge n + k ) \cr 3 & \quad A[ p + k .. n + k - 1 ] \gets A[ p .. n - 1 ] \cr 4 & \quad A[ p .. p + k - 1 ] \gets C;\ n \gets n + k \cr 5 & \quad \textbf{return}\ p \end{array}} \]

Precondition
\( 0 \le p \le n \)
Postcondition
\( k \) items more, those before \( p \) where they were and those from \( p \) \( k \) places on (an audit)

◆ next()

template<class T>
Pos ADS::ArraySequence< T >::next ( Pos p) const
nodiscard

the position \( p + 1 \)

Precondition
\( -1 \le p < n \)

◆ operator=()

template<class T>
ArraySequence & ADS::ArraySequence< T >::operator= ( ArraySequence< T > other)
noexcept

the items of other, by copy and swap

◆ operator==()

template<class T>
bool ADS::ArraySequence< T >::operator== ( const ArraySequence< T > & o) const
nodiscard

the same items in the same order

◆ operator[]() [1/2]

template<class T>
T & ADS::ArraySequence< T >::operator[] ( Pos i)
nodiscard

the item at the index \( i \)

Precondition
\( 0 \le i < n \)

◆ operator[]() [2/2]

template<class T>
const T & ADS::ArraySequence< T >::operator[] ( Pos i) const
nodiscard

the item at the index \( i \), constant

Precondition
\( 0 \le i < n \)

◆ prev()

template<class T>
Pos ADS::ArraySequence< T >::prev ( Pos p) const
nodiscard

the position \( p - 1 \)

Precondition
\( 0 \le p \le n \)

◆ push_back()

template<class T>
void ADS::ArraySequence< T >::push_back ( const T & v)

\( v \) at the end, for std::back_inserter

◆ rbegin() [1/2]

template<class T>
reverse_iterator ADS::ArraySequence< T >::rbegin ( void )
inlinenodiscard

the iterator on the last item, backwards

◆ rbegin() [2/2]

template<class T>
const_reverse_iterator ADS::ArraySequence< T >::rbegin ( void ) const
inlinenodiscard

the constant iterator on the last item, backwards

◆ read()

template<class T>
const T & ADS::ArraySequence< T >::read ( Pos p) const
nodiscard

the item at the index \( p \)

Precondition
\( 0 \le p < n \)

◆ remove()

template<class T>
Pos ADS::ArraySequence< T >::remove ( Pos p)

removes the item at the index \( p \), the following ones moving one place back; a vector left a quarter full is halved

In pseudocode (sections 4.2 and 4.2.1), the halving at a quarter keeping \( O( 1 ) \) amortized time per operation:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{remove}( p ) \cr 2 & \quad \text{shift } A[ p + 1 .. n - 1 ] \text{ one place left};\ n \gets n - 1 \cr 3 & \quad \textbf{if}\ 4 n \le c\ \textbf{then}\ \mathrm{resize}( c / 2 )\quad \triangleright\ \text{a quarter full: the vector halves} \cr 4 & \quad \textbf{return}\ p\quad \triangleright \ \text{now the position of the next item} \end{array}} \]

Precondition
\( 0 \le p < n \)
Postcondition
one item less, the others where they were before \( p \) and one place back after it (an audit)

◆ rend() [1/2]

template<class T>
reverse_iterator ADS::ArraySequence< T >::rend ( void )
inlinenodiscard

the iterator before the first item, backwards

◆ rend() [2/2]

template<class T>
const_reverse_iterator ADS::ArraySequence< T >::rend ( void ) const
inlinenodiscard

the constant iterator before the first item, backwards

◆ size()

template<class T>
Pos ADS::ArraySequence< T >::size ( void ) const
nodiscard

the number of items, \( n \)

◆ tail()

template<class T>
Pos ADS::ArraySequence< T >::tail ( void ) const
nodiscard

the position of the last item, \( n - 1 \)

◆ write()

template<class T>
void ADS::ArraySequence< T >::write ( Pos p,
const T & v )

writes \( v \) at the index \( p \)

Precondition
\( 0 \le p < n \)
Postcondition
read( p ) is \( v \), the other items unchanged (an audit)

The documentation for this class was generated from the following file: