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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! 乱数生成マクロ。

use super::bitop;

#[macro_export]
macro_rules! rand_gen_builder {
    ( @gen { [ $($cur:tt)* ] } ) => {
        $crate::rand_gen_builder!(@vec @gen {} @rest $($cur)*)
    };
    ( @gen { [ $($cur:tt)* ] where { $($f:ident $(= $a:expr)?),* } } ) => {
        $crate::rand_gen_builder!(@vec @gen {} @rest $($cur)*)
            .options() $(.$f($($a)?))*
    };
    ( @gen { [ $($cur:tt)* ] where { $($f:ident $(= $a:expr)?),*, } } ) => {
        $crate::rand_gen_builder!(@vec @gen {} @rest $($cur)*)
            .options() $(.$f($($a)?))*
    };
    ( @vec @gen { $($x:tt)* } @rest ; $($rest:tt)* ) => {
        $crate::rand_gen_builder!(@vec @gen { $($x)* } @len { $($rest)* })
    };
    ( @vec @gen { $($cur:tt)* } @rest $tt:tt $($rest:tt)* ) => {
        $crate::rand_gen_builder!(@vec @gen { $($cur)* $tt } @rest $($rest)*)
    };
    ( @vec @gen { $($x:tt)* } @len { $($len:tt)* }) => {
        VecMarker::new($crate::rand_gen_builder!(@gen { $($x)* }), $($len)*)
    };

    ( @gen { $($x:tt)* } ) => {
        $crate::rand_gen_builder!(@gen {} @rest $($x)*)
    };
    ( @gen { $($cur:tt)* } @rest where { $($where:tt)* } ) => {
        $crate::rand_gen_builder!(@gen { $($cur)* } @where { $($where)* } @rest)
    };
    ( @gen { $($cur:tt)* } @rest $tt:tt $($rest:tt)* ) => {
        $crate::rand_gen_builder!(@gen { $($cur)* $tt } @rest $($rest)*)
    };
    ( @gen { $($cur:tt)* } @where { $($f:ident $(= $a:expr)?),* } @rest ) => {
        $crate::rand_gen_builder!(@gen { $($cur)* } @rest)
            .options() $(.$f($($a)?))*
    };
    ( @gen { $($cur:tt)* } @where { $($f:ident $(= $a:expr)?),*, } @rest ) => {
        $crate::rand_gen_builder!(@gen { $($cur)* } @rest)
            .options() $(.$f($($a)?))*
    };
    ( @gen { $($x:tt)* } @rest ) => {
        ($($x)*)
    };
}

