Skip to content

Commit 1d401e4

Browse files
akzhanRX14
authored andcommittedOct 14, 2017
Optimized BitArray.hash(hasher) (#5101)
* Optimized BitArray.hash(hasher) * Add note about different hashes for same bit arrays after downsizing. Just like BitArray.==
1 parent bdf35a8 commit 1d401e4

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed
 

Diff for: ‎src/bit_array.cr

+9-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ struct BitArray
3535
def ==(other : BitArray)
3636
return false if size != other.size
3737
# NOTE: If BitArray implements resizing, there may be more than 1 binary
38-
# representation for equivalent BitArrays after a downsize as the discarded
39-
# bits may not have been zeroed.
38+
# representation and their hashes for equivalent BitArrays after a downsize as the
39+
# discarded bits may not have been zeroed.
4040
return LibC.memcmp(@bits, other.@bits, malloc_size) == 0
4141
end
4242

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

229+
# See `Object#hash(hasher)`
230+
def hash(hasher)
231+
hasher = size.hash(hasher)
232+
hasher = to_slice.hash(hasher)
233+
hasher
234+
end
235+
229236
private def bit_index_and_sub_index(index)
230237
bit_index_and_sub_index(index) { raise IndexError.new }
231238
end

0 commit comments

Comments
 (0)
Please sign in to comment.