|
| | RingDeque (void)=default |
| | the empty deque, with no places
|
| | RingDeque (std::initializer_list< T > l) |
| | the items of l, in order
|
| | RingDeque (const RingDeque &o) |
| | a copy of o
|
| | RingDeque (RingDeque &&o) noexcept |
| | the vector of o, which is left empty with no capacity
|
| RingDeque & | operator= (RingDeque o) noexcept |
| | the items of o, by copy and swap
|
| | ~RingDeque () |
| | destroys the items and frees the places
|
| void | swap (RingDeque &o) noexcept |
| | exchanges the vector with that of o
|
| bool | empty (void) const |
| | true if there is no item, i.e., the two counters are equal
|
| std::size_t | size (void) const |
| | the number of items, the difference of the two counters
|
| std::size_t | capacity (void) const |
| | the places, 0 or a power of 2
|
| void | reserve (std::size_t n) |
| | at least \( n \) places
|
| T & | front (void) |
| | the item at the front
|
| const T & | front (void) const |
| | the item at the front, constant
|
| T & | back (void) |
| | the item at the back
|
| const T & | back (void) const |
| | the item at the back, constant
|
| T & | operator[] (std::size_t i) |
| | the item of rank \( i \) from the front, at the place \( ( h + i ) \mathbin{\&} ( m - 1 ) \)
|
| const T & | operator[] (std::size_t i) const |
| | the item of rank \( i \) from the front, constant
|
| void | push_back (const T &v) |
| | \( v \) at the back, emplace_back( v )
|
| void | push_back (T &&v) |
| | \( v \) at the back, moved
|
| void | push_front (const T &v) |
| | \( v \) at the front, emplace_front( v )
|
| void | push_front (T &&v) |
| | \( v \) at the front, moved
|
| template<class... Args> |
| T & | emplace_back (Args &&... args) |
| | the item built from args at the back, \( O( 1 ) \) amortized
|
| template<class... Args> |
| T & | emplace_front (Args &&... args) |
| | the item built from args at the front, \( O( 1 ) \) amortized
|
| void | pop_front (void) |
| | removes the item at the front, advancing \( h \)
|
| void | pop_back (void) |
| | removes the item at the back, moving \( t \) back
|
| void | clear (void) |
| | removes all the items, the places kept
|
| void | insert (std::size_t i, const T &v) |
| | puts \( v \) at the rank \( i \), moving by one place the items on the shorter side of \( i \), in \( O( 1 + \min\{ i , n - i \} ) \) amortized time (the add( i , x ) of the ArrayDeque of section 2.4 of Morin's book)
|
| void | erase (std::size_t i) |
| | removes the item of rank \( i \), moving by one place the items on the shorter side, in \( O( 1 + \min\{ i , n - i \} ) \) time (the remove( i ) of Morin's ArrayDeque)
|
| void | rotate (std::size_t r) |
| | the item of rank \( i \) moved to the rank \( ( i + r ) \bmod n \), for every \( i \): \( O( 1 ) \) time when the vector is full, the two counters alone moving, and \( O( 1 + \min\{ r , n - r \} ) \) otherwise, the items moved one by one from one end to the other (exercise 2.5 of Morin's book)
|
| void | shrink_to_fit (void) |
| | the least number of places, a power of 2 and 8 at least, that holds the \( n \) items, the items moved there in order; the deque never shrinks by itself (Morin's ArrayDeque shrinks when \( 3 n < m \)), so that its space is that of its largest size until this call
|
| bool | operator== (const RingDeque &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
|
template<class T>
class ADS::RingDeque< T >
a double ended queue on a circular vector of \( 2^k \) places that doubles
template<class T>
template<class... Args>
the item built from args at the back, \( O( 1 ) \) amortized
In pseudocode (Knuth, section 2.2.2, the circular queue, with the doubling of section 4.2 of the book), \( A[ 0 .. m - 1 ] \) the places, \( h \) and \( t \) the counters of the front and of the back:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{emplace\_back}( \mathit{args} ) \cr
2 & \quad \textbf{if}\ t - h = m\ \textbf{then}\quad \triangleright
\ \text{full} \cr
3 & \qquad B \gets \text{a vector of } \max( 2 m , 8 ) \text{ places} \cr
4 & \qquad \text{move } A[ h \bmod m .. ) \text{ and then the part }
\text{that wrapped around to } B[ 0 .. t - h ) \cr
5 & \qquad A \gets B;\ m \gets \max( 2 m , 8 );\ t \gets t - h;\ h
\gets 0 \cr
6 & \quad A[ t \bmod m ] \gets \text{the item built from }
\mathit{args};\ t \gets t + 1 \cr
7 & \quad \textbf{return}\ A[ ( t - 1 ) \bmod m ]
\end{array}}
\]
- Postcondition
- one item more, the new one at the back
puts \( v \) at the rank \( i \), moving by one place the items on the shorter side of \( i \), in \( O( 1 + \min\{ i , n - i \} ) \) amortized time (the add( i , x ) of the ArrayDeque of section 2.4 of Morin's book)
In pseudocode (Morin, section 2.4), the ranks \( 0 .. n - 1 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{insert}( i , v ) \cr
2 & \quad \textbf{if}\ i < \lfloor n / 2 \rfloor\ \textbf{then} \cr
3 & \qquad \mathrm{pushFront}( D[ 0 ] ) \cr
4 & \qquad \textbf{for}\ k \gets 1\ \textbf{to}\ i - 1\ \textbf{do}
\ D[ k ] \gets D[ k + 1 ] \cr
5 & \quad \textbf{else} \cr
6 & \qquad \mathrm{pushBack}( D[ n - 1 ] ) \cr
7 & \qquad \textbf{for}\ k \gets n - 1\ \textbf{downto}\ i + 1
\ \textbf{do}\ D[ k ] \gets D[ k - 1 ] \cr
8 & \quad D[ i ] \gets v
\end{array}}
\]
- Precondition
- \( i \le n \)
- Postcondition
- one item more, \( v \) at the rank \( i \)
the item of rank \( i \) moved to the rank \( ( i + r ) \bmod n \), for every \( i \): \( O( 1 ) \) time when the vector is full, the two counters alone moving, and \( O( 1 + \min\{ r , n - r \} ) \) otherwise, the items moved one by one from one end to the other (exercise 2.5 of Morin's book)
In pseudocode (Morin, exercise 2.5), \( r \) reduced modulo \( n \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{rotate}( r ) \cr
2 & \quad \textbf{if}\ n = m\ \textbf{then}\ h \gets h - r;
\ t \gets t - r;\ \textbf{return} \cr
3 & \quad \textbf{if}\ r \le n - r\ \textbf{then}\ \textbf{repeat}
\ r\ \text{ times: }\mathrm{pushFront}( \mathrm{popBack}() ) \cr
4 & \quad \textbf{else}\ \textbf{repeat}\ n - r\ \text{ times: }
\mathrm{pushBack}( \mathrm{popFront}() )
\end{array}}
\]