/// 乱数生成マクロ。
///
/// # Notes
/// 作りかけなのでいろいろ足りない。
///
/// # Examples
/// ```
/// use rand::SeedableRng;
/// use rand_chacha::ChaCha20Rng;
///
/// use nekolib::rand_gen;
/// use nekolib::utils::ascii::*;
/// use nekolib::utils::rand_gen_macro::*;
///
/// rand_gen! {
///     rng: ChaCha20Rng;
///
///     n in 1_usize..=10;
///     a in [1_i64..=100; n];
///     s in AsciiString(16) where {
///         distribution = &[
///             (ASCII_LOWERCASE, 10),
///             (ASCII_UPPERCASE, 10),
///             (ASCII_DIGIT, 6),
///             (charset(b"~!@#$%+?|()^*_-=[]{};:,./"), 5),
///         ],
///     };
/// }
///
/// assert_eq!(a.len(), n);
/// assert!(a.iter().all(|ai| (1..=100).contains(ai)));
/// assert_eq!(s.len(), 16);
/// // Possible value of `s`: `"3e)xIjos2^M/XI1T"`, `"X52dhjDk%i6)p1F9"`
/// ```
///
/// 以下のような出力が得られるため、これを用いて再現することができる。
///
/// ````text
/// To reproduce:
///
/// ```
/// rand_gen! {
///     rng = ChaCha20Rng::from_seed([250, 120, 31, 164, 15, 176, 41, 144, 61, 59, 224, 119, 135, 238, 14, 193, 149, 124, 228, 39, 107, 208, 243, 180, 7, 177, 21, 88, 19, 5, 225, 3]);
///     // ...
/// }
/// ```
/// ````
///
/// ```
/// use rand::SeedableRng;
/// use rand_chacha::ChaCha20Rng;
///
/// use nekolib::rand_gen;
/// use nekolib::utils::ascii::*;
/// use nekolib::utils::rand_gen_macro::*;
///
/// rand_gen! {
///     rng = ChaCha20Rng::from_seed([250, 120, 31, 164, 15, 176, 41, 144, 61, 59, 224, 119, 135, 238, 14, 193, 149, 124, 228, 39, 107, 208, 243, 180, 7, 177, 21, 88, 19, 5, 225, 3]);
///
///     n in 1_usize..=10;
///     a in [1_i64..=100; n];
///     s in AsciiString(16) where {
///         distribution = &[
///             (ASCII_LOWERCASE, 10),
///             (ASCII_UPPERCASE, 10),
///             (ASCII_DIGIT, 6),
///             (charset(b"~!@#$%+?|()^*_-=[]{};:,./"), 5),
///         ],
///     };
///     b in [[[1_u8..100; 3]; 2]; 2];
///     p in Permutation(5);
///     mut x in 1_i64..10;
/// }
/// x += 1;
///
/// assert_eq!(a, [32, 86, 41, 68, 66, 46, 56, 82, 40, 1]);
/// assert_eq!(s, "X52dhjDk%i6)p1F9");
/// assert_eq!(b, [[[75, 20, 23], [63, 21, 58]], [[12, 6, 57], [51, 95, 70]]]);
/// assert_eq!(p, [3, 1, 0, 4, 2]);
/// assert_eq!(x, 5);
/// ```
///
/// `oj` を用いて、hack をするのに使うとよい。
/// 以下の例は、想定解が Rust で標的が Python のプログラムのもの。
///
/// ```sh
/// % oj g/i --hack-expected target/release/x --hack-actual 'python3 x-to-be-hacked.py' target/release/x-gen
/// ```
#[macro_export]
macro_rules! rand_gen {
    ( @seed $seed:ident { mut $a:ident in $($r:tt)* } @rest ) => {
        let mut $a = $seed.generate($crate::rand_gen_builder!(@gen { $($r)* }));
    };
    ( @seed $seed:ident { $a:ident in $($r:tt)* } @rest ) => {
        let $a = $seed.generate($crate::rand_gen_builder!(@gen { $($r)* }));
    };
    ( @seed $seed:ident { $a:ident in $($r:tt)* } @rest ; $($rest:tt)* ) => {
        rand_gen!(@seed $seed { $a in $($r)* } @rest);
        rand_gen!(@seed $seed {} @rest $($rest)*);
    };
    ( @seed $seed:ident { mut $a:ident in $($r:tt)* } @rest ; $($rest:tt)* ) => {
        rand_gen!(@seed $seed { mut $a in $($r)* } @rest);
        rand_gen!(@seed $seed {} @rest $($rest)*);
    };
    ( @seed $seed:ident { $($cur:tt)* } @rest $tt:tt $($rest:tt)* ) => {
        rand_gen!(@seed $seed { $($cur)* $tt } @rest $($rest)* );
    };
    ( @seed $seed:ident {} @rest ) => {};
    ( $seed:ident: $ty:ty; $($rest:tt)* ) => {
        // let mut $seed = <$ty as RandomWordGenerator>::auto_init();
        let mut $seed = <$ty>::from_entropy();
        eprintln!(r#"
To reproduce:

```
rand_gen! {{
    {} = {};
    // ...
}}
```
"#, stringify!($seed), $seed.inspect());
        rand_gen!(@seed $seed {} @rest $($rest)*);
    };
    ( $seed:ident = $s:expr; $($rest:tt)* ) => {
        let mut $seed = $s;
        // $seed.inspect(stringify!($seed));
        rand_gen!(@seed $seed {} @rest $($rest)*);
    };
}

// .options() いらないかも
pub trait GenOptions {
    type OptionType;
    fn options(self) -> Self::OptionType;
}

// ---
// ```
// pub trait RandomGenerator<Input> {
//     type Output;
//     fn generate(&mut self) -> Self::Output;
// }
// ```
// みたいなのを作って、impl RandChaCha とかする?
// Input = RangeInclusive<i64>, Output = i64 とかを想定している。
//
// 今だと impl RandGen for Input して rand_gen の引数に &mut RandChaCha とかの
// を渡すようにしているのか、むー。でもこれが適当な impl trait なのでつらいんだね
//
// なんかできてそう。
// あとは、過去のを消しつつ、options をよしなに解決すればよさそう。

#[derive(Clone, Copy)]
pub struct AsciiString(pub usize);

impl GenOptions for AsciiString {
    type OptionType = Self;
    fn options(self) -> Self { self }
}

#[derive(Clone, Copy)]
pub struct AsciiStringOfCharset(AsciiGen, usize);

#[derive(Clone)]
pub struct AsciiStringOfDistribution(BTreeMap<u32, AsciiGen>, u32, usize);

impl AsciiString {
    pub fn charset(self, cs: u128) -> AsciiStringOfCharset {
        AsciiStringOfCharset(AsciiGen::new(cs), self.0)
    }
    pub fn distribution(self, d: &[(u128, u32)]) -> AsciiStringOfDistribution {
        let mut map = BTreeMap::new();
        let mut acc = 0;
        for &(mask, p) in d.iter().filter(|&&(_, p)| p > 0) {
            acc += p;
            map.insert(acc, AsciiGen::new(mask));
        }
        AsciiStringOfDistribution(map, acc, self.0)
    }
}

impl RandomGenerator<AsciiStringOfCharset> for ChaCha20Rng {
    type Output = String;
    fn generate(&mut self, subject: AsciiStringOfCharset) -> String {
        let AsciiStringOfCharset(gen, len) = subject;
        if len == 0 {
            "".to_owned()
        } else {
            (0..len).map(|_| self.generate(gen)).collect()
        }
    }
}

impl RandomGenerator<AsciiStringOfDistribution> for ChaCha20Rng {
    type Output = String;
    fn generate(&mut self, subject: AsciiStringOfDistribution) -> String {
        let AsciiStringOfDistribution(ref gen_map, acc, len) = subject;
        let large = Uniform::from(0..acc);
        (0..len)
            .map(|_| {
                let c = large.sample(self);
                let small = gen_map.range(c..).next().unwrap().1;
                self.generate(*small)
            })
            .collect()
    }
}

#[derive(Clone, Copy)]
pub struct Ascii;

impl GenOptions for Ascii {
    type OptionType = Self;
    fn options(self) -> Ascii { self }
}

impl Ascii {
    pub fn charset(self, cs: u128) -> AsciiGen { AsciiGen::new(cs) }
}

#[derive(Clone, Copy)]
pub struct AsciiGen(PdepPextMaskU128, u32);

impl AsciiGen {
    pub fn new(mask: u128) -> Self {
        Self(PdepPextMaskU128::new(mask), mask.count_ones())
    }
}

impl RandomGenerator<AsciiGen> for ChaCha20Rng {
    type Output = char;
    fn generate(&mut self, subject: AsciiGen) -> char {
        let AsciiGen(mask, pop) = subject;
        assert_ne!(pop, 0, "empty charset");
        let c = Uniform::from(0..pop).sample(self);
        (1 << c).pdep(mask).trailing_zeros() as u8 as char
    }
}

use std::collections::{BTreeMap, BTreeSet};
use std::ops::{Range, RangeInclusive};

use rand::distributions::{Distribution, Uniform};
use rand_chacha::ChaCha20Rng;

use bitop::{Pdep, PdepPextMaskU128};

pub trait SeedableRngInspect {
    fn inspect(&self) -> String;
}

impl SeedableRngInspect for ChaCha20Rng {
    fn inspect(&self) -> String {
        format!("ChaCha20Rng::from_seed({:?})", self.get_seed())
    }
}

pub trait RandomGenerator<Input> {
    type Output;
    fn generate(&mut self, subject: Input) -> Self::Output;
}

macro_rules! impl_range {
    ( $($t:ty)* ) => { $(
        impl RandomGenerator<RangeInclusive<$t>> for ChaCha20Rng {
            type Output = $t;
            fn generate(&mut self, s: RangeInclusive<$t>) -> $t {
                let between = Uniform::from(s);
                between.sample(self)
            }
        }
        impl RandomGenerator<Range<$t>> for ChaCha20Rng {
            type Output = $t;
            fn generate(&mut self, s: Range<$t>) -> $t {
                let between = Uniform::from(s);
                between.sample(self)
            }
        }
    )* }
}

impl_range! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }

