Class: ExternalBitField
- Inherits:
-
Object
- Object
- ExternalBitField
- Defined in:
- lib/bloomfilter/external_bloom_filter.rb
Constant Summary collapse
- ELEMENT_WIDTH =
8
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](position) ⇒ Object
Read a bit (1/0).
-
#initialize(path) ⇒ ExternalBitField
constructor
A new instance of ExternalBitField.
- #set(position) ⇒ Object
Constructor Details
#initialize(path) ⇒ ExternalBitField
Returns a new instance of ExternalBitField.
15 16 17 18 |
# File 'lib/bloomfilter/external_bloom_filter.rb', line 15 def initialize(path) @size = File.size(path) * ELEMENT_WIDTH @field = File.new(path, "r+") end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
4 5 6 |
# File 'lib/bloomfilter/external_bloom_filter.rb', line 4 def size @size end |
Class Method Details
.create(path, size) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/bloomfilter/external_bloom_filter.rb', line 8 def self.create(path, size) File.open(path, "w") do |file| file.write "\000" * (((size - 1) / ELEMENT_WIDTH) + 1) end ExternalBitField.new(path) end |
Instance Method Details
#[](position) ⇒ Object
Read a bit (1/0)
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bloomfilter/external_bloom_filter.rb', line 36 def [](position) # seek to the appropriate byte position @field.seek(position / ELEMENT_WIDTH) # read the value val = @field.read(1)[0] # figure out if our bit is flipped or not (val & (1 << (position % ELEMENT_WIDTH))) > 0 end |
#set(position) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bloomfilter/external_bloom_filter.rb', line 20 def set(position) #debugger # seek to the position in the file where we'll be making changes @field.seek(position / ELEMENT_WIDTH) # read the old value, converted to an integer old_val = @field.read(1)[0] # seek back to our spot @field.seek(position / ELEMENT_WIDTH) # write the new value, as a character again this time @field.write( (old_val | 1 << (position % ELEMENT_WIDTH)).chr ) end |