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
use num_traits::{FromPrimitive, One};
use seeds::PcgSeeder;
pub trait Stream<Itype> {
fn build(seed: Option<&mut PcgSeeder<Itype>>) -> Self;
fn set_stream(&mut self, _stream_seq: Itype) {
panic!("Stream setting unimplemented for this stream type");
}
fn increment(&self) -> Itype;
fn get_stream(&self) -> Itype;
}
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct OneSeqStream;
macro_rules! make_one_seq {
( $( $t:ty => $e:expr);* ) => {
$(impl Stream<$t> for OneSeqStream {
fn build(_: Option<&mut PcgSeeder<$t>>) -> Self {
OneSeqStream
}
#[inline(always)]
fn increment(&self) -> $t {
$e
}
fn get_stream(&self) -> $t {
$e
}
})*
}
}
make_one_seq! {
u32 => 2_891_336_453u32;
u64 => 1_442_695_040_888_963_407u64;
u128 => 117_397_592_171_526_113_268_558_934_119_004_209_487u128 }
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct NoSeqStream;
macro_rules! make_no_seq {
( $( $t:ty => $e:expr);* ) => {
$(impl Stream<$t> for NoSeqStream {
fn build(_: Option<&mut PcgSeeder<$t>>) -> Self {
NoSeqStream
}
#[inline(always)]
fn increment(&self) -> $t {
$e
}
fn get_stream(&self) -> $t {
$e
}
})*
}
}
make_no_seq! {
u32 => 0;
u64 => 0;
u128 => 0
}
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct SpecificSeqStream<Itype> {
inc: Itype,
}
macro_rules! make_set_seq {
( $( $t:ident => $e:expr);* ) => {
$(impl Stream<$t> for SpecificSeqStream<$t> {
fn build(seed: Option<&mut PcgSeeder<$t>>) -> Self {
match seed {
None => SpecificSeqStream {
inc : $e,
},
Some(seed) => SpecificSeqStream {
inc: seed.get(),
},
}
}
fn set_stream(&mut self, stream_seq : $t) {
self.inc = stream_seq | $t::one();
}
#[inline(always)]
fn increment(&self) -> $t {
self.inc
}
fn get_stream(&self) -> $t {
self.inc
}
})*
}
}
make_set_seq! {
u32 => 2_891_336_453u32;
u64 => 1_442_695_040_888_963_407u64;
u128 => 117_397_592_171_526_113_268_558_934_119_004_209_487u128 }
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct UniqueSeqStream;
impl<Itype> Stream<Itype> for UniqueSeqStream
where
Itype: FromPrimitive + ::seeds::ReadByteOrder,
{
fn build(_: Option<&mut PcgSeeder<Itype>>) -> Self {
UniqueSeqStream
}
#[inline(always)]
fn increment(&self) -> Itype {
Itype::from_usize(self as *const UniqueSeqStream as usize | 1).unwrap()
}
fn get_stream(&self) -> Itype {
Itype::from_usize(self as *const UniqueSeqStream as usize | 1).unwrap()
}
}