Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ tail(x::Cons) = x.tail
Base.:(==)(x::Nil, y::Nil) = true
Base.:(==)(x::Cons, y::Cons) = (x.head == y.head) && (x.tail == y.tail)

const _nil_hashseed = UInt === UInt64 ? 0x0f8c7d6e5b4a3928 : 0x0f8c7d6e
Base.hash(x::Nil, h::UInt) = h + _nil_hashseed

const _cons_hashseed = UInt === UInt64 ? 0x6b9d0f31e57c24a9 : 0x6b9d0f31
function Base.hash(x::Cons, h::UInt)
h += _cons_hashseed
for (i, v) in enumerate(x)
h += i * hash(v)
end
return h
end

function Base.show(io::IO, l::LinkedList{T}) where T
if isa(l,Nil)
if T === Any
Expand Down
3 changes: 3 additions & 0 deletions src/multi_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Base.sizehint!(d::MultiDict, sz::Integer) = (sizehint!(d.d, sz); d)
Base.copy(d::MultiDict) = MultiDict(d)
Base.empty(d::MultiDict{K,V}) where {K,V} = MultiDict{K,V}()
Base.:(==)(d1::MultiDict, d2::MultiDict) = d1.d == d2.d

const _multidict_hashseed = UInt === UInt64 ? 0x4e6f8d2a91bc53f7 : 0x4e6f8d2a
Base.hash(d::MultiDict, h::UInt) = hash(d.d, h + _multidict_hashseed)
Base.delete!(d::MultiDict, key) = (delete!(d.d, key); d)
Base.empty!(d::MultiDict) = (empty!(d.d); d)

Expand Down
9 changes: 9 additions & 0 deletions src/mutable_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ function Base.:(==)(l1::MutableLinkedList{T}, l2::MutableLinkedList{T}) where T
return true
end

const _mutablelinkedlist_hashseed = UInt === UInt64 ? 0x9d7b57c3132cb765 : 0x9d7b57c3
function Base.hash(l::MutableLinkedList, h::UInt)
h += _mutablelinkedlist_hashseed
for (i, v) in enumerate(l)
h += i * hash(v)
end
return h
end

function Base.map(f::Base.Callable, l::MutableLinkedList{T}) where T
if isempty(l) && f isa Function
S = Core.Compiler.return_type(f, Tuple{T})
Expand Down
9 changes: 9 additions & 0 deletions src/queue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,12 @@ Iterators.reverse(q::Queue) = Iterators.reverse(q.store)
Verify if queues `x` and `y` are equivalent in their contents.
"""
Base.:(==)(x::Queue, y::Queue) = x.store == y.store

const _queue_hashseed = UInt === UInt64 ? 0x24f5ac7735aaed19 : 0x24f5ac77
function Base.hash(x::Queue, h::UInt)
h += _queue_hashseed
for (i, v) in enumerate(x)
h += i * hash(v)
end
return h
end
10 changes: 10 additions & 0 deletions src/sorted_multi_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,16 @@ function Base.isequal(m1::SortedMultiDict{K, D, Ord},
end
end

const _sortedmultidict_hashseed = UInt === UInt64 ? 0xaf45d92c17e83b61 : 0xaf45d92c
function Base.hash(m::SortedMultiDict, h::UInt)
h += _sortedmultidict_hashseed
h += hash(orderobject(m))
for (i, v) in enumerate(m)
h += i * hash(v)
end
return h
end


function mergetwo!(m::SortedMultiDict{K,D,Ord}, iterable) where {K,D,Ord <: Ordering}
for (k,v) in iterable
Expand Down
9 changes: 9 additions & 0 deletions src/stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,12 @@ false
```
"""
Base.:(==)(x::Stack, y::Stack) = x.store == y.store

const _stack_hashseed = UInt === UInt64 ? 0x7c3f61a9e48390b7 : 0x61a9e483
function Base.hash(x::Stack, h::UInt)
h += _stack_hashseed
for (i, v) in enumerate(x)
h += i * hash(v)
end
return h
end
21 changes: 21 additions & 0 deletions test/test_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,25 @@
@test isa(l12, Cons{Float64})
end

@testset "hash" begin
a = list([1])
b = list([1])
@test isequal(a, b)
@test hash(a) == hash(b)
@test length(Set([a, b])) == 1

