|
| | 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
|
template<class T>
class ADS::ArraySequence< T >
a SEQUENCE realized with a vector that doubles when it is full
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)
template<class T>
template<std::input_iterator I, std::sentinel_for< I > S>
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)