/
util.js
37 lines (34 loc) · 981 Bytes
/
util.js
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
var Util = function(){};
/*
@swap
objects and arrays muat have the same number of elements / properties
objects must have the same property names
numbers cannot be swaped in-place since they are never passed by reference
*/
Util.prototype.swap = function(a, b) {
var temp;
if (typeof a !== typeof b) throw 'type mismatch: both arguments must be the same type';
if (typeof a !== 'object' && typeof a !== 'array') {
temp = a;
a = b;
b = temp;
return [a, b]
} else if (typeof a.length !== 'undefined') {
if (a.length !== b.length) throw 'Error: arrays must have the same length';
temp = $.extend(true, [], a);
}
else {
temp = $.extend(true, {}, a);
}
$.each(a, function(i, v){
a[i] = b[i];
b[i] = temp[i];
});
return true
}
Util.prototype.init_2d_arr = function(size, arr){
for(var i = 0; i < size; i++){
arr[i] = [];
}
return arr;
}