Skip to content

Commit

Permalink
Update to Crystal 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
willhbr committed Aug 25, 2023
1 parent da0d148 commit 0a1b20c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: bloom_filter
version: 0.1.0

crystal: 1.9.2

authors:
- Potapov Sergey <[email protected]>

Expand Down
35 changes: 19 additions & 16 deletions src/bloom_filter/filter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ module BloomFilter
class Filter
@bitsize : UInt32

getter :hash_num, :bitsize, :bytesize, :bitmap
getter hash_num : UInt8
getter bitsize : UInt32
getter bitmap : Array(UInt8)
getter bytesize : UInt32

SEED_A = 0xdeadbeef_u32
SEED_B = 0x71fefeed_u32

MULT_A = 0xb8b34b2d_u32
MULT_B = 0x52c6a2d9_u32

def initialize(@bytesize, hash_num, @bitmap = Array(UInt8).new(bytesize, 0_u8))
@bitsize = bytesize * 8
def initialize(@bytesize, hash_num, @bitmap = Array(UInt8).new(bytesize.to_i32, 0_u8))
@bitsize = (bytesize * 8).to_u32
@hash_num = hash_num.to_u8
end

Expand All @@ -27,7 +30,7 @@ module BloomFilter
@bytesize += 1
end

@bitsize = bytesize * 8
@bitsize = @bytesize.to_u32 * 8
end

def insert(str : String)
Expand Down Expand Up @@ -57,41 +60,41 @@ module BloomFilter
end

def ==(another : Filter)
@bytesize == another.bytesize && @hash_num == another.hash_num && @bitmap == another.bitmap
self.bytesize == another.bytesize && @hash_num == another.hash_num && @bitmap == another.bitmap
end

# Get a union of two filters.
def |(another : Filter) : Filter
raise(ArgumentError.new("Cannot unite filters of different size")) unless another.bytesize == @bytesize
raise(ArgumentError.new("Cannot unite filters of different size")) unless another.bytesize == self.bytesize
raise(ArgumentError.new("Cannot unite filters with different number of hash functions")) unless another.hash_num == @hash_num

union_bitmap = Array(UInt8).new(bytesize.to_i) do |index|
union_bitmap = Array(UInt8).new(bytesize) do |index|
@bitmap[index] | another.bitmap[index]
end
Filter.new(@bytesize, @hash_num, union_bitmap)
Filter.new(self.bytesize, @hash_num, union_bitmap)
end

# Get intersection of two filters.
def &(another : Filter) : Filter
raise(ArgumentError.new("Cannot unite filters of different size")) unless another.bytesize == @bytesize
raise(ArgumentError.new("Cannot unite filters of different size")) unless another.bytesize == self.bytesize
raise(ArgumentError.new("Cannot unite filters with different number of hash functions")) unless another.hash_num == @hash_num

intersection_bitmap = Array(UInt8).new(bytesize.to_i) do |index|
intersection_bitmap = Array(UInt8).new(bytesize) do |index|
@bitmap[index] & another.bitmap[index]
end
Filter.new(@bytesize, @hash_num, intersection_bitmap)
Filter.new(self.bytesize, @hash_num, intersection_bitmap)
end

@[AlwaysInline]
private def set(index : UInt32)
item_index = index / 8
item_index = index // 8
bit_index = index % 8
@bitmap[item_index] = @bitmap[item_index] | (1 << bit_index)
end

@[AlwaysInline]
private def set?(index : UInt32) : Bool
item_index = index / 8
item_index = index // 8
bit_index = index % 8
@bitmap[item_index] & (1 << bit_index) != 0
end
Expand Down Expand Up @@ -136,11 +139,11 @@ module BloomFilter
ha = SEED_A
hb = SEED_B
u = str.to_unsafe
(str.bytesize / 4).times do
(str.bytesize // 4).times do
v = 0_u32
4.times { |i| v |= u[i].to_u32 << (i*8) }
ha = hswap(ha ^ v) * MULT_A
hb = (hswap(hb) ^ v) * MULT_B
ha = hswap(ha ^ v) &* MULT_A
hb = (hswap(hb) ^ v) &* MULT_B
u += 4
end
v = 0_u32
Expand Down

0 comments on commit 0a1b20c

Please sign in to comment.