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
//! Generate random numbers that are not repeated in the range  
//! 
//! # Example
//! 
//! ```
//! use rand::thread_rng;
//! # use randge::*;
//! 
//! let v = randge(-15..15, 5, thread_rng());
//! let v: Vec<_> = v.collect();
//! # drop(v);
//! // output: like [13, -3, -14, 5, 3]
//! ```
//!
//! # Features
//! - `rand`
//! 

mod barrel;
mod fn_rand;
pub mod impls;
mod linear;
mod tree;
mod utils;
pub use fn_rand::*;
use impls::*;
use num_traits::{one, zero, AsPrimitive, PrimInt, Zero};
use std::{fmt::Debug, ops::Range};
use utils::*;

#[inline(always)]
fn check<T: PrimInt + AsPrimitive<usize>>(range: Range<T>, n: T) -> (usize, T, T) {
    let (min, max) = (range.start.min(range.end), range.start.max(range.end));
    let size = abs(max - min);
    if size.is_zero() {
        panic!("Range cannot be 0")
    }
    if is_negative(n) {
        panic!("n must be >= 0")
    }
    if size < n {
        panic!("The required count is greater than the allowed range")
    }
    (n.as_(), min, max)
}

/// Alias of [`randge_tree`](fn.randge_tree.html)
#[inline(always)]
pub fn randge<T: PrimInt + AsPrimitive<usize>>(range: Range<T>, n: T, rand: impl FnRand<T>) -> RandgeIter<T, impl FnRand<T>, RangesTree<T>> {
    randge_tree(range, n, rand)
}

/// Generate random numbers that are not repeated in the range  
/// - Based on linear algorithm, the worst time complexity is `O(n)` and the best `O(1)`
/// - Minimal memory usage
/// - Slowest
/// 
/// # Example
/// 
/// ```
/// use rand::thread_rng;
/// # use randge::*;
/// 
/// let v = randge_linear(-15..15, 5, thread_rng());
/// let v: Vec<_> = v.collect();
/// # drop(v);
/// // output: like [13, -3, -14, 5, 3]
/// ```
#[inline]
pub fn randge_linear<T: PrimInt + AsPrimitive<usize>>(range: Range<T>, n: T, rand: impl FnRand<T>) -> RandgeIter<T, impl FnRand<T>, RangesLinear<T>> {
    let (len, min, max) = check(range, n);
    let take = RangesLinear::new(min, max);
    RandgeIter::new(len, take, rand)
}

/// Generate random numbers that are not repeated in the range  
/// - Using tree-based algorithm, the time complexity is `O(logn)`
/// - Moderate memory usage
/// 
/// # Example
/// 
/// ```
/// use rand::thread_rng;
/// # use randge::*;
/// 
/// let v = randge_tree(-15..15, 5, thread_rng());
/// let v: Vec<_> = v.collect();
/// # drop(v);
/// // output: like [13, -3, -14, 5, 3]
/// ```
#[inline]
pub fn randge_tree<T: PrimInt + AsPrimitive<usize>>(range: Range<T>, n: T, rand: impl FnRand<T>) -> RandgeIter<T, impl FnRand<T>, RangesTree<T>> {
    let (len, min, max) = check(range, n);
    let take = RangesTree::new(min, max);
    RandgeIter::new(len, take, rand)
}

/// Generate random numbers that are not repeated in the range  
/// - Space for time, time complexity is `O(1)`
/// - Maximum memory usage
/// - Fastest
/// 
/// # Example
/// 
/// ```
/// use rand::thread_rng;
/// # use randge::*;
/// 
/// let v = randge_barrel(-15..15, 5, thread_rng());
/// let v: Vec<_> = v.collect();
/// # drop(v);
/// // output: like [13, -3, -14, 5, 3]
/// ```
#[inline]
pub fn randge_barrel<T: PrimInt + AsPrimitive<usize>>(range: Range<T>, n: T, rand: impl FnRand<T>) -> RandgeIter<T, impl FnRand<T>, RangesBarrel<T>>
where
    T: AsPrimitive<usize>,
    Range<T>: Iterator<Item = T>,
{
    let (len, min, max) = check(range, n);
    let take = RangesBarrel::new(min, max);
    RandgeIter::new(len, take, rand)
}

#[cfg(test)]
mod tests;

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

    #[test]
    fn test_linear() {
        let v = randge_linear(5..15, 5, thread_rng());
        let v: Vec<_> = v.collect();
        println!("{:?}", v);
    }

    #[test]
    fn test_tree() {
        let v = randge_tree(5..15, 5, thread_rng());
        let v: Vec<_> = v.collect();
        println!("{:?}", v);
    }

    #[test]
    fn test_barrel() {
        let v = randge_barrel(5..15, 5, thread_rng());
        let v: Vec<_> = v.collect();
        println!("{:?}", v);
    }
}