Skip to content

Commit

Permalink
Optimized BitArray.hash(hasher) (#5101)
Browse files Browse the repository at this point in the history
* Optimized BitArray.hash(hasher)

* Add note about different hashes for same bit arrays after downsizing.
Just like BitArray.==
  • Loading branch information
akzhan authored and RX14 committed Oct 14, 2017
1 parent bdf35a8 commit 1d401e4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/bit_array.cr
Expand Up @@ -35,8 +35,8 @@ struct BitArray
def ==(other : BitArray)
return false if size != other.size
# NOTE: If BitArray implements resizing, there may be more than 1 binary
# representation for equivalent BitArrays after a downsize as the discarded
# bits may not have been zeroed.
# representation and their hashes for equivalent BitArrays after a downsize as the
# discarded bits may not have been zeroed.
return LibC.memcmp(@bits, other.@bits, malloc_size) == 0
end

Expand Down Expand Up @@ -226,6 +226,13 @@ struct BitArray
Slice.new(@bits.as(Pointer(UInt8)), (@size / 8.0).ceil.to_i)
end

# See `Object#hash(hasher)`
def hash(hasher)
hasher = size.hash(hasher)
hasher = to_slice.hash(hasher)
hasher
end

private def bit_index_and_sub_index(index)
bit_index_and_sub_index(index) { raise IndexError.new }
end
Expand Down

0 comments on commit 1d401e4

Please sign in to comment.