From be88ef5821b5b0d3fa297ff10d2f7e17500db1d7 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Mon, 5 Sep 2011 13:23:08 +0000 Subject: [PATCH] speedup of bits.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@1513 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/lib/maasha/bits.rb | 58 ++++++++++++++---------------------- 1 file changed, 23 insertions(+), 35 deletions(-) diff --git a/code_ruby/lib/maasha/bits.rb b/code_ruby/lib/maasha/bits.rb index b976c88..1ed9e56 100644 --- a/code_ruby/lib/maasha/bits.rb +++ b/code_ruby/lib/maasha/bits.rb @@ -22,65 +22,53 @@ # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +require 'narray' + # Error class for all exceptions to do with String. class StringError < StandardError; end # Monkey patching Class String to add bitwise operators. -# Behaviour matching Perl's: -# http://perldoc.perl.org/perlop.html#Bitwise-String-Operators class String # Method to that returns the case senstive Hamming Distance between two strings. # http://en.wikipedia.org/wiki/Hamming_distance - def self.hamming_dist(str1, str2) - raise StringError, "Uneven string lengths: #{str1.length} != #{str2.length}" if str1.length != str2.length - (str1 ^ str2 ).count("\x01-\xff") + def hamming_distance(str) + (self ^ str).tr("\x00",'').length end # Method that performs bitwise AND operation where bits - # are copied if they exists in BOTH operands. If the operand - # sizes are different, the & operator methods acts as though - # the longer operand were truncated to the length of the shorter. + # are copied if they exists in BOTH operands. def &(str) - new = "" - - (0 ... [self.length, str.length].min).each do |i| - new << (self[i].ord & str[i].ord) - end + narray1, narray2 = to_narray(self, str) - new + (narray1 & narray2).to_s end # Method that performs bitwise OR operation where bits - # are copied if they exists in EITHER operands. If the operand - # sizes differ, the shorter operand is extended with the terminal - # part of the longer operand. + # are copied if they exists in EITHER operands. def |(str) - new = "" - - min = [self.length, str.length].min - - (0 ... min).each do |i| - new << (self[i].ord | str[i].ord) - end + narray1, narray2 = to_narray(self, str) - if self.length > str.length - new << self[min ... self.length] - elsif self.length < str.length - new << str[min ... str.length] - end - - new + (narray1 | narray2).to_s end # Method that performs bitwise XOR operation where bits # are copied if they exists in ONE BUT NOT BOTH operands. def ^(str) - new = "" + narray1, narray2 = to_narray(self, str) + + (narray1 ^ narray2).to_s + end + + private - (0 ... [self.length, str.length].min).each do |i| - new << (self[i].ord ^ str[i].ord) + def to_narray(str1, str2) + if str1.length != str2.length + raise StringError, "Uneven string lengths: #{str1.length} != #{str2.length}" end - new + narray1 = NArray.to_na(str1, "byte") + narray2 = NArray.to_na(str2, "byte") + + [narray1, narray2] end end -- 2.39.5