@@ -19,6 +19,8 @@ mutable struct BinaryTreePrefixSearch{T<:Real}
1919 offset:: Int64 # 2^(depth - 1). Index of first leaf and number of leaves.
2020 cnt:: Int64 # Number of leaves in use. Logical number of entries. cnt > 0.
2121 initial_allocation:: Int64
22+ cache:: Dict{Int64,T}
23+ cached_cnt:: Int64
2224end
2325
2426
@@ -31,7 +33,7 @@ The optional hint, N, is the number of values to pre-allocate.
3133function BinaryTreePrefixSearch {T} (N= 32 ) where {T<: Real }
3234 depth, offset, array_cnt = _btps_sizes (N)
3335 b = zeros (T, array_cnt)
34- BinaryTreePrefixSearch {T} (b, depth, offset, 0 , N)
36+ BinaryTreePrefixSearch {T} (b, depth, offset, 0 , N, Dict {Int64,T} (), 0 )
3537end
3638
3739
@@ -42,6 +44,8 @@ function Base.empty!(ps::BinaryTreePrefixSearch)
4244 ps. depth = depth
4345 ps. offset = offset
4446 ps. cnt = 0
47+ empty! (ps. cache)
48+ ps. cached_cnt = 0
4549end
4650
4751function Base. copy! (dst:: BinaryTreePrefixSearch{T} , src:: BinaryTreePrefixSearch{T} ) where {T}
@@ -50,6 +54,8 @@ function Base.copy!(dst::BinaryTreePrefixSearch{T}, src::BinaryTreePrefixSearch{
5054 dst. offset = src. offset
5155 dst. cnt = src. cnt
5256 dst. initial_allocation = src. initial_allocation
57+ copy! (dst. cache, src. cache)
58+ dst. cached_cnt = src. cached_cnt
5359end
5460
5561
@@ -93,7 +99,7 @@ function Base.resize!(pst::BinaryTreePrefixSearch{T}, newcnt) where {T}
9399end
94100
95101
96- Base. length (ps:: BinaryTreePrefixSearch ) = ps. cnt
102+ Base. length (ps:: BinaryTreePrefixSearch ) = ps. cnt + ps . cached_cnt
97103allocated (ps:: BinaryTreePrefixSearch ) = ps. offset
98104
99105
@@ -125,7 +131,15 @@ function choose(pst::BinaryTreePrefixSearch{T}, value) where {T}
125131end
126132
127133
128- Base. sum! (pst:: BinaryTreePrefixSearch ) = pst. array[1 ]
134+ # You have to call sum! before calling getindex, or it won't be updated.
135+ function Base. sum! (pst:: BinaryTreePrefixSearch )
136+ if ! isempty (pst. cache)
137+ set_multiple! (pst, pairs (pst. cache))
138+ empty! (pst. cache)
139+ pst. cached_cnt = 0
140+ end
141+ pst. array[1 ]
142+ end
129143
130144
131145"""
@@ -161,16 +175,19 @@ end
161175
162176
163177function Base. push! (pst:: BinaryTreePrefixSearch{T} , value:: T ) where T
164- set_multiple! (pst, [(pst. cnt + 1 , value)])
178+ pst. cached_cnt += 1
179+ pst. cache[pst. cnt + pst. cached_cnt] = value
165180 return value
166181end
167182
168183
169184"""
170185 setindex!(A, X, inds...)
186+
187+ You have to call sum! before calling getindex.
171188"""
172189function Base. setindex! (pst:: BinaryTreePrefixSearch{T} , value:: T , index) where T
173- set_multiple! ( pst, [( index, value)])
190+ pst. cache[ index] = value
174191end
175192
176193function Base. getindex (pst:: BinaryTreePrefixSearch{T} , index) where {T}
@@ -200,8 +217,8 @@ Random.rand(rng::AbstractRNG, d::Random.SamplerTrivial{BinaryTreePrefixSearch{T}
200217
201218Base. haskey (md:: BinaryTreePrefixSearch , clock) = false
202219
203- function Base. haskey (md :: BinaryTreePrefixSearch{T} , clock:: Int ) where {T}
204- if 0 < clock ≤ length (md )
220+ function Base. haskey (pst :: BinaryTreePrefixSearch{T} , clock:: Int ) where {T}
221+ if 0 < clock ≤ length (pst )
205222 return getindex (pst, clock) > zero (T)
206223 else
207224 return false
0 commit comments