diff --git a/base/hashing.jl b/base/hashing.jl index b75a5c9524016..b4bb9de5c1bda 100644 --- a/base/hashing.jl +++ b/base/hashing.jl @@ -3,6 +3,10 @@ hash(x::Any) = hash(x, zero(Uint)) hash(w::WeakRef, h::Uint) = hash(w.value, h) +## hashing general objects ## + +hash(x::ANY, h::Uint) = hash(object_id(x), h) + ## core data hashing functions ## function hash_64_64(n::Uint64) diff --git a/base/hashing2.jl b/base/hashing2.jl index 61e3468bd2c76..106d4e21b3d76 100644 --- a/base/hashing2.jl +++ b/base/hashing2.jl @@ -154,16 +154,6 @@ end hash(x::Float16, h::Uint) = hash(float64(x), h) -## hashing strings ## - -const memhash = Uint == Uint64 ? :memhash_seed : :memhash32_seed - -function hash{T<:ByteString}(s::Union(T,SubString{T}), h::Uint) - h += uint(0x71e729fd56419c81) - ccall(memhash, Uint, (Ptr{Uint8}, Csize_t, Uint32), s, sizeof(s), h) + h -end -hash(s::String, h::Uint) = hash(bytestring(s), h) - ## hashing collections ## function hash(a::AbstractArray, h::Uint) @@ -191,11 +181,6 @@ function hash(s::Set, h::Uint) return h end -hash(::(), h::Uint) = h + uint(0x77cfa1eef01bca90) -hash(x::(Any,), h::Uint) = hash(x[1], hash((), h)) -hash(x::(Any,Any), h::Uint) = hash(x[1], hash(x[2], hash((), h))) -hash(x::Tuple, h::Uint) = hash(x[1], hash(x[2], hash(tupletail(x), h))) - # hashing ranges by component at worst leads to collisions for very similar ranges function hash(r::Range, h::Uint) h += uint(0x80707b6821b70087) @@ -203,7 +188,3 @@ function hash(r::Range, h::Uint) h = hash(step(r), h) h = hash(last(r), h) end - -## hashing general objects ## - -hash(x::ANY, h::Uint) = hash(object_id(x), h) diff --git a/base/string.jl b/base/string.jl index 77b6838c27a6c..be974ef01681d 100644 --- a/base/string.jl +++ b/base/string.jl @@ -672,6 +672,16 @@ end isascii(s::SubString{ASCIIString}) = true +## hashing strings ## + +const memhash = Uint == Uint64 ? :memhash_seed : :memhash32_seed + +function hash{T<:ByteString}(s::Union(T,SubString{T}), h::Uint) + h += uint(0x71e729fd56419c81) + ccall(memhash, Uint, (Ptr{Uint8}, Csize_t, Uint32), s, sizeof(s), h) + h +end +hash(s::String, h::Uint) = hash(bytestring(s), h) + ## efficient representation of repeated strings ## immutable RepString <: String diff --git a/base/tuple.jl b/base/tuple.jl index 112030aa6cc0f..b21799a42a727 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -95,6 +95,11 @@ function ==(t1::Tuple, t2::Tuple) return true end +hash(::(), h::Uint) = h + uint(0x77cfa1eef01bca90) +hash(x::(Any,), h::Uint) = hash(x[1], hash((), h)) +hash(x::(Any,Any), h::Uint) = hash(x[1], hash(x[2], hash((), h))) +hash(x::Tuple, h::Uint) = hash(x[1], hash(x[2], hash(tupletail(x), h))) + function isless(t1::Tuple, t2::Tuple) n1, n2 = length(t1), length(t2) for i = 1:min(n1, n2)