#[derive(Clone)]
pub struct VecMarker<T> {
    inner: T,
    len: usize,
}

impl<T> VecMarker<T> {
    pub fn new(inner: T, len: usize) -> Self { Self { inner, len } }
}

// sorted は Ord、distinct は Eq が必要になるけど、そもそも
// 範囲で取ってランダムに生成とか言っている時点でそれらは仮定していそう。
pub struct VecOptionsMarker<T> {
    inner: T,
    len: usize,
    sorted: bool,
    distinct: bool,
}

impl<T> GenOptions for VecMarker<T> {
    type OptionType = VecOptionsMarker<T>;
    fn options(self) -> VecOptionsMarker<T> {
        let Self { inner, len } = self;
        VecOptionsMarker { inner, len, sorted: false, distinct: false }
    }
}

impl<T> VecOptionsMarker<T> {
    pub fn sorted(mut self) -> Self {
        self.sorted = true;
        self
    }
    pub fn distinct(mut self) -> Self {
        self.distinct = true;
        self
    }
}

// impl<T> U<Vec<T>> for S where S implements U<T> { ... }
// みたいなのってもしかしてできない?
// マクロで定数段だけやればとりあえずいいか。

macro_rules! impl_vec_range {
    ( $($t:ty)* ) => { $(
        impl RandomGenerator<VecMarker<RangeInclusive<$t>>> for ChaCha20Rng {
            type Output = Vec<$t>;
            fn generate(
                &mut self,
                s: VecMarker<RangeInclusive<$t>>
            ) -> Self::Output {
                let VecMarker { inner, len } = s;
                let between = Uniform::from(inner);
                (0..len).map(|_| between.sample(self)).collect()
            }
        }
        impl RandomGenerator<VecMarker<Range<$t>>> for ChaCha20Rng {
            type Output = Vec<$t>;
            fn generate(&mut self, s: VecMarker<Range<$t>>) -> Self::Output {
                let VecMarker { inner, len } = s;
                let between = Uniform::from(inner);
                (0..len).map(|_| between.sample(self)).collect()
            }
        }
    )* }
}

