Class: ANArray

Inherits:
NArray
  • Object
show all
Defined in:
lib/narray_ffi.rb

Constant Summary collapse

FFITYPECODES =
{
  NArray::BYTE => :char,
  NArray::SINT => :short,
  NArray::INT => :int,
  NArray::SFLOAT => :float,
  NArray::FLOAT => :double,
  NArray::SCOMPLEX => :float,
  NArray::COMPLEX => :double
}

Class Method Summary collapse

Class Method Details

.byte(alignment, *size) ⇒ Object


31
32
33
# File 'lib/narray_ffi.rb', line 31

def self.byte(alignment, *size)
  return self.new(NArray::BYTE, alignment, *size)
end

.complex(alignment, *size) ⇒ Object


55
56
57
# File 'lib/narray_ffi.rb', line 55

def self.complex(alignment, *size)
  return self.new(NArray::COMPLEX, alignment, *size)
end

.float(alignment, *size) ⇒ Object


47
48
49
# File 'lib/narray_ffi.rb', line 47

def self.float(alignment, *size)
  return self.new(NArray::FLOAT, alignment, *size)
end

.int(alignment, *size) ⇒ Object


39
40
41
# File 'lib/narray_ffi.rb', line 39

def self.int(alignment, *size)
  return self.new(NArray::INT, alignment, *size)
end

.new(typecode, alignment, *size) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/narray_ffi.rb', line 17

def self.new(typecode, alignment, *size)
  raise "Wrong type code" if not FFITYPECODES[typecode]
  raise "Invalid alignment" unless alignment > 0 and ( alignment & (alignment - 1) == 0 )
  total = size[0]
  size[1..-1].each { |sz| total = total * sz }
  total = 2*total if typecode == NArray::COMPLEX or typecode == NArray::SCOMPLEX
  mem = FFI::MemoryPointer::new( FFITYPECODES[typecode], total + alignment - 1 )
  address = mem.address
  offset = address & (alignment - 1)
  offset = alignment - offset unless offset == 0
  mem = mem.slice(offset, total*mem.type_size)
  return NArray.to_na(mem, typecode, *size)
end

.object(alignment, *size) ⇒ Object


59
60
61
# File 'lib/narray_ffi.rb', line 59

def self.object(alignment, *size)
  return self.new(NArray::OBJECT, alignment, *size)
end

.scomplex(alignment, *size) ⇒ Object


51
52
53
# File 'lib/narray_ffi.rb', line 51

def self.scomplex(alignment, *size)
  return self.new(NArray::SCOMPLEX, alignment, *size)
end

.sfloat(alignment, *size) ⇒ Object


43
44
45
# File 'lib/narray_ffi.rb', line 43

def self.sfloat(alignment, *size)
  return self.new(NArray::SFLOAT, alignment, *size)
end

.sint(alignment, *size) ⇒ Object


35
36
37
# File 'lib/narray_ffi.rb', line 35

def self.sint(alignment, *size)
  return self.new(NArray::SINT, alignment, *size)
end