Collection of functions that, given a x and y value, returns an index in a one dimensional array. These functions can also be used as pairing functions. Supports AMD and use in the browser.
tdarrayindex: {
encode: function(x, y, method, negative), // method & negative are optional
decode: function(value, method, negative) // method & negative are optional
}
method = "szudzik";
A index derived through the szudzik pairing function by Matthew Szudzik at Wolfram Research. Read about it here. This is the default.
method = "triangle";
A index derived through triangular numbers. Read about it here. Does not have decode function.
method = "interleave";
A index derived by rearranging the digits of each number. Read about it here.
method = "primefactor";
A index derived through prime factors. Read about it here. Note: this method creates very large arrays. Does not have decode function.
method = "cantorpair";
A index derived through the cantor pairing function. Read about it here and read source here.
method = "bitwise";
A index derived through bitwise functions. Read the source here.
method = "hopullman";
A index derived through a pairing function devised by Hopcroft and Ullman. Read about it here. Does not have decode function.
method = "rosenstrong";
A index derived through the Rosenburg-Strong pairing function. Read about it here. Does not have decode function.
To use tdarrayindex.decode
, just input value from tdarrayindex.encode
to get an array containing the x and y value.
Example:
var test = tdarrayindex.encode(2, 4, "szudzik"); // 18
var xandy = tdarrayindex.decode(test, "szudzik"); // [2, 4]
negative = false
this specifies that negative values for either x or y are not to be supported. This is the default.
negative = true
This specifies that negative values for either x or y are supported. Be warned that this method makes twice as large arrays if you only use positive values!
Check out this page for average function times and efficiency.
Note: This library only returns the index or gets x and y values from an index. It does not return a array or mutate an existing array.
If you like this javascript library, a ⭐ would be appreciated!