Skip to content

Commit a8703aa

Browse files
committed
add deleteat! for OrderedDict
1 parent c3687a8 commit a8703aa

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/OrderedCollections.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module OrderedCollections
1515
searchsortedfirst, searchsortedlast, in,
1616
filter, filter!, ValueIterator, eachindex, keytype,
1717
valtype, lastindex, nextind,
18-
copymutable, emptymutable, dict_with_eltype
18+
copymutable, emptymutable, dict_with_eltype,
19+
deleteat!
1920

2021
export OrderedDict, OrderedSet, LittleDict
2122
export freeze

src/ordered_dict.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,33 @@ function delete!(h::OrderedDict, key)
449449
return h
450450
end
451451

452+
function deleteat!(od::OrderedDict, i::Integer, rehash=true)
453+
key = od.keys[i]
454+
index = OrderedCollections.ht_keyindex(od, key, false)
455+
@inbounds ki = od.slots[index]
456+
@inbounds od.slots[index] = -ki
457+
ccall(:jl_arrayunset, Cvoid, (Any, UInt), od.keys, ki-1)
458+
ccall(:jl_arrayunset, Cvoid, (Any, UInt), od.vals, ki-1)
459+
od.ndel += 1
460+
od.dirty = true
461+
rehash && OrderedCollections.rehash!(od)
462+
end
463+
464+
function deleteat!(od::OrderedDict,
465+
inds::Union{AbstractUnitRange{<:Integer}, AbstractVector{<:Integer}})
466+
for i in inds
467+
deleteat!(od, i, false)
468+
end
469+
OrderedCollections.rehash!(od)
470+
end
471+
472+
function deleteat!(od::OrderedDict, inds::AbstractVector{<:Bool})
473+
for (i,b) in enumerate(inds)
474+
b && deleteat!(od, i, false)
475+
end
476+
OrderedCollections.rehash!(od)
477+
end
478+
452479
function iterate(t::OrderedDict)
453480
t.ndel > 0 && rehash!(t)
454481
length(t.keys) < 1 && return nothing

0 commit comments

Comments
 (0)