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.




214
215
216
217
# File 'lib/mdarray/counter.rb', line 214

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_currentObject


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




204
205
206
207
# File 'lib/mdarray/counter.rb', line 204

def get_current
  set_counter_fast(self.counter)
  get_at_counter
end

#get_scalarObject





185
186
187
# File 'lib/mdarray/counter.rb', line 185

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




194
195
196
197
# File 'lib/mdarray/counter.rb', line 194

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.




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

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.




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

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





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

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