-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHyperArray.hx
More file actions
300 lines (250 loc) · 7.62 KB
/
HyperArray.hx
File metadata and controls
300 lines (250 loc) · 7.62 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package moon.data.array;
import haxe.ds.Vector;
using moon.tools.VectorTools;
typedef Coords = Array<Int>;
typedef Index = Int;
/**
* Multi-dimensional array of any dimensions.
* Theses dimensions can be specified at runtime.
*
* For a slightly faster version, see MultiArray, which
* is the same as this, except it uses macros to create
* an abstract of Vector<T>.
*
* Usage:
* var ha = new HyperArray<Int>([3, 2, 3], 0);
*
* for (i in 0...ma.size(0))
* for (j in 0...ma.size(1))
* for (k in 0...ma.size(2))
* trace('($i,$j,$k) ==> ' + ma.get([i, j, k]));
*
* @author Munir Hussin
*/
class HyperArray<T>
{
private var sizes:Vector<Int>; // size of each dimension
private var strides:Vector<Int>; // multiplier to calculate index
public var dimensions(get, never):Int; // number of dimensions
public var length(get, never):Int; // raw length of data vector
private var data(default, null):Vector<T>; // 1d data
public function new(dimensions:Array<Int>, ?init:T)
{
sizes = new Vector<Int>(dimensions.length);
strides = new Vector<Int>(dimensions.length);
strides[0] = 1;
sizes[0] = dimensions[0];
var length = dimensions[0];
for (i in 1...dimensions.length)
{
strides[i] = strides[i - 1] * dimensions[i - 1];
sizes[i] = dimensions[i];
length *= dimensions[i];
}
data = new Vector<T>(length);
if (init != null)
for (i in 0...length)
data[i] = init;
}
private inline function get_length():Int
{
return data.length;
}
private inline function get_dimensions():Int
{
return sizes.length;
}
/**
* The length of a particular dimension.
* The arg `dimension` is 0-indexed.
*/
public inline function size(dimension:Int):Int
{
return sizes[dimension];
}
/**
* Given coordinates, return its index to the 1D array.
* Inverse of coords(index)
*/
public inline function index(coords:Coords):Index
{
// i = c0 + c1*n0 + c2*n0*n1 + c3*n0*n1*n2
// i = c0*s0 + c1*s1 + c2*s2 + c3*s3
// i = i0 + i1 + i2
// i0 = c0 * s0
// i1 = c1 * s1
// i2 = c2 * s2
var idx = coords[0];
for (i in 1...coords.length)
idx += coords[i] * strides[i];
return idx;
}
/**
* Given an index to the 1D array, return its actual coordinates.
* Inverse of index(coords)
*/
public inline function coords(index:Index):Coords
{
return [for (i in 0...dimensions) Std.int(index / strides[i]) % size(i)];
}
public inline function get(coords:Coords):T
{
return data[index(coords)];
}
public inline function set(coords:Coords, value:T):T
{
return data[index(coords)] = value;
}
public inline function getAt(i:Index):T
{
return data[i];
}
public inline function setAt(i:Index, value:T):T
{
return data[i] = value;
}
public inline function swap(a:Coords, b:Coords):Void
{
swapAt(index(a), index(b));
}
public inline function swapAt(i:Index, j:Index):Void
{
var t:T = data[i];
data[i] = data[j];
data[j] = t;
}
public inline function fill(value:T):Void
{
for (i in 0...length) data[i] = value;
}
public function fillValues(values:Iterable<T>):Void
{
var i = 0;
for (v in values)
if (i < length) data[i++] = v;
else return;
}
public function fillRegion(coords:Iterator<Coords>, values:Iterable<T>):Void
{
for (v in values)
if (coords.hasNext()) set(coords.next(), v);
else return;
}
public inline function fillEach(fn:Index->Coords->T):Void
{
for (i in 0...length) data[i] = fn(i, coords(i));
}
/**
* Returns a coordinate iterator that iterates through
* all the coordinates within the lower bound coordinates
* and the upper bound coordinates.
*
* Usage:
*
* var ha = new HyperArray<Int>([3, 2, 3], 0);
* for (coords in ha.region([0, 0, 0], [ha.size(0), ha.size(1), ha.size(2)]))
* {
* trace(coords + ' ==> ' + ha.get(coords));
* }
*/
public function region(?lbound:Coords, ?ubound:Coords):Iterator<Coords>
{
if (lbound == null) lbound = [for (i in 0...dimensions) 0];
if (ubound == null) ubound = [for (i in 0...dimensions) size(i)];
return new HyperCoordsIterator(lbound, ubound);
}
public inline function iterator():Iterator<T>
{
return data.iterator();
}
/**
* Slice the hyper-array to get a subset of the hyper-array.
* The result has the same number of dimensions, but the size
* of each dimension may be smaller.
*/
public function slice(lbound:Coords, ubound:Coords):HyperArray<T>
{
if (lbound == null) lbound = [for (i in 0...dimensions) 0];
if (ubound == null) ubound = [for (i in 0...dimensions) size(i)];
var h = new HyperArray<T>([for (i in 0...dimensions) ubound[i] - lbound[i]]);
var i:Int = 0;
for (c in region(lbound, ubound))
h.data[i++] = get(c);
return h;
}
public function map<U>(fn:T->U):HyperArray<U>
{
var h = new HyperArray<U>(sizes.toArray());
for (i in 0...length)
h.data[i] = fn(data[i]);
return h;
}
/**
* Searches the array for a value and returns its index
*/
public inline function indexOf(value:T):Index
{
return data.indexOf(value);
}
/**
* Searches the array for a value and returns its coordinates
*/
public inline function coordsOf(value:T):Coords
{
return coords(indexOf(value));
}
/**
* Create a shallow copy of this array
*/
public function copy():HyperArray<T>
{
var h = new HyperArray<T>(sizes.toArray());
Vector.blit(data, 0, h.data, 0, length);
return h;
}
public function toString():String
{
return Std.string(data);
}
}
class HyperCoordsIterator
{
private var curr:Coords;
private var coords:Coords;
private var lbound:Coords;
private var ubound:Coords;
public function new(lbound:Coords, ubound:Coords)
{
this.lbound = lbound;
this.ubound = ubound;
this.coords = lbound.copy();
this.curr = lbound.copy();
}
public function hasNext():Bool
{
// i < n for each dimension
for (i in 0...coords.length)
if (coords[i] < ubound[i])
return true;
return false;
}
public function next():Coords
{
// copy current results
for (i in 0...coords.length)
curr[i] = coords[i];
// advance to next one
for (i in 0...coords.length)
{
if (++coords[i] < ubound[i])
return curr;
else
coords[i] = lbound[i]; // wrap back to lower bound
}
// code reaches here means its the last one.
// since there was a wrap-around, we need to set
// coords to upper bound to prevent infinite loop.
coords = ubound;
return curr;
}
}