-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwmath_util.cpp
More file actions
60 lines (60 loc) · 1.45 KB
/
wmath_util.cpp
File metadata and controls
60 lines (60 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef WMATH_UTIL_H
#define WMATH_UTIL_H
#include "wmath_forward.hpp"
#include "wmath_bits.hpp"
namespace wmath{
// random_order_iterator iterates range in random order
template<class iterator,class generator>
class random_order_iterator{
private:
const iterator begin;
const iterator end;
const size_t n;
const size_t m;
size_t i = 0;
size_t a;
size_t b;
public:
random_order_iterator(
const iterator begin,
const iterator end,
generator& rng
): begin(begin),end(end),n(end-begin),m(roundup_2(n)) {
uniform_int_distribution<size_t> distr(m);
a = 2*distr(rng)+1;
b = distr(rng);
}
random_order_iterator(
const iterator begin,
const iterator end,
const size_t& n,
const size_t& m,
const size_t& i,
const size_t& a,
const size_t& b
): begin(begin),end(end),n(n),m(m),i(i),a(a),b(b) {
uniform_int_distribution<size_t> distr(m);
a = 2*distr(rng)+1;
b = distr(rng);
}
}
random_order_iterator& operator++(){ // prefix
++i;
return *this;
}
random_order_iterator operator++(int){ // postfix
random_order_iterator pre = *this;
++i;
return pre;
}
random_order_iterator& operator--(){ // prefix
--i;
return *this;
}
random_order_iterator operator--(int){ // postfix
random_order_iterator pre = *this;
--i;
return pre;
}
}
#endif // WMATH_UTIL_H