|
ADS
Algorithms and Data Structures
|
The interfaces of chapter 1 of Morin's book for the sequences, beyond the book, as concepts of C++20. More...
Files | |
| file | ADS/sequence/IndexedList.h |
| The interfaces of chapter 1 of Morin's book for the sequences, beyond the book, as concepts of C++20 [see ADT.h for those of the book]: | |
Concepts | |
| concept | ADS::IndexedList |
| the LIST of Morin: a sequence read, written, extended and shortened by rank | |
| concept | ADS::IndexedDeque |
| the DEQUE of Morin: a sequence changed and read at both ends | |
Functions | |
| template<class T> | |
| void | ADS::rotateArray (std::span< T > a, std::size_t r) |
| the item \( a[ i ] \) moved to \( a[ ( i + r ) \bmod n ] \), for every \( i \), by three reversals: \( n \) swaps at most and \( O( 1 ) \) space | |
| template<IndexedList L> | |
| void | ADS::rotate (L &l, std::size_t r) |
| the item of rank \( i \) of \( l \) moved to the rank \( ( i + r ) \bmod n \): the rotate() of the list if it has one, the cycles of the permutation otherwise, \( n + \gcd( n , r ) \) moves | |
The interfaces of chapter 1 of Morin's book for the sequences, beyond the book, as concepts of C++20.
The definitions, the invariants and the costs of the area are in Theory of sequences and lists.
| void ADS::rotate | ( | L & | l, |
| std::size_t | r ) |
the item of rank \( i \) of \( l \) moved to the rank \( ( i + r ) \bmod n \): the rotate() of the list if it has one, the cycles of the permutation otherwise, \( n + \gcd( n , r ) \) moves
In pseudocode (Morin, exercise 2.5, by Bentley's cycles), \( r \) reduced modulo \( n \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{rotate}( l , r ) \cr 2 & \quad \textbf{for}\ s \gets 0\ \textbf{to}\ \gcd( n , r ) - 1 \ \textbf{do} \cr 3 & \qquad x \gets l[ s ];\ j \gets s \cr 4 & \qquad \textbf{loop} \cr 5 & \qquad\quad p \gets ( j - r ) \bmod n \cr 6 & \qquad\quad \textbf{if}\ p = s\ \textbf{then break} \cr 7 & \qquad\quad l[ j ] \gets l[ p ];\ j \gets p \cr 8 & \qquad l[ j ] \gets x \end{array}} \]
| void ADS::rotateArray | ( | std::span< T > | a, |
| std::size_t | r ) |
the item \( a[ i ] \) moved to \( a[ ( i + r ) \bmod n ] \), for every \( i \), by three reversals: \( n \) swaps at most and \( O( 1 ) \) space
In pseudocode (Morin, exercise 2.4, by Bentley's reversals), with \( r \) reduced modulo \( n \), the last \( r \) items becoming the first ones:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{rotateArray}( a , r ) \cr 2 & \quad \mathrm{reverse}( a[ 0 .. n - 1 ] ) \cr 3 & \quad \mathrm{reverse}( a[ 0 .. r - 1 ] );\ \mathrm{reverse}( a[ r .. n - 1 ] ) \end{array}} \]