-
Notifications
You must be signed in to change notification settings - Fork 1
/
2darray.js
137 lines (132 loc) · 5.94 KB
/
2darray.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
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
// 2darray.js by TheGreatRambler https://github.com/TheGreatRambler/2D-Array-JS/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
root["tdarrayindex"] = factory();
}
}(typeof self !== 'undefined' ? self : this, function() {
var tdarrayindex = {
encode: function(xval, yval, method, negative) {
var x, y;
if (typeof negative === "undefined" || negative === false) {
x = xval;
y = yval;
} else if (negative === true) {
x = xval >= 0 ? xval * 2 : xval * -2 - 1;
y = yval >= 0 ? yval * 2 : yval * -2 - 1;
} else {
console.warn(negative + " is not valid; this option must be a bool");
}
if (!method || method === "szudzik") {
var number = (x >= y) ? (x * x + x + y) : (y * y + x);
return number;
} else if (method === "interleave") {
var xstring = x.toString();
var ystring = y.toString();
if (xstring.length > ystring.length) {
var zeroarray = Array(xstring.length - ystring.length + 1).join("0");
ystring = zeroarray + ystring;
} else if (ystring.length > xstring.length) {
var zeroarray = Array(ystring.length - xstring.length + 1).join("0");
xstring = zeroarray + xstring;
}
var l = xstring.length;
var finalstring = '';
for (var i = 0; i < l; i++) {
finalstring += xstring[i] + ystring[i];
}
return Number(finalstring);
} else if (method === "triangle") {
var number = (Math.pow((x + y - 1), 2) + x + y - 1) / 2 + y;
return number;
} else if (method === "primefactor") {
var number = Math.pow(2, y) * Math.pow(3, x);
return number;
} else if (method === "cantorpair") {
var number = ((x + y) * (x + y + 1) / 2) + y;
return number;
} else if (method === "bitwise") {
var number = x << 16 & 0xffff0000 | y & 0x0000ffff;
return number;
} else if (method === "hopullman") {
var number = ((x + y - 2) * (x + y - 1)) / 2 + x;
return number;
} else if (method === "rosenstrong") {
var number = Math.pow((Math.max(x, y)), 2) + Math.max(x, y) + x - y;
return number;
} else {
console.warn(method + " is not a valid method");
}
},
decode: function(val, method, negative, debug) {
var debugbool;
if (typeof debug === "undefined" || debug === true) {
debugbool = true;
} else if (debug === false) {
debugbool = false;
} else {
console.warn(debug + "incorrect option; must be bool");
}
var result;
if (!method || method === "szudzik") {
var sqrtz = Math.floor(Math.sqrt(val)),
sqz = sqrtz * sqrtz;
result = ((val - sqz) >= sqrtz) ? [sqrtz, val - sqz - sqrtz] : [val - sqz, sqrtz];
} else if (method === "interleaving") {
var numstring = val.toString();
var resultstringx;
var resultstringy;
if (numstring.length % 2 !== 0) {
numstring = "0" + numstring;
}
for (var f = 0; f < numstring.length - 1; f += 2) {
resultstringx += numstring.charAt(f);
resultstringy += numstring.charAt(f + 1);
}
result = [Number(resultstringx), Number(resultstringy)];
} else if (method === "triangle") {
if (debugbool) {
console.warn("decoding method for triangular number does not exist");
}
} else if (method === "primefactor") {
if (debugbool) {
console.warn("decoding method for prime factorization does not exist");
}
} else if (method === "cantorpair") {
var t = Math.floor((Math.sqrt(1 + 8 * val) - 1) / 2);
var xval = t * (t + 3) / 2 - val;
var yval = val - t * (t + 1) / 2;
result = [xval, yval];
} else if (method === "bitwise") {
var xval = val >> 16 & 0xFFFF;
var yval = val & 0xFFFF;
result = [xval, yval];
} else if (method === "hopullman") {
if (debugbool) {
console.warn("decoding method for Hopkins Ullman pairing algorithm does not exist");
}
} else if (method === "rosenstrong") {
// var m = Math.floor(Math.sqrt(val));
// result = (val - (m ^ 2) < m) ? [val - (m ^ 2), m] : [m, (m ^ 2) - (2 * m) - val];
if (debugbool) {
console.warn("decoding method for Rosenburg Strong pairing algorithm does not exist");
}
} else {
console.warn(method + " is not a valid method");
}
if (!negative || negative === false) {
return result;
} else if (negative === true) {
var x = result[0] % 2 === 0 ? result[0] / 2 : ((result[0] + 1) / 2) * -1;
var x = result[1] % 2 === 0 ? result[1] / 2 : ((result[1] + 1) / 2) * -1;
return [x, y];
} else {
console.warn(negative + " is not valid; this option must be a bool");
}
}
};
return tdarrayindex;
}));