forked from brendan-w/Array2D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2D.js
More file actions
554 lines (440 loc) · 12.6 KB
/
Array2D.js
File metadata and controls
554 lines (440 loc) · 12.6 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//Written by Brendan Whitfield
/*
* Array2D Constructor
*
* new Array2D(<Array2D>);
* new Array2D(<x>, <y>;)
*/
window.Array2D = function() {
"use strict";
this.default_value = 0;
var args = arguments; //for brevity, nothing more
this.__assert__.totalArgs.call(this, "constructor", [1,2,3], args);
switch(args.length)
{
case 1: //copy constructor
this.__assert__.typeOf.call(this, "constructor", args[0], Array2D);
var old = args[0];
this.__build__.call(this, old.x, old.y, old.default_value);
this.forEach(function(v, x, y, a) {
a[x][y] = old[x][y];
});
break;
case 2: //normal Array2D constructor
this.__assert__.areNumbers.call(this, "constructor", args[0], args[1]);
this.__assert__.validDimensions.call(this, "constructor", args[0], args[1]);
this.__build__(args[0], args[1], 0);
break;
case 3: //normal Array2D constructor, with default value parameter
this.__assert__.areNumbers.call(this, "constructor", args[0], args[1]);
this.__assert__.validDimensions.call(this, "constructor", args[0], args[1]);
this.__build__.apply(this, args);
break;
}
};
/*
* Internal functions
*/
Array2D.prototype.__assert__ = {
totalArgs : function(sourceName, nums, args) {
for(var i = 0; i < nums.length; i++)
{
if(nums[i] === args.length) { return; }
}
throw "Array2D Error [" + sourceName + "]: wrong number of arguments";
},
areNumbers : function(sourceName) {
for(var i = 1; i < arguments.length; i++)
{
if(isNaN(arguments[i]))
{
throw "Array2D Error [" + sourceName + "]: argument not a number";
}
}
},
isFunction : function(sourceName, callback) {
if((callback === undefined) || !(callback instanceof Function))
{
throw "Array2D Error [" + sourceName + "]: callback is not a function";
}
},
inBounds : function(sourceName, x, y, w, h) {
switch(arguments.length)
{
case 3:
if(!this.inBounds(x, y))
{
throw "Array2D Error [" + sourceName + "]: array index (" + x + ", " + y + ") out of bounds";
}
break;
case 5:
if(!this.inBounds(x, y, w, h))
{
throw "Array2D Error [" + sourceName + "]: rectangular area (" + x + ", " + y + ", " + w + ", " + h + ") out of bounds";
}
break;
}
},
validDimensions : function(sourceName, x, y) {
if((x <= 0) || (y <= 0))
{
throw "Array2D Error [" + sourceName + "]: can't create array with dimensions (" + x + ", " + y + ")";
}
},
typeOf : function(sourceName, obj, type) {
if(!(obj instanceof type))
{
throw "Array2D Error [" + sourceName + "]: parameter must be of type [" + type.name + "]";
}
},
};
Array2D.prototype.__build__ = function(nx, ny, def) {
this.x = nx;
this.y = ny;
this.default_value = def !== undefined ? def : this.default_value;
for(var x = 0; x < this.x; x++)
{
this[x] = {};
for(var y = 0; y < this.y; y++)
{
this[x][y] = this.default_value;
}
}
};
/*
* API functions
*/
/*
* Iterators
*/
Array2D.prototype.forEach = function(callback) {
this.__assert__.totalArgs.call(this, "forEach", [1], arguments);
this.__assert__.isFunction.call(this, "forEach", callback);
for(var x = 0; x < this.x; x++)
{
for(var y = 0; y < this.y; y++)
{
callback(this[x][y], x, y, this);
}
}
};
Array2D.prototype.forArea = function(x, y, w, h, callback) {
this.__assert__.totalArgs.call(this, "forArea", [5], arguments);
this.__assert__.areNumbers.call(this, "forArea", x, y, w, h);
this.__assert__.isFunction.call(this, "forArea", callback);
this.__assert__.inBounds.call(this, "forArea", x, y, w, h);
for(var cx = x; cx < x+w; cx++)
{
for(var cy = y; cy < y+h; cy++)
{
callback(this[cx][cy], cx, cy, this);
}
}
};
Array2D.prototype.forRow = function(y, callback) {
this.__assert__.totalArgs.call(this, "forRow", [2], arguments);
this.__assert__.areNumbers.call(this, "forRow", y);
this.__assert__.isFunction.call(this, "forRow", callback);
this.__assert__.inBounds.call(this, "forRow", 0, y);
for(var x = 0; x < this.x; x++)
{
callback(this[x][y], x, y, this);
}
};
Array2D.prototype.forCol = function(x, callback) {
this.__assert__.totalArgs.call(this, "forCol", [2], arguments);
this.__assert__.areNumbers.call(this, "forCol", x);
this.__assert__.isFunction.call(this, "forCol", callback);
this.__assert__.inBounds.call(this, "forCol", x, 0);
for(var y = 0; y < this.y; y++)
{
callback(this[x][y], x, y, this);
}
};
/*
* Fill statements
*/
//fill()
//fill(value)
Array2D.prototype.fill = function(value) {
value = value === undefined ? this.default_value : value;
this.forEach(function(v, x, y) {
this[x][y] = value;
});
return this;
};
Array2D.prototype.fillArea = function(x, y, w, h, value) {
this.__assert__.totalArgs.call(this, "fillArea", [4, 5], arguments);
this.__assert__.areNumbers.call(this, "fillArea", x, y, w, h);
value = value === undefined ? this.default_value : value;
for(var cx = x; cx < x+w; cx++)
{
for(var cy = y; cy < y+h; cy++)
{
this[cx][cy] = value;
}
}
return this;
};
//fillRow(y)
//fillRow(y, value)
Array2D.prototype.fillRow = function(y, value) {
this.__assert__.totalArgs.call(this, "fillRow", [1, 2], arguments);
this.__assert__.areNumbers.call(this, "fillRow", y);
this.__assert__.inBounds.call(this, "fillRow", 0, y);
value = value === undefined ? this.default_value : value;
for(var x = 0; x < this.x; x++)
{
this[x][y] = value;
}
return this;
};
//fillCol(y)
//fillCol(y, value)
Array2D.prototype.fillCol = function(x, value) {
this.__assert__.totalArgs.call(this, "fillCol", [1, 2], arguments);
this.__assert__.areNumbers.call(this, "fillCol", x);
this.__assert__.inBounds.call(this, "fillCol", x, 0);
value = value === undefined ? this.default_value : value;
for(var y = 0; y < this.y; y++)
{
this[x][y] = value;
}
return this;
};
/*
* Row & Column operations
*/
Array2D.prototype.getRow = function(y) {
this.__assert__.totalArgs.call(this, "getRow", [1], arguments);
this.__assert__.areNumbers.call(this, "getRow", y);
this.__assert__.inBounds.call(this, "getRow", 0, y);
var array = [];
for(var x = 0; x < this.x; x++)
{
array[x] = this[x][y];
}
return array;
};
Array2D.prototype.getCol = function(x) {
this.__assert__.totalArgs.call(this, "getCol", [1], arguments);
this.__assert__.areNumbers.call(this, "getCol", x);
this.__assert__.inBounds.call(this, "getCol", x, 0);
var array = [];
for(var y = 0; y < this.y; y++)
{
array[y] = this[x][y];
}
return array;
};
Array2D.prototype.setRow = function(y, array) {
this.__assert__.totalArgs.call(this, "setRow", [2], arguments);
this.__assert__.areNumbers.call(this, "setRow", y);
this.__assert__.inBounds.call(this, "setRow", 0, y);
this.__assert__.typeOf.call(this, "setRow", array, Array);
for(var x = 0; (x < this.x) && (x < array.length); x++)
{
this[x][y] = array[x];
}
return this;
};
Array2D.prototype.setCol = function(x, array) {
this.__assert__.totalArgs.call(this, "setCol", [2], arguments);
this.__assert__.areNumbers.call(this, "setCol", x);
this.__assert__.inBounds.call(this, "setCol", x, 0);
this.__assert__.typeOf.call(this, "setCol", array, Array);
for(var y = 0; (y < this.y) && (y < array.length); y++)
{
this[x][y] = array[y];
}
return this;
};
Array2D.prototype.swapRow = function(y1, y2) {
this.__assert__.totalArgs.call(this, "swapRow", [2], arguments);
this.__assert__.areNumbers.call(this, "swapRow", y1, y2);
this.__assert__.inBounds.call(this, "swapRow", y1, 0);
this.__assert__.inBounds.call(this, "swapRow", y2, 0);
if(y1 != y2)
{
for(var x = 0; x < this.x; x++)
{
var temp = this[x][y1];
this[x][y1] = this[x][y2];
this[x][y2] = temp;
}
}
return this;
};
Array2D.prototype.swapCol = function(x1, x2) {
this.__assert__.totalArgs.call(this, "swapCol", [2], arguments);
this.__assert__.areNumbers.call(this, "swapCol", x1, x2);
this.__assert__.inBounds.call(this, "swapCol", x1, 0);
this.__assert__.inBounds.call(this, "swapCol", x2, 0);
if(x1 != x2)
{
for(var y = 0; y < this.y; y++)
{
var temp = this[x1][y];
this[x1][y] = this[x2][y];
this[x2][y] = temp;
}
}
return this;
};
/*
//TODO
Array2D.prototype.spliceRow = function(array, y) {
this.__assert__.totalArgs.call(this, "spliceRow", [2], arguments);
this.__assert__.areNumbers.call(this, "spliceRow", y);
this.__assert__.typeOf.call(this, "spliceRow", array, Array);
return this;
};
//TODO
Array2D.prototype.spliceCol = function(array, x) {
this.__assert__.totalArgs.call(this, "spliceCol", [2], arguments);
this.__assert__.areNumbers.call(this, "spliceCol", x);
this.__assert__.typeOf.call(this, "spliceCol", array, Array);
return this;
};
*/
/*
* 2D Transformations
*/
//resize(_x)
//resize(_x, _y)
//resize(_x, _y, x_)
//resize(_x, _y, x_, y_)
Array2D.prototype.resize = function(_x, _y, x_, y_) {
this.__assert__.totalArgs.call(this, "resize", [1,2,3,4], arguments);
_x = (_x === undefined) || (isNaN(_x)) ? 0 : _x;
_y = (_y === undefined) || (isNaN(_y)) ? 0 : _y;
x_ = (x_ === undefined) || (isNaN(x_)) ? 0 : x_;
y_ = (y_ === undefined) || (isNaN(y_)) ? 0 : y_;
//compute new dimensions
var nx = this.x + _x + x_;
var ny = this.y + _y + y_;
this.__assert__.validDimensions.call(this, "resize", nx, ny);
//save a copy of this array, and rebuild for new dimensions
var existingData = new Array2D(this);
this.__build__(nx, ny, this.default_value);
var _this = this; //because of callback function below
existingData.forEach(function(v, x, y) {
//destination coordinates
var dx = x + x_;
var dy = y + y_;
if(_this.inBounds(dx, dy))
{
_this[dx][dy] = existingData[x][y];
}
});
return this;
};
Array2D.prototype.crop = function(x, y, w, h) {
this.__assert__.totalArgs.call(this, "crop", [4], arguments);
this.__assert__.areNumbers.call(this, "crop", x, y, w, h);
this.__assert__.inBounds.call(this, "crop", x, y, w, h);
this.__assert__.validDimensions.call(this, "crop", w, h);
this.resize((x+w)-this.x, (y+h)-this.y, -x, -y);
return this;
};
//shift(x, y)
//shift(x, y, wrap)
Array2D.prototype.shift = function(x, y, wrap) {
this.__assert__.totalArgs.call(this, "shift", [2,3], arguments);
this.__assert__.areNumbers.call(this, "shift", x, y);
wrap = (wrap === undefined) || (typeof wrap !== "boolean") ? true : wrap;
if((x !== 0) && (y !== 0))
{
this.resize(-x, -y, x, y);
}
return this;
};
//rotate()
//rotate(clockwise)
Array2D.prototype.rotate = function(clockwise) {
this.__assert__.totalArgs.call(this, "rotate", [0,1], arguments);
clockwise = (clockwise === undefined) || (typeof clockwise !== "boolean") ? true : clockwise;
//save a copy of this array, and rebuild for new dimensions
var existingData = new Array2D(this);
this.__build__(this.y, this.x, this.default_value);
return this;
};
Array2D.prototype.invertX = function() {
var _this = this;
var existingData = new Array2D(this);
existingData.forEach(function(v, x, y, a) {
_this[x][y] = existingData[a.x - x - 1][y];
});
return this;
};
Array2D.prototype.invertY = function() {
var _this = this;
var existingData = new Array2D(this);
existingData.forEach(function(v, x, y, a) {
_this[x][y] = existingData[x][a.y - y - 1];
});
return this;
};
/*
* Misc utilities
*/
//inBounds(x, y)
//inBounds(x, y, w, h)
Array2D.prototype.inBounds = function(x, y, w, h) {
this.__assert__.totalArgs.call(this, "inBounds", [2,4], arguments);
switch(arguments.length)
{
case 2:
return (x >= 0) && (x < this.x) && (y >= 0) && (y < this.y);
case 4:
return this.inBounds(x, y) && this.inBounds(x+w-1, y+h-1);
}
};
//log()
//log(renderFunction)
Array2D.prototype.log = function(renderFunction) {
this.__assert__.totalArgs.call(this, "log", [0,1], arguments);
if((renderFunction === undefined) || !(renderFunction instanceof Function))
{
renderFunction = function(data) { return data; }; //default render function
}
function genChars(c, l)
{
var str = "";
for(var i = 0; i < l; i++) { str += c; }
return str;
}
function padLeft(str, len)
{
var space = genChars(" ", len - str.length);
str = space + str;
return str;
}
function padRight(str, len)
{
var space = genChars(" ", len - str.length);
str = str + space;
return str;
}
var maxYWidth = String(this.y - 1).length;
var maxElementWidth = 0;
this.forEach(function(v) {
var width = String(v).length;
if(width > maxElementWidth)
{
maxElementWidth = width;
}
});
var header = genChars(" ", maxYWidth + 1) + genChars("_", (maxElementWidth + 1) * this.x) + "_";
console.log(header);
for(var y = 0; y < this.y; y++)
{
var line = padLeft(String(y), maxYWidth) + "| ";
for(var x = 0; x < this.x; x++)
{
var element = String(renderFunction(this[x][y], x, y, this));
line += padRight(element, maxElementWidth + 1);
}
console.log(line);
}
};