Skip to content

Commit b68a54b

Browse files
committed
Store based on Dict
1 parent bd25031 commit b68a54b

File tree

7 files changed

+29
-22
lines changed

7 files changed

+29
-22
lines changed

src/base.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function show(io::IO, ev::AbstractEvent)
2727
end
2828

2929
function show(io::IO, env::Environment)
30-
if env.active_proc == nothing
30+
if env.active_proc === nothing
3131
print(io, "$(typeof(env)) time: $(now(env)) active_process: nothing")
3232
else
3333
print(io, "$(typeof(env)) time: $(now(env)) active_process: $(env.active_proc)")
@@ -47,14 +47,14 @@ function state(ev::AbstractEvent) :: EVENT_STATE
4747
end
4848

4949
function append_callback(func::Function, ev::AbstractEvent, args::Any...) :: Function
50-
ev.bev.state == processed && throw(EventProcessed(ev))
50+
ev.bev.state === processed && throw(EventProcessed(ev))
5151
cb = ()->func(ev, args...)
5252
push!(ev.bev.callbacks, cb)
5353
cb
5454
end
5555

5656
macro callback(expr::Expr)
57-
expr.head != :call && error("Expression is not a function call!")
57+
expr.head !== :call && error("Expression is not a function call!")
5858
esc(:(SimJulia.append_callback($(expr.args...))))
5959
end
6060

@@ -64,7 +64,7 @@ function remove_callback(cb::Function, ev::AbstractEvent)
6464
end
6565

6666
function schedule(ev::AbstractEvent, delay::Number=zero(Float64); priority::Int=0, value::Any=nothing)
67-
state(ev) == processed && throw(EventProcessed(ev))
67+
state(ev) === processed && throw(EventProcessed(ev))
6868
env = environment(ev)
6969
bev = ev.bev
7070
bev.value = value

src/events.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct Event <: AbstractEvent
66
end
77

88
function succeed(ev::Event; priority::Int=0, value::Any=nothing) :: Event
9-
state(ev) != idle && throw(EventNotIdle(ev))
9+
state(ev) !== idle && throw(EventNotIdle(ev))
1010
schedule(ev; priority=priority, value=value)
1111
end
1212

src/operators.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ end
2323

