Class: Hornetseye::MultiArray

Inherits:
Object
  • Object
show all
Extended by:
MultiArrayConstructor
Defined in:
lib/multiarray/multiarray.rb

Overview

This class provides methods for initialising multi-dimensional arrays

Class Method Summary collapse

Methods included from MultiArrayConstructor

method_missing

Class Method Details

.[](*args) ⇒ Node

Convert Ruby array to uniform multi-dimensional array

Type matching is used to find a common element type. Furthermore the required shape of the array is determined. Finally the elements are coopied to the resulting array.

Parameters:

Returns:

  • (Node)

    Uniform multi-dimensional array.



67
68
69
70
# File 'lib/multiarray/multiarray.rb', line 67

def []( *args )
  target = Node.fit args
  target[ *args ]
end

.import(typecode, string, *shape) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/multiarray/multiarray.rb', line 47

def import( typecode, string, *shape )
  t = Hornetseye::MultiArray typecode, *shape
  if string.is_a? Malloc
    memory = string
  else
    memory = Malloc.new t.storage_size
    memory.write string
  end
  t.new memory
end

.new(typecode, *shape) ⇒ Object

Create new multi-dimensional array

Parameters:

  • typecode (Class)

    The type of elements

  • *shape (Array<Integer>)

    The shape of the multi-dimensional array.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/multiarray/multiarray.rb', line 29

def new( typecode, *shape )
  options = shape.last.is_a?( Hash ) ? shape.pop : {}
  count = options[ :count ] || 1
  if shape.empty?
    memory = options[ :memory ] ||
             typecode.memory.new( typecode.storage_size * count )
    Hornetseye::Pointer( typecode ).new memory
  else
    size = shape.pop
    stride = shape.inject( 1 ) { |a,b| a * b }
    Hornetseye::lazy( size ) do |index|
      pointer = new typecode, *( shape + [ :count => count * size,
                                           :memory => options[ :memory ] ] )
      Lookup.new pointer, index, INT.new( stride )
    end
  end
end