Skip to content

Commit 787ac72

Browse files
authored
Merge pull request #629 from JuliaCollections/robindict_key_error
Correct the behavior of RobinDict for get, getkey, haskey, getindex
2 parents f5c2614 + 11a5b20 commit 787ac72

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DataStructures"
22
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
3-
version = "0.17.17"
3+
version = "0.17.18"
44

55
[deps]
66
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

src/robin_dict.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ function empty!(h::RobinDict{K,V}) where {K, V}
291291
return h
292292
end
293293

294-
function rh_search(h::RobinDict{K, V}, key::K) where {K, V}
294+
function rh_search(h::RobinDict{K, V}, key) where {K, V}
295295
sz = length(h.keys)
296296
chash = hash_key(key)
297297
index = desired_index(chash, sz)
@@ -367,8 +367,7 @@ function _get!(default::Callable, h::RobinDict{K,V}, key::K) where V where K
367367
return v
368368
end
369369

370-
function getindex(h::RobinDict{K, V}, key0) where {K, V}
371-
key = convert(K, key0)
370+
function getindex(h::RobinDict{K, V}, key) where {K, V}
372371
index = rh_search(h, key)
373372
@inbounds return (index < 0) ? throw(KeyError(key)) : h.vals[index]
374373
end
@@ -392,8 +391,7 @@ julia> get(d, "c", 3)
392391
"""
393392
get(collection, key, default)
394393

395-
function get(h::RobinDict{K,V}, key0, default) where {K, V}
396-
key = convert(K, key0)
394+
function get(h::RobinDict{K,V}, key, default) where {K, V}
397395
index = rh_search(h, key)
398396
@inbounds return (index < 0) ? default : h.vals[index]::V
399397
end
@@ -415,8 +413,7 @@ end
415413
"""
416414
get(::Function, collection, key)
417415

418-
function get(default::Callable, h::RobinDict{K,V}, key0) where {K, V}
419-
key = convert(K, key0)
416+
function get(default::Callable, h::RobinDict{K,V}, key) where {K, V}
420417
index = rh_search(h, key)
421418
@inbounds return (index < 0) ? default() : h.vals[index]::V
422419
end
@@ -462,8 +459,7 @@ julia> getkey(D, 'd', 'a')
462459
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
463460
```
464461
"""
465-
function getkey(h::RobinDict{K,V}, key0, default) where {K, V}
466-
key = convert(K, key0)
462+
function getkey(h::RobinDict{K,V}, key, default) where {K, V}
467463
index = rh_search(h, key)
468464
@inbounds return (index < 0) ? default : h.keys[index]::K
469465
end

test/test_robin_dict.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ end
107107
end
108108
@test get_KeyError
109109
end
110+
111+
h = RobinDict{Char, Int}()
112+
@test_throws KeyError h[0.01]
110113
end
111114

112115
@testset "Filter function" begin
@@ -214,7 +217,7 @@ end
214217
@test eq(RobinDict(), sizehint!(RobinDict(),96))
215218

216219
# Dictionaries of different types
217-
@test_throws MethodError eq(RobinDict(1 => 2), RobinDict("dog" => "bone"))
220+
@test !eq(RobinDict(1 => 2), RobinDict("dog" => "bone"))
218221
@test eq(RobinDict{Int,Int}(), RobinDict{AbstractString,AbstractString}())
219222
end
220223

@@ -310,14 +313,14 @@ end
310313
@test haskey(h, 1) == true
311314
@test haskey(h, 2) == true
312315
@test haskey(h, 3) == false
313-
@test_throws MethodError haskey(h, "1")
316+
@test haskey(h, "1") == false
314317
end
315318

316319
@testset "getkey" begin
317320
h = RobinDict(1=>2, 3 => 6, 5=>10)
318321
@test getkey(h, 1, 7) == 1
319322
@test getkey(h, 4, 6) == 6
320-
@test_throws MethodError getkey(h, "1", 8)
323+
@test getkey(h, "1", 8) == 8
321324
end
322325

323326
@testset "empty" begin

0 commit comments

Comments
 (0)