Skip to content

Commit

Permalink
move functionality related to push! for VarNameVector into push!
Browse files Browse the repository at this point in the history
  • Loading branch information
torfjelde committed Nov 14, 2023
1 parent 8d05586 commit 7801fe1
Showing 1 changed file with 44 additions and 41 deletions.
85 changes: 44 additions & 41 deletions src/varnamevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Base.length(vnv::VarNameVector) =
end
Base.size(vnv::VarNameVector) = (length(vnv),)

Check warning on line 122 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L122

Added line #L122 was not covered by tests

# TODO: We should probably remove this
Base.IndexStyle(::Type{<:VarNameVector}) = IndexLinear()

Check warning on line 125 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L125

Added line #L125 was not covered by tests

# Dictionary interface.
Expand Down Expand Up @@ -229,59 +230,61 @@ end
function Base.push!(vnv::VarNameVector, vn::VarName, val, transform=FromVec(val))

Check warning on line 230 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L230

Added line #L230 was not covered by tests
# Error if we already have the variable.
haskey(vnv, vn) && throw(ArgumentError("variable name $vn already exists"))

Check warning on line 232 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L232

Added line #L232 was not covered by tests
return update!(vnv, vn, val, transform)
# NOTE: We need to compute the `nextrange` BEFORE we start mutating
# the underlying; otherwise we might get some strange behaviors.
val_vec = tovec(val)
r_new = nextrange(vnv, val_vec)
vnv.varname_to_index[vn] = length(vnv.varname_to_index) + 1
push!(vnv.varnames, vn)
push!(vnv.ranges, r_new)
append!(vnv.vals, val_vec)
push!(vnv.transforms, transform)
return nothing

Check warning on line 242 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L235-L242

Added lines #L235 - L242 were not covered by tests
end

# `update!` and `update!!`: update a variable in the varname vector.
function update!(vnv::VarNameVector, vn::VarName, val, transform=FromVec(val))
val_vec = tovec(val)
if !haskey(vnv, vn)

Check warning on line 247 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L246-L247

Added lines #L246 - L247 were not covered by tests
# Here we just add a new entry.
# NOTE: We need to compute the `nextrange` BEFORE we start mutating
# the underlying; otherwise we might get some strange behaviors.
return push!(vnv, vn, val, transform)

Check warning on line 249 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L249

Added line #L249 was not covered by tests
end

# Here we update an existing entry.
val_vec = tovec(val)
idx = getidx(vnv, vn)
r_old = getrange(vnv, idx)
n_old = length(r_old)
n_new = length(val_vec)

Check warning on line 257 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L253-L257

Added lines #L253 - L257 were not covered by tests
# Existing keys needs to be handled differently depending on
# whether the size of the value is increasing or decreasing.
if n_new > n_old

Check warning on line 260 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L260

Added line #L260 was not covered by tests
# Add the new range.
r_new = nextrange(vnv, val_vec)
vnv.varname_to_index[vn] = length(vnv.varname_to_index) + 1
push!(vnv.varnames, vn)
push!(vnv.ranges, r_new)
append!(vnv.vals, val_vec)
push!(vnv.transforms, transform)
vnv.ranges[idx] = r_new

Check warning on line 263 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L262-L263

Added lines #L262 - L263 were not covered by tests
# Grow the underlying vector to accomodate the new value.
resize!(vnv.vals, r_new[end])

Check warning on line 265 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L265

Added line #L265 was not covered by tests
# Keep track of the deleted ranges.
push!(vnv.inactive_ranges, r_old)

Check warning on line 267 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L267

Added line #L267 was not covered by tests
else
# Here we update the existing entry.
idx = getidx(vnv, vn)
r_old = getrange(vnv, idx)
n_old = length(r_old)
n_new = length(val_vec)
# Existing keys needs to be handled differently depending on
# whether the size of the value is increasing or decreasing.
if n_new > n_old
# Add the new range.
r_new = nextrange(vnv, val_vec)
vnv.ranges[idx] = r_new
# Grow the underlying vector to accomodate the new value.
resize!(vnv.vals, r_new[end])
# Keep track of the deleted ranges.
push!(vnv.inactive_ranges, r_old)
else
# `n_new <= n_old`
# Just decrease the current range.
r_new = r_old[1]:(r_old[1] + n_new - 1)
vnv.ranges[idx] = r_new
# And mark the rest as inactive if needed.
if n_new < n_old
push!(vnv.inactive_ranges, r_old[n_new]:r_old[end])
end
# `n_new <= n_old`
# Just decrease the current range.
r_new = r_old[1]:(r_old[1]+n_new-1)
vnv.ranges[idx] = r_new

Check warning on line 272 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L271-L272

Added lines #L271 - L272 were not covered by tests
# And mark the rest as inactive if needed.
if n_new < n_old
push!(vnv.inactive_ranges, r_old[n_new]:r_old[end])

Check warning on line 275 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L274-L275

Added lines #L274 - L275 were not covered by tests
end
end

# Update the value.
vnv.vals[r_new] = val_vec
# Update the transform.
vnv.transforms[idx] = transform
# Update the value.
vnv.vals[r_new] = val_vec

Check warning on line 280 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L280

Added line #L280 was not covered by tests
# Update the transform.
vnv.transforms[idx] = transform

Check warning on line 282 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L282

Added line #L282 was not covered by tests

# TODO: Should we maybe sweep over inactive ranges and re-contiguify
# if we the total number of inactive elements is "large" in some sense?
end
# TODO: Should we maybe sweep over inactive ranges and re-contiguify
# if we the total number of inactive elements is "large" in some sense?

return vnv
return nothing

Check warning on line 287 in src/varnamevector.jl

View check run for this annotation

Codecov / codecov/patch

src/varnamevector.jl#L287

Added line #L287 was not covered by tests
end

function recontiguify_ranges!(ranges::AbstractVector{<:AbstractRange})
Expand Down

0 comments on commit 7801fe1

Please sign in to comment.