class PatronusFati::BitField

Attributes

bits[RW]
tracked_count[RW]

Public Class Methods

new(count) click to toggle source
# File lib/patronus_fati/bit_field.rb, line 19
def initialize(count)
  raise ArgumentError if count <= 0

  self.bits = 0
  self.tracked_count = count
end

Public Instance Methods

any_set_in?(rng) click to toggle source
# File lib/patronus_fati/bit_field.rb, line 5
def any_set_in?(rng)
  rng.find { |i| bit_set?(i) }
end
bit_set?(bit) click to toggle source
# File lib/patronus_fati/bit_field.rb, line 9
def bit_set?(bit)
  raise ArgumentError, "Bit #{bit} is out of range of #{tracked_count}" unless valid_bit?(bit)
  (bits & (1 << (bit - 1))) > 0
end
highest_bit_set() click to toggle source
# File lib/patronus_fati/bit_field.rb, line 14
def highest_bit_set
  return nil if bits == 0
  tracked_count.times.reverse_each.find { |i| bit_set?(i + 1) } + 1
end
lowest_bit_set() click to toggle source
# File lib/patronus_fati/bit_field.rb, line 26
def lowest_bit_set
  return nil if bits == 0
  tracked_count.times.each.find { |i| bit_set?(i + 1) } + 1
end
set_bit(bit) click to toggle source
# File lib/patronus_fati/bit_field.rb, line 31
def set_bit(bit)
  raise ArgumentError, "Bit #{bit} is out of range of #{tracked_count}" unless valid_bit?(bit)
  self.bits |= (1 << bit - 1)
end
to_s() click to toggle source
# File lib/patronus_fati/bit_field.rb, line 40
def to_s
  bits.to_s(2).rjust(tracked_count, '0')
end
valid_bit?(bit) click to toggle source
# File lib/patronus_fati/bit_field.rb, line 36
def valid_bit?(bit)
  bit > 0 && bit <= tracked_count
end