smallperm 0.1.0

High-performance pseudo-random permutations using Feistel networks with O(1) memory and O(1) time per element
Documentation
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
//! # smallperm
//!
//! High-performance pseudo-random permutations using Feistel networks.
//!
//! This library provides O(1) memory and O(1) expected time per element for generating
//! pseudo-random permutations, making it ideal for shuffling large datasets in machine
//! learning contexts where time-to-first-sample and memory overhead are critical.
//!
//! ## Features
//!
//! - **O(1) memory**: No need to store the entire permutation
//! - **O(1) random access**: Get any element of the permutation instantly
//! - **Invertible**: Map both forward and backward between indices and values
//! - **Deterministic**: Same seed produces same permutation across runs
//! - **Supports up to 2^128 elements**
//!
//! ## Example
//!
//! ```rust
//! use smallperm::Permutation;
//!
//! // Create a permutation of 1000 elements with seed 42
//! let perm = Permutation::new(1000, 42);
//!
//! // Iterate through the permutation
//! for (i, value) in perm.iter().enumerate().take(10) {
//!     println!("perm[{}] = {}", i, value);
//! }
//!
//! // Random access
//! let value = perm.get(500).unwrap();
//! println!("perm[500] = {}", value);
//!
//! // Inverse lookup
//! let index = perm.index_of(value).unwrap();
//! assert_eq!(index, 500);
//! ```

mod feistel;

pub use feistel::{FeistelNetwork, Permutor};

/// A pseudo-random permutation with O(1) memory and O(1) access time.
///
/// This struct wraps `Permutor` and provides a cleaner API for common use cases.
#[derive(Debug, Clone)]
pub struct Permutation {
    permutor: Permutor,
}

impl Permutation {
    /// Create a new permutation of `len` elements using a u64 seed.
    ///
    /// # Panics
    ///
    /// Panics if `len` is 0.
    ///
    /// # Example
    ///
    /// ```rust
    /// use smallperm::Permutation;
    ///
    /// let perm = Permutation::new(100, 42);
    /// assert_eq!(perm.len(), 100);
    /// ```
    pub fn new(len: u128, seed: u64) -> Self {
        assert!(len > 0, "Permutation length must be greater than 0");
        Self {
            permutor: Permutor::new_with_u64_key(len, seed),
        }
    }

    /// Create a new permutation using a 256-bit key.
    ///
    /// # Panics
    ///
    /// Panics if `len` is 0.
    pub fn new_with_key(len: u128, key: [u8; 32]) -> Self {
        assert!(len > 0, "Permutation length must be greater than 0");
        Self {
            permutor: Permutor::new_with_slice_key(len, key),
        }
    }

    /// Returns the length of the permutation.
    #[inline]
    pub fn len(&self) -> u128 {
        self.permutor.max
    }

    /// Returns true if the permutation is empty (always false since len > 0 is required).
    #[inline]
    pub fn is_empty(&self) -> bool {
        false
    }

    /// Get the value at index `i` in the permutation.
    ///
    /// Returns `None` if `i >= len`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use smallperm::Permutation;
    ///
    /// let perm = Permutation::new(100, 42);
    /// let value = perm.get(50).unwrap();
    /// assert!(value < 100);
    /// ```
    #[inline]
    pub fn get(&self, i: u128) -> Option<u128> {
        if i >= self.permutor.max {
            None
        } else {
            Some(self.permutor.forward(i))
        }
    }

    /// Get the value at index `i` without bounds checking.
    ///
    /// # Safety
    ///
    /// Caller must ensure `i < len`.
    #[inline]
    pub unsafe fn get_unchecked(&self, i: u128) -> u128 {
        self.permutor.forward(i)
    }

    /// Find the index of `value` in the permutation (inverse lookup).
    ///
    /// Returns `None` if `value >= len`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use smallperm::Permutation;
    ///
    /// let perm = Permutation::new(100, 42);
    /// let value = perm.get(50).unwrap();
    /// let index = perm.index_of(value).unwrap();
    /// assert_eq!(index, 50);
    /// ```
    #[inline]
    pub fn index_of(&self, value: u128) -> Option<u128> {
        if value >= self.permutor.max {
            None
        } else {
            Some(self.permutor.backward(value))
        }
    }

    /// Returns an iterator over the permutation.
    ///
    /// # Example
    ///
    /// ```rust
    /// use smallperm::Permutation;
    ///
    /// let perm = Permutation::new(10, 42);
    /// let values: Vec<u128> = perm.iter().collect();
    /// assert_eq!(values.len(), 10);
    /// ```
    pub fn iter(&self) -> PermutationIter {
        PermutationIter {
            permutor: self.permutor.clone(),
        }
    }
}

