ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
IndexedList.h File Reference

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]: More...

#include "ADS/Contract.h"
#include <concepts>
#include <cstddef>
#include <numeric>
#include <span>
#include <utility>
#include <vector>
#include "ADS/sequence/impl/IndexedList.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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

Detailed description

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]:

  • IndexedList, the LIST of section 1.2.2 of Morin, a sequence \( x_0 , \ldots , x_{n-1} \) read and written by rank: size(), operator[] for get( i ) and set( i , x ), insert( i , x ) for add( i , x ) and erase( i ) for remove( i ), the names of the standard library for those of Morin; RingDeque, DualArrayDeque, Treque, ContiguousDeque, RootishArrayStack, TieredVector, CubishArrayStack and SEList are realizations;
  • IndexedDeque, the DEQUE of section 1.2.1 of Morin, a sequence changed at both ends, by push_front(), push_back(), pop_front() and pop_back(), read by front() and back().

Two rotations come with them: rotateArray(), the item \( a[ i ] \) of an array moved to \( a[ ( i + r ) \bmod n ] \) by three reversals (exercise 2.4 of Morin), \( n \) swaps at most and no memory, and rotate(), the same on an IndexedList, which calls the rotate() of the list when it has one (\( O( 1 + \min\{ r , n - r \} ) \) on RingDeque, DualArrayDeque and List, exercises 2.5 and 3.8) and otherwise follows the \( \gcd( n , r ) \) cycles of the permutation by operator[], \( n + \gcd( n , r ) \) moves.

The contracts [see Contract.h]: none but those of the lists; the rotations ensure that the items are where they should be (an audit). The algorithms are defined in IndexedList.tpp.

See also
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, sections 1.2.1 and 1.2.2 and exercises 2.4 and 2.5
J. Bentley, Programming Pearls, 2nd ed., Addison-Wesley, 2000, section 2.3 (the rotation by reversals and by cycles)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa