1
1
2
2
"""
3
- ReservoirSample([rng], T , method = AlgRSWRSKIP())
4
- ReservoirSample([rng], T , n::Int, method = AlgL(); ordered = false)
3
+ ReservoirSample{T} ([rng], method = AlgRSWRSKIP())
4
+ ReservoirSample{T} ([rng], n::Int, method = AlgL(); ordered = false)
5
5
6
6
Initializes a reservoir sample which can then be fitted with [`fit!`](@ref).
7
7
The first signature represents a sample where only a single element is collected.
@@ -10,19 +10,21 @@ they were collected with [`ordvalue`](@ref).
10
10
11
11
Look at the [`Sampling Algorithms`](@ref) section for the supported methods.
12
12
"""
13
- function ReservoirSample (T, method:: ReservoirAlgorithm = AlgRSWRSKIP ())
14
- return ReservoirSample (Random. default_rng (), T, method, MutSample ())
13
+ struct ReservoirSample{T} 1 === 1 end
14
+
15
+ function ReservoirSample {T} (method:: ReservoirAlgorithm = AlgRSWRSKIP ()) where T
16
+ return ReservoirSample {T} (Random. default_rng (), method, MutSample ())
15
17
end
16
- function ReservoirSample (rng:: AbstractRNG , T, method:: ReservoirAlgorithm = AlgRSWRSKIP ())
17
- return ReservoirSample (rng, T , method, MutSample ())
18
+ function ReservoirSample {T} (rng:: AbstractRNG , method:: ReservoirAlgorithm = AlgRSWRSKIP ()) where T
19
+ return ReservoirSample {T} (rng, method, MutSample ())
18
20
end
19
- Base. @constprop :aggressive function ReservoirSample (T, n:: Integer , method:: ReservoirAlgorithm = AlgL ();
20
- ordered = false )
21
- return ReservoirSample (Random. default_rng (), T , n, method, MutSample (), ordered ? Ord () : Unord ())
21
+ Base. @constprop :aggressive function ReservoirSample {T} ( n:: Integer , method:: ReservoirAlgorithm = AlgL ();
22
+ ordered = false ) where T
23
+ return ReservoirSample {T} (Random. default_rng (), n, method, MutSample (), ordered ? Ord () : Unord ())
22
24
end
23
- Base. @constprop :aggressive function ReservoirSample (rng:: AbstractRNG , T , n:: Integer ,
24
- method:: ReservoirAlgorithm = AlgL (); ordered = false )
25
- return ReservoirSample (rng, T , n, method, MutSample (), ordered ? Ord () : Unord ())
25
+ Base. @constprop :aggressive function ReservoirSample {T} (rng:: AbstractRNG , n:: Integer ,
26
+ method:: ReservoirAlgorithm = AlgL (); ordered = false ) where T
27
+ return ReservoirSample {T} (rng, n, method, MutSample (), ordered ? Ord () : Unord ())
26
28
end
27
29
28
30
"""
@@ -86,7 +88,6 @@ function Base.merge!(::AbstractReservoirSample)
86
88
error (" Abstract Version" )
87
89
end
88
90
89
-
90
91
"""
91
92
Base.merge(rs::AbstractReservoirSample...)
92
93
@@ -98,6 +99,31 @@ function OnlineStatsBase.merge(::AbstractReservoirSample)
98
99
error (" Abstract Version" )
99
100
end
100
101
102
+ """
103
+ StreamSample{T}([rng], iter, n, [N], method = AlgS())
104
+
105
+ Initializes a stream sample, which can then be iterated over
106
+ to return the sampling elements of the iterable `iter` which
107
+ is assumed to have a eltype of `T`. The methods implemented in
108
+ [`StreamSample`](@ref) require the knowledge of the total number
109
+ of elements in the stream `N`, if not provided it is assumed to be
110
+ available by calling `length(iter)`.
111
+ """
112
+ struct StreamSample{T} 1 === 1 end
113
+
114
+ function StreamSample {T} (iter, n, N, method:: StreamAlgorithm = AlgORDS ()) where T
115
+ return StreamSample {T} (Random. default_rng (), iter, n, N, method)
116
+ end
117
+ function StreamSample {T} (iter, n, method:: StreamAlgorithm = AlgORDS ()) where T
118
+ return StreamSample {T} (Random. default_rng (), iter, n, length (iter), method)
119
+ end
120
+ function StreamSample {T} (rng:: AbstractRNG , iter, n, method:: StreamAlgorithm = AlgORDS ()) where T
121
+ return StreamSample {T} (rng, iter, n, length (iter), method)
122
+ end
123
+ function StreamSample {T} (rng:: AbstractRNG , iter, n, N, method:: StreamAlgorithm = AlgORDS ()) where T
124
+ return StreamSample {T} (rng, iter, n, N, method)
125
+ end
126
+
101
127
"""
102
128
itsample([rng], iter, method = AlgRSWRSKIP())
103
129
itsample([rng], iter, wfunc, method = AlgWRSWRSKIP())
@@ -145,37 +171,38 @@ end
145
171
Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, method = AlgRSWRSKIP ();
146
172
iter_type = infer_eltype (iter))
147
173
if Base. IteratorSize (iter) isa Base. SizeUnknown
148
- s = ReservoirSample (rng, iter_type , method, ImmutSample ())
174
+ s = ReservoirSample {iter_type} (rng, method, ImmutSample ())
149
175
return update_all! (s, iter)
150
176
else
151
- return sortedindices_sample (rng, iter)
177
+ return sorted_sample_single (rng, iter)
152
178
end
153
179
end
154
180
Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, n:: Int , method = AlgL ();
155
181
iter_type = infer_eltype (iter), ordered = false )
156
182
if Base. IteratorSize (iter) isa Base. SizeUnknown
157
- s = ReservoirSample (rng, iter_type , n, method, ImmutSample (), ordered ? Ord () : Unord ())
183
+ s = ReservoirSample {iter_type} (rng, n, method, ImmutSample (), ordered ? Ord () : Unord ())
158
184
return update_all! (s, iter, ordered)
159
185
else
160
- replace = method isa AlgL || method isa AlgR ? NoReplace () : Replace ()
161
- sortedindices_sample (rng, iter, n, replace; iter_type, ordered)
186
+ m = method isa AlgL || method isa AlgR ? AlgORDS () : AlgORDSWR ()
187
+ s = collect (StreamSample {iter_type} (rng, iter, n, length (iter), m))
188
+ return ordered ? s : shuffle! (rng, s)
162
189
end
163
190
end
164
191
function itsample (rng:: AbstractRNG , iter, wv:: Function , method = AlgWRSWRSKIP (); iter_type = infer_eltype (iter))
165
- s = ReservoirSample (rng, iter_type , method, ImmutSample ())
192
+ s = ReservoirSample {iter_type} (rng, method, ImmutSample ())
166
193
return update_all! (s, iter, wv)
167
194
end
168
195
Base. @constprop :aggressive function itsample (rng:: AbstractRNG , iter, wv:: Function , n:: Int , method = AlgAExpJ ();
169
196
iter_type = infer_eltype (iter), ordered = false )
170
- s = ReservoirSample (rng, iter_type , n, method, ImmutSample (), ordered ? Ord () : Unord ())
197
+ s = ReservoirSample {iter_type} (rng, n, method, ImmutSample (), ordered ? Ord () : Unord ())
171
198
return update_all! (s, iter, ordered, wv)
172
199
end
173
200
function itsample (rngs:: Tuple , iters:: Tuple , n:: Int ,; iter_types = infer_eltype .(iters))
174
201
n_it = length (iters)
175
202
vs = Vector {Vector{Union{iter_types...}}} (undef, n_it)
176
203
ps = Vector {Float64} (undef, n_it)
177
204
Threads. @threads for i in 1 : n_it
178
- s = ReservoirSample (rngs [i], iter_types [i], n, AlgRSWRSKIP (), ImmutSample (), Unord ())
205
+ s = ReservoirSample {iter_types [i]} (rngs [i], n, AlgRSWRSKIP (), ImmutSample (), Unord ())
179
206
vs[i], ps[i] = update_all_p! (s, iters[i])
180
207
end
181
208
ps /= sum (ps)
@@ -186,7 +213,7 @@ function itsample(rngs::Tuple, iters::Tuple, wfuncs::Tuple, n::Int; iter_types =
186
213
vs = Vector {Vector{Union{iter_types...}}} (undef, n_it)
187
214
ps = Vector {Float64} (undef, n_it)
188
215
Threads. @threads for i in 1 : n_it
189
- s = ReservoirSample (rngs [i], iter_types [i], n, AlgWRSWRSKIP (), ImmutSample (), Unord ())
216
+ s = ReservoirSample {iter_types [i]} (rngs [i], n, AlgWRSWRSKIP (), ImmutSample (), Unord ())
190
217
vs[i], ps[i] = update_all_p! (s, iters[i], wfuncs[i])
191
218
end
192
219
ps /= sum (ps)
0 commit comments