impl IntoIterator for Permutation {
    type Item = u128;
    type IntoIter = PermutationIter;

    fn into_iter(self) -> Self::IntoIter {
        PermutationIter {
            permutor: self.permutor,
        }
    }
}

impl<'a> IntoIterator for &'a Permutation {
    type Item = u128;
    type IntoIter = PermutationIter;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// Iterator over a permutation.
#[derive(Debug, Clone)]
pub struct PermutationIter {
    permutor: Permutor,
}

impl Iterator for PermutationIter {
    type Item = u128;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.permutor.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = (self.permutor.max - self.permutor.values_returned) as usize;
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for PermutationIter {}

/// Sample `k` unique random indices from `[0, n)`.
///
/// This is equivalent to taking the first `k` elements of a permutation of `n` elements.
///
/// # Panics
///
/// Panics if `n == 0` or `k > n`.
///
/// # Example
///
/// ```rust
/// use smallperm::sample_indices;
///
/// let indices = sample_indices(100, 10, 42);
/// assert_eq!(indices.len(), 10);
/// for &idx in &indices {
///     assert!(idx < 100);
/// }
/// ```
pub fn sample_indices(n: u128, k: usize, seed: u64) -> Vec<u128> {
    assert!(n > 0, "n must be greater than 0");
    assert!(k as u128 <= n, "k must be <= n");

    let perm = Permutation::new(n, seed);
    perm.iter().take(k).collect()
}

/// Sample `k` unique random elements from a slice.
///
/// # Panics
///
/// Panics if `slice.is_empty()` or `k > slice.len()`.
///
/// # Example
///
/// ```rust
/// use smallperm::sample;
///
/// let data = vec![10, 20, 30, 40, 50];
/// let sampled = sample(&data, 3, 42);
/// assert_eq!(sampled.len(), 3);
/// ```
pub fn sample<T: Clone>(slice: &[T], k: usize, seed: u64) -> Vec<T> {
    assert!(!slice.is_empty(), "slice must not be empty");
    assert!(k <= slice.len(), "k must be <= slice length");

    let indices = sample_indices(slice.len() as u128, k, seed);
    indices.iter().map(|&i| slice[i as usize].clone()).collect()
}

/// Shuffle a slice and return a new Vec with the shuffled elements.
///
/// # Panics
///
/// Panics if `slice.is_empty()`.
///
/// # Example
///
/// ```rust
/// use smallperm::shuffle;
///
/// let data = vec![1, 2, 3, 4, 5];
/// let shuffled = shuffle(&data, 42);
/// assert_eq!(shuffled.len(), 5);
/// ```
pub fn shuffle<T: Clone>(slice: &[T], seed: u64) -> Vec<T> {
    assert!(!slice.is_empty(), "slice must not be empty");

    let perm = Permutation::new(slice.len() as u128, seed);
    (0..slice.len())
        .map(|i| {
            let perm_idx = perm.get(i as u128).unwrap() as usize;
            slice[perm_idx].clone()
        })
        .collect()
}

/// Shuffle a mutable slice in place.
///
/// Note: This requires O(n) additional memory for the temporary buffer.
/// For true O(1) memory shuffling, iterate using `Permutation` directly.
///
/// # Panics
///
/// Panics if `slice.is_empty()`.
///
/// # Example
///
/// ```rust
/// use smallperm::shuffle_in_place;
///
/// let mut data = vec![1, 2, 3, 4, 5];
/// shuffle_in_place(&mut data, 42);
/// ```
pub fn shuffle_in_place<T: Clone>(slice: &mut [T], seed: u64) {
    if slice.is_empty() {
        return;
    }

    let shuffled = shuffle(slice, seed);
    slice.clone_from_slice(&shuffled);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_permutation_basic() {
        let perm = Permutation::new(100, 42);
        assert_eq!(perm.len(), 100);

        let value = perm.get(50).unwrap();
        assert!(value < 100);

        let index = perm.index_of(value).unwrap();
        assert_eq!(index, 50);
    }

    #[test]
    fn test_permutation_iterator() {
        let perm = Permutation::new(10, 42);
        let values: Vec<u128> = perm.iter().collect();
        assert_eq!(values.len(), 10);

        // Check all values are unique and in range
        let mut sorted = values.clone();
        sorted.sort();
        sorted.dedup();
        assert_eq!(sorted.len(), 10);
        assert!(sorted.iter().all(|&v| v < 10));
    }

    #[test]
    fn test_sample_indices() {
        let indices = sample_indices(100, 10, 42);
        assert_eq!(indices.len(), 10);

        // Check all indices are unique and in range
        let mut sorted = indices.clone();
        sorted.sort();
        sorted.dedup();
        assert_eq!(sorted.len(), 10);
        assert!(sorted.iter().all(|&i| i < 100));
    }

    #[test]
    fn test_sample() {
        let data: Vec<i32> = (0..100).collect();
        let sampled = sample(&data, 10, 42);
        assert_eq!(sampled.len(), 10);
    }

    #[test]
    fn test_shuffle() {
        let data: Vec<i32> = (0..10).collect();
        let shuffled = shuffle(&data, 42);
        assert_eq!(shuffled.len(), 10);

        // Check all elements are present
        let mut sorted = shuffled.clone();
        sorted.sort();
        assert_eq!(sorted, data);
    }

    #[test]
    fn test_shuffle_in_place() {
        let mut data: Vec<i32> = (0..10).collect();
        let original = data.clone();
        shuffle_in_place(&mut data, 42);

        // Check all elements are present
        let mut sorted = data.clone();
        sorted.sort();
        assert_eq!(sorted, original);
    }

    #[test]
    fn test_deterministic() {
        let perm1 = Permutation::new(100, 42);
        let perm2 = Permutation::new(100, 42);

        for i in 0..100 {
            assert_eq!(perm1.get(i), perm2.get(i));
        }
    }

    #[test]
    fn test_different_seeds() {
        let perm1 = Permutation::new(100, 42);
        let perm2 = Permutation::new(100, 43);

        // Very unlikely to be the same
        let same_count = (0..100)
            .filter(|&i| perm1.get(i) == perm2.get(i))
            .count();
        assert!(same_count < 10);
    }

    #[test]
    #[should_panic(expected = "Permutation length must be greater than 0")]
    fn test_zero_length_panics() {
        Permutation::new(0, 42);
    }

    #[test]
    fn test_out_of_bounds() {
        let perm = Permutation::new(10, 42);
        assert!(perm.get(10).is_none());
        assert!(perm.get(100).is_none());
        assert!(perm.index_of(10).is_none());
    }

    /// Chi-square test to verify that the permutation distribution is reasonably uniform.
    /// This is ported from bench/chisquaretest.py in the original smallperm.
    ///
    /// Uses a simple LCG (Linear Congruential Generator) to generate random seeds,
    /// mimicking the Python test which uses np.random.randint().
    ///
    /// Note: The Feistel network doesn't produce perfectly uniform permutations,
    /// but produces permutations where each element appears within ~10% of uniform
    /// expected frequency, which is sufficient for shuffling applications.
    #[test]
    fn test_chi_square_uniformity() {
        use std::collections::HashMap;

        let n = 4usize; // Permutation length
        let num_shuffles = 100_000u64;
        let factorial_n = (1..=n).product::<usize>(); // 24 possible permutations

        // Simple LCG for generating random seeds (mimics Python's random)
        let mut lcg_state: u64 = 12345; // Fixed seed for reproducibility
        let mut next_seed = || {
            // LCG parameters from Numerical Recipes
            lcg_state = lcg_state.wrapping_mul(6364136223846793005).wrapping_add(1);
            lcg_state
        };

        // Count occurrences of each permutation
        let mut counts: HashMap<Vec<u128>, u64> = HashMap::new();
        for _ in 0..num_shuffles {
            let seed = next_seed();
            let perm = Permutation::new(n as u128, seed);
            let values: Vec<u128> = perm.iter().collect();
            *counts.entry(values).or_insert(0) += 1;
        }

        // Expected count per permutation under uniform distribution
        let expected = num_shuffles as f64 / factorial_n as f64;

        // Check that all permutations were observed
        assert_eq!(
            counts.len(),
            factorial_n,
            "Not all permutations were observed"
        );

        // Verify each permutation count is within a reasonable range (±15% of expected)
        // This is a practical test that the distribution is "good enough" for shuffling
        let min_acceptable = (expected * 0.85) as u64;
        let max_acceptable = (expected * 1.15) as u64;

        for (perm, count) in &counts {
            assert!(
                *count >= min_acceptable && *count <= max_acceptable,
                "Permutation {:?} has count {} which is outside acceptable range [{}, {}]",
                perm,
                count,
                min_acceptable,
                max_acceptable
            );
        }

        // Calculate chi-square statistic for informational purposes
        let chi_square: f64 = counts
            .values()
            .map(|&observed| {
                let diff = observed as f64 - expected;
                diff * diff / expected
            })
            .sum();

        // Print the chi-square statistic for informational purposes
        // (visible when running tests with --nocapture)
        println!("Chi-square statistic: {:.2}", chi_square);
        println!(
            "Expected ~{} for uniform distribution (df={})",
            factorial_n - 1,
            factorial_n - 1
        );
    }
}