Skip to content

Commit

Permalink
fix: check key values, not get return for haskey(k, orderedrobindic…
Browse files Browse the repository at this point in the history
…t) (JuliaCollections#858)

* fix: check key values, not `get` return for haskey(k, orderedrobindict)

* Fix: use isequal in setindex!

* re-fix: use `get` for `in keys()` but descend to the actual id-dict

* Remove accidental paste in docstring

* consolidate tests for issue under testset

---------

Co-authored-by: Micah Rufsvold <mjrufsvold@mathematica-mpr.com>
  • Loading branch information
mrufsvold and Micah Rufsvold committed Jun 29, 2023
1 parent d79c5d5 commit e62f469
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/ordered_robin_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function Base.setindex!(h::OrderedRobinDict{K, V}, v0, key0) where {K,V}
else
@assert haskey(h, key0)
@inbounds orig_v = h.vals[index]
(orig_v != v0) && (@inbounds h.vals[index] = v0)
!isequal(orig_v, v0) && (@inbounds h.vals[index] = v0)
end

check_for_rehash(h) && rehash!(h)
Expand Down Expand Up @@ -305,8 +305,8 @@ julia> haskey(D, 'c')
false
```
"""
Base.haskey(h::OrderedRobinDict, key) = (get(h.dict, key, -2) > 0)
Base.in(key, v::Base.KeySet{K,T}) where {K,T<:OrderedRobinDict{K}} = (get(v.dict, key, -1) >= 0)
Base.haskey(h::OrderedRobinDict, key) = (get(h.dict, key, -1) > 0)
Base.in(key, v::Base.KeySet{K,T}) where {K,T<:OrderedRobinDict{K}} = (get(v.dict.dict, key, -1) >= 0)

"""
getkey(collection, key, default)
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import DataStructures: IntSet
@test [] == detect_ambiguities(Core, DataStructures)
@test [] == detect_ambiguities(Base, DataStructures)

tests = ["deprecations",
tests = [
"deprecations",
"int_set",
"sparse_int_set",
"deque",
Expand Down
7 changes: 7 additions & 0 deletions test/test_ordered_robin_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
@test od60[14] == 15
end

@testset "Fixes issue 857" begin
h = OrderedRobinDict{Any,Any}([("a", missing), ("b", -2)])
@test 5 == (h["a"] = 5)
@test "b" in keys(h)
@test haskey(h,"b")
end


# #############################
# Copied and modified from Base/test/dict.jl
Expand Down

0 comments on commit e62f469

Please sign in to comment.