@@ -26,6 +26,7 @@ export srand,
26
26
27
27
abstract type AbstractRNG end
28
28
29
+
29
30
# ## integers
30
31
31
32
# we define types which encode the generation of a specific number of bits
@@ -83,7 +84,9 @@ const BitFloatType = Union{Type{Float16},Type{Float32},Type{Float64}}
83
84
84
85
# ## Sampler
85
86
86
- abstract type Sampler end
87
+ abstract type Sampler{E} end
88
+
89
+ Base. eltype (:: Sampler{E} ) where {E} = E
87
90
88
91
# temporarily for BaseBenchmarks
89
92
RangeGenerator (x) = Sampler (GLOBAL_RNG, x)
@@ -109,41 +112,48 @@ Sampler(rng::AbstractRNG, ::Type{X}) where {X} = Sampler(rng, X, Val(Inf))
109
112
# ### pre-defined useful Sampler types
110
113
111
114
# default fall-back for types
112
- struct SamplerType{T} <: Sampler end
115
+ struct SamplerType{T} <: Sampler{T} end
113
116
114
117
Sampler (:: AbstractRNG , :: Type{T} , :: Repetition ) where {T} = SamplerType {T} ()
115
118
116
- Base. getindex (sp :: SamplerType{T} ) where {T} = T
119
+ Base. getindex (:: SamplerType{T} ) where {T} = T
117
120
118
121
# default fall-back for values
119
- struct SamplerTrivial{T} <: Sampler
122
+ struct SamplerTrivial{T,E } <: Sampler{E}
120
123
self:: T
121
124
end
122
125
123
- Sampler (:: AbstractRNG , X, :: Repetition ) = SamplerTrivial (X)
126
+ SamplerTrivial (x:: T ) where {T} = SamplerTrivial {T,eltype(T)} (x)
127
+
128
+ Sampler (:: AbstractRNG , x, :: Repetition ) = SamplerTrivial (x)
124
129
125
130
Base. getindex (sp:: SamplerTrivial ) = sp. self
126
131
127
132
# simple sampler carrying data (which can be anything)
128
- struct SamplerSimple{T,S} <: Sampler
133
+ struct SamplerSimple{T,S,E } <: Sampler{E}
129
134
self:: T
130
135
data:: S
131
136
end
132
137
138
+ SamplerSimple (x:: T , data:: S ) where {T,S} = SamplerSimple {T,S,eltype(T)} (x, data)
139
+
133
140
Base. getindex (sp:: SamplerSimple ) = sp. self
134
141
135
142
# simple sampler carrying a (type) tag T and data
136
- struct SamplerTag{T,S} <: Sampler
143
+ struct SamplerTag{T,S,E } <: Sampler{E}
137
144
data:: S
138
- SamplerTag {T} (s:: S ) where {T,S} = new {T,S} (s)
145
+ SamplerTag {T} (s:: S ) where {T,S} = new {T,S,eltype(T) } (s)
139
146
end
140
147
141
148
142
149
# ### helper samplers
143
150
151
+ # TODO : make constraining constructors to enforce that those
152
+ # types are <: Sampler{T}
153
+
144
154
# #### Adapter to generate a randome value in [0, n]
145
155
146
- struct LessThan{T<: Integer ,S} <: Sampler
156
+ struct LessThan{T<: Integer ,S} <: Sampler{T}
147
157
sup:: T
148
158
s:: S # the scalar specification/sampler to feed to rand
149
159
end
@@ -155,7 +165,7 @@ function rand(rng::AbstractRNG, sp::LessThan)
155
165
end
156
166
end
157
167
158
- struct Masked{T<: Integer ,S} <: Sampler
168
+ struct Masked{T<: Integer ,S} <: Sampler{T}
159
169
mask:: T
160
170
s:: S
161
171
end
@@ -164,7 +174,7 @@ rand(rng::AbstractRNG, sp::Masked) = rand(rng, sp.s) & sp.mask
164
174
165
175
# #### Uniform
166
176
167
- struct UniformT{T} <: Sampler end
177
+ struct UniformT{T} <: Sampler{T} end
168
178
169
179
uniform (:: Type{T} ) where {T} = UniformT {T} ()
170
180
0 commit comments