Class: R4r::RingBits

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • size (Fixnum)

    the size of ring bits buffer

Raises:

  • (ArgumentError)

    if the specified size is negitive



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

#cardinalityObject (readonly)

Returns the value of attribute cardinality.



2
3
4
# File 'lib/r4r/ring_bits.rb', line 2

def cardinality
  @cardinality
end

#indexObject (readonly)

Returns the value of attribute index.



2
3
4
# File 'lib/r4r/ring_bits.rb', line 2

def index
  @index
end

#lengthObject (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_sizeObject

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.

Parameters:

  • value (Boolean)

    is a boolean value to set

Returns:

  • (Fixnum)

    the number of bits set to true



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

#sizeObject

Current ring bits buffer size.



20
21
22
# File 'lib/r4r/ring_bits.rb', line 20

def size
  @size
end