X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=code_ruby%2Flib%2Fmaasha%2Fbitarray.rb;h=e65ab3ca4429f9be7da258c6bb8a2f1f8b0dc673;hb=4d59c18790b6cc5951f7700392018d6ca1e6b411;hp=4cb77b4ce8c53d65c1791d02a99e449301a8c20e;hpb=a41841117e4bdc4618d78dbb4847d98a9ecd23ab;p=biopieces.git 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 = ""