ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
TreapList.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File TreapList.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
47/*--------------------------------------------------------------------------*/
48/*----------------------------- DEFINITIONS --------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#ifndef __ADS_TreapList
52 #define __ADS_TreapList
53/* self-identification: #endif constant */
54
55/*--------------------------------------------------------------------------*/
56/*------------------------------ INCLUDES ----------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#include "ADS/Contract.h"
60#include "ADS/Iterators.h"
62
63#include <cstddef>
64#include <cstdint>
65#include <memory>
66#include <utility>
67
68/*--------------------------------------------------------------------------*/
69/*--------------------------- NAMESPACE ------------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72namespace ADS
73{
74
75/*--------------------------------------------------------------------------*/
76/*---------------------------- CLASS TreapList -----------------------------*/
77/*--------------------------------------------------------------------------*/
78
81template< class T >
83{
84
85/*--------------------------------------------------------------------------*/
86 public:
87 using Item = T;
88
91 explicit TreapList( std::uint64_t seed = detail::instanceSeed() )
92 : f_g( seed ) { }
93
95 [[nodiscard]] std::size_t size( void ) const { return( sz( f_root ) ); }
96
98 [[nodiscard]] bool empty( void ) const { return( ! f_root ); }
99
116 [[nodiscard]] const T & get( std::size_t i ) const;
117
121 void set( std::size_t i , const T & v );
122
143 void add( std::size_t i , const T & v );
144
147 [[nodiscard]] const T & operator[]( std::size_t i ) const {
148 return( get( i ) );
149 }
150
154 [[nodiscard]] T & operator[]( std::size_t i ) {
155 ADS_PRE( i < size() );
156 return( nodeAt( i )->item );
157 }
158
162 void insert( std::size_t i , const T & v ) { add( i , v ); }
163
167 void erase( std::size_t i ) { ( void )remove( i ); }
168
170 void push_back( const T & v ) { add( size() , v ); }
171
176 T remove( std::size_t i );
177
197 [[nodiscard]] TreapList truncate( std::size_t i );
198
215 void absorb( TreapList & s );
216
217/*--------------------------------------------------------------------------*/
218 private:
220 struct Node
221 {
222 T item;
223 std::uint64_t priority;
224 std::size_t s = 1;
225 std::unique_ptr< Node > left ,
226 right;
227 };
228
229 public:
230/*------------------------- THE STANDARD INTERFACE -------------------------*/
231
233 struct Shape
234 {
236 static auto root( const auto & o ) { return( o.f_root.get() ); }
238 static auto left( const auto * n ) { return( n->left.get() ); }
240 static auto right( const auto * n ) { return( n->right.get() ); }
242 static const T & read( const auto * n ) { return( n->item ); }
243 };
244
245 using value_type = T;
246 using size_type = std::size_t;
247 using difference_type = std::ptrdiff_t;
248 using reference = const T &;
249 using const_reference = const T &;
253
255 [[nodiscard]] const_iterator begin( void ) const {
256 return( const_iterator( this , true ) );
257 }
258
259 [[nodiscard]] const_iterator end( void ) const {
260 return( const_iterator( this , false ) );
261 }
262
263 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
265 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
266
267 private:
268 using Ptr = std::unique_ptr< Node >;
269
270 static std::size_t sz( const Ptr & t ) { return( t ? t->s : 0 ); }
271 static void update( Ptr & t ) { t->s = 1 + sz( t->left ) + sz( t->right ); }
272 static void rotateRight( Ptr & t );
273 static void rotateLeft( Ptr & t );
274 Node * nodeAt( std::size_t i ) const;
275 void add( Ptr & t , std::size_t i , const T & v );
276 static T remove( Ptr & t , std::size_t i );
277 static std::pair< Ptr , Ptr > split( Ptr t , std::size_t i );
278 static Ptr join( Ptr a , Ptr b );
279 static bool valid( const Ptr & t );
280
281 Ptr f_root;
282 SplitMix64 f_g;
283
284 }; // end( class TreapList )
285
286/*--------------------------------------------------------------------------*/
287
288} // end( namespace ADS )
289
290/*--------------------------------------------------------------------------*/
291/*-------------------------- IMPLEMENTATION --------------------------------*/
292/*--------------------------------------------------------------------------*/
293
294#include "ADS/sequence/impl/TreapList.tpp"
295
296/*--------------------------------------------------------------------------*/
297
298#endif /* TreapList.h included */
299
300/*--------------------------------------------------------------------------*/
301/*-------------------------- End File TreapList.h --------------------------*/
302/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
Hashing with random tables, beyond the book:
a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order,...
Definition Iterators.h:867
void absorb(TreapList &s)
the items of appended, in order, and left empty, by the join of the two treaps, in expected time
bool empty(void) const
true if there is no item
Definition TreapList.h:98
T remove(std::size_t i)
the item of index removed and returned, in expected time, the node rotated down as in Treap::remove...
const_iterator end(void) const
the iterator past the last item
Definition TreapList.h:259
std::size_t size(void) const
the number of items
Definition TreapList.h:95
T value_type
the items
Definition TreapList.h:245
TreapList truncate(std::size_t i)
the items of index to taken out and returned as a sequence, by the split of the treap at ,...
const_iterator cbegin(void) const
as begin()
Definition TreapList.h:263
void insert(std::size_t i, const T &v)
inserted at , add( i , v ) (the name of IndexedList)
Definition TreapList.h:162
void push_back(const T &v)
appended, add( size() , v )
Definition TreapList.h:170
const T & const_reference
a constant reference
Definition TreapList.h:249
T & operator[](std::size_t i)
the item of index , to be read or written, by the search of get()
Definition TreapList.h:154
const T & get(std::size_t i) const
the item of index , in expected time
const_iterator begin(void) const
the iterator on the first item
Definition TreapList.h:255
T Item
the type of the items
Definition TreapList.h:87
const_iterator iterator
the constant iterator
Definition TreapList.h:252
PathIterator< TreapList, Shape > const_iterator
the constant iterator in the order of the sequence
Definition TreapList.h:251
const_iterator cend(void) const
as end()
Definition TreapList.h:265
std::size_t size_type
the type of the sizes
Definition TreapList.h:246
const T & reference
a constant reference
Definition TreapList.h:248
void add(std::size_t i, const T &v)
inserted at the index , in expected time
std::ptrdiff_t difference_type
the type of the distances
Definition TreapList.h:247
TreapList(std::uint64_t seed=detail::instanceSeed())
the empty sequence, its priorities drawn from the seed, by default one of its own [see detail::instan...
Definition TreapList.h:91
void set(std::size_t i, const T &v)
the item of index replaced by , by the descent of get()
const T & operator[](std::size_t i) const
the item of index , as get( i ) (the name of IndexedList)
Definition TreapList.h:147
void erase(std::size_t i)
the item of index removed, remove( i ) (the name of IndexedList)
Definition TreapList.h:167
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96
the shape of the tree, for PathIterator
Definition TreapList.h:234
static auto left(const auto *n)
the left child of the node n
Definition TreapList.h:238
static auto right(const auto *n)
the right child of the node n
Definition TreapList.h:240
static auto root(const auto &o)
the root of the tree o
Definition TreapList.h:236
static const T & read(const auto *n)
the item of the node n
Definition TreapList.h:242