Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_random_queue.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
43# ifndef TPL_RANDOM_QUEUE_H
44# define TPL_RANDOM_QUEUE_H
45
46# include <ah-random.H>
47# include <htlist.H>
48# include <ah-dry.H>
49# include <tpl_dynArray.H>
50# include <utility>
51#include <ah-iterator.H>
52 # include <ah-errors.H>
53
54namespace Aleph {
55
79 template <class T>
81 : public LocateFunctions<Random_Set<T>, T>,
82 public FunctionalMethods<Random_Set<T>, T>,
83 public GenericKeys<Random_Set<T>, T>,
84 public StlAlephIterator<Random_Set<T>>
85{
86private:
87
89 mutable Random_Source r; // drawn from, and seeded, by const members
90
91public:
92
94
95 using Item_Type = T;
96
98 [[nodiscard]] Random_Source & random_source() noexcept { return r; }
99
102 [[nodiscard]] const Random_Source & random_source() const noexcept { return r; }
103
116 [[deprecated("it makes a Mersenne Twister in a set that has none: "
117 "use random_source(); it goes in 8.0.0")]]
118 [[nodiscard]] Gsl_Compatible_Rng & rng() const { return r.mersenne_twister(); }
119
120# if ALEPH_WITH_GSL
129 [[deprecated("the generator is no longer of GSL: use random_source(); it goes in 8.0.0")]]
130 [[nodiscard]] gsl_rng * get_rng() const { return r.gsl(); }
131# endif
132
142 void set_seed(unsigned long seed) const
143 {
145 }
146
148 [[nodiscard]] size_t size() const { return array.size(); }
149
153 : array(0)
154 {
155 // empty
156 }
157
167 void swap(Random_Set & s) noexcept(noexcept(array.swap(s.array)))
168 {
169 array.swap(s.array);
170 r.swap(s.r);
171 }
172
175 {
176 s.for_each([this] (const auto & item) { this->append(item); });
177 }
178
194 noexcept(std::is_nothrow_move_constructible_v<DynArray<T>>)
195 : array(std::move(s.array)), r(std::move(s.r))
196 {
197 // empty
198 }
199
201 {
202 if (this == &s)
203 return *this;
204
205 array.cut(0); // Clear the array first
206 s.for_each([this] (const auto & item) { this->append(item); });
207
208 return *this;
209 }
210
221 Random_Set & operator = (Random_Set && s) noexcept(noexcept(swap(s)))
222 {
223 swap(s);
224 return *this;
225 }
226
229 {
230 l.for_each([this] (const T & item) { append(item); });
231 }
232
235 template <class It> Random_Set(It b, It e) : Random_Set()
236 {
237 for (It it = b; it != e; ++it)
238 append(*it);
239 }
240
242 Random_Set(std::initializer_list<T> l) : Random_Set()
243 {
244 for (const auto & item : l)
245 append(item);
246 }
247
257 void put(const T & item)
258 {
259 static_assert(std::is_copy_constructible_v<T>,
260 "Random_Set::put requires copyable T");
261 array.append(item);
262 }
263
264
274 void put(T && item)
275 {
276 static_assert(std::is_move_constructible_v<T>,
277 "Random_Set::put requires moveable T");
278 array.append(std::forward<T>(item));
279 }
280
292 T & append(const T & item)
293 {
294 put(item);
295 const size_t last_index = array.size() - 1;
296 const size_t pos = r.uniform_int_ne(array.size()); // at random
297 if (pos != last_index)
298 std::swap(array(pos), array(last_index));
299 return array(pos);
300 }
301
308 T & append(T && item)
309 {
310 put(std::forward<T>(item));
311 const size_t last_index = array.size() - 1;
312 const size_t pos = r.uniform_int_ne(array.size()); // at random
313 if (pos != last_index)
314 std::swap(array(pos), array(last_index));
315 return array(pos);
316 }
317
320 {
321 ah_underflow_error_if(array.size() == 0) << "Random set is empty";
322
323 const size_t pos = r.uniform_int_ne(array.size()); // at random
324 T ret_val = array.access(pos);
325 array.access(pos) = array.access(array.size() - 1);
326 array.cut(array.size() - 1);
327 return ret_val;
328 }
329
330 // pick() is like get() but without removing the item
331 T pick() const
332 {
333 ah_underflow_error_if(array.size() == 0) << "Random set is empty";
334
335 const size_t pos = r.uniform_int_ne(array.size()); // at random
336 return array.access(pos);
337 }
338
340 bool is_empty() const { return array.size() == 0; }
341
349 struct Iterator : public DynArray<T>::Iterator
350 {
351 using Base = typename DynArray<T>::Iterator;
352
354 Iterator() noexcept = default;
355
359 Iterator(Random_Set & q) noexcept : Base(q.array) {}
360
362 Iterator(const Random_Set &) = delete;
363 };
364
367
373 template <class Operation>
375 bool traverse(Operation & operation)
376 noexcept(noexcept(array.traverse(operation)))
377 {
378 return array.traverse(operation);
379 }
380
382 template <class Operation>
384 bool traverse(Operation & operation) const
385 noexcept(noexcept(array.traverse(operation)))
386 {
387 return array.traverse(operation);
388 }
389
391 template <class Operation>
393 bool traverse(Operation && operation = Operation()) const
394 noexcept(noexcept(array.traverse(operation)))
395 {
396 return traverse(operation);
397 }
398
400 template <class Operation>
402 bool traverse(Operation && operation = Operation())
403 noexcept(noexcept(array.traverse(operation)))
404 {
405 return traverse(operation);
406 }
407};
408
409
418 template <typename T, template <typename> class C>
419auto shuffle(const C<T> & c)
420{
422 c.for_each([&q] (const T & item) { q.put(&const_cast<T&>(item)); });
423
424 C<T> ret;
425 while (not q.is_empty())
426 ret.append(*q.get());
427
428 return ret;
429}
430
431
439 template <typename T, template <typename> class C>
440C<T*> shuffle_ptr(const C<T> & c)
441{
443 c.for_each([&q] (const T & item) { q.put(&const_cast<T&>(item)); });
444
445 C<T*> ret;
446 while (not q.is_empty())
447 ret.append(q.get());
448
449 return ret;
450}
451
452} // end namespace Aleph
453
454# endif // TPL_RANDOM_QUEUE_H
455
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
STL-compatible iterator adapters for Aleph containers.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Read-only view of an Aleph iterator: the iterator of a constant container.
Iterator on the items of array.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
void put(const T &item)
Insert an item by copy.
void set_seed(unsigned long seed) const
Seeds the Mersenne Twister of the set, as gsl_rng_set() did: the positions are from then on those of ...
Random_Set(It b, It e)
Initialize the random queue with the items between b and e where these parameters are stl iterators.
Gsl_Compatible_Rng & rng() const
The Mersenne Twister of the set, which a set that was given no seed makes here, and draws from it fro...
Random_Set(Random_Set &&s) noexcept(std::is_nothrow_move_constructible_v< DynArray< T > >)
Take the items and the generator of another set, which is left empty, and draws from then on from a S...
Random_Source & random_source() noexcept
The type of data contained in the queue.
bool traverse(Operation &&operation=Operation()) const noexcept(noexcept(array.traverse(operation)))
See traverse(Operation & operation).
Random_Set & operator=(const Random_Set &s)
Random_Set()
A random queue that draws its positions from a SplitMix64 with a seed of its own.
bool is_empty() const
Return true if the queue is empty.
T Item_Type
The type of set.
T get()
Extract randomly an item.
T & append(T &&item)
Insert randomly an item by moving.
void put(T &&item)
Insert an item by moving.
DynArray< T > array
bool traverse(Operation &&operation=Operation()) noexcept(noexcept(array.traverse(operation)))
See traverse(Operation & operation).
const Random_Source & random_source() const noexcept
The source of random numbers, to look at it: its engine, or whether it is equal to another one.
Random_Set(const Random_Set &s)
Copy constructor.
T & append(const T &item)
Insert randomly an item by copy.
bool traverse(Operation &operation) noexcept(noexcept(array.traverse(operation)))
Conditional traversing of the random queue.
Random_Set(const DynList< T > &l)
Initialize the random queue with the elements of l
Random_Set(std::initializer_list< T > l)
Initialize the random queue with the items of l
size_t size() const
Return the number of items in the queue.
bool traverse(Operation &operation) const noexcept(noexcept(array.traverse(operation)))
See traverse(Operation & operation).
void swap(Random_Set &s) noexcept(noexcept(array.swap(s.array)))
Swap in constant time this with s: their items and their generators.
The source of random numbers of a randomized structure, which draws from one of two generators.
Definition ah-random.H:883
@ Mersenne_Twister
A Gsl_Compatible_Rng, in dynamic memory.
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
Definition ah-random.H:1087
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
Definition ah-random.H:1040
void swap(Random_Source &o) noexcept
Swaps the generators, with their states.
Definition ah-random.H:1021
std::uint64_t uniform_int_ne(const std::uint64_t n) noexcept
An integer in [0, n), each with the same probability, without checking n.
Definition ah-random.H:1147
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Common sequential searching methods on containers.
Definition ah-dry.H:555
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
f(args...) is a valid call whose result is usable as a condition.
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
C< T * > shuffle_ptr(const C< T > &c)
Return a random sequence of pointers to items of a sequence.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
auto shuffle(const C< T > &c)
Randomly shuffle a sequence.
STL namespace.
Iterator of the items of the queue.
typename DynArray< T >::Iterator Base
Iterator() noexcept=default
Default constructor creates an "end" iterator.
Iterator(const Random_Set &)=delete
A constant set is iterated through Const_Iterator.
Generic list of items stored in a container.
Definition ah-dry.H:2896
ValueArg< size_t > seed
Definition testHash.C:53
Lazy and scalable dynamic array implementation.
DynList< int > l