2424
function check(ev::AbstractEvent, op::Operator, event_state_values::Dict{AbstractEvent, StateValue})
2525
val = value(ev)
26-
if state(op) == idle
26+
if state(op) === idle
2727
if isa(val, Exception)
2828
schedule(op; value=val)
2929
else
3030
event_state_values[ev] = StateValue(state(ev), val)
3131
op.eval(collect(values(event_state_values))) && schedule(op; value=event_state_values)
3232
end
33-
elseif state(op) == scheduled
33+
elseif state(op) === scheduled
3434
if isa(val, Exception)
3535
schedule(op; priority=typemax(Int), value=val)
3636
else
@@ -40,11 +40,11 @@ function check(ev::AbstractEvent, op::Operator, event_state_values::Dict{Abstrac
4040
end
4141

4242
function eval_and(state_values::Vector{StateValue})
43-
all(map((sv)->sv.state == processed, state_values))
43+
all(map((sv)->sv.state === processed, state_values))
4444
end
4545

4646
function eval_or(state_values::Vector{StateValue})
47-
any(map((sv)->sv.state == processed, state_values))
47+
any(map((sv)->sv.state === processed, state_values))
4848
end
4949

5050
function (&)(ev1::AbstractEvent, ev2::AbstractEvent)

src/processes.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mutable struct Process <: DiscreteProcess
2121
end
2222

2323
macro process(expr)
24-
expr.head != :call && error("Expression is not a function call!")
24+
expr.head !== :call && error("Expression is not a function call!")
2525
esc(:(Process($(expr.args...))))
2626
end
2727

@@ -31,7 +31,7 @@ function execute(ev::AbstractEvent, proc::Process)
3131
set_active_process(env, proc)
3232
target = proc.fsmi(value(ev))
3333
reset_active_process(env)
34-
if proc.fsmi._state == 0xff
34+
if proc.fsmi._state === 0xff
3535
schedule(proc; value=target)
3636
else
3737
proc.target = state(target) == processed ? timeout(env; value=value(target)) : target
@@ -56,7 +56,7 @@ end
5656

5757
function interrupt(proc::Process, cause::Any=nothing)
5858
env = environment(proc)
59-
if proc.fsmi._state != 0xff
59+
if proc.fsmi._state !== 0xff
6060
proc.target isa Initialize && schedule(proc.target; priority=typemax(Int))
6161
target = schedule(Interrupt(env); priority=typemax(Int), value=InterruptException(active_process(env), cause))
6262
@callback execute_interrupt(target, proc)

src/resources/base.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ struct Get <: ResourceEvent
2323
end
2424

2525
function isless(a::ResourceKey, b::ResourceKey)
26-
(a.priority < b.priority) || (a.priority == b.priority && a.id < b.id)
26+
(a.priority < b.priority) || (a.priority === b.priority && a.id < b.id)
2727
end
2828

2929
function trigger_put(put_ev::ResourceEvent, res::AbstractResource)
3030
queue = DataStructures.PriorityQueue(res.put_queue)
3131
while length(queue) > 0
3232
(put_ev, key) = DataStructures.peek(queue)
3333
proceed = do_put(res, put_ev, key)
34-
state(put_ev) == scheduled && DataStructures.dequeue!(res.put_queue, put_ev)
34+
state(put_ev) === scheduled && DataStructures.dequeue!(res.put_queue, put_ev)
3535
proceed ? DataStructures.dequeue!(queue) : break
3636
end
3737
end
@@ -41,7 +41,7 @@ function trigger_get(get_ev::ResourceEvent, res::AbstractResource)
4141
while length(queue) > 0
4242
(get_ev, key) = DataStructures.peek(queue)
4343
proceed = do_get(res, get_ev, key)
44-
state(get_ev) == scheduled && DataStructures.dequeue!(res.get_queue, get_ev)
44+
state(get_ev) === scheduled && DataStructures.dequeue!(res.get_queue, get_ev)
4545
proceed ? DataStructures.dequeue!(queue) : break
4646
end
4747
end

src/resources/stores.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ end
1414
mutable struct Store{T} <: AbstractResource
1515
env :: Environment
1616
capacity :: UInt
17-
items :: Set{T}
17+
load :: UInt
18+
items :: Dict{T, UInt}
1819
seid :: UInt
1920
put_queue :: DataStructures.PriorityQueue{Put, StorePutKey{T}}
2021
get_queue :: DataStructures.PriorityQueue{Get, StoreGetKey}
2122
function Store{T}(env::Environment; capacity::UInt=typemax(UInt)) where {T}
22-
new(env, capacity, Set{T}(), zero(UInt), DataStructures.PriorityQueue{Put, StorePutKey{T}}(), DataStructures.PriorityQueue{Get, StoreGetKey}())
23+
new(env, capacity, zero(UInt), Dict{T, UInt}(), zero(UInt), DataStructures.PriorityQueue{Put, StorePutKey{T}}(), DataStructures.PriorityQueue{Get, StoreGetKey}())
2324
end
2425
end
2526

@@ -42,17 +43,23 @@ function get(sto::Store{T}, filter::Function=get_any_item; priority::Int=0) wher
4243
end
4344

4445
function do_put(sto::Store{T}, put_ev::Put, key::StorePutKey{T}) where {T}
45-
if length(sto.items) < sto.capacity
46-
push!(sto.items, key.item)
46+
if sto.load < sto.capacity
47+
sto.load += one(UInt)
48+
sto.items[key.item] = get!(sto.items, key.item, zero(UInt)) + one(UInt)
4749
schedule(put_ev)
4850
end
4951
false
5052
end
5153

5254
function do_get(sto::Store{T}, get_ev::Get, key::StoreGetKey) where {T}
53-
for item in sto.items
55+
for (item, number) in sto.items
5456
if key.filter(item)
55-
delete!(sto.items, item)
57+
sto.load -= one(UInt)
58+
if number === one(UInt)
59+
delete!(sto.items, item)
60+
else
61+
sto.items[item] = number - one(UInt)
62+
end
5663
schedule(get_ev; value=item)
5764
break
5865
end

src/simulations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct EventKey
1515
end
1616

1717
function isless(a::EventKey, b::EventKey) :: Bool
18-
(a.time < b.time) || (a.time == b.time && a.priority > b.priority) || (a.time == b.time && a.priority == b.priority && a.id < b.id)
18+
(a.time < b.time) || (a.time === b.time && a.priority > b.priority) || (a.time === b.time && a.priority === b.priority && a.id < b.id)
1919
end
2020

2121
mutable struct Simulation <: Environment

0 commit comments

Comments
 (0)