Class: Hornetseye::Field_

Inherits:
Object show all
Defined in:
lib/multiarray/field.rb

Overview

Class for representing n-dimensional native arrays

Defined Under Namespace

Modules: Match

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.dimensionInteger

Number of dimensions

Returns:

  • (Integer)

    Number of dimensions.



33
34
35
# File 'lib/multiarray/field.rb', line 33

def dimension
  @dimension
end

.typecodeClass

Type of array elements

Returns:

  • (Class)

    Type of array elements.



28
29
30
# File 'lib/multiarray/field.rb', line 28

def typecode
  @typecode
end

Class Method Details

.[](*args) ⇒ Node

Construct native array from Ruby array

Parameters:

Returns:

  • (Node)

    Native array with specified values.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/multiarray/field.rb', line 85

def [](*args)
  def arr_shape(args)
    if args.is_a? Array
      args.collect do |arg|
        arr_shape arg
      end.inject([]) do |a,b|
        (0 ... [a.size, b.size].max).collect do |i|
          [i < a.size ? a[i] : 0, i < b.size ? b[i] : 0].max
        end
      end + [args.size]
    else
      []
    end
  end
  retval = new *arr_shape(args)
  def recursion(element, args)
    if element.dimension > 0
      args.each_with_index do |arg,i|
        recursion element.element(INT.new(i)), arg
      end
    else
      element[] = args
    end
  end
  recursion retval, args
  retval
end

.basetypeClass

Base type of this data type

Returns:

  • (Class)

    Returns element_type.



166
167
168
# File 'lib/multiarray/field.rb', line 166

def basetype
  typecode.basetype
end

.boolClass

Get corresponding boolean type

Returns:

  • (Class)

    Returns type for array of boolean values.



213
214
215
# File 'lib/multiarray/field.rb', line 213

def bool
  Hornetseye::MultiArray typecode.bool, dimension
end

.byteClass

Get corresponding byte type

Returns:

  • (Class)

    Returns type based on byte.



267
268
269
# File 'lib/multiarray/field.rb', line 267

def byte
  Hornetseye::MultiArray typecode.byte, dimension
end

.coercion(other) ⇒ Class

Type coercion for native elements

Parameters:

  • other (Class)

    Other native datatype to coerce with.

Returns:

  • (Class)

    Result of coercion.



186
187
188
189
# File 'lib/multiarray/field.rb', line 186

def coercion( other )
  Hornetseye::MultiArray typecode.coercion(other.typecode),
                    [dimension, other.dimension].max
end

.coercion_bool(other) ⇒ Class

Coerce and convert to boolean type

Returns:

  • (Class)

    Returns type for array of boolean values.



222
223
224
# File 'lib/multiarray/field.rb', line 222

def coercion_bool(other)
  coercion(other).bool
end

.coercion_byte(other) ⇒ Class

Coerce and convert to byte type

Returns:

  • (Class)

    Returns type based on byte.



276
277
278
# File 'lib/multiarray/field.rb', line 276

def coercion_byte(other)
  coercion(other).byte
end

.coercion_maxint(other) ⇒ Class

Coerce and convert to maximum integer type

Returns:

  • (Class)

    Returns type based on maximum integers.



258
259
260
# File 'lib/multiarray/field.rb', line 258

def coercion_maxint(other)
  coercion(other).maxint
end

.compilable?Boolean

Check whether this array expression allows compilation

Returns:

  • (Boolean)

    Returns true if this expression supports compilation.



320
321
322
# File 'lib/multiarray/field.rb', line 320

def compilable?
  typecode.compilable?
end

.cond(a, b) ⇒ Class

Coerce with two other types

Returns:

  • (Class)

    Result of coercion.



303
304
305
306
# File 'lib/multiarray/field.rb', line 303

def cond(a, b)
  t = a.coercion b
  Hornetseye::MultiArray t.typecode, dimension
end

.floatClass

Convert to type based on floating point numbers

Returns:

  • (Class)

    Corresponding type based on floating point numbers.



285
286
287
# File 'lib/multiarray/field.rb', line 285

def float
  Hornetseye::MultiArray typecode.field, dimension
end

.float_scalarClass

Get corresponding floating point type

Returns:

  • (Class)

    Returns type for array of floating point numbers.



240
241
242
# File 'lib/multiarray/field.rb', line 240

def float_scalar
  Hornetseye::MultiArray typecode.float_scalar, dimension
end

.floating(other) ⇒ Class

Coerce and convert to type based on floating point numbers

Returns:

  • (Class)

    Corresponding type based on floating point numbers.



294
295
296
# File 'lib/multiarray/field.rb', line 294

def floating(other)
  coercion(other).float
end

.identityClass

Get this type

Returns:

  • (Class)

    Returns self.



204
205
206
# File 'lib/multiarray/field.rb', line 204

def identity
  self
end

.indgen(*shape, offset = 0, increment = 1) ⇒ Node

Create (lazy) index array

Parameters:

  • shape (Array<Integer>)

    Dimensions of resulting array.

  • offset (Object) (defaults to: 0)

    (0) First value of array.

  • increment (Object) (defaults to: 1)

    (1) Increment for subsequent values.

