Class: MDArray::Counter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mdarray/counter.rb

Overview


Counters for Multidimensional arrays. A Counter refers to a particular element of an array. This is a generalization of index as int[].


Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mdarray) ⇒ Counter





56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mdarray/counter.rb', line 56

def initialize(mdarray)

  @mdarray = mdarray
  @nc_index = mdarray.nc_array.getIndex()
  @shape = @nc_index.getShape().to_a

  # by default the starting index is the [0] index (first element)
  shape = @shape.dup
  set_start(shape.fill(0))
  # by default the finish index is the last element of the array
  finish = Array.new
  @shape.each do |val|
    finish << val - 1
  end

  set_finish(finish)

end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



50
51
52
# File 'lib/mdarray/counter.rb', line 50

def counter
  @counter
end

#finishObject (readonly)

Returns the value of attribute finish.



49
50
51
# File 'lib/mdarray/counter.rb', line 49

def finish
  @finish
end

#mdarrayObject (readonly)

Returns the value of attribute mdarray.



45
46
47
# File 'lib/mdarray/counter.rb', line 45

def mdarray
  @mdarray
end

#nc_indexObject (readonly)

Returns the value of attribute nc_index.



46
47
48
# File 'lib/mdarray/counter.rb', line 46

def nc_index
  @nc_index
end

#shapeObject (readonly)

Returns the value of attribute shape.



47
48
49
# File 'lib/mdarray/counter.rb', line 47

def shape
  @shape
end

#startObject (readonly)


Accessor methods for start, finish and position.




110
111
112
# File 'lib/mdarray/counter.rb', line 110

def start
  @start
end

Instance Method Details

#[](*counter) ⇒ Object


Gets the element at the given counter. If counter is not basic, try to fix it to its basic form.




166
167
168
169
# File 'lib/mdarray/counter.rb', line 166

def [](*counter)
  set_counter(counter)
  get_at_counter
end

#[]=(counter, value) ⇒ Object


Sets the value of counter. If counter is not basic, try to fix it to its basic form.




251
252
253
254
# File 'lib/mdarray/counter.rb', line 251

def []= (counter, value)
  set_counter(counter)
  set_at_counter(value)
end

#eachObject





134
135
136
137
138
139
140
141
# File 'lib/mdarray/counter.rb', line 134

def each

  reset_counter
  begin
    yield self.counter if block_given?
  end while(get_next_counter)

end

#each_along_axes(axes) ⇒ Object


Walks the counter along each of the axes. For instance if given axes [0, 2] and the array shape is [4, 3, 2], then the counter will be [0, 0, 0], [0, 0, 1],

1, 0, 1], [1, 0, 1], [2, 0, 0], … [3, 0, 1



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/mdarray/counter.rb', line 149

def each_along_axes(axes)

  reset_counter
  @axes = axes

  axis = [0, self.counter.size - 2]
  begin
    yield self.counter, axis[1] if block_given?
  end while (axis = walk_along_axes)
  
end

#get(counter) ⇒ Object


Gets the element at the given counter. Assumes that the counter is of the proper shape.




176
177
178
179
# File 'lib/mdarray/counter.rb', line 176

def get(counter)
  set_counter_fast(counter)
  get_at_counter
end

#get_as(type, count = nil) ⇒ Object


Gets the element at the courrent counter with the given type




185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mdarray/counter.rb', line 185

def get_as(type, count = nil)

  count ? set_counter_fast(count) : set_counter_fast(self.counter)

  begin
    case type
    when :boolean
      @mdarray.nc_array.getBoolean(@nc_index)
    when :byte
      @mdarray.nc_array.getByte(@nc_index)
    when :char
      @mdarray.nc_array.getChar(@nc_index)
    when :short
      @mdarray.nc_array.getShort(@nc_index)
    when :int
      @mdarray.nc_array.getInt(@nc_index)
    when :long
      @mdarray.nc_array.getLong(@nc_index)
    when :float 
      @mdarray.nc_array.getFloat(@nc_index)
    when :double
      @mdarray.nc_array.getDouble(@nc_index)
    when :string
      @mdarray.nc_array.getObject(@nc_index).to_s
    else 
      @mdarray.nc_array.getObject(@nc_index)
    end
  rescue Java::UcarMa2::ForbiddenConversionException
    raise "cannot convert to type #{type}"
  end

end

#get_currentObject


Gets element at current counter. Can be done fast, as counter is always of the proper shape.




241
242
243
244
# File 'lib/mdarray/counter.rb', line 241

def get_current
  set_counter_fast(self.counter)
  get_at_counter
end

#get_scalarObject





222
223
224
# File 'lib/mdarray/counter.rb', line 222

def get_scalar
  @mdarray.nc_array.get
end

#jget(counter) ⇒ Object


Gets the element at the given counter. Assumes that the counter is of the proper shape. Also, counter should be an int java array




231
232
233
234
# File 'lib/mdarray/counter.rb', line 231

def jget(counter)
  jset_counter_fast(counter)
  get_at_counter
end

#reset_counterObject


Reset the counter to the defined start value




126
127
128
# File 'lib/mdarray/counter.rb', line 126

def reset_counter
  @counter = @start.dup
end

#set(counter, value) ⇒ Object


Sets the value of counter. Assume that counter is on its basic form.




260
261
262
263
# File 'lib/mdarray/counter.rb', line 260

def set(counter, value)
  set_counter_fast(counter)
  set_at_counter(value)
end

#set_current(value) ⇒ Object


Sets value of current counter. Can be done fast, as the current counter is always in its basic shape.




278
279
280
281
# File 'lib/mdarray/counter.rb', line 278

def set_current(value)
  set_counter_fast(self.counter)
  set_at_counter(value)
end

#set_finish(finish) ⇒ Object


Sets the finishing position of the index




95
96
97
98
99
100
101
102
103
104
# File 'lib/mdarray/counter.rb', line 95

def set_finish(finish)

  finish = reshape(finish)
  if (finish[0])
    raise "Cannot set index finish position to an array section"
  else
    @finish = finish[1].reverse
  end

end

#set_scalar(value) ⇒ Object





269
270
271
# File 'lib/mdarray/counter.rb', line 269

def set_scalar(value)
  @mdarray.nc_array.set(value)
end

#set_start(start) ⇒ Object


Sets the starting position of the index




79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mdarray/counter.rb', line 79

def set_start(start)

  start = reshape(start)
  if (start[0])
    raise "Cannot set index starting position to an array section"
  else
    @start = start[1].reverse
    reset_counter
  end

end