Skip to content

Commit

Permalink
add deleteat! for OrderedDict
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed Jul 28, 2023
1 parent c3687a8 commit a8703aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/OrderedCollections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module OrderedCollections
searchsortedfirst, searchsortedlast, in,
filter, filter!, ValueIterator, eachindex, keytype,
valtype, lastindex, nextind,
copymutable, emptymutable, dict_with_eltype
copymutable, emptymutable, dict_with_eltype,
deleteat!

export OrderedDict, OrderedSet, LittleDict
export freeze
Expand Down
27 changes: 27 additions & 0 deletions src/ordered_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,33 @@ function delete!(h::OrderedDict, key)
return h
end

function deleteat!(od::OrderedDict, i::Integer, rehash=true)
key = od.keys[i]
index = OrderedCollections.ht_keyindex(od, key, false)
@inbounds ki = od.slots[index]
@inbounds od.slots[index] = -ki
ccall(:jl_arrayunset, Cvoid, (Any, UInt), od.keys, ki-1)
ccall(:jl_arrayunset, Cvoid, (Any, UInt), od.vals, ki-1)
od.ndel += 1
od.dirty = true
rehash && OrderedCollections.rehash!(od)
end

function deleteat!(od::OrderedDict,
inds::Union{AbstractUnitRange{<:Integer}, AbstractVector{<:Integer}})
for i in inds
deleteat!(od, i, false)
end
OrderedCollections.rehash!(od)
end

function deleteat!(od::OrderedDict, inds::AbstractVector{<:Bool})
for (i,b) in enumerate(inds)
b && deleteat!(od, i, false)
end
OrderedCollections.rehash!(od)
end

function iterate(t::OrderedDict)
t.ndel > 0 && rehash!(t)
length(t.keys) < 1 && return nothing
Expand Down

0 comments on commit a8703aa

Please sign in to comment.