]> git.donarmstrong.com Git - biopieces.git/commitdiff
upgraded bitarray to work with narray for speed
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Mon, 17 Oct 2011 12:22:02 +0000 (12:22 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Mon, 17 Oct 2011 12:22:02 +0000 (12:22 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1578 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/bitarray.rb

index 04a25b9619ccec85d77ac2e868ba30d1c7dc3406..4ce560b3b7cf94e0b45f04477d2722ffb14dff9c 100644 (file)
@@ -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