-
Notifications
You must be signed in to change notification settings - Fork 32
/
wrapper.rs
99 lines (87 loc) · 2.6 KB
/
wrapper.rs
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
extern crate rand;
use rand::prelude::*;
// =============================================================================
// Random Wrapper
// =============================================================================
/// Extract no-duplicate sample from Vector
pub trait SampleRNG {
type Item;
fn sample(&self, n: usize) -> Vec<Self::Item>;
}
impl SampleRNG for Vec<usize> {
type Item = usize;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for Vec<u32> {
type Item = u32;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for Vec<u64> {
type Item = u64;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for Vec<isize> {
type Item = isize;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for Vec<i32> {
type Item = i32;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for Vec<i64> {
type Item = i64;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for Vec<f64> {
type Item = f64;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for Vec<f32> {
type Item = f32;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for Vec<char> {
type Item = char;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl<'a> SampleRNG for Vec<&'a str> {
type Item = &'a str;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.iter().map(|x| *x).choose_multiple(&mut rng, n)
}
}
impl SampleRNG for String {
type Item = char;
fn sample(&self, n: usize) -> Vec<Self::Item> {
let mut rng = thread_rng();
self.chars().choose_multiple(&mut rng, n)
}
}