-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharray_map.lua
222 lines (191 loc) · 7.42 KB
/
array_map.lua
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
-- array_map.lua - a tiny multidimensional array library for luajit
-- Copyright Christoph Straehle ([email protected])
-- License: BSD
local math = require("math")
local ffi = require("ffi")
local bitop = require("bit")
local helpers = require("helpers")
local operator = helpers.operator
local isarray = helpers.isarray
--- iterates over an array and calls a function for each value
--
-- @param f function to call. the function receives the
-- value of the array element as first argument
-- and optionally table with the coordinate of
-- the current element
-- the return value of the function for each
-- element is stored in the array.
-- @param call_with_position if true, the funciton f will be
-- called with the currents array element position
--
Array.mapInplace = function(self,f, call_with_position)
local pos = {}
local ldim = 0
for d = 0, self.ndim-1,1 do
pos[d] = 0
if self.shape[d] > 1 then
ldim = d
end
end
local finished = false
local offset = 0
while true do
if call_with_position == false then
for toffset = offset, offset + self.shape[ldim]*self.strides[ldim]-1,self.strides[ldim] do
self.data[toffset] = f(self.data[toffset])
end
else
for toffset = offset, offset + self.shape[ldim]*self.strides[ldim]-1,self.strides[ldim] do
self.data[toffset] = f(self.data[toffset],pos)
pos[ldim] = pos[ldim] + 1
end
pos[ldim] = 0
end
local dim = ldim - 1
if dim < 0 then
return
end
pos[dim] = pos[dim] + 1
offset = offset + self.strides[dim]
while pos[dim] >= self.shape[dim] do
pos[dim] = 0
offset = offset - self.shape[dim] * self.strides[dim]
dim = dim - 1
if dim < 0 then
return
end
pos[dim] = pos[dim] + 1
offset = offset + self.strides[dim]
end
end
end
--- iterates jointly over two arrays and calls a function with the two values of the arrays at the current coordinate
--
-- @param other the other array, must have the same dimension and shape
-- @param f function to call. the function receives the
-- value of the array element as first argument
-- and the value of the second arrays as second arguemnt.
-- third argument is optionally a table with the coordinate of
-- the current elements.
-- The return value of f for each element pair
-- is stored in the first array.
-- @param call_with_position optional, if true, the funciton f will be
-- called with the currents array element position
function Array.mapBinaryInplace(self,other,f, call_with_position)
local pos = {}
local ldim = 0
for d = 0, self.ndim-1,1 do
pos[d] = 0
if self.shape[d] > 1 then
ldim = d
end
end
local finished = false
local offset = 0
local offset2 = 0
while true do
if call_with_position == false then
for toffset = offset, offset + self.shape[ldim]*self.strides[ldim]-1,self.strides[ldim] do
self.data[toffset] = f(self.data[toffset],other.data[offset2])
offset2 = offset2 + other.strides[ldim]
end
else
for toffset = offset, offset + self.shape[ldim]*self.strides[ldim]-1,self.strides[ldim] do
self.data[toffset] = f(self.data[toffset],other.data[offset2],pos)
pos[ldim] = pos[ldim] + 1
offset2 = offset2 + other.strides[ldim]
end
pos[ldim] = 0
end
local dim = ldim - 1
if dim < 0 then
return
end
offset2 = offset2 - self.shape[ldim]*other.strides[ldim]
pos[dim] = pos[dim] + 1
offset = offset + self.strides[dim]
offset2 = offset2 + other.strides[dim]
while pos[dim] >= self.shape[dim] do
pos[dim] = 0
offset = offset - self.shape[dim] * self.strides[dim]
offset2 = offset2 - self.shape[dim] * other.strides[dim]
dim = dim - 1
if dim < 0 then
return
end
pos[dim] = pos[dim] + 1
offset = offset + self.strides[dim]
offset2 = offset2 + other.strides[dim]
end
end
end
--- iterates jointly over three arrays and calls a function with the three values of the arrays at the current coordinate
--
-- @param other the second array, must have the same dimension and shape
-- @param other2 the third array, must have the same dimension and shape
-- @param f the function to call. the function receives the
-- value of the array element as first argument
-- and the value of the second arrays as second arguemnt
-- and the value of the third array as third argument.
-- third argument is optionally a table with the coordinate of
-- the current elements.
-- The return value of f for each element triple is stored
-- in the first array.
-- @param call_with_position optional, if true, the funciton f will be
-- called with the currents array element position
function Array.mapTenaryInplace(self,other, other2,f, call_with_position)
local pos = {}
local ldim = 0
for d = 0, self.ndim-1,1 do
pos[d] = 0
if self.shape[d] > 1 then
ldim = d
end
end
local finished = false
local offset = 0
local offset2 = 0
local offset3 = 0
while finished == false do
if call_with_position == false then
for toffset = offset, offset + self.shape[ldim]*self.strides[ldim]-1,self.strides[ldim] do
self.data[toffset] = f(self.data[toffset],other.data[offset2],other2.data[offset3])
offset2 = offset2 + other.strides[ldim]
offset3 = offset3 + other2.strides[ldim]
end
else
for toffset = offset, offset + self.shape[ldim]*self.strides[ldim]-1,self.strides[ldim] do
self.data[toffset] = f(self.data[toffset],other.data[offset2],other2.data[offset3],pos)
pos[ldim] = pos[ldim] + 1
offset2 = offset2 + other.strides[ldim]
offset3 = offset3 + other2.strides[ldim]
end
pos[ldim] = 0
end
local dim = ldim - 1
if dim < 0 then
break
end
offset2 = offset2 - self.shape[ldim]*other.strides[ldim]
offset3 = offset3 - self.shape[ldim]*other2.strides[ldim]
pos[dim] = pos[dim] + 1
offset = offset + self.strides[dim]
offset2 = offset2 + other.strides[dim]
offset3 = offset3 + other2.strides[dim]
while pos[dim] >= self.shape[dim] do
pos[dim] = 0
offset = offset - self.shape[dim] * self.strides[dim]
offset2 = offset2 - self.shape[dim] * other.strides[dim]
offset3 = offset3 - self.shape[dim] * other2.strides[dim]
dim = dim - 1
if dim < 0 then
finished = true
break
end
pos[dim] = pos[dim] + 1
offset = offset + self.strides[dim]
offset2 = offset2 + other.strides[dim]
offset3 = offset3 + other2.strides[dim]
end
end
end