Returns:

  • (Node)

    Lazy term generating the array.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/multiarray/field.rb', line 121

def indgen(*args)
  unless args.size.between? dimension, dimension + 2
    raise "#{inspect}.indgen requires between #{dimension} and #{dimension + 2} arguments"
  end
  shape = args[0 ... dimension]
  offset = args.size > dimension ? args[dimension] : 0
  increment = args.size > dimension + 1 ? args[dimension + 1] : 1
  step = shape[0 ... -1].inject 1, :*
  Hornetseye::lazy(shape.last) do |i|
    (step * increment * i +
     Hornetseye::MultiArray(typecode, dimension - 1).
       indgen(*(shape[0 ... -1] + [offset, increment]))).to_type typecode
  end
end

.inherit(typecode, dimension) ⇒ Object



35
36
37
38
39
40
# File 'lib/multiarray/field.rb', line 35

def inherit(typecode, dimension)
  retval = Class.new self
  retval.typecode = typecode
  retval.dimension = dimension
  retval
end

.inspectString

Display this type

Returns:

  • (String)

    String with description of this type.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/multiarray/field.rb', line 64

def inspect
  if typecode and dimension
    if dimension != 1
      "MultiArray(#{typecode.inspect},#{dimension})"
    else
      "Sequence(#{typecode.inspect})"
    end
  else
    'Field(?,?)'
  end
end

.maxintClass

Get corresponding maximum integer type

Returns:

  • (Class)

    Returns type based on maximum integers.



249
250
251
# File 'lib/multiarray/field.rb', line 249

def maxint
  Hornetseye::MultiArray typecode.maxint, dimension
end

.new(*shape) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/multiarray/field.rb', line 42

def new(*shape)
  options = shape.last.is_a?( Hash ) ? shape.pop : {}
  raise "Constructor requires #{dimension} arguments" unless dimension == shape.size
  count = options[:count] || 1
  if shape.empty?
    memory = options[:memory] ||
             typecode.memory_type.new(typecode.storage_size * count)
    Hornetseye::Pointer( typecode ).new memory
  else
    size = shape.pop
    stride = shape.inject 1, :*
    Hornetseye::lazy(size) do |index|
      pointer = Field_.inherit(typecode, dimension - 1).
        new *(shape + [:count => count * size, :memory => options[:memory]])
      Lookup.new pointer, index, INT.new(stride)
    end
  end
end

.random(*shape, n) ⇒ Node

Generate random number array

Generate integer or floating point random numbers in the range 0 … n.

Parameters:

  • shape (Array<Integer>)

    Dimensions of resulting array.

  • n (Integer, Float)

    (1) Upper boundary for random numbers

Returns:

  • (Node)

    Array with random numbers.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/multiarray/field.rb', line 145

def random(*args)
  unless args.size.between? dimension, dimension + 1
    raise "#{inspect}.random requires between #{dimension} and #{dimension + 1} arguments"
  end
  shape = args[0 ... dimension]
  n = args.size > dimension ? args[dimension] : 1
  n = typecode.maxint.new n unless n.matched?
  retval = new *shape
  unless compilable? and dimension > 0
    Random.new(retval, n).demand
  else
    GCCFunction.run Random.new(retval, n)
  end
  retval
end

.rgb?Boolean

Check whether delayed operation will have colour

Returns:

  • (Boolean)

    Boolean indicating whether the array has elements of type RGB.



195
196
197
# File 'lib/multiarray/field.rb', line 195

def rgb?
  typecode.rgb?
end

.scalarClass

Get corresponding scalar type

Returns:

  • (Class)

    Returns type for array of scalars.



231
232
233
# File 'lib/multiarray/field.rb', line 231

def scalar
  Hornetseye::MultiArray typecode.scalar, dimension
end

.storage_size(*shape) ⇒ Integer

Get storage size of array type

Parameters:

  • shape (Array<Integer>)

    Shape of desired array.

Returns:

  • (Integer)

    Storage size of array.



175
176
177
# File 'lib/multiarray/field.rb', line 175

def storage_size(*shape)
  shape.inject typecode.storage_size, :*
end

.to_sObject



76
77
78
# File 'lib/multiarray/field.rb', line 76

def to_s
  inspect
end

.to_type(dest) ⇒ Class

Replace element type

Returns:

  • (Class)

    Result of conversion.



313
314
315
# File 'lib/multiarray/field.rb', line 313

def to_type(dest)
  Hornetseye::MultiArray typecode.to_type(dest), dimension
end

Instance Method Details

#arr_shape(args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/multiarray/field.rb', line 86

def arr_shape(args)
  if args.is_a? Array
    args.collect do |arg|
      arr_shape arg
    end.inject([]) do |a,b|
      (0 ... [a.size, b.size].max).collect do |i|
        [i < a.size ? a[i] : 0, i < b.size ? b[i] : 0].max
      end
    end + [args.size]
  else
    []
  end
end

#recursion(element, args) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/multiarray/field.rb', line 100

def recursion(element, args)
  if element.dimension > 0
    args.each_with_index do |arg,i|
      recursion element.element(INT.new(i)), arg
    end
  else
    element[] = args
  end
end