class BitField
attr_reader :size
include Enumerable
ELEMENT_WIDTH = 32
def initialize(size)
@size = size
@field = Array.new(((size - 1) / ELEMENT_WIDTH) + 1, 0)
end
def []=(position, value)
if value == 1
@field[position / ELEMENT_WIDTH] |= 1 << (position % ELEMENT_WIDTH)
elsif (@field[position / ELEMENT_WIDTH]) & (1 << (position % ELEMENT_WIDTH)) != 0
@field[position / ELEMENT_WIDTH] ^= 1 << (position % ELEMENT_WIDTH)
end
end
def [](position)
@field[position / ELEMENT_WIDTH] & 1 << (position % ELEMENT_WIDTH) > 0 ? 1 : 0
end
def each(&block)
@size.times { |position| yield self[position] }
end
def to_s
inject("") { |a, b| a + b.to_s }
end
def total_set
@field.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
end
endHere's the tests if you want to run:
require "test/unit"
require "bitfield"
class TestLibraryFileName < Test::Unit::TestCase
def setup
@public_bf = BitField.new(1000)
end
def test_basic
assert_equal 0, BitField.new(100)[0]
assert_equal 0, BitField.new(100)[1]
end
def test_setting_and_unsetting
@public_bf[100] = 1
assert_equal 1, @public_bf[100]
@public_bf[100] = 0
assert_equal 0, @public_bf[100]
end
def test_random_setting_and_unsetting
100.times do
index = rand(1000)
@public_bf[index] = 1
assert_equal 1, @public_bf[index]
@public_bf[index] = 0
assert_equal 0, @public_bf[index]
end
end
def test_multiple_setting
1.upto(999) do |pos|
2.times { @public_bf[pos] = 1 }
assert_equal 1, @public_bf[pos]
end
end
def test_multiple_unsetting
1.upto(999) do |pos|
2.times { @public_bf[pos] = 0 }
assert_equal 0, @public_bf[pos]
end
end
def test_size
assert_equal 1000, @public_bf.size
end
def test_to_s
bf = BitField.new(10)
bf[1] = 1
bf[5] = 1
assert_equal "0100010000", bf.to_s
end
def test_total_set
bf = BitField.new(10)
bf[1] = 1
bf[5] = 1
assert_equal 2, bf.total_set
end
end