initial checkin
[org.ibex.widgets.git] / src / ibex / util / vector.t
1 <!-- Copyleft 2004 - see COPYING for details [LGPL] -->
2
3 <ibex>
4     <meta:doc>
5         Author: Charles Goodwin
6
7         TODO:
8             Add move(v) function
9     </meta:doc>
10
11     // start of static content
12
13     newVector = function(v) {
14         var new_vec = ibex.box;
15         ibex..ibex.util.vector(new_vec);
16         if (v) new_vec.push(v);
17         return new_vec;
18     }
19
20     // end of static content
21
22     <ui:box>
23
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
28
29         // private function to add to an empty elements
30         var addtoempty = function(v) {
31             elements[v] = { self: v };
32             obj1 = objn = v;
33             ++count;
34             return true;
35         }
36
37         // private function to leave elements empty
38         var makesempty = function(v) {
39             elements[v] = null;
40             obj1 = objn = null;
41             --count;
42             return true;
43         }
44
45         // read trap to return the first object in elements
46         thisbox.first ++= function() { return obj1; }
47
48         // read trap to return the last object in elements
49         thisbox.last ++= function() { return objn; }
50
51         // read trap to return the length of elements
52         thisbox.length ++= function() { return count; }
53
54         // function to return the object after v in elements
55         thisbox.after = function(v) { return elements[v].next ? elements[v].next.self : null; }
56
57         // function to return the object before v in elements
58         thisbox.before = function(v) { return elements[v].prev ? elements[v].prev.self : null; }
59
60         // function to check where elements contains v
61         thisbox.contains = function(v) { return elements[v] ? true : false; }
62
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;
69                 elements[w].next = v;
70                 ++count;
71                 return true;
72             }
73             else return false;
74         }
75
76         // function to remove v
77         thisbox.remove = function(v) {
78             if (elements[v]) {
79                 if (count == 1) return makesempty(v);
80                 else {
81                     elements[elements[v].prev].next = elements[v].next;
82                     elements[elements[v].next].prev = elements[v].prev;
83                     elements[v] = null;
84                     --count;
85                     return true;
86                 }
87             }
88             else return false;
89         }
90
91         // function to pop last object from elements
92         thisbox.pop = function() {
93             if (count == 1) return makesempty(objn);
94             else if (count) {
95                 var drop = objn;
96                 elements[elements[objn].prev].next = null;
97                 elements[drop] = null;
98                 objn = elements[elements[drop].prev].self;
99                 --count;
100                 return drop;
101             }
102             else return null;
103         }
104
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];
111                 objn = v;
112                 ++count;
113                 return true;
114             }
115             return false;
116         }
117
118         // function to shift first object from elements
119         thisbox.shift = function() {
120             if (count == 1) return makesempty(obj1);
121             else if (count) {
122                 var drop = obj1;
123                 elements[elements[drop].next].prev = null;
124                 elements[drop] = null;
125                 obj1 = elements[elements[drop].next].self;
126                 --count;
127                 return drop;
128             }
129             else return null;
130         }
131
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];
138                 obj1 = v;
139                 ++count;
140                 return true;
141             }
142             else return false;
143         }
144
145     </ui:box>
146 </ibex>