Class: Xor::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/xor.rb

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Filter

Returns a new instance of Filter.



5
6
7
8
# File 'lib/xor.rb', line 5

def initialize(size)
  @size = size
  @hashes = Array.new(size, 0)
end

Instance Method Details

#add(value) ⇒ Object



10
11
12
13
# File 'lib/xor.rb', line 10

def add(value)
  hash1, hash2 = hash(value)
  @hashes[hash1 % @size] ^= hash2
end

#include?(value) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/xor.rb', line 15

def include?(value)
  hash1, hash2 = hash(value)
  (@hashes[hash1 % @size] ^ hash2).zero?
end