-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray3.rb
More file actions
38 lines (38 loc) · 936 Bytes
/
array3.rb
File metadata and controls
38 lines (38 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Lambda
def lookup( value, stride )
Lambda.new @index, @term.lookup( value, stride )
end
end
class Lookup
def lookup( value, stride )
Lookup.new @p.lookup( value, stride ), @index, @stride
end
end
class MultiArray
def MultiArray.new( typecode, *shape )
options = shape.last.is_a?( Hash ) ? shape.pop : {}
count = options[ :count ] || 1
if shape.empty?
memory = Malloc.new typecode.storage_size * count
Pointer( typecode ).new memory
else
size = shape.pop
stride = shape.inject( 1 ) { |a,b| a * b }
pointer = new typecode, *( shape + [ :count => count * size ] )
index = Variable.new INDEX( size )
Lambda.new index, pointer.lookup( index, INT( stride ) )
end
end
end
# $\textcolor{commentgray}{\emph{Example use}}$
m = MultiArray.new UBYTE, 2, 4, 3
m[ 1, 2, 0 ] = 3
# 3
m[ 1, 2, 0 ]
# 3
m[ 0 ][ 2 ][ 1 ]
# 3
m[ 0 ][ 2 ][ 1 ] = 5
# 5
m[ 1, 2, 0 ]
# 5