@@ -28,22 +28,22 @@ function DynamicSampler(rng)
28
28
level_max = Float64[0.0 ]
29
29
level_inds = Int[]
30
30
return DynamicSampler (rng, Ref (0 ), Ref (totweight), weights, level_weights,
31
- level_buckets, level_max, level_inds)
31
+ level_buckets, level_max, level_inds)
32
32
end
33
33
34
- struct DynamicIndex
34
+ struct IndexInfo
35
35
idx:: Int
36
36
weight:: Float64
37
37
level:: Int
38
38
idx_in_level:: Int
39
39
end
40
- DynamicIndex (idx, weight) = DynamicIndex (idx, weight, 0 , 0 )
40
+ IndexInfo (idx, weight) = IndexInfo (idx, weight, 0 , 0 )
41
41
42
42
Base. sizehint! (s:: DynamicSampler , N) = resize_w! (s, N)
43
43
44
44
function Base. push! (S:: DynamicSampler , e:: Tuple )
45
45
idx, weight = e
46
- resize_w! (s , idx)
46
+ resize_w! (S , idx)
47
47
S. weights[idx] != 0.0 && error ()
48
48
S. totweight[] += weight
49
49
S. totvalues[] += 1
@@ -53,7 +53,7 @@ function Base.push!(S::DynamicSampler, e::Tuple)
53
53
S. level_max[level] = max (S. level_max[level], weight)
54
54
S. level_weights[level] += weight
55
55
push! (S. level_buckets[level], idx)
56
- S. weights[idx] = weight
56
+ S. weights[idx] = weight
57
57
return S
58
58
end
59
59
@@ -117,7 +117,7 @@ function Base.rand(S::DynamicSampler; info = false)
117
117
weight = S. weights[idx]
118
118
rand (S. rng) * level_max <= weight && break
119
119
end
120
- return info == false ? idx : DynamicIndex (idx, weight, level, idx_in_level)
120
+ return info == false ? idx : IndexInfo (idx, weight, level, idx_in_level)
121
121
end
122
122
123
123
function Base. deleteat! (S:: DynamicSampler , idx)
@@ -128,7 +128,7 @@ function Base.deleteat!(S::DynamicSampler, idx)
128
128
_deleteat! (S, idx, weight, level, idx_in_level)
129
129
return S
130
130
end
131
- function Base. deleteat! (S:: DynamicSampler , e:: DynamicIndex )
131
+ function Base. deleteat! (S:: DynamicSampler , e:: IndexInfo )
132
132
idx, weight, level, idx_in_level = e. idx, e. weight, e. level, e. idx_in_level
133
133
_deleteat! (S, idx, weight, level, idx_in_level)
134
134
return S
@@ -148,6 +148,15 @@ Base.isempty(S::DynamicSampler) = S.totvalues[] == 0
148
148
149
149
allvalues (s:: DynamicSampler ) = reduce (vcat, s. level_buckets)
150
150
151
+ function Base. show (io:: IO , mime:: MIME"text/plain" , s:: DynamicSampler )
152
+ inds = allvalues (s)
153
+ print (" DynamicSampler(indices = $(inds) , weights = $(s. weights[inds]) )" )
154
+ end
155
+
156
+ function Base. show (io:: IO , mime:: MIME"text/plain" , di:: IndexInfo )
157
+ print (" IndexInfo(idx = $(di. idx) , weight = $(di. weight) )" )
158
+ end
159
+
151
160
function resize_w! (s, N)
152
161
N_curr = length (s. weights)
153
162
if N > N_curr
184
193
185
194
getlevel (minlevel, weight) = ceil (Int, log2 (weight)) - minlevel + 1
186
195
187
- end
196
+ end
0 commit comments