impl_vec_range! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }

macro_rules! impl_vecvec_range {
    ( $( ($marker:ty, $vec:ty), )* ) => { $(
        impl RandomGenerator<$marker> for ChaCha20Rng {
            type Output = $vec;
            fn generate(&mut self, s: $marker) -> Self::Output {
                let VecMarker { inner, len } = s;
                (0..len).map(|_| self.generate(inner.clone())).collect()
            }
        }
    )* };
    ( $( $t:ty )* ) => { $(
        impl_vecvec_range! {
            // 2
            (VecMarker<VecMarker<RangeInclusive<$t>>>, Vec<Vec<$t>>),
            (VecMarker<VecMarker<Range<$t>>>, Vec<Vec<$t>>),

            // 3
            (
                VecMarker<VecMarker<VecMarker<RangeInclusive<$t>>>>,
                Vec<Vec<Vec<$t>>>
            ),
            (VecMarker<VecMarker<VecMarker<Range<$t>>>>, Vec<Vec<Vec<$t>>>),

            // 4
            (
                VecMarker<VecMarker<VecMarker<VecMarker<RangeInclusive<$t>>>>>,
                Vec<Vec<Vec<Vec<$t>>>>
            ),
            (
                VecMarker<VecMarker<VecMarker<VecMarker<Range<$t>>>>>,
                Vec<Vec<Vec<Vec<$t>>>>
            ),
        }
    )* }
}

