Class: BloomFilter::BitVector
- Inherits:
-
Object
- Object
- BloomFilter::BitVector
- Defined in:
- lib/bloom_filter/bit_vector.rb
Constant Summary collapse
- IS_RUBY_19 =
RUBY_VERSION >= '1.9'
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #eql?(o) ⇒ Boolean (also: #==)
-
#initialize(bits, from_str = nil) ⇒ BitVector
constructor
A new instance of BitVector.
- #to_s ⇒ Object
Constructor Details
#initialize(bits, from_str = nil) ⇒ BitVector
Returns a new instance of BitVector.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/bloom_filter/bit_vector.rb', line 6 def initialize(bits, from_str = nil) @bytes = Array.new((bits.to_f / 8).ceil, 0) @size = bits if from_str from_str.size.times do |i| @bytes[i] = IS_RUBY_19 ? from_str[i].ord : from_str[i] end end end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
3 4 5 |
# File 'lib/bloom_filter/bit_vector.rb', line 3 def size @size end |
Instance Method Details
#[](index) ⇒ Object
17 18 19 |
# File 'lib/bloom_filter/bit_vector.rb', line 17 def [](index) (@bytes[index.to_i / 8] >> (index % 8)) & 0b1 end |
#[]=(index, value) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bloom_filter/bit_vector.rb', line 21 def []=(index, value) new_value = value && value != 0 ? 1 : 0 current_value = self[index] if current_value != new_value if new_value == 0 @bytes[index.to_i / 8] -= 1 << (index % 8) else @bytes[index.to_i / 8] += 1 << (index % 8) end end end |
#eql?(o) ⇒ Boolean Also known as: ==
39 40 41 42 43 44 45 |
# File 'lib/bloom_filter/bit_vector.rb', line 39 def eql?(o) return false unless o.size == self.size self.size.times do |i| return false unless self[i] == o[i] end true end |
#to_s ⇒ Object
33 34 35 36 37 |
# File 'lib/bloom_filter/bit_vector.rb', line 33 def to_s str = "" @bytes.each { |byte| str << byte.chr } str end |