n1 = nil(Int)
n2 = nil(Float64)
@test isequal(n1, n2)
@test hash(n1) == hash(n2)
@test length(Set([n1, n2])) == 1

@test isequal(list(1), list(1.0))
@test hash(list(1)) == hash(list(1.0))

c = list(2)
@test !isequal(list(1), c)
@test hash(list(1)) != hash(c)
end

end # @testset LinkedList
18 changes: 18 additions & 0 deletions test/test_multi_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@
end
end

@testset "hash" begin
a = MultiDict(1 => 2)
b = MultiDict(1 => 2)
@test isequal(a, b)
@test hash(a) == hash(b)
@test length(Set([a, b])) == 1

c = MultiDict(1 => 2, 3 => 4)
d = MultiDict(3 => 4, 1 => 2)
@test isequal(c, d)
@test hash(c) == hash(d)
@test length(Set([c, d])) == 1

e = MultiDict(1 => 3)
@test !isequal(a, e)
@test hash(a) != hash(e)
end

@testset "special functions: count, enumerateall" begin
#not appending arrays to one array, using array of arrays
d = MultiDict{Char,Array{Int,1}}()
Expand Down
18 changes: 18 additions & 0 deletions test/test_mutable_list.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@
end
end

@testset "hash" begin
a = MutableLinkedList{Int}(1)
b = MutableLinkedList{Int}(1)
@test isequal(a, b)
@test hash(a) == hash(b)
@test length(Set([a, b])) == 1

a = MutableLinkedList{Int}()
b = MutableLinkedList{Int}()
@test isequal(a, b)
@test hash(a) == hash(b)
@test length(Set([a, b])) == 1

c = MutableLinkedList{Int}(2)
@test !isequal(MutableLinkedList{Int}(1), c)
@test hash(MutableLinkedList{Int}(1)) != hash(c)
end

@testset "random operations" begin
l = MutableLinkedList{Int}()
r = Int[]
Expand Down
19 changes: 19 additions & 0 deletions test/test_queue.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@
end
end

@testset "hash" begin
a = Queue{Int}()
b = Queue{Int}()
@test isequal(a, b)
@test hash(a) == hash(b)
@test length(Set([a, b])) == 1

push!(a, 1)
push!(b, 1)
@test isequal(a, b)
@test hash(a) == hash(b)
@test length(Set([a, b])) == 1

c = Queue{Int}()
push!(c, 2)
@test !isequal(a, c)
@test hash(a) != hash(c)
end

@testset "emptyness" begin
s = Queue{Int}()
push!(s, 1)
Expand Down
13 changes: 13 additions & 0 deletions test/test_sorted_containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,19 @@ function testSortedMultiDict()
checkcorrectness(m6.bt, true)
my_assert(isequal(m1,m6))

smdhash1 = SortedMultiDict(1=>2)
smdhash2 = SortedMultiDict(1=>2)
my_assert(isequal(smdhash1, smdhash2))
my_assert(hash(smdhash1) == hash(smdhash2))
my_assert(length(Set([smdhash1, smdhash2])) == 1)
my_assert(hash(m1) == hash(m2))
my_assert(length(Set([m1, m2])) == 1)
smdhash3 = SortedMultiDict(1=>3)
my_assert(!isequal(smdhash1, smdhash3))
my_assert(hash(smdhash1) != hash(smdhash3))
my_assert(!isequal(m1, m3))
my_assert(hash(m1) != hash(m3))

m1 = SortedMultiDict(zip(["bananas", "apples", "cherries", "cherries", "oranges"],
[1.0, 2.0, 3.0, 4.0, 5.0]))
my_assert(typeof(m1) == SortedMultiDict{String, Float64, ForwardOrdering})
Expand Down
19 changes: 19 additions & 0 deletions test/test_stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@
end
end

@testset "hash" begin
a = Stack{Int}()
b = Stack{Int}()
@test isequal(a, b)
@test hash(a) == hash(b)
@test length(Set([a, b])) == 1

push!(a, 1)
push!(b, 1)
@test isequal(a, b)
@test hash(a) == hash(b)
@test length(Set([a, b])) == 1

c = Stack{Int}()
push!(c, 2)
@test !isequal(a, c)
@test hash(a) != hash(c)
end

@testset "empty!" begin
s = Stack{Int}(1)
push!(s, 10)
Expand Down
Loading