Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BitArray
- include Enumerable
- def initialize size
- @size = size
- @field = 2**size
- end
- def set positions
- bits = positions.kind_of?(Integer) ? [positions] : positions
- bits.each { |position| @field |= 1 << (@size - position) }
- self
- end
- def clear positions
- bits = positions.kind_of?(Integer) ? [positions] : positions
- bits.each { |position| @field ^= 1 << (@size - position) }
- self
- end
- def get position
- (@field >> (@size - position) ).to_s(2)[-1].to_i
- end
- def each(&block)
- @field.to_s(2)[1..-1].split("").each { |bit_string| yield(bit_string.to_i) }
- end
- def to_s
- @field.to_s(2)[1..-1]
- end
- def count
- @field.to_s(2)[1..-1].split("").inject(0) {|sum,bit| sum + bit.to_i}
- end
- end
- require 'minitest/autorun'
- require_relative 'bit_array'
- class BitArrayTest < MiniTest::Unit::TestCase
- def test_equal
- assert_equal "00000", BitArray.new(5).to_s
- end
- def test_set
- assert_equal "00100", BitArray.new(5).set(3).to_s
- assert_equal "00010", BitArray.new(5).set(4).to_s
- assert_equal "11100", BitArray.new(5).set([1,2,3]).to_s
- assert_equal "100000000101", BitArray.new(12).set([1,10,12]).to_s
- end
- def test_clear
- assert_equal "01000", BitArray.new(5).set([1,2]).clear(1).to_s
- end
- def test_get
- assert_equal 0, BitArray.new(5).set([1,2]).get(3)
- assert_equal 0, BitArray.new(12).set([12]).get(11)
- assert_equal 0, BitArray.new(12).set([12]).get(1)
- end
- def test_count
- assert_equal 2, BitArray.new(5).set([1,2]).count
- end
- def test_iterate
- assert_equal ["0", "0", "0", "0", "0"], BitArray.new(5).each {|n| puts n }
- end
- end
- def each(&block)
- # block argument ignored, no iteration; passes tests anyway
- @field.to_s(2)[1..-1].split("")
- end
- expected = [1, 1, 0]
- BitArray.new(3).set([1,2]).each do |bit|
- assert_equal expected.shift, bit
- end
- assert expected.empty? # check that we looped the expected number of times
- def set *positions # use a splat
- positions.each { |position| @field |= 1 << (@size - position) }
- self
- end
- def set *positions
- positions.each { |position| @field |= 2**position }
- self
- end
- def count
- inject(0, :+)
- end
- class BitArray
- include Enumerable
- def initialize
- @bits = 0
- end
- def set *positions
- positions.each { |position| @bits |= 2**position }
- self
- end
- def unset *positions
- positions.each { |position| @bits ^= 2**position }
- self
- end
- def get position
- (@bits >> position) & 1
- end
- def each(&block)
- bits = @bits
- until bits == 0
- yield bits & 1
- bits = bits >> 1
- end
- end
- def to_s
- @bits.to_s(2).reverse
- end
- def count
- inject(0, :+)
- end
- end
- require 'minitest/autorun'
- class BitArrayTest < MiniTest::Test
- def test_equal
- assert_equal "0", BitArray.new.to_s
- end
- def test_set
- assert_equal "0001", BitArray.new.set(3).to_s
- assert_equal "1011", BitArray.new.set(0, 2, 3).to_s
- assert_equal "0100000000101", BitArray.new.set(1, 10, 12).to_s
- end
- def test_unset
- assert_equal "101", BitArray.new.set(0, 1, 2).unset(1).to_s
- end
- def test_get
- assert_equal 1, BitArray.new.set(1, 2).get(2)
- assert_equal 0, BitArray.new.set(12).get(11)
- assert_equal 1, BitArray.new.set(12).get(12)
- end
- def test_count
- assert_equal 3, BitArray.new.set(1, 2, 5).count
- end
- def test_iterate
- expected = [1, 1, 0, 1]
- BitArray.new.set(0, 1, 3).each do |bit|
- assert_equal expected.shift, bit
- end
- assert expected.empty?
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement