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
/*
 * PCG Random Number Generation for Rust
 *
 * Copyright 2015 John Brooks <robojeb@robojeb.xyz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * This work is derived from the implementation PCG RNG for C++ by
 * Melissa O'Neill.
 *
 * For additional information about the PCG random number generation scheme,
 * including its license and other licensing options, visit
 *
 *     http://www.pcg-random.org
 */

use num_traits::{FromPrimitive, One};
use seeds::PcgSeeder;

/// A stream provides the increment to the LCG. This increment should be
/// an odd number or the period of the generator will not be the full size
/// of the state.
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;
}

/// This sequence stream defines constants as provided by the PCG paper.
/// This struct is implemented with a macro to provide values for each
/// Stream<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 //u128::from_parts(6364136223846793005,1442695040888963407)
}

/// This stream provides an increment of 0 to the LCG. This turns the
/// LCG into a MCG, which while being less statistically sound than an LCG,
/// it is faster.
#[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
}

/// By default this stream provides the same stream as OneSeqStream. The
/// advantage to this stream is it can be changed at runtime. This incurs an
/// extra Itype of storage overhead.
#[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 //u128::from_parts(6364136223846793005,1442695040888963407)
}

/// This stream provides a stream based on the current location of the
/// generator in memory. This means that two PCG with the same seed
/// can produce different sequences of numbers. Though if the generator is
/// moved it will change the stream.
#[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()
    }
}