class BitVector::BitVector

Provides a class to represent a bit vector using a number and size.

Constants

DEFAULT_SIZE

Attributes

hash[R]
number[R]
size[R]
to_i[R]

Public Class Methods

dump(vector) click to toggle source

Dumps vector to value.

# File lib/bit_vector.rb, line 81
def self.dump(vector)
  vector.to_i
end
from_indexes(indexes, size = DEFAULT_SIZE) click to toggle source

Creates a new vector with bits at indexes set.

# File lib/bit_vector.rb, line 86
def self.from_indexes(indexes, size = DEFAULT_SIZE)
  new indexes.inject(0) { |number, index| number |= 1 << index; number }, size
end
load(value) click to toggle source

Loads vector from value.

# File lib/bit_vector.rb, line 76
def self.load(value)
  new value.to_i
end
new(number = 0, size = DEFAULT_SIZE) click to toggle source

Returns new bit vector initialized to optional number and size.

# File lib/bit_vector.rb, line 11
def initialize(number = 0, size = DEFAULT_SIZE)
  @number, @size = number, size
  raise ArgumentError, "number must be =< #{max_number}" if number > max_number
end

Public Instance Methods

&(other) click to toggle source

Returns a new vector containing the common bits with other vector.

# File lib/bit_vector.rb, line 56
def &(other)
  raise ArgumentError, "size mismatch" unless other.size == size
  self.class.new number & other.number, size
end
Also aliased as: -
+(other)
Alias for: |
-(other)
Alias for: &
==(other) click to toggle source

Returns true if equal to other vector. Two vectors are considered equal if their numbers and sizes are equal.

# File lib/bit_vector.rb, line 39
def ==(other)
  number == other.number && size == other.size
end
[](index) click to toggle source

Returns the bit at index.

# File lib/bit_vector.rb, line 32
def [](index)
  raise ArgumentError, "index must be < #{size}" if index >= size
  number[index]
end
[]=(index, value) click to toggle source

Sets or clears the bit at index.

# File lib/bit_vector.rb, line 25
def []=(index, value)
  raise ArgumentError, "index must be < #{size}" if index >= size
  mask = 1 << index
  @number = value && value != 0 ? number | mask : number & ~mask
end
eql?(other) click to toggle source

Returns true if equal to other vector and of the same class.

# File lib/bit_vector.rb, line 44
def eql?(other)
  other.is_a?(self.class) && self == other
end
exclude?(indexes) click to toggle source

Returns true if all the bits at indexes are unset, false otherwise.

# File lib/bit_vector.rb, line 68
def exclude?(indexes)
  indexes.all? { |index| self[index] == 0 }
end
include?(indexes) click to toggle source

Returns true if all the bits at indexes are set, false otherwise.

# File lib/bit_vector.rb, line 63
def include?(indexes)
  indexes.all? { |index| self[index] == 1 }
end
to_s() click to toggle source

Returns a string representation.

# File lib/bit_vector.rb, line 17
def to_s
  "%0#{size}b" % number
end
|(other) click to toggle source

Returns a new vector containing the combined bits of both vectors.

# File lib/bit_vector.rb, line 49
def |(other)
  raise ArgumentError, "size mismatch" unless other.size == size
  self.class.new number | other.number, size
end
Also aliased as: +

Private Instance Methods

max_number() click to toggle source
# File lib/bit_vector.rb, line 92
def max_number
  2 ** size - 1
end