Class: FasterBitField

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

Constant Summary collapse

ELEMENT_WIDTH =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, field = nil) ⇒ FasterBitField

Returns a new instance of FasterBitField.



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

def initialize(size, field = nil)
  @size = size
  @field = field || "\000" * (((size - 1) / ELEMENT_WIDTH) + 1)
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#[](position) ⇒ Object

Read a bit (1/0)



21
22
23
24
# File 'lib/bloomfilter/bloomfilter.rb', line 21

def [](position)
  get_c(@field, position)
  #@field[position / ELEMENT_WIDTH] & (1 << (position % ELEMENT_WIDTH)) > 0
end

#set(position) ⇒ Object



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

def set(position)
  set_c(@field, position)
  #@field[position / ELEMENT_WIDTH] |= (1 << (position % ELEMENT_WIDTH))
end

#to_sObject



45
46
47
# File 'lib/bloomfilter/bloomfilter.rb', line 45

def to_s
  @field
end