+# Copyright (C) 2007-2011 Martin A. Hansen.
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# This software is part of the Biopieces framework (www.biopieces.org).
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
BitsInChar = 8
# Error class for all exceptions to do with BitArray.
def initialize(size)
@size = size
@byte_array = init_byte_array
- @count_table = init_count_table
+ @count_array = init_count_array
end
# Method to set a bit to 1 at a given position in the bit array.
(@byte_array[byte_pos(pos)].ord & bit_pos(pos)) != 0
end
- # Method that returns the number of bits set "on" in a bit array.
- def bits_on1
- bits_on = 0
-
- (0 ... @size).each do |pos|
- bits_on += 1 if bit_set?(pos)
- end
-
- bits_on
- end
-
# Method that returns the number of bits set "on" in a bit array.
def bits_on
bits_on = 0
(0 ... self.byte_array.size).each do |byte|
- bits_on += @count_table[byte]
+ bits_on += @count_array[self.byte_array[byte].ord]
end
bits_on
byte_array
end
- # Method that creates a table with number of bits set.
- def init_count_table
- count_table = []
+ # Method that returns an array where the element index value is
+ # the number of bits set for that index value.
+ def init_count_array
+ count_array = []
+ (0 ... (2 ** BitsInChar)).each do |i|
+ count_array << bits_in_char(i)
+ end
+
+ count_array
+ end
+
+ # Method that returns the number of set bits in a byte.
+ def bits_in_char(char)
bits = 0
- (0 ... BitsInChar).each do |i|
- (0 ... BitsInChar).each do |c|
- bits += 1
- end
+ (0 ... BitsInChar).each do |pos|
+ bits += 1 if ((char & bit_pos(pos)) != 0)
end
- puts bits
+ bits
end
# Method that returns the byte position in the byte array for a given bit position.
+# Copyright (C) 2007-2011 Martin A. Hansen.
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# This software is part of the Biopieces framework (www.biopieces.org).
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# Residue alphabets
DNA = %w[a t c g]