@@ -12,7 +12,7 @@ re-enables the same set of clocks, this is the faster choice.
1212"""
1313struct KeyedKeepPrefixSearch{T,P} <: KeyedPrefixSearch
1414 # Map from clock name to index in propensity array.
15- index:: Dict{T, Int}
15+ index:: Dict{T,Int}
1616 # Map from index in propensity array to clock name.
1717 key:: Vector{T}
1818 prefix:: P
3737
3838
3939Base. length (kp:: KeyedKeepPrefixSearch ) = length (kp. index)
40+ key_type (kp:: KeyedKeepPrefixSearch{T,P} ) where {T,P} = T
4041time_type (kp:: KeyedKeepPrefixSearch{T,P} ) where {T,P} = time_type (P)
4142
4243
5455
5556
5657isenabled (kp:: KeyedKeepPrefixSearch , clock) = (
57- haskey (kp. index, clock) && kp. prefix[clock] > zero (time_type (kp))
58+ haskey (kp. index, clock) && kp. prefix[kp . index[ clock] ] > zero (time_type (kp))
5859)
5960
61+ """
62+ Construct a set that checks which values are zeroed out because this
63+ prefix sum doesn't mark what has been deleted. That's faster for small
64+ sets of keys but makes getting the set that's enabled more difficult.
65+ A hazard rate that is set to zero at enabling will give a funny count
66+ because it's `enabled!()` by the user but set to never fire.
67+ """
68+ struct PrefixEnabled{K,P,KK} <: AbstractSet{K}
69+ prefix:: P
70+ keys:: KK
71+ end
72+
73+ # Implements the interface to return a set of enabled clock keys.
74+ function enabled (prefix:: KeyedKeepPrefixSearch{T,P} ) where {T,P}
75+ kk = keys (prefix. index)
76+ PrefixEnabled {T,typeof(prefix),typeof(kk)} (prefix, kk)
77+ end
78+
79+ function Base. iterate (nre:: PrefixEnabled )
80+ res = iterate (nre. keys)
81+ res === nothing && return res
82+ while ! isenabled (nre. prefix, res[1 ])
83+ res = iterate (nre. keys, res[2 ])
84+ res === nothing && return res
85+ end
86+ return res
87+ end
88+
89+
90+ function Base. iterate (nre:: PrefixEnabled , state)
91+ res = iterate (nre. keys, state)
92+ res === nothing && return res
93+ while ! isenabled (nre. prefix, res[1 ])
94+ res = iterate (nre. keys, res[2 ])
95+ res === nothing && return res
96+ end
97+ return res
98+ end
99+
100+ Base. length (nre:: PrefixEnabled ) = count (x -> isenabled (nre. prefix, x), nre. keys)
101+ Base. in (x, nre:: PrefixEnabled ) = isenabled (nre. prefix, x)
102+ Base. eltype (:: Type{PrefixEnabled{C}} ) where {C} = C
60103
61104Base. delete! (kp:: KeyedKeepPrefixSearch , clock) = kp. prefix[kp. index[clock]] = zero (time_type (kp))
62105function Base. sum! (kp:: KeyedKeepPrefixSearch )
72115
73116function Random. rand (
74117 rng:: AbstractRNG , d:: Random.SamplerTrivial{KeyedKeepPrefixSearch{T,P}}
75- ) where {T,P}
118+ ) where {T,P}
76119 total = sum! (d[])
77120 LocalTime = time_type (P)
78121 choose (d[], rand (rng, Uniform {LocalTime} (zero (LocalTime), total)))
@@ -87,7 +130,7 @@ a large key space, this will use less memory.
87130"""
88131struct KeyedRemovalPrefixSearch{T,P} <: KeyedPrefixSearch
89132 # Map from clock name to index in propensity array.
90- index:: Dict{T, Int}
133+ index:: Dict{T,Int}
91134 # Map from index in propensity array to clock name.
92135 key:: Vector{T}
93136 free:: Set{Int}
@@ -114,6 +157,7 @@ function Base.copy!(dst::KeyedRemovalPrefixSearch{T,P}, src::KeyedRemovalPrefixS
114157end
115158
116159Base. length (kp:: KeyedRemovalPrefixSearch ) = length (kp. index)
160+ key_type (kp:: KeyedRemovalPrefixSearch{T,P} ) where {T,P} = T
117161time_type (kp:: KeyedRemovalPrefixSearch{T,P} ) where {T,P} = time_type (P)
118162
119163function Base. setindex! (kp:: KeyedRemovalPrefixSearch , val, clock)
@@ -134,10 +178,8 @@ function Base.setindex!(kp::KeyedRemovalPrefixSearch, val, clock)
134178end
135179
136180
137- isenabled (kp:: KeyedRemovalPrefixSearch , clock) = (
138- haskey (kp. index, clock) && kp. prefix[clock] > zero (time_type (kp))
139- )
140-
181+ isenabled (kp:: KeyedRemovalPrefixSearch , clock) = haskey (kp. index, clock)
182+ enabled (kp:: KeyedRemovalPrefixSearch ) = keys (kp. index)
141183
142184function Base. getindex (kp:: KeyedRemovalPrefixSearch , clock)
143185 if haskey (kp. index, clock)
170212
171213function Random. rand (
172214 rng:: AbstractRNG , d:: Random.SamplerTrivial{KeyedRemovalPrefixSearch{T,P}}
173- ) where {T,P}
215+ ) where {T,P}
174216 total = sum! (d[])
175217 LocalTime = time_type (P)
176218 choose (d[], rand (rng, Uniform {LocalTime} (zero (LocalTime), total)))
0 commit comments