Class: R4r::RingBits
- Inherits:
-
Object
- Object
- R4r::RingBits
- Defined in:
- lib/r4r/ring_bits.rb
Instance Attribute Summary collapse
-
#cardinality ⇒ Object
readonly
Returns the value of attribute cardinality.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
-
#bit_set_size ⇒ Object
An actual ring bits buffer capacity.
-
#initialize(size:, bit_set_class: nil) ⇒ RingBits
constructor
Creates a ring bit set whose size is large enough to explicitly represent bits with indices in the range 0 through size-1.
-
#set_next(value) ⇒ Fixnum
Sets the bit at the next index to the specified value.
-
#size ⇒ Object
Current ring bits buffer size.
Constructor Details
#initialize(size:, bit_set_class: nil) ⇒ RingBits
Creates a ring bit set whose size is large enough to explicitly represent bits with indices in the range 0 through size-1. All bits are initially set to false.
10 11 12 13 14 15 16 17 |
# File 'lib/r4r/ring_bits.rb', line 10 def initialize(size:, bit_set_class: nil) @size = size @bit_set = (bit_set_class || RingBitsExt).new(size.to_i) @is_full = false @index = -1 @length = 0 @cardinality = 0 end |
Instance Attribute Details
#cardinality ⇒ Object (readonly)
Returns the value of attribute cardinality.
2 3 4 |
# File 'lib/r4r/ring_bits.rb', line 2 def cardinality @cardinality end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
2 3 4 |
# File 'lib/r4r/ring_bits.rb', line 2 def index @index end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
2 3 4 |
# File 'lib/r4r/ring_bits.rb', line 2 def length @length end |
Instance Method Details
#bit_set_size ⇒ Object
An actual ring bits buffer capacity.
25 26 27 |
# File 'lib/r4r/ring_bits.rb', line 25 def bit_set_size @bit_set.size end |
#set_next(value) ⇒ Fixnum
Sets the bit at the next index to the specified value.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/r4r/ring_bits.rb', line 33 def set_next(value) increase_length new_index = (@index + 1) % @size previous = @bit_set.set(new_index, value == true) ? 1 : 0 current = value == true ? 1 : 0 @index = new_index @cardinality = @cardinality - previous + current end |
#size ⇒ Object
Current ring bits buffer size.
20 21 22 |
# File 'lib/r4r/ring_bits.rb', line 20 def size @size end |