28 # ifndef TPL_RANDOM_QUEUE_H 29 # define TPL_RANDOM_QUEUE_H 31 # include <gsl/gsl_rng.h> 34 # include <tpl_dynArray.H> 56 public StlAlephIterator<Random_Set<T>>
61 gsl_rng * r =
nullptr;
73 void set_seed(
unsigned long seed) noexcept { gsl_rng_set(r, seed); }
80 r = gsl_rng_alloc (gsl_rng_mt19937);
82 throw std::bad_alloc();
84 gsl_rng_set(r, time(
nullptr) % gsl_rng_max(r));
133 for (It it = b; it != e; ++it)
140 for (
const auto & item : l)
171 void put(T && item) { array.
append(std::forward<T>(item)); }
185 auto pos = gsl_rng_uniform_int(r, array.
size());
186 std::swap(array(pos), array(array.
size() - 1));
201 auto pos = gsl_rng_uniform_int(r, array.
size());
202 std::swap(array(pos), array(array.
size() - 1));
208 if (array.
size() == 0)
209 throw std::underflow_error(
"Random set is empty");
211 const size_t pos = gsl_rng_uniform_int(r, array.
size());
212 T ret_val = array.
access(pos);
239 template <
class Operation>
241 noexcept(noexcept(array.
traverse(operation)))
247 template <
class Operation>
248 bool traverse(Operation & operation)
const noexcept(noexcept(operation))
254 template <
class Operation>
255 bool traverse(Operation && operation = Operation()) const
256 noexcept(noexcept(operation))
262 template <
class Operation>
263 bool traverse(Operation && operation = Operation())
264 noexcept(noexcept(operation))
279 template <
typename T,
template <
typename>
class C>
283 c.
for_each([&q] (
const T & item) { q.
put(&const_cast<T&>(item)); });
287 ret.append(*q.
get());
300 template <
typename T,
template <
typename>
class C>
304 c.
for_each([&q] (
const T & item) { q.
put(&const_cast<T&>(item)); });
315 # endif // TPL_RANDOM_QUEUE_H C< T * > shuffle_ptr(const C< T > &c)
Definition: tpl_random_queue.H:301
Definition: htlist.H:1290
void reserve(const size_t l, const size_t r)
Definition: tpl_dynArray.H:998
void put(const T &item)
Definition: tpl_random_queue.H:159
size_t size() const
Return the number of items inthe queue.
Definition: tpl_random_queue.H:76
void append(const T &item)
Definition: tpl_random_queue.H:182
void cut(const size_t new_dim=0)
Definition: tpl_dynArray.H:1104
Random_Set(Random_Set &&s)
Move constructor.
Definition: tpl_random_queue.H:101
auto shuffle(const C< T > &c)
Definition: tpl_random_queue.H:280
void append(T &&item)
Definition: tpl_random_queue.H:198
gsl_rng * get_rng() const
The type of data contained in the queue.
Definition: tpl_random_queue.H:70
Random_Set(It b, It e)
Definition: tpl_random_queue.H:131
T & access(const size_t i) const noexcept
Definition: tpl_dynArray.H:874
void for_each(Operation &operation) noexcept(noexcept(operation))
Definition: htlist.H:589
Random_Set(const DynList< T > &l)
Initialize the random queue with the elements of l
Definition: tpl_random_queue.H:124
bool traverse(Operation &operation) const noexcept(noexcept(operation))
Definition: tpl_random_queue.H:248
void swap(DynArray< T > &array) noexcept
Definition: tpl_dynArray.H:819
Random_Set(const Random_Set &s)
Copy constrcutor.
Definition: tpl_random_queue.H:95
bool traverse(Operation &operation) noexcept(noexcept(array.traverse(operation)))
Definition: tpl_random_queue.H:240
bool traverse(Operation &operation) const noexcept(noexcept(operation))
Definition: tpl_dynArray.H:1379
Definition: tpl_random_queue.H:51
void put(T &&item)
Definition: tpl_random_queue.H:171
void set_seed(unsigned long seed) noexcept
Set the seed of ramdom number generator.
Definition: tpl_random_queue.H:73
T get()
Extract randomly an item.
Definition: tpl_random_queue.H:206
T & append()
Definition: tpl_dynArray.H:1149
Random_Set(std::initializer_list< T > l)
Initialize the random queue with the items of l
Definition: tpl_random_queue.H:138
size_t size() const noexcept
Definition: tpl_dynArray.H:641
Definition: tpl_dynArray.H:159
void swap(Random_Set &s)
Swap in constant time this with s
Definition: tpl_random_queue.H:88
bool traverse(Operation &&operation=Operation()) noexcept(noexcept(operation))
Definition: tpl_random_queue.H:263
Definition: tpl_dynArray.H:1258
bool is_empty() const
Return true if the queue is empty.
Definition: tpl_random_queue.H:219
T Item_Type
The type of set.
Definition: tpl_random_queue.H:67
Definition: htlist.H:1323
bool traverse(Operation &&operation=Operation()) const noexcept(noexcept(operation))
Definition: tpl_random_queue.H:255