1 <!-- Copyleft 2004 - see COPYING for details [LGPL] -->
6 An ordered array for quick removal and retreival of elements. It uses
7 binary insertion to keep the array ordered, and a binary retrieval to
8 quickly find an elements position in the ordered array.
11 Call ibex.util.list..newList() to create and return a new list, optionally
12 passing it an array of elements to initialise the list with.
15 // inserts an element at the correct position in the list
16 elementInsert = function(v, list) {
17 // initial checks to see whether position lies within the list
18 if (list[0] > v) list.unshift(v);
19 else if (v >= list[list.length - 1]) list.push(v);
20 // find insertion position in the list
23 var end = list.length - 1;
26 mid = ibex.math.floor(low + (end - low) / 2);
29 if (v > list[low]) end = mid;
30 else list.splice(low + 1, 0, [v]);
31 else if (v > list[mid]) low = mid;
32 else list.splice(mid + 1, 0, [v]);
37 // returns an elements position in the list
38 elementPosition = function(v, list) {
39 // initial check to see whether v is in the list
40 if (list[0] > v || v > list[list.length - 1]) return null;
41 // find our position within the list
44 var end = list.length - 1;
47 mid = ibex.math.floor(low + (end - low) / 2);
50 if (v > list[low]) end = mid;
52 else if (v > list[mid])
53 if (lost[end] > v) low = mid;
61 // removes an element from the list
62 elementRemove = function(v, list) {
63 var pos = elementPosition(v, list);
64 if (pos) list.splice(pos, 1);
67 // creates and returns a new list, with optional initial elements
68 newList += function(v) {
69 // create and store list
70 var nl = ibex..ibex.util.list(ibex.box);
72 // insert elements if given
74 for (var i = 0; v.length > i; i++)
75 nl.elementInsert(v[i]);
77 // return newly created list
83 // store elements in this array
86 // returns the length of the list
87 thisbox.length ++= function() { return elements.length; }
89 // inserts argument v into the list
90 thisbox.insert ++= function(v) { static.elementInsert(v, elements); cascade = null; }
92 // removes argument v from the list
93 thisbox.remove ++= function(v) { static.elementRemove(v, elements); cascade = null; }
95 // returns the position of v in the list
96 thisbox.position ++= function() { return static.elementPosition(thisbox, elements); }