Class: Hornetseye::Field_

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

Overview

Class for representing n-dimensional native arrays

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 'docs/multiarray/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 'docs/multiarray/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 'docs/multiarray/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

.compilable?Boolean

Check whether this array expression allows compilation

Returns:

  • (Boolean)

    Returns true if this expression supports compilation.



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

def compilable?
  typecode.compilable?
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 'docs/multiarray/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 'docs/multiarray/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 'docs/multiarray/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

.new(*shape) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'docs/multiarray/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 'docs/multiarray/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 'docs/multiarray/lib/multiarray/field.rb', line 195

def rgb?
  typecode.rgb?
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 'docs/multiarray/lib/multiarray/field.rb', line 175

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

.to_sObject



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

def to_s
  inspect
end

Instance Method Details

#arr_shape(args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'docs/multiarray/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 'docs/multiarray/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