Skip to content

Commit

Permalink
remove type signatures from kwargs in resources
Browse files Browse the repository at this point in the history
  • Loading branch information
hdavid16 committed Aug 3, 2023
1 parent 54238ec commit 136aecf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
24 changes: 12 additions & 12 deletions src/resources/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ mutable struct Container{N<:Real, T<:Number} <: AbstractResource
seid :: UInt
put_queue :: DataStructures.PriorityQueue{Put, ContainerKey{N, T}}
get_queue :: DataStructures.PriorityQueue{Get, ContainerKey{N, T}}
function Container{N, T}(env::Environment, capacity::N=one(N); level::N=zero(N)) where {N<:Real, T<:Number}
new(env, capacity, level, zero(UInt), DataStructures.PriorityQueue{Put, ContainerKey{N, T}}(), DataStructures.PriorityQueue{Get, ContainerKey{N, T}}())
function Container{N, T}(env::Environment, capacity::N=one(N); level=zero(N)) where {N<:Real, T<:Number}
new(env, capacity, N(level), zero(UInt), DataStructures.PriorityQueue{Put, ContainerKey{N, T}}(), DataStructures.PriorityQueue{Get, ContainerKey{N, T}}())
end
end

function Container(env::Environment, capacity::N=one(N); level::N=zero(N)) where {N<:Real}
Container{N, Int}(env, capacity, level=level)
function Container(env::Environment, capacity::N=one(N); level=zero(N)) where {N<:Real}
Container{N, Int}(env, capacity; level=N(level))
end

function Container{T}(env::Environment, capacity::N=one(N); level::N=zero(N)) where {N<:Real, T<:Number}
Container{N, T}(env, capacity, level=level)
function Container{T}(env::Environment, capacity::N=one(N); level=zero(N)) where {N<:Real, T<:Number}
Container{N, T}(env, capacity; level=N(level))
end

const Resource = Container{Int, Int}

function put!(con::Container{N, T}, amount::N; priority::U=zero(T)) where {N<:Real, T<:Number, U<:Number}
function put!(con::Container{N, T}, amount::N; priority=zero(T)) where {N<:Real, T<:Number}
put_ev = Put(con.env)
con.put_queue[put_ev] = ContainerKey{N,T}(con.seid+=one(UInt), amount, convert(T,priority))
con.put_queue[put_ev] = ContainerKey{N,T}(con.seid+=one(UInt), amount, T(priority))
@callback trigger_get(put_ev, con)
trigger_put(put_ev, con)
put_ev
Expand All @@ -56,7 +56,7 @@ Locks the Container (or Resources) and return the lock event.
If the capacity of the Container is greater than 1,
multiple requests can be made before blocking occurs.
"""
request(res::Resource; priority::Number=0) = put!(res, 1; priority=priority)
request(res::Resource; priority=0) = put!(res, 1; priority)

"""
tryrequest(res::Container)
Expand All @@ -80,14 +80,14 @@ julia> tryrequest(res)
false
```
"""
function tryrequest(res::Container; priority::Int=0)
function tryrequest(res::Container; priority=0)
islocked(res) && return false # TODO check priority
request(res; priority)
end

function get(con::Container{N, T}, amount::N; priority::U=zero(T)) where {N<:Real, T<:Number, U<:Number}
function get(con::Container{N, T}, amount::N; priority=zero(T)) where {N<:Real, T<:Number}
get_ev = Get(con.env)
con.get_queue[get_ev] = ContainerKey(con.seid+=one(UInt), amount, convert(T,priority))
con.get_queue[get_ev] = ContainerKey(con.seid+=one(UInt), amount, T(priority))
@callback trigger_put(get_ev, con)
trigger_get(get_ev, con)
get_ev
Expand Down
18 changes: 9 additions & 9 deletions src/resources/stores.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,33 @@ mutable struct Store{N, T<:Number} <: AbstractResource
seid :: UInt
put_queue :: DataStructures.PriorityQueue{Put, StorePutKey{N, T}}
get_queue :: DataStructures.PriorityQueue{Get, StoreGetKey{T}}
function Store{N, T}(env::Environment; capacity::UInt=typemax(UInt)) where {N, T<:Number}
new(env, capacity, zero(UInt), Dict{N, UInt}(), zero(UInt), DataStructures.PriorityQueue{Put, StorePutKey{N, T}}(), DataStructures.PriorityQueue{Get, StoreGetKey{T}}())
function Store{N, T}(env::Environment; capacity=typemax(UInt)) where {N, T<:Number}
new(env, UInt(capacity), zero(UInt), Dict{N, UInt}(), zero(UInt), DataStructures.PriorityQueue{Put, StorePutKey{N, T}}(), DataStructures.PriorityQueue{Get, StoreGetKey{T}}())
end
end

function Store{N}(env::Environment; capacity::UInt=typemax(UInt)) where {N}
Store{N, Int}(env; capacity)
function Store{N}(env::Environment; capacity=typemax(UInt)) where {N}
Store{N, Int}(env; capacity=UInt(capacity))
end

"""
put!(sto::Store, item::T)
Put an item into the store. Returns the put event, blocking if the store is full.
"""
function put!(sto::Store{N, T}, item::N; priority::U=zero(T)) where {N, T<:Number, U<:Number}
function put!(sto::Store{N, T}, item::N; priority=zero(T)) where {N, T<:Number}
put_ev = Put(sto.env)
sto.put_queue[put_ev] = StorePutKey{N, T}(sto.seid+=one(UInt), item, convert(T,priority))
sto.put_queue[put_ev] = StorePutKey{N, T}(sto.seid+=one(UInt), item, T(priority))
@callback trigger_get(put_ev, sto)
trigger_put(put_ev, sto)
put_ev
end

get_any_item(::N) where N = true

function get(sto::Store{N, T}, filter::Function=get_any_item; priority::U=zero(T)) where {N, T<:Number, U<:Number}
function get(sto::Store{N, T}, filter::Function=get_any_item; priority=zero(T)) where {N, T<:Number}
get_ev = Get(sto.env)
sto.get_queue[get_ev] = StoreGetKey(sto.seid+=one(UInt), filter, convert(T,priority))
sto.get_queue[get_ev] = StoreGetKey(sto.seid+=one(UInt), filter, T(priority))
@callback trigger_put(get_ev, sto)
trigger_get(get_ev, sto)
get_ev
Expand Down Expand Up @@ -129,4 +129,4 @@ tryrequest(::Store) = error("There is no well defined way to \"request\" a Store
An alias for `get(::Store)` for easier interoperability with the `Base.Channel` interface. Blocks if the store is empty.
"""
take!(sto::Store, filter::Function=get_any_item; priority::Int=0) = get(sto, filter; priority)
take!(sto::Store, filter::Function=get_any_item; priority=0) = get(sto, filter; priority)

0 comments on commit 136aecf

Please sign in to comment.