Skip to content

Commit

Permalink
initial changes to the way tags are stored
Browse files Browse the repository at this point in the history
  • Loading branch information
ba2tro committed Apr 15, 2024
1 parent d3a0822 commit ad650ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 12 additions & 3 deletions src/queries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ Assign a tag to a slot in a register.
It returns the list of all currently present tags for that register.
See also: [`query`](@ref), [`untag!`](@ref)"""
function tag!(ref::RegRef, tag::Tag)
push!(ref.reg.tags[ref.idx], tag)
function tag!(ref::RegRef, tag::Tag, time=nothing)
push!(ref.reg.tags, tag)
push!(ref.tag_idx, size(ref.reg.tags)[1])
push!(ref.tag_time, time)
end

tag!(ref, tag) = tag!(ref, Tag(tag))
Expand All @@ -23,7 +25,14 @@ See also: [`query`](@ref), [`tag!`](@ref)
function untag!(ref::RegRef, tag::Tag) # TODO rather slow implementation. See issue #74
tags = ref.reg.tags[ref.idx]
i = findfirst(==(tag), tags)
isnothing(i) ? throw(KeyError(tag)) : deleteat!(tags, i) # TODO make sure there is a clear error message
if isnothing(i)
throw(KeyError(tag)) # TODO make sure there is a clear error message
else
idx = findfirst(==(i), ref.tag_idx) # everytime a tag is untagged or `querydelete!` is called the `idx` is removed from ref.tag_idx, so we won't have any duplicates
deleteat!(tags, i)
deleteat!(ref.tag_idx, idx)
deleteat!(ref.tag_time, idx)
end
end


Expand Down
9 changes: 7 additions & 2 deletions src/states_registers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ struct Register # TODO better type description
stateindices::Vector{Int}
accesstimes::Vector{Float64} # TODO do not hardcode the type
locks::Vector{Any}
tags::Vector{Vector{Tag}} # TODO this is a rather inefficient way to store tags, but at least it provides a FIFO ordering; see issue #74
tags::Vector{Tag} # TODO this is a rather inefficient way to store tags, but at least it provides a FIFO ordering; see issue #74
end

function Register(traits, reprs, bg, sr, si, at)
env = ConcurrentSim.Simulation()
Register(traits, reprs, bg, sr, si, at, [ConcurrentSim.Resource(env) for _ in traits], [Vector{Tag}() for _ in traits])
Register(traits, reprs, bg, sr, si, at, [ConcurrentSim.Resource(env) for _ in traits], Vector{Tag}())
end
Register(traits,reprs,bg,sr,si) = Register(traits,reprs,bg,sr,si,zeros(length(traits)))
Register(traits,reprs,bg) = Register(traits,reprs,bg,fill(nothing,length(traits)),zeros(Int,length(traits)),zeros(length(traits)))
Expand All @@ -49,6 +49,11 @@ julia> r = Register(2)
struct RegRef
reg::Register
idx::Int
tag_idx::Vector{Int}
tag_time::Vector{Float64}
end

function RegRef(r::Register, idx::Int)
RegRef(r, idx, [], [])
end
#Base.:(==)(r1::Register, r2::Register) =

0 comments on commit ad650ed

Please sign in to comment.