Skip to content
Permalink
Browse files
Optimized BitArray.hash(hasher) (#5101)
* 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 1d401e4ad352571e695ba9a0d532d6dd97511275
Showing 1 changed file with 9 additions and 2 deletions.
@@ -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

@@ -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

0 comments on commit 1d401e4

Please sign in to comment.