class BioDSL::CAry
Class to manipulate a Ruby byte array which is fit for inline C manipulation.
Attributes
Public Class Methods
Method to initialize a new CAry
object which is either empty or created from a given byte string. Count
is the number of elements in the ary, and size is the byte size of a element.
# File lib/BioDSL/cary.rb, line 69 def initialize(count, size, ary = nil) fail CAryError, "count must be positive - not #{count}" if count <= 0 fail CAryError, "size must be positive - not #{size}" if size <= 0 @count = count @size = size @ary = ary || "\0" * count * size end
Class method to retrieve and return an ary from a given file.
# File lib/BioDSL/cary.rb, line 52 def self.retrieve(file) count = nil size = nil ary = nil File.open(file) do |ios| count = ios.read(4).unpack('I').first size = ios.read(4).unpack('I').first ary = ios.read end CAry.new(count, size, ary) end
Class method to store to a given file a given ary.
# File lib/BioDSL/cary.rb, line 41 def self.store(file, ary) File.open(file, 'w') do |ios| ios.write([ary.count].pack('I')) ios.write([ary.size].pack('I')) ios.write(ary.ary) end nil end
Public Instance Methods
Method to do bitwise AND operation between two CArys.
# File lib/BioDSL/cary.rb, line 101 def &(other) unless other.is_a? CAry fail BioDSL::CAryError, "Bad object type: #{other.class}" end if @count != other.count fail BioDSL::CAryError, "Counts mismatch: #{@count} != #{other.count}" end if @size != other.size fail BioDSL::CAryError, "Sizes mismatch: #{@size} != #{other.size}" end bitwise_and_C(@ary, other.ary, @count * @size) self end
Method to do bitwise XOR operation between two CArys.
# File lib/BioDSL/cary.rb, line 139 def ^(other) unless other.is_a? CAry fail BioDSL::CAryError, "Bad object type: #{other.class}" end if @count != other.count fail BioDSL::CAryError, "Counts mismatch: #{@count} != #{other.count}" end if @size != other.size fail BioDSL::CAryError, "Sizes mismatch: #{@size} != #{other.size}" end bitwise_xor_C(@ary, other.ary, @count * @size) self end
Method to set all members in an ary to 1.
# File lib/BioDSL/cary.rb, line 85 def fill CAry.new(@count, @size).fill! end
Method to set all members in an ary to 1.
# File lib/BioDSL/cary.rb, line 79 def fill! self.zero! self.~ end
Method that returns a string from an ary.
# File lib/BioDSL/cary.rb, line 164 def to_s @ary.unpack('B*').first end
Method to set all members in an ary to zero.
# File lib/BioDSL/cary.rb, line 96 def zero CAry.new(@count, @size).zero! end
Method to set all members in an ary to zero.
# File lib/BioDSL/cary.rb, line 90 def zero! zero_ary_C(@ary, @count * @size) self end
Method to do bitwise OR operation between two CArys.
# File lib/BioDSL/cary.rb, line 120 def |(other) unless other.is_a? CAry fail BioDSL::CAryError, "Bad object type: #{other.class}" end if @count != other.count fail BioDSL::CAryError, "Counts mismatch: #{@count} != #{other.count}" end if @size != other.size fail BioDSL::CAryError, "Sizes mismatch: #{@size} != #{other.size}" end bitwise_or_C(@ary, other.ary, @count * @size) self end
Method to complement all bits in an ary.
# File lib/BioDSL/cary.rb, line 158 def ~ complement_ary_C(@ary, @count * @size) self end