Class: Hornetseye::Lookup

Inherits:
Node show all
Defined in:
lib/multiarray/lookup.rb

Overview

Class for lazy array lookup

Instance Method Summary collapse

Methods inherited from Node

===, #[], #[]=, array_type, basetype, #basetype, bool, byte, #check_shape, #coerce, coercion_bool, coercion_byte, coercion_maxint, #compilable?, compilable?, cond, contiguous, #demand, descriptor, dimension, #dimension, #dup, empty?, #empty?, finalised?, #finalised?, float, float_scalar, floating, #force, #get, height, #height, indgen, #inspect, match, maxint, pointer_type, #pointer_type, rgb?, #rgb?, scalar, #shape, shape, #simplify, size, #size, #storage_size, strip, subst, #to_a, #to_s, to_s, to_type, #typecode, typecode, typecodes, variables, #width, width

Methods included from FLOAT_::Match

#align, #fit

Methods included from OBJECT::Match

#align, #fit

Methods included from COMPLEX_::Match

#align, #fit

Methods included from Sequence_::Match

#align, #fit

Methods included from BOOL::Match

#fit

Methods included from RGB_::Match

#align, #fit

Methods included from INT_::Match

#fit

Methods included from Operations

#+@, #<=>, #b=, #b_with_decompose, #collect, #conditional, #convolve, define_binary_op, define_unary_op, #diagonal, #eq_with_multiarray, #fill!, #g=, #g_with_decompose, #histogram, #imag=, #imag_with_decompose, #inject, #integral, #lut, #max, #min, #normalise, #product, #r=, #r_with_decompose, #range, #real=, #real_with_decompose, #roll, #sum, #to_type, #to_type_with_rgb, #transpose, #unroll

Constructor Details

#initialize(p, index, stride) ⇒ Lookup

Create array lookup

Parameters:

  • p (Node)

    Object supporting lookup.

  • index (Variable)

    The array index.

  • stride (Node)

    The array stride.



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

def initialize( p, index, stride )
  @p, @index, @stride = p, index, stride
end

Instance Method Details

#array_typeClass

Get type of result of delayed operation

Returns:

  • (Class)

    Type of result.



56
57
58
# File 'lib/multiarray/lookup.rb', line 56

def array_type
  @p.array_type
end

#decompose(i) ⇒ Node

Decompose composite elements

This method decomposes composite elements into array.

Returns:

  • (Node)

    Result of decomposition.



156
157
158
159
160
161
162
# File 'lib/multiarray/lookup.rb', line 156

def decompose( i )
  if typecode < Composite
    Lookup.new @p.decompose( i ), @index, @stride * typecode.num_elements
  else
    Lookup.new @p.decompose( i ), @index, @stride
  end
end

#descriptor(hash) ⇒ String

Get unique descriptor of this object

Parameters:

  • hash (Hash)

    Labels for any variables.

Returns:

  • (String)

    Descriptor of this object,



46
47
48
49
# File 'lib/multiarray/lookup.rb', line 46

def descriptor( hash )
  "Lookup(#{@p.descriptor( hash )},#{@index.descriptor( hash )}," +
    "#{@stride.descriptor( hash )})"
end

#element(i) ⇒ Node, Object

Get element if lookup term

Parameters:

  • i (Integer, Node)

    Index of desired element.

Returns:



135
136
137
# File 'lib/multiarray/lookup.rb', line 135

def element( i )
  Lookup.new @p.element( i ), @index, @stride
end

#lookup(value, stride) ⇒ Object

Lookup element of an array

Parameters:

  • value (Node)

    Index of element.

  • stride (Node)

    Stride for iterating over elements.



104
105
106
107
108
109
110
# File 'lib/multiarray/lookup.rb', line 104

def lookup( value, stride )
  if value.is_a? Variable
    Lookup.new self, value, stride
  else
    Lookup.new @p.lookup( value, stride ), @index, @stride
  end
end

#memoryObject



32
33
34
35
36
37
# File 'lib/multiarray/lookup.rb', line 32

def memory
  #if array_type.storage_size != @stride.get * typecode.storage_size
  #  raise 'Memory is not contiguous'
  #end
  @p.memory
end

#skip(index, start) ⇒ Node

Skip elements of an array

Parameters:

  • index (Variable)

    Variable identifying index of array.

  • start (Node)

    Wrapped integer with number of elements to skip.

Returns:

  • (Node)

    Lookup object with elements skipped.



120
121
122
123
124
125
126
# File 'lib/multiarray/lookup.rb', line 120

def skip( index, start )
  if @index == index
    Lookup.new @p.lookup( start, @stride ), @index, @stride
  else
    Lookup.new @p.skip( index, start ), @index, @stride
  end
end

#slice(start, length) ⇒ Node

Extract array view with part of array

Parameters:

  • start (Integer, Node)

    Number of elements to skip.

  • length (Integer, Node)

    Size of array view.

Returns:

  • (Node)

    Array view with the specified elements.



147
148
149
# File 'lib/multiarray/lookup.rb', line 147

def slice( start, length )
  Lookup.new @p.slice( start, length ), @index, @stride
end

#stripArray<Array,Node>

Strip of all values

Split up into variables, values, and a term where all values have been replaced with variables.

values, and the term based on variables.

Returns:



91
92
93
94
95
96
# File 'lib/multiarray/lookup.rb', line 91

def strip
  vars1, values1, term1 = @p.strip
  vars2, values2, term2 = @stride.strip
  return vars1 + vars2, values1 + values2,
    Lookup.new( term1, @index, term2 )
end

#subst(hash) ⇒ Node

Substitute variables

Substitute the variables with the values given in the hash.

Parameters:

  • hash (Hash)

    Substitutions to apply.

Returns:

  • (Node)

    Term with substitutions applied.



69
70
71
# File 'lib/multiarray/lookup.rb', line 69

def subst( hash )
  @p.subst( hash ).lookup @index.subst( hash ), @stride.subst( hash )
end

#variablesSet

Get variables contained in this object

Returns:

  • (Set)

    Returns Set[ self ].



78
79
80
# File 'lib/multiarray/lookup.rb', line 78

def variables
  @p.variables + @index.variables + @stride.variables
end