V
VK
While working on JSONet project
<http://groups.google.com/group/comp..._frm/thread/f083e3925a345f31/bc987f7013afde92>
I came to the conclusion (possibly wrongly) that Vector data type is
much more convenient than Array or Hashtable to keep threads count.
Major benefits: i) "real" length at any given time, ii) auto
shrink/expand on adding/removing new items. Overall it seems to give as
given a lot of things you have to emulate by multiple pass over
Array/Hashtable elements.
The code below is quick'n'durty version of what I'm thinking to
implement. Is it total b.s.? (the idea, not the code).
Also please note that in this code I used my PGO (Pretty Good
Objecting) coding style which wollows the OOP style and namespace
security while let you relax about the infamous "this" issue.
<html>
<head>
<title>Vector</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function VK_VectorAdd(v) {
var i = document.createElement('OPTION');
if (typeof v == 'object') {
i.value = v.id || v.toString();
}
else {
i.value = v;
}
this.$_vector.appendChild(i);
}
function VK_VectorItem(i) {
return this.$_vector.options.value;
}
function VK_VectorRemove(i) {
this.$_vector.options = null;
}
function VK_VectorClear() {
this.$_vector.options.length = 0;
}
function VK_VectorSize() {
return this.$_vector.options.length;
}
function Vector() {
if (!(document.createElement)) {return null;}
this.$_vector = document.createElement('SELECT');
this.add = VK_VectorAdd;
this.item = VK_VectorItem;
this.remove = VK_VectorRemove;
this.clear = VK_VectorClear;
this.size = VK_VectorSize;
}
function demo() {
var v = new Vector();
v.add(1);
v.add('foo');
alert(v.size());
v.remove(0);
alert(v.size());
alert(v.item(0));
}
window.onload = demo;
</script>
</head>
<body>
</body>
</html>
<http://groups.google.com/group/comp..._frm/thread/f083e3925a345f31/bc987f7013afde92>
I came to the conclusion (possibly wrongly) that Vector data type is
much more convenient than Array or Hashtable to keep threads count.
Major benefits: i) "real" length at any given time, ii) auto
shrink/expand on adding/removing new items. Overall it seems to give as
given a lot of things you have to emulate by multiple pass over
Array/Hashtable elements.
The code below is quick'n'durty version of what I'm thinking to
implement. Is it total b.s.? (the idea, not the code).
Also please note that in this code I used my PGO (Pretty Good
Objecting) coding style which wollows the OOP style and namespace
security while let you relax about the infamous "this" issue.
<html>
<head>
<title>Vector</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function VK_VectorAdd(v) {
var i = document.createElement('OPTION');
if (typeof v == 'object') {
i.value = v.id || v.toString();
}
else {
i.value = v;
}
this.$_vector.appendChild(i);
}
function VK_VectorItem(i) {
return this.$_vector.options.value;
}
function VK_VectorRemove(i) {
this.$_vector.options = null;
}
function VK_VectorClear() {
this.$_vector.options.length = 0;
}
function VK_VectorSize() {
return this.$_vector.options.length;
}
function Vector() {
if (!(document.createElement)) {return null;}
this.$_vector = document.createElement('SELECT');
this.add = VK_VectorAdd;
this.item = VK_VectorItem;
this.remove = VK_VectorRemove;
this.clear = VK_VectorClear;
this.size = VK_VectorSize;
}
function demo() {
var v = new Vector();
v.add(1);
v.add('foo');
alert(v.size());
v.remove(0);
alert(v.size());
alert(v.item(0));
}
window.onload = demo;
</script>
</head>
<body>
</body>
</html>