Class: FasterBitField
- Inherits:
-
Object
- Object
- FasterBitField
- Defined in:
- lib/bloomfilter/bloomfilter.rb
Constant Summary collapse
- ELEMENT_WIDTH =
8
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#[](position) ⇒ Object
Read a bit (1/0).
-
#initialize(size, field = nil) ⇒ FasterBitField
constructor
A new instance of FasterBitField.
- #set(position) ⇒ Object
- #to_s ⇒ Object
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
#size ⇒ Object (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_s ⇒ Object
45 46 47 |
# File 'lib/bloomfilter/bloomfilter.rb', line 45 def to_s @field end |