Sha256: bfd16190c3258b9d5da1262265e6ed8d0277ee60be29f987f75b0c3c9591398d

Contents?: true

Size: 1013 Bytes

Versions: 6

Compression:

Stored size: 1013 Bytes

Contents

module PatronusFati
  class BitField
    attr_accessor :bits, :tracked_count

    def any_set_in?(rng)
      rng.find { |i| bit_set?(i) }
    end

    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

    def highest_bit_set
      return nil if bits == 0
      tracked_count.times.reverse_each.find { |i| bit_set?(i + 1) } + 1
    end

    def initialize(count)
      raise ArgumentError if count <= 0

      self.bits = 0
      self.tracked_count = count
    end

    def lowest_bit_set
      return nil if bits == 0
      tracked_count.times.each.find { |i| bit_set?(i + 1) } + 1
    end

    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

    def valid_bit?(bit)
      bit > 0 && bit <= tracked_count
    end

    def to_s
      bits.to_s(2).rjust(tracked_count, '0')
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
patronus_fati-1.3.8 lib/patronus_fati/bit_field.rb
patronus_fati-1.3.7 lib/patronus_fati/bit_field.rb
patronus_fati-1.3.6 lib/patronus_fati/bit_field.rb
patronus_fati-1.3.5 lib/patronus_fati/bit_field.rb
patronus_fati-1.3.4 lib/patronus_fati/bit_field.rb
patronus_fati-1.3.3 lib/patronus_fati/bit_field.rb