impl_vecvec_range! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }

impl RandomGenerator<VecMarker<Permutation>> for ChaCha20Rng {
    type Output = Vec<Vec<usize>>;
    fn generate(&mut self, s: VecMarker<Permutation>) -> Self::Output {
        let VecMarker { inner, len } = s;
        (0..len).map(|_| self.generate(inner)).collect()
    }
}

impl RandomGenerator<VecOptionsMarker<RangeInclusive<i64>>> for ChaCha20Rng {
    type Output = Vec<i64>;
    fn generate(
        &mut self,
        subject: VecOptionsMarker<RangeInclusive<i64>>,
    ) -> Vec<i64> {
        let VecOptionsMarker { inner, len, sorted, distinct } = subject;

        if len == 0 {
            return vec![];
        }

        let start = *inner.start();
        let end = *inner.end();
        if start > end {
            panic!("Emptyset");
        }
        if distinct {
            let u_len = end - start;
            // 全域だったらオーバーフローするので +1 しないでおく。
            // len == 0 は上で処理しているので、len-1 はオーバーフローしない。
            // 符号つきだから u_len の時点でオーバーフローしうるじゃんね。
            // 全域でなければ Range の方を使うようにする? 全域なら別で考える?
            // 嫌だけど。

            // if end - start + 1 < len {}
            if u_len == 0 && len == 1 {
                return vec![start];
            }
            if (u_len as u128) < len as u128 - 1 {
                panic!("by pigeonhole principle, it is infeasible");
            }

            // (end - start + 1, len)
            // (_, 0) => []
            // (1, 1) => [start]
            // (x, y) if x < y => panic!()

            let mut res = vec![];
            // k >~ n/log(n) くらいなら dense だと思っていい?
            // ゼロ割りは嫌なので k log(n) >~ n。
            let lg_len = len.next_power_of_two().trailing_zeros() as i64;
            let sparse = u_len * lg_len < len as i64;

            if sparse {
                let between = Uniform::from(inner);
                let mut seen = BTreeSet::new();
                while res.len() < len {
                    // 失敗しまくったら下のやり方に fallback した方がいい?
                    let cur = between.sample(self);
                    if seen.insert(cur) {
                        res.push(cur);
                    }
                }
            } else {
                let mut pool: Vec<_> = (start..=end).collect();
                for _ in 0..len {
                    let u = Uniform::from(0..pool.len());
                    let i = u.sample(self);
                    res.push(pool.swap_remove(i));
                }
            }

            if sorted {
                res.sort_unstable();
            }
            return res;
        }

        if !sorted {
            return self.generate(VecMarker { inner, len });
        }

        // todo うまくやる
        // distinct のオプションがあるとやりやすいかも
        // 関数が同じだから再帰でやるかどうしようか
        // [start..=end + len - 1; len] where { distinct }
        // を作り、ソートして、a[i] -= i して返す。
        //
        // distinct に作るパートが難しくて、sparse か dense かで分ける?
        // infeasible なら panic!() で。

        // let mut tmp = self.generate(VecMarker { inner, len });
        // tmp.sort_unstable();
        // tmp

        let start = *inner.start();
        let end = inner.end() + len as i64 - 1;
        let mut res = self.generate(VecOptionsMarker {
            inner: start..=end,
            len,
            sorted: true,
            distinct: true,
        });
        for i in 0..len {
            res[i] -= i as i64;
        }
        res
    }
}

#[derive(Clone, Copy)]
pub struct Permutation(pub usize);

impl RandomGenerator<Permutation> for ChaCha20Rng {
    type Output = Vec<usize>;
    fn generate(&mut self, subject: Permutation) -> Vec<usize> {
        let n = subject.0;
        let mut res: Vec<_> = (0..n).collect();
        for i in (1..n).rev() {
            let j = self.generate(0..=i);
            res.swap(j, i);
        }
        res
    }
}