From: Evertjan. on
VK wrote on 01 mei 2010 in comp.lang.javascript:

> Indeed I was getting nuts by trying to visualize that "5-dimensional
> array of 72 elements" because I hate to use things I do not "see" even
> if they work properly.

Having a 5 dimensional array, which each array maximized at 1000 is easy,
if you do not fill the array to much.

Use an object [here 3 members are used]:

<script type='text/javascript'>

var my5dim = {
'000!235!148!999!400':'qwerty',
'074!088!002!000!874':'asd',
'000!088!148!999!401':'blah'};

function getArrayVal(a,b,c,d,e) {
return my5dim[t3(a)+'!'+t3(b)+'!'+t3(c)+'!'+t3(d)+'!'+t3(e)];
};

function t3(x){return (1000+x+'').substr(1)};

alert( getArrayVal(0,235,148,999,400) ); // qwerty
alert( getArrayVal(0,235,148,999,401) ); // undefined

</script>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
From: VK on
On May 1, 3:44 pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net> wrote:
> VK wrote on 01 mei 2010 in comp.lang.javascript:
>
> > Indeed I was getting nuts by trying to visualize that "5-dimensional
> > array of 72 elements" because I hate to use things I do not "see" even
> > if they work properly.
>
> Having a 5 dimensional array, which each array maximized at 1000 is easy,
> if you do not fill the array to much.
>
> Use an object [here 3 members are used]:
>
> <script type='text/javascript'>
>
> var my5dim = {
>  '000!235!148!999!400':'qwerty',
>  '074!088!002!000!874':'asd',
>  '000!088!148!999!401':'blah'};
>
> function getArrayVal(a,b,c,d,e) {
>   return my5dim[t3(a)+'!'+t3(b)+'!'+t3(c)+'!'+t3(d)+'!'+t3(e)];
>
> };
>
> function t3(x){return (1000+x+'').substr(1)};
>
> alert( getArrayVal(0,235,148,999,400) ); // qwerty
> alert( getArrayVal(0,235,148,999,401) ); // undefined
>
> </script>

There is always a way to do something in several ways. I am just
"felling in love" with trees and the idea of thinking of trees, node
levels, leaves and paths rather than of nested arrays, indexes and
array elements. It seems much more visual and elegant, not talking of
production and usage cost.

<script type="text/javascript">

function BinaryTree(depth) {

/* Building a perfect binary tree */

// Prevent calling our constructor
// as a regular function:
if (!(this instanceof BinaryTree)) {
return new Error('Illegal constructor call');
}

// Argument validity check:
if (
(typeof depth != 'number') ||
(depth == 0) ||
(depth != Math.round(depth))
) {
return new Error('Illegal depth value');
}

// The Array instance index must be in the range
// [0, 4294967295] which is
// [0, 11111111111111111111111111111111] binary
// With the "holding bit" usage that gives us
// 31 dimensions/ tree depth limit or 32 without
// that bit.
if (depth > 31) {
return new Error('Depth value is out of range');
}

var Tree = new Array();
var lBound = '';
var uBound = '';

do {
lBound+= '0';
} while (lBound.length < depth);
lBound = parseInt('1'+lBound, 2);

do {
uBound+= '1';
} while (uBound.length < depth);
uBound = parseInt('1'+uBound, 2);

for (var i = lBound; i<uBound; i++) {
Tree[i] = 0; // or another init value
}

this.Tree = Tree;
this.tree = this.Tree // lower case alias
this.lBound = lBound;
this.uBound = uBound;
this.length = i;
this.get = BinaryTree.get;
this.set = BinaryTree.set;
}

BinaryTree.get = function(i) {
return this.Tree[parseInt(i,2)];
}

BinaryTree.set = function(i, val) {
this.Tree[parseInt(i,2)] = val;
}


// 20 levels depth, 2097151 leaves
var tree = new BinaryTree(20);

window.alert(
tree.get('100000000000000000011')
); // 0

tree.set('100000000000000000011', 1);

window.alert(
tree.get('100000000000000000011')
); // 1
</script>


The instantiation of 20 level (dimensions) in depth structure with
2,097,151 leaves makes Fx even on my Intel Dual 1.6GHz to think for a
couple of second, but nothing close to a crucial delay. After that the
get/set usage cost is negligible.

From: VK on
On May 1, 6:16 pm, VK <schools_r...(a)yahoo.com> wrote:
> The instantiation of 20 level (dimensions) in depth structure with
> 2,097,151 leaves makes Fx even on my Intel Dual 1.6GHz to think for a
> couple of second, but nothing close to a crucial delay. After that the
> get/set usage cost is negligible.

