Class: BioDSL::CAry

Inherits:
Object
  • Object
show all
Defined in:
lib/BioDSL/cary.rb

Overview

Class to manipulate a Ruby byte array which is fit for inline C manipulation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count, size, ary = nil) ⇒ CAry

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.



69
70
71
72
73
74
75
76
# 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

Instance Attribute Details

#aryObject (readonly)

Returns the value of attribute ary.



38
39
40
# File 'lib/BioDSL/cary.rb', line 38

def ary
  @ary
end

#countObject (readonly)

Returns the value of attribute count.



38
39
40
# File 'lib/BioDSL/cary.rb', line 38

def count
  @count
end

#sizeObject (readonly)

Returns the value of attribute size.



38
39
40
# File 'lib/BioDSL/cary.rb', line 38

def size
  @size
end

Class Method Details

.retrieve(file) ⇒ Object

Class method to retrieve and return an ary from a given file.



52
53
54
55
56
57
58
59
60
61
62
63
64
# 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

.store(file, ary) ⇒ Object

Class method to store to a given file a given ary.



41
42
43
44
45
46
47
48
49
# 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

Instance Method Details

#&(other) ⇒ Object

Method to do bitwise AND operation between two CArys.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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

#^(other) ⇒ Object

Method to do bitwise XOR operation between two CArys.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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

#fillObject

Method to set all members in an ary to 1.



85
86
87
# File 'lib/BioDSL/cary.rb', line 85

def fill
  CAry.new(@count, @size).fill!
end

#fill!Object

Method to set all members in an ary to 1.



79
80
81
82
# File 'lib/BioDSL/cary.rb', line 79

def fill!
  self.zero!
  self.~
end

#to_sObject

Method that returns a string from an ary.



164
165
166
# File 'lib/BioDSL/cary.rb', line 164

def to_s
  @ary.unpack('B*').first
end

#zeroObject

Method to set all members in an ary to zero.



96
97
98
# File 'lib/BioDSL/cary.rb', line 96

def zero
  CAry.new(@count, @size).zero!
end

#zero!Object

Method to set all members in an ary to zero.



90
91
92
93
# File 'lib/BioDSL/cary.rb', line 90

def zero!
  zero_ary_C(@ary, @count * @size)
  self
end

#|(other) ⇒ Object

Method to do bitwise OR operation between two CArys.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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

#~Object

Method to complement all bits in an ary.



158
159
160
161
# File 'lib/BioDSL/cary.rb', line 158

def ~
  complement_ary_C(@ary, @count * @size)
  self
end