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

a double ended queue on a circular vector of \( 2^k \) places that doubles More...

#include <ADS/queue/RingDeque.h>

Public types

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 iterator = detail::MaskIterator< T , false >
 the iterator on the items
using const_iterator = detail::MaskIterator< T , true >
 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

 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

Detailed description

template<class T>
class ADS::RingDeque< T >

a double ended queue on a circular vector of \( 2^k \) places that doubles

Member typedef documentation

◆ const_iterator

template<class T>
using ADS::RingDeque< T >::const_iterator = detail::MaskIterator< T , true >

the iterator on the constant items

◆ const_reference

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

a constant reference to an item

◆ const_reverse_iterator

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

the iterator on the constant items, backwards

◆ difference_type

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

the type of the distances

◆ iterator

template<class T>
using ADS::RingDeque< T >::iterator = detail::MaskIterator< T , false >

the iterator on the items

◆ reference

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

a reference to an item

◆ reverse_iterator

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

the iterator on the items, backwards

◆ size_type

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

the type of the sizes

◆ value_type

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

the type of the items

Constructor and destructor documentation

◆ RingDeque() [1/4]

template<class T>
ADS::RingDeque< T >::RingDeque ( void )
default

the empty deque, with no places

◆ RingDeque() [2/4]

template<class T>
ADS::RingDeque< T >::RingDeque ( std::initializer_list< T > l)

the items of l, in order

◆ RingDeque() [3/4]

template<class T>
ADS::RingDeque< T >::RingDeque ( const RingDeque< T > & o)

a copy of o

◆ RingDeque() [4/4]

template<class T>
ADS::RingDeque< T >::RingDeque ( RingDeque< T > && o)
noexcept

the vector of o, which is left empty with no capacity

◆ ~RingDeque()

template<class T>
ADS::RingDeque< T >::~RingDeque ( )

destroys the items and frees the places

Member function documentation

◆ back() [1/2]

template<class T>
T & ADS::RingDeque< T >::back ( void )
nodiscard

the item at the back

Precondition
the deque is not empty

◆ back() [2/2]

template<class T>
const T & ADS::RingDeque< T >::back ( void ) const
nodiscard

the item at the back, constant

Precondition
the deque is not empty

◆ begin() [1/2]

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

the iterator on the first item

◆ begin() [2/2]

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

the constant iterator on the first item

◆ capacity()

template<class T>
std::size_t ADS::RingDeque< T >::capacity ( void ) const
inlinenodiscard

the places, 0 or a power of 2

◆ cbegin()

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

the constant iterator on the first item

◆ cend()

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

the constant iterator past the last item

◆ clear()

template<class T>
void ADS::RingDeque< T >::clear ( void )

removes all the items, the places kept

◆ emplace_back()

template<class T>
template<class... Args>
T & ADS::RingDeque< T >::emplace_back ( Args &&... 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

◆ emplace_front()

template<class T>
template<class... Args>
T & ADS::RingDeque< T >::emplace_front ( Args &&... args)

the item built from args at the front, \( O( 1 ) \) amortized

In pseudocode (as emplace_back()), the front moving back by one place:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{emplace\_front}( \mathit{args} ) \cr 2 & \quad \textbf{if}\ t - h = m\ \textbf{then}\ \text{double } A \text{ as } \mathrm{emplace\_back} \text{ does}\quad \triangleright \ A \text{ is full} \cr 3 & \quad h \gets h - 1;\ \text{build in } A[ h \bmod m ] \text{ the item of } \mathit{args} \cr 4 & \quad \textbf{return}\ A[ h \bmod m ] \end{array}} \]

Postcondition
one item more, the new one at the front

◆ empty()

template<class T>
bool ADS::RingDeque< T >::empty ( void ) const
inlinenodiscard

true if there is no item, i.e., the two counters are equal

◆ end() [1/2]

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

the iterator past the last item

