Skip to content

Commit e3c9c56

Browse files
authored
Merge pull request #653 from eulerkochy/robin-fix-1
Remove import statement from robin_dict.jl
2 parents aca8a5e + c6d57cf commit e3c9c56

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

src/DataStructures.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ module DataStructures
1212
isless, union, intersect, symdiff, setdiff, issubset,
1313
searchsortedfirst, searchsortedlast, in,
1414
eachindex, keytype, valtype, minimum, maximum, size,
15-
zero, checkbounds
15+
zero, checkbounds, filter!, isbitstype, isbitsunion,
16+
isiterable, dict_with_eltype, KeySet, Callable, _tablesz
1617

1718

1819
using Compat # Provides Base.Order.ReverseOrdering(). May remove this line with julia 1.4

src/robin_dict.jl

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import Base: setindex!, sizehint!, empty!, isempty, length, copy, empty,
2-
getindex, getkey, haskey, iterate, @propagate_inbounds, merge,
3-
pop!, delete!, get, get!, isbitstype, in, hashindex, isbitsunion,
4-
isiterable, dict_with_eltype, KeySet, Callable, _tablesz, filter!
5-
61
# the load factor after which the dictionary `rehash` happens
72
const ROBIN_DICT_LOAD_FACTOR = 0.70
83

@@ -237,8 +232,8 @@ function sizehint!(d::RobinDict, newsz)
237232
rehash!(d, newsz)
238233
end
239234

240-
@propagate_inbounds isslotfilled(h::RobinDict, index) = (h.hashes[index] != 0)
241-
@propagate_inbounds isslotempty(h::RobinDict, index) = (h.hashes[index] == 0)
235+
Base.@propagate_inbounds isslotfilled(h::RobinDict, index) = (h.hashes[index] != 0)
236+
Base.@propagate_inbounds isslotempty(h::RobinDict, index) = (h.hashes[index] == 0)
242237

243238

244239
function setindex!(h::RobinDict{K,V}, v0, key0) where {K, V}
@@ -579,15 +574,6 @@ function delete!(h::RobinDict{K, V}, key0) where {K, V}
579574
return h
580575
end
581576

582-
function get_idxfloor(h::RobinDict)
583-
@inbounds for i = 1:length(h.keys)
584-
if isslotfilled(h, i)
585-
return i
586-
end
587-
end
588-
return 0
589-
end
590-
591577
function get_next_filled(h::RobinDict, i)
592578
L = length(h.keys)
593579
(1 <= i <= L) || return 0
@@ -599,11 +585,11 @@ function get_next_filled(h::RobinDict, i)
599585
return 0
600586
end
601587

602-
@propagate_inbounds _iterate(t::RobinDict{K,V}, i) where {K,V} = i == 0 ? nothing : (Pair{K,V}(t.keys[i],t.vals[i]), i == typemax(Int) ? 0 : get_next_filled(t, i+1))
603-
@propagate_inbounds function iterate(t::RobinDict)
588+
Base.@propagate_inbounds _iterate(t::RobinDict{K,V}, i) where {K,V} = i == 0 ? nothing : (Pair{K,V}(t.keys[i],t.vals[i]), i == typemax(Int) ? 0 : get_next_filled(t, i+1))
589+
Base.@propagate_inbounds function iterate(t::RobinDict)
604590
_iterate(t, t.idxfloor)
605591
end
606-
@propagate_inbounds iterate(t::RobinDict, i) = _iterate(t, get_next_filled(t, i))
592+
Base.@propagate_inbounds iterate(t::RobinDict, i) = _iterate(t, get_next_filled(t, i))
607593

608594
filter!(f, d::RobinDict) = Base.filter_in_one_pass!(f, d)
609595

test/test_robin_dict.jl

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
include("../src/robin_dict.jl")
2-
31
@testset "Constructors" begin
42
h1 = RobinDict()
53
@test length(h1) == 0
@@ -349,19 +347,6 @@ end
349347
@test length(h) == 0
350348
end
351349

352-
@testset "get_idxfloor" begin
353-
h = RobinDict()
354-
@test get_idxfloor(h) == 0
355-
356-
h["a"] = 1
357-
h[2] = "b"
358-
@test h.idxfloor == get_idxfloor(h)
359-
pop!(h)
360-
@test h.idxfloor == get_idxfloor(h)
361-
pop!(h)
362-
@test h.idxfloor == get_idxfloor(h) == 0
363-
end
364-
365350
@testset "merge" begin
366351
h1 = RobinDict("a" => 1, "b" => 2)
367352
h2 = RobinDict("c" => 3, "d" => 4)
@@ -385,6 +370,28 @@ end
385370
end
386371

387372
@testset "invariants" begin
373+
# Functions which are not exported, but are required for checking invariants
374+
hash_key(key) = (hash(key)%UInt32) | 0x80000000
375+
desired_index(hash, sz) = (hash & (sz - 1)) + 1
376+
isslotfilled(h::RobinDict, index) = (h.hashes[index] != 0)
377+
isslotempty(h::RobinDict, index) = (h.hashes[index] == 0)
378+
379+
function calculate_distance(h::RobinDict{K, V}, index) where {K, V}
380+
@assert isslotfilled(h, index)
381+
sz = length(h.keys)
382+
@inbounds index_init = desired_index(h.hashes[index], sz)
383+
return (index - index_init + sz) & (sz - 1)
384+
end
385+
386+
function get_idxfloor(h::RobinDict)
387+
@inbounds for i = 1:length(h.keys)
388+
if isslotfilled(h, i)
389+
return i
390+
end
391+
end
392+
return 0
393+
end
394+
388395
h1 = RobinDict{Int, Int}()
389396
for i in 1:300
390397
h1[i] = i
@@ -452,4 +459,17 @@ end
452459
h[i] = i+1
453460
end
454461
check_invariants(h)
462+
463+
@testset "get_idxfloor" begin
464+
h = RobinDict()
465+
@test get_idxfloor(h) == 0
466+
467+
h["a"] = 1
468+
h[2] = "b"
469+
@test h.idxfloor == get_idxfloor(h)
470+
pop!(h)
471+
@test h.idxfloor == get_idxfloor(h)
472+
pop!(h)
473+
@test h.idxfloor == get_idxfloor(h) == 0
474+
end
455475
end

0 commit comments

Comments
 (0)