From 0bd30b142eb5bdadb99a4fea48e1cba087884a0d Mon Sep 17 00:00:00 2001 From: martinahansen Date: Mon, 17 Oct 2011 12:22:02 +0000 Subject: [PATCH] upgraded bitarray to work with narray for speed git-svn-id: http://biopieces.googlecode.com/svn/trunk@1578 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/lib/maasha/bitarray.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/code_ruby/lib/maasha/bitarray.rb b/code_ruby/lib/maasha/bitarray.rb index 04a25b9..4ce560b 100644 --- a/code_ruby/lib/maasha/bitarray.rb +++ b/code_ruby/lib/maasha/bitarray.rb @@ -22,6 +22,8 @@ # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +require 'narray' + BitsInChar = 8 # Error class for all exceptions to do with BitArray. @@ -29,7 +31,8 @@ class BitArrayError < StandardError; end # Class containing methods for creating and manipulating a bit array. class BitArray - attr_reader :size, :byte_array + attr_accessor :byte_array + attr_reader :size # Method to initialize a new bit array of a given size in bits. def initialize(size) @@ -43,7 +46,7 @@ class BitArray 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 - @byte_array[byte_pos(pos)] = (@byte_array[byte_pos(pos)].ord | bit_pos(pos)).chr + @byte_array[byte_pos(pos)] = @byte_array[byte_pos(pos)] | bit_pos(pos) end # Method to check if a bit at a given position in the bit array is set. @@ -78,9 +81,7 @@ class BitArray result = BitArray.new(ba.size) - (0 ... ba.byte_array.size).each do |byte| - result.byte_array[byte] = (self.byte_array[byte].ord & ba.byte_array[byte].ord).chr - end + result.byte_array = self.byte_array & ba.byte_array result end @@ -93,9 +94,7 @@ class BitArray result = BitArray.new(ba.size) - (0 ... ba.byte_array.size).each do |byte| - result.byte_array[byte] = (self.byte_array[byte].ord | ba.byte_array[byte].ord).chr - end + result.byte_array = self.byte_array | ba.byte_array result end @@ -108,9 +107,7 @@ class BitArray result = BitArray.new(ba.size) - (0 ... ba.byte_array.size).each do |byte| - result.byte_array[byte] = (self.byte_array[byte].ord ^ ba.byte_array[byte].ord).chr - end + result.byte_array = self.byte_array ^ ba.byte_array result end @@ -143,6 +140,8 @@ class BitArray byte_array << 0.chr * (((@size - 1) / BitsInChar) + 1) byte_array + + NArray.byte(((@size - 1) / BitsInChar) + 1) end # Method that returns an array where the element index value is -- 2.39.5