-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib_math_fixedperm.d
More file actions
296 lines (225 loc) · 6.52 KB
/
Copy pathlib_math_fixedperm.d
File metadata and controls
296 lines (225 loc) · 6.52 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
module d_glat.lib_math_fixedperm;
import std.conv : to;
/*
Memory-less, fixed, random-looking permutation of any number of
indices.
Implementation: builds upon a mathematical result for memory-less
permutations of power-of-two arrays of elements:
https://glat.info/cipo/
Example of usage:
-- Context: running many simulations across combinations of
parameter values, distributed across `n_processes`.
-- Issue: say the structure of the simulation and parameters leads
to having 1 iteration running much slower, every 4 iterations. If
`n_processes%4==0`, then some of the processes will finish much
later than the others.
-- Solution 1: use a big-enough, prime number of processes. A bit
constraining.
-- Solution 2: shuffle the order of the simulations using
FixedPerm(n): `foreach (i; FixedPerm(n))`
By Guillaume Lathoud, 2022
glat@glat.info
The Boost license applies to this file, as described in ./LICENSE
*/
alias FixedPerm = FixedPermT!size_t;
alias FixedPermInfinite = FixedPermT!(size_t, true);
alias FixedPermBigFirst = FixedPermT!(size_t, false, true);
alias FixedPermBigFirstInfinite = FixedPermT!(size_t, true, true);
struct FixedPermT( T, bool infinite = false, bool big_first = false )
// Typical behaviour (big_first=false): the very first few numbers are
// small. To start with big numbers right away, try big_first=true.
{
immutable T n;
this(T2)( in T2 n_0 )
{
n = cast(T)( n_0 );
immutable prev_pow_2 = (){
T a = 1, b = n;
while (b)
{
a <<= 1;
b >>= 1;
}
return a;
}();
next_pow_2 = (n == prev_pow_2) ? n : (prev_pow_2 << 1);
i_in = 0;
static if (!big_first)
{
step_init = 1;
i_out_init = 0;
}
else
{
step_init = next_pow_2 - 1;
i_out_init = n-1;
}
step = step_init;
i_out = i_out_init;
}
static if (!infinite)
{
size_t length() const pure @safe @nogc { return cast(size_t)( n ); }
}
bool empty() const pure @safe @nogc {
static if (infinite)
return false;
else
return i_in >= n;
}
T front() const pure @safe @nogc { return i_out; }
void popFront() pure @safe @nogc {
do {
static if (!big_first)
{
i_out = (i_out + step) % next_pow_2;
step = (1 + step) % next_pow_2;
}
else
{
i_out = (i_out - step + next_pow_2) % next_pow_2;
step = (-1 + step + next_pow_2) % next_pow_2;
}
} while (i_out >= n);
++i_in;
static if (infinite)
{
if (0 == (i_in % n))
{
i_out = i_out_init;
step = step_init;
}
}
}
T getAtForward( in T i ) pure @safe
/*
Convenient to stride. Constraint: always forward, i.e. the value
of the input `i` must never decrease.
Example:
foreach (i; iota( 2, N, 7 ))
{
immutable v = rfp.getAtForward( i );
...
}
*/
{
static if (!infinite)
{
if (i < i_in)
assert( false, "lib_math_fixedperm: getAtForward( i ) requires i to be in the present or the future, not in the past. i: "~to!string( i )~", i_in: "~to!string( i_in ) );
}
while (i != i_in)
popFront();
return front();
}
private:
immutable T next_pow_2;
immutable T step_init, i_out_init;
T i_in, step, i_out;
}
unittest
{
import std.stdio;
import std.path;
writeln;
writeln( "unittest starts: ", baseName( __FILE__ ) );
immutable verbose = false;
import std.algorithm;
import std.range;
void test_one( in size_t N )
{
if (verbose)
{
writeln;
writeln( "begin test_one: ", N );
}
{
auto rfp = FixedPerm( N );
assert(rfp.empty == (N == 0));
size_t c = 0;
foreach (i; rfp) { ++c; }
assert( c == N );
if (verbose)
{
writeln("loop done, rfp.i_in: ", rfp.i_in);
writeln("loop done, rfp.i_out: ", rfp.i_out);
}
}
static foreach (BIG_FIRST; [false,true])
{
{
if (verbose)
{
writeln;
writeln("N:", N, ", BIG_FIRST: ", BIG_FIRST);
}
if (0 < N)
{
auto rfp = FixedPermT!(size_t, /*infinite:*/true, BIG_FIRST)( N );
assert(!rfp.empty);
const arr = rfp.take( 3*N ).array;
assert(!rfp.empty);
if (verbose)
{
if (N < 6)
{
writeln("(infinite) N ", N);
writeln("(infinite) arr ", arr);
}
else
{
scope const arrN = arr[0..N];
writeln("(infinite) N ", N);
writeln("(infinite) arrN[0..min($,30)] ", arr[0..min($,30)]);
writeln("(infinite) arrN[($>>1)..min($,($>>1)+30)] ", arr[($>>1)..min($,($>>1)+30)]);
writeln("(infinite) arrN[($-min($,30))..$] ", arr[($-min($,30))..$]);
}
}
assert( arr[0..N] == arr[N..(2*N)] );
assert( arr[(2*N)..(3*N)] == arr[N..(2*N)] );
assert( arr[0..N].dup.sort.array == iota(N).array );
}
}
}
{
auto rfp = FixedPerm( N );
const arr = rfp.array;
const arr2 = arr.dup.sort.array;
if (verbose)
{
writeln( "arr: ", arr );
writeln( "arr sorted: ", arr2 );
}
assert( (arr == arr2) == (N < 4) );
assert( arr2 == iota( N ).array );
// Can repeat usage (because: `struct`)
{
auto arr3 = rfp.array;
assert( arr3.dup.sort.array == arr2 );
arr3 ~= [12345];
assert( arr != arr3 );
assert( arr2 != arr3 );
}
}
{
const arr = FixedPerm( N ).array;
if (verbose)
writeln( "stride: arr: ", arr );
auto rfp = FixedPerm( N );
foreach (i; iota( 2, N, 7 ))
{
immutable v = rfp.getAtForward( i );
if (verbose)
writeln("stride: i: ", i, " => v: ", v);
assert( v == arr[ i ] );
}
}
if (verbose)
{
writeln( "end test_one: ", N );
}
}
foreach (N; chain( iota( 50 ), [1235], iota( 4093, 4100 ) ))
test_one( N );
writeln( "unittest passed: ", baseName( __FILE__ ) );
}