Skip to content

Commit

Permalink
rearrange hash methods. probably fixes #7000
Browse files Browse the repository at this point in the history
code for string Dicts was getting inferred before hash was defined for
strings. key properties like hash should be defined along with types.
  • Loading branch information
JeffBezanson committed May 28, 2014
1 parent 01552f6 commit 9040d0e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 4 additions & 0 deletions base/hashing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 0 additions & 19 deletions base/hashing2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -191,19 +181,10 @@ 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)
h = hash(first(r), h)
h = hash(step(r), h)
h = hash(last(r), h)
end

## hashing general objects ##

hash(x::ANY, h::Uint) = hash(object_id(x), h)
10 changes: 10 additions & 0 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9040d0e

Please sign in to comment.