Full capacity 31 levels in depth tree (2,147,483,648 leaves) will take
approx. 1024 times longer. On the same system it would be approx. 34
mins to wait which is way beyond a regular acceptable waiting period.
So if one needs to use trees with more than 20 levels in depth another
approach should be used. I guess the Object instance augmentation
should be used without the initial properties instantiation, so
undefined or 0 both treated as 0.
I just don't need such monstrosity at this moment, so not coding that
variant.


From: VK on
On May 1, 6:34 pm, VK <schools_r...(a)yahoo.com> wrote:
> On May 1, 6:16 pm, VK <schools_r...(a)yahoo.com> wrote:
>
> > The instantiation of 20 level (dimensions) in depth structure with
> > 2,097,151 leaves makes Fx even on my Intel Dual 1.6GHz to think for a
> > couple of second, but nothing close to a crucial delay. After that the
> > get/set usage cost is negligible.
>
> Full capacity 31 levels in depth tree (2,147,483,648 leaves) will take
> approx. 1024 times longer. On the same system it would be approx. 34
> mins to wait which is way beyond a regular acceptable waiting period.
> So if one needs to use trees with more than 20 levels in depth another
> approach should be used. I guess the Object instance augmentation
> should be used without the initial properties instantiation, so
> undefined or 0 both treated as 0.
> I just don't need such monstrosity at this moment, so not coding that
> variant.

Just as a proof of concept I've made a working ternary tree
constructor ready for base-3 operations:

<script type="text/javascript">

function TernaryTree(depth) {

/* Building a perfect ternary tree */

// Prevent calling our constructor
// as a regular function:
if (!(this instanceof TernaryTree)) {
return new Error('Illegal constructor call');
}

// Argument validity check:
if (
(typeof depth != 'number') ||
(depth == 0) ||
(depth != Math.round(depth))
) {
return new Error('Illegal depth value');
}

// The Array instance index must be in the range
// [0, 4294967295] which is
// [0, 11111111111111111111111111111111] binary
// With the "holding bit" usage that gives us
// 10 dimensions/ tree depth limit as we have
// 111 111 111 111 111 111 111 111 111 111 for trits.
// That is 613,566,756 leaf nodes.
if (depth > 10) {
return new Error('Depth value is out of range');
}
var Tree = new Array();
var lBound = '';
var uBound = '';
var _depth = depth * 3;

do {
lBound+= '000';
} while (lBound.length < _depth);
lBound = parseInt('1'+lBound, 2);

do {
uBound+= '100';
} while (uBound.length < _depth);
uBound = parseInt('1'+uBound, 2);

for (var i = lBound; i<uBound; i++) {
Tree[i] = 0; // or another init value
}

this.Tree = Tree;
this.tree = this.Tree // lower case alias
this.lBound = lBound;
this.uBound = uBound;
this.length = i;
this.get = TernaryTree.get;
}

TernaryTree.get = function(trits) {
var path = trits.join('');
return this.Tree[parseInt('1'+path,2)];
}



var left = '001';
var right = '100';
var straight = '010';

var tree = null;

// 5 levels depth, 18724(?) leaves
if ((tree = new TernaryTree(5)) instanceof Error) {
window.alert(tree.message);
}
else {
window.alert(tree.get([
left, left, right, straight, left
])); // 0
}
</script>
From: Spamless on
On 2010-05-01, Evertjan. <exjxw.hannivoort(a)interxnl.net> wrote:
> VK wrote on 01 mei 2010 in comp.lang.javascript:
>
>> Indeed I was getting nuts by trying to visualize that "5-dimensional
>> array of 72 elements" because I hate to use things I do not "see" even
>> if they work properly.
>
> Having a 5 dimensional array, which each array maximized at 1000 is easy,
> if you do not fill the array to much.
>
> Use an object [here 3 members are used]:
>
><script type='text/javascript'>
>
> var my5dim = {
> '000!235!148!999!400':'qwerty',
> '074!088!002!000!874':'asd',
> '000!088!148!999!401':'blah'};

I was writing a little recursive routine in Javascript (it's a simple
language and I have the spider monkey interpreter) needing what was
effectively a two-dimensional array and originally used an object
(keys: ""+i1+","+i2)

In use, parameters were 100 to 500 and ... well, it was OK, but a
bit slow so I implemented it as a one dimensional array with
_indexer (one-one map from whole number pairs to whole numbers)
and that sped it up *quite* dramatically. It seems that with thousands
of keys, Javascript is not too efficient in searching the key space
to get a value. Perhaps I should have tried perl!