ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RandomQueue.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File RandomQueue.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------*/
40/*----------------------------- DEFINITIONS --------------------------------*/
41/*--------------------------------------------------------------------------*/
42
43#ifndef __ADS_RandomQueue
44 #define __ADS_RandomQueue
45/* self-identification: #endif constant */
46
47/*--------------------------------------------------------------------------*/
48/*------------------------------ INCLUDES ----------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#include "ADS/Contract.h"
52#include "ADS/Portable.h"
53#include "ADS/Wide.h"
55
56#include <cstddef>
57#include <cstdint>
58#include <utility>
59#include <vector>
60
61/*--------------------------------------------------------------------------*/
62/*--------------------------- NAMESPACE ------------------------------------*/
63/*--------------------------------------------------------------------------*/
64
65namespace ADS
66{
67
68/*--------------------------------------------------------------------------*/
69/*--------------------------- CLASS RandomQueue ----------------------------*/
70/*--------------------------------------------------------------------------*/
71
73template< class T >
75{
76
77/*--------------------------------------------------------------------------*/
78 public:
79 using Item = T;
80
82 explicit RandomQueue( std::uint64_t seed = 1 ) : f_g( seed ) { }
83
85 [[nodiscard]] bool empty( void ) const { return( f_A.empty() ); }
86
88 [[nodiscard]] std::size_t size( void ) const { return( f_A.size() ); }
89
91 void push( const T & x ) { f_A.push_back( x ); }
92
94 void push( T && x ) { f_A.push_back( std::move( x ) ); }
95
110 T pop( void );
111
114 [[nodiscard]] const T & sample( void );
115
132 [[nodiscard]] std::size_t uniform( std::size_t n );
133
135 void clear( void ) { f_A.clear(); }
136
138 [[nodiscard]] const std::vector< T > & items( void ) const { return( f_A ); }
139
140/*--------------------------------------------------------------------------*/
141 private:
142 std::vector< T > f_A;
143 SplitMix64 f_g;
144
145 }; // end( class RandomQueue )
146
147/*--------------------------------------------------------------------------*/
148
149} // end( namespace ADS )
150
151/*--------------------------------------------------------------------------*/
152/*-------------------------- IMPLEMENTATION --------------------------------*/
153/*--------------------------------------------------------------------------*/
154
155#include "ADS/queue/impl/RandomQueue.tpp"
156
157/*--------------------------------------------------------------------------*/
158
159#endif /* RandomQueue.h included */
160
161/*--------------------------------------------------------------------------*/
162/*------------------------- End File RandomQueue.h -------------------------*/
163/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
Hashing with random tables, beyond the book:
The arithmetic of two words that C++20 does not spell, in one form for every compiler: the product of...
T Item
the type of the items
Definition RandomQueue.h:79
std::size_t uniform(std::size_t n)
a place uniform in , by the multiplication of Lemire
const std::vector< T > & items(void) const
the items, in no order
Definition RandomQueue.h:138
T pop(void)
takes out an item chosen uniformly at random and returns it, (Morin's remove())
std::size_t size(void) const
the number of items
Definition RandomQueue.h:88
bool empty(void) const
true if there is no item
Definition RandomQueue.h:85
void push(T &&x)
puts in, moved
Definition RandomQueue.h:94
RandomQueue(std::uint64_t seed=1)
the empty queue, its draws from the seed
Definition RandomQueue.h:82
void push(const T &x)
puts in, amortized (Morin's add( x ))
Definition RandomQueue.h:91
void clear(void)
removes all the items
Definition RandomQueue.h:135
const T & sample(void)
an item chosen uniformly at random, left in
the generator SplitMix64, a UniformRandomBitGenerator
Definition RandomHashing.h:122
Definition ADT.h:96