1 <!-- Copyleft 2004 - see COPYING for details [LGPL] -->
5 Author: Charles Goodwin
11 // start of static content
13 newVector = function(v) {
14 var new_vec = ibex.box;
15 ibex..ibex.util.vector(new_vec);
16 if (v) new_vec.push(v);
20 // end of static content
24 var elements = {}; // hash containing all our elements
25 var obj1; // the first (#1) object in elements
26 var objn; // the nth (last) object in elements
27 var count = 0; // counts the number of elements
29 // private function to add to an empty elements
30 var addtoempty = function(v) {
31 elements[v] = { self: v };
37 // private function to leave elements empty
38 var makesempty = function(v) {
45 // read trap to return the first object in elements
46 thisbox.first ++= function() { return obj1; }
48 // read trap to return the last object in elements
49 thisbox.last ++= function() { return objn; }
51 // read trap to return the length of elements
52 thisbox.length ++= function() { return count; }
54 // function to return the object after v in elements
55 thisbox.after = function(v) { return elements[v].next ? elements[v].next.self : null; }
57 // function to return the object before v in elements
58 thisbox.before = function(v) { return elements[v].prev ? elements[v].prev.self : null; }
60 // function to check where elements contains v
61 thisbox.contains = function(v) { return elements[v] ? true : false; }
63 // function to insert v optionally after w
64 thisbox.insert = function(v, w) {
65 if (arguments.length == 1) return push(v);
66 else if (!elements[v] and elements[w]) {
67 elements[v] = { next: elements[w].next, prev: w, self: v };
68 elements[elements[w].next].prev = v;
76 // function to remove v
77 thisbox.remove = function(v) {
79 if (count == 1) return makesempty(v);
81 elements[elements[v].prev].next = elements[v].next;
82 elements[elements[v].next].prev = elements[v].prev;
91 // function to pop last object from elements
92 thisbox.pop = function() {
93 if (count == 1) return makesempty(objn);
96 elements[elements[objn].prev].next = null;
97 elements[drop] = null;
98 objn = elements[elements[drop].prev].self;
105 // function to push v onto the end of elements
106 thisbox.push = function(v) {
107 if (!count) return addtoempty(v);
108 else if (!elements[v]) {
109 elements[v] = { prev: elements[objn], self: v };
110 elements[objn].next = elements[v];
118 // function to shift first object from elements
119 thisbox.shift = function() {
120 if (count == 1) return makesempty(obj1);
123 elements[elements[drop].next].prev = null;
124 elements[drop] = null;
125 obj1 = elements[elements[drop].next].self;
132 // function to unshfit v onto the front of elements
133 thisbox.unshift = function(v) {
134 if (!count) return addtoempty(v);
135 else if (!elements[v]) {
136 elements[v] = { next: elements[obj1], self: v };
137 elements[obj1].prev = elements[v];