◆ end() [2/2]

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

the constant iterator past the last item

◆ erase()

template<class T>
void ADS::RingDeque< T >::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)

In pseudocode (Morin, section 2.4):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{erase}( i ) \cr 2 & \quad \textbf{if}\ i < \lfloor n / 2 \rfloor\ \textbf{then} \cr 3 & \qquad \textbf{for}\ k \gets i\ \textbf{downto}\ 1\ \textbf{do} \ D[ k ] \gets D[ k - 1 ] \cr 4 & \qquad \mathrm{popFront}() \cr 5 & \quad \textbf{else} \cr 6 & \qquad \textbf{for}\ k \gets i\ \textbf{to}\ n - 2\ \textbf{do} \ D[ k ] \gets D[ k + 1 ] \cr 7 & \qquad \mathrm{popBack}() \end{array}} \]

Precondition
\( i < n \)
Postcondition
one item less

◆ front() [1/2]

template<class T>
T & ADS::RingDeque< T >::front ( void )
nodiscard

the item at the front

Precondition
the deque is not empty

◆ front() [2/2]

template<class T>
const T & ADS::RingDeque< T >::front ( void ) const
nodiscard

the item at the front, constant

Precondition
the deque is not empty

◆ insert()

template<class T>
void ADS::RingDeque< T >::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)

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 \)

◆ operator=()

template<class T>
RingDeque & ADS::RingDeque< T >::operator= ( RingDeque< T > o)
noexcept

the items of o, by copy and swap

◆ operator==()

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

the same items in the same order

◆ operator[]() [1/2]

template<class T>
T & ADS::RingDeque< T >::operator[] ( std::size_t i)
nodiscard

the item of rank \( i \) from the front, at the place \( ( h + i ) \mathbin{\&} ( m - 1 ) \)

Precondition
\( i < n \)

◆ operator[]() [2/2]

template<class T>
const T & ADS::RingDeque< T >::operator[] ( std::size_t i) const
nodiscard

the item of rank \( i \) from the front, constant

Precondition
\( i < n \)

◆ pop_back()

template<class T>
void ADS::RingDeque< T >::pop_back ( void )

removes the item at the back, moving \( t \) back

Precondition
the deque is not empty
Postcondition
one item less

◆ pop_front()

template<class T>
void ADS::RingDeque< T >::pop_front ( void )

removes the item at the front, advancing \( h \)

Precondition
the deque is not empty
Postcondition
one item less

◆ push_back() [1/2]

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

\( v \) at the back, emplace_back( v )

◆ push_back() [2/2]

template<class T>
void ADS::RingDeque< T >::push_back ( T && v)
inline

\( v \) at the back, moved

◆ push_front() [1/2]

template<class T>
void ADS::RingDeque< T >::push_front ( const T & v)
inline

\( v \) at the front, emplace_front( v )

◆ push_front() [2/2]

template<class T>
void ADS::RingDeque< T >::push_front ( T && v)
inline

\( v \) at the front, moved

◆ rbegin() [1/2]

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

the iterator on the last item, backwards

◆ rbegin() [2/2]

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

the constant iterator on the last item, backwards

◆ rend() [1/2]

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

the iterator before the first item, backwards

◆ rend() [2/2]

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

the constant iterator before the first item, backwards

◆ reserve()

template<class T>
void ADS::RingDeque< T >::reserve ( std::size_t n)

at least \( n \) places

◆ rotate()

template<class T>
void ADS::RingDeque< T >::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)

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}} \]

◆ shrink_to_fit()

template<class T>
void ADS::RingDeque< T >::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

◆ size()

template<class T>
std::size_t ADS::RingDeque< T >::size ( void ) const
inlinenodiscard

the number of items, the difference of the two counters

◆ swap()

template<class T>
void ADS::RingDeque< T >::swap ( RingDeque< T > & o)
noexcept

exchanges the vector with that of o


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