From 4d59c18790b6cc5951f7700392018d6ca1e6b411 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Mon, 17 Oct 2011 14:40:37 +0000 Subject: [PATCH] added methods to bitarray.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@1582 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/lib/maasha/bitarray.rb | 34 +++++++++++++++++++++++++- code_ruby/test/maasha/test_bitarray.rb | 31 ++++++++++++++++++++--- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/code_ruby/lib/maasha/bitarray.rb b/code_ruby/lib/maasha/bitarray.rb index 4cb77b4..e65ab3c 100644 --- a/code_ruby/lib/maasha/bitarray.rb +++ b/code_ruby/lib/maasha/bitarray.rb @@ -32,7 +32,7 @@ class BitArrayError < StandardError; end # Class containing methods for creating and manipulating a bit array. class BitArray attr_accessor :byte_array - attr_reader :size + attr_reader :size # Method to initialize a new bit array of a given size in bits. def initialize(size) @@ -41,6 +41,20 @@ class BitArray @count_array = init_count_array end + # Method that sets all bits in the bit array. + def fill! + self.byte_array.fill!(~0) + + self + end + + # Method that unsets all bits in the bit array. + def clear! + self.byte_array.fill!(0) + + self + end + # Method to set a bit to 1 at a given position in the bit array. def bit_set(pos) raise BitArrayError, "Position #{pos} must be an integer." unless pos.is_a? Fixnum @@ -49,6 +63,17 @@ class BitArray @byte_array[byte_pos(pos)] = @byte_array[byte_pos(pos)] | bit_pos(pos) end + # Method to set a bit to 0 at a given position in the bit array. + def bit_unset(pos) + raise BitArrayError, "Position #{pos} must be an integer." unless pos.is_a? Fixnum + raise BitArrayError, "Position #{pos} outside of range: 0 ... #{@size}" unless (0 ... @size ).include? pos + + mask = bit_pos(pos) + mask = ~mask + + @byte_array[byte_pos(pos)] = @byte_array[byte_pos(pos)] & mask + end + # Method to check if a bit at a given position in the bit array is set. def bit_set?(pos) raise BitArrayError, "Position #{pos} must be an integer." unless pos.is_a? Fixnum @@ -106,6 +131,13 @@ class BitArray self end + # Method to flip all bits in a bit array. All set bits become unset and visa versa. + def ~ + self.byte_array = ~(self.byte_array) + + self + end + # Method to convert a bit array to a string. def to_s string = "" diff --git a/code_ruby/test/maasha/test_bitarray.rb b/code_ruby/test/maasha/test_bitarray.rb index 4745401..182ec27 100755 --- a/code_ruby/test/maasha/test_bitarray.rb +++ b/code_ruby/test/maasha/test_bitarray.rb @@ -55,22 +55,38 @@ class TestBitArray < Test::Unit::TestCase assert_equal("0000000000", @ba.to_s) end + def test_BitArray_fill_returns_correctly + assert_equal("1111111111", @ba.fill!.to_s) + end + + def test_BitArray_clear_returns_correctly + assert_equal("0000000000", @ba.fill!.clear!.to_s) + end + def test_BitArray_bit_set_with_bad_pos_raises [-1, 10, 1.1].each do |pos| assert_raise(BitArrayError) { @ba.bit_set(pos) } end end - def test_BitArray_bit_set - str = "0000000000" + def test_BitArray_bit_unset + @ba.fill! + + str = "1111111111" (0.upto 9).each do |pos| - @ba.bit_set(pos) - str[pos] = "1" + @ba.bit_unset(pos) + str[pos] = "0" assert_equal(str, @ba.to_s) end end + def test_BitArray_bit_unset_with_bad_pos_raises + [-1, 10, 1.1].each do |pos| + assert_raise(BitArrayError) { @ba.bit_unset(pos) } + end + end + def test_BitArray_bit_set_questionmark_with_bad_pos_raises [-1, 10, 1.1].each do |pos| assert_raise(BitArrayError) { @ba.bit_set?(pos) } @@ -137,5 +153,12 @@ class TestBitArray < Test::Unit::TestCase ba.bit_set(6) assert_equal( "0000101000", (@ba ^ ba).to_s) end + + def test_BitArray_FLIP_returns_correctly + @ba.bit_set(0) + @ba.bit_set(9) + + assert_equal( "0111111110", (~@ba).to_s) + end end -- 2.39.2