1 | |
import java.util.*; |
2 | |
|
3 | 3065 | abstract class absVector { |
4 | |
|
5 | |
static final boolean equals (absVector x, absVector y) { |
6 | |
|
7 | 1572 | if (x instanceof absEmpty && y instanceof absEmpty) { |
8 | 157 | return ((absEmpty) x).size==((absEmpty) y).size |
9 | |
&& ((absEmpty) x).increment==((absEmpty) y).increment |
10 | |
&& ((absEmpty) x).capacity==((absEmpty) y).capacity; |
11 | 1415 | } else if (x instanceof absMap && y instanceof absMap) { |
12 | 1415 | return ((absMap) x).index==((absMap) y).index |
13 | |
&& ((absMap) x).object.equals(((absMap) y).object) |
14 | |
&& equals(((absMap) x).vector,((absMap) y).vector); |
15 | |
} else { |
16 | 0 | return false; |
17 | |
} |
18 | |
} |
19 | |
|
20 | |
static final Object [] copyInto (absVector v) { |
21 | 0 | Object [] array = new TestDriver.Vobject [size(v)]; |
22 | 0 | while (v instanceof absMap) { |
23 | 0 | array [((absMap) v).index] = ((absMap) v).object; |
24 | 0 | v = ((absMap) v).vector; |
25 | |
} |
26 | 0 | return array; |
27 | |
} |
28 | |
|
29 | |
static final absVector trimToSize (absVector v) { |
30 | 349 | if (v instanceof absEmpty) { |
31 | 38 | return new absEmpty ( |
32 | |
((absEmpty) v).size, |
33 | |
((absEmpty) v).increment, |
34 | |
((absEmpty) v).size); |
35 | |
} else { |
36 | 311 | return new absMap ( |
37 | |
((absMap) v).index, |
38 | |
((absMap) v).object, |
39 | |
trimToSize (((absMap) v).vector)); |
40 | |
} |
41 | |
} |
42 | |
|
43 | |
static final absVector ensureCapacity (int minCapacity, absVector v) { |
44 | 343 | if (v instanceof absEmpty) { |
45 | 62 | int incr = ((absEmpty) v).increment; |
46 | 62 | int oldCap = ((absEmpty) v).capacity; |
47 | 62 | int newCap = |
48 | |
(minCapacity > oldCap) |
49 | |
? Math.max (minCapacity, incr==0 ? 2 * oldCap : oldCap + incr) |
50 | |
: oldCap; |
51 | 62 | return new absEmpty (((absEmpty) v).size, incr, newCap); |
52 | |
} else { |
53 | 281 | return new absMap ( |
54 | |
((absMap) v).index, |
55 | |
((absMap) v).object, |
56 | |
ensureCapacity (minCapacity, ((absMap) v).vector)); |
57 | |
} |
58 | |
} |
59 | |
|
60 | |
static final absVector setSize (int newSize, absVector v) { |
61 | 0 | if (newSize > size(v)) |
62 | 0 | return setSize1(newSize, ensureCapacity (newSize, v)); |
63 | |
else |
64 | 0 | return setNullAbove (newSize, v); |
65 | |
} |
66 | |
|
67 | |
static final absVector setSize1 (int newSize, absVector v) { |
68 | 0 | if (v instanceof absEmpty) { |
69 | 0 | return new absEmpty ( |
70 | |
newSize, |
71 | |
((absEmpty) v).increment, |
72 | |
((absEmpty) v).capacity); |
73 | |
} else { |
74 | 0 | return new absMap ( |
75 | |
((absMap) v).index, |
76 | |
((absMap) v).object, |
77 | |
setSize1 (newSize, ((absMap) v).vector)); |
78 | |
} |
79 | |
} |
80 | |
|
81 | |
|
82 | |
static final private absVector setNullAbove (int newSize, absVector v) { |
83 | 0 | if (v instanceof absEmpty) { |
84 | 0 | return new absEmpty ( |
85 | |
newSize, |
86 | |
((absEmpty) v).increment, |
87 | |
((absEmpty) v).capacity); |
88 | |
} else { |
89 | 0 | absVector tmp = setNullAbove (newSize, ((absMap) v).vector); |
90 | 0 | int i = ((absMap) v).index; |
91 | 0 | if (i >= newSize) return tmp; |
92 | 0 | else return new absMap (((absMap) v).index, ((absMap) v).object, tmp); |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
static final int capacity (absVector v) { |
97 | 382 | if (v instanceof absEmpty) return ((absEmpty) v).capacity; |
98 | 341 | else return capacity(((absMap) v).vector); |
99 | |
} |
100 | |
|
101 | |
static final int size (absVector v) { |
102 | 2137 | if (v instanceof absEmpty) return ((absEmpty) v).size; |
103 | 1917 | else return size (((absMap) v).vector); |
104 | |
} |
105 | |
|
106 | |
static final boolean isEmpty (absVector v) { |
107 | 0 | return size(v) == 0; |
108 | |
} |
109 | |
|
110 | |
static final Enumeration elements (Vector concrete, absVector v) { |
111 | 199 | return new VectorEnumerator(concrete); |
112 | |
} |
113 | |
|
114 | |
static final boolean contains (Object o, absVector v) { |
115 | 310 | if (v instanceof absEmpty) return false; |
116 | 286 | else if (((absMap) v).object.equals(o)) return true; |
117 | 271 | else return contains (o, ((absMap) v).vector); |
118 | |
} |
119 | |
|
120 | |
static final int indexOf (Object o, absVector v) { |
121 | 151 | return indexOf (o, 0, v); |
122 | |
} |
123 | |
|
124 | |
static final int indexOf (Object o, int i, absVector v) { |
125 | |
|
126 | 3418 | if (v instanceof absEmpty) return -1; |
127 | |
else { |
128 | 3056 | int next = indexOf (o, i, ((absMap) v).vector); |
129 | 3056 | if (((absMap) v).object.equals(o)) { |
130 | 188 | int curr = ((absMap) v).index; |
131 | 188 | if (curr >= i && (next == -1 || curr < next)) return curr; |
132 | 85 | else return next; |
133 | 2868 | } else return next; |
134 | |
} |
135 | |
} |
136 | |
|
137 | |
static final int lastIndexOf (Object o, absVector v) { |
138 | 45 | return lastIndexOf (o, size(v)-1, v); |
139 | |
} |
140 | |
|
141 | |
static final int lastIndexOf (Object o, int i, absVector v) { |
142 | |
|
143 | 917 | if (v instanceof absEmpty) return -1; |
144 | |
else { |
145 | 826 | int next = lastIndexOf (o, i, ((absMap) v).vector); |
146 | 826 | if (((absMap) v).object.equals(o)) { |
147 | 111 | int curr = ((absMap) v).index; |
148 | 111 | if (curr <= i && (next == -1 || curr > next)) return curr; |
149 | 0 | else return next; |
150 | 715 | } else return next; |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
static final Object elementAt(int index, absVector v) { |
155 | 43 | if (v instanceof absEmpty) { |
156 | 0 | if (index < ((absEmpty) v).capacity) return null; |
157 | 0 | else throw new RuntimeException ("absVector elementAt CONSISTENCY"); |
158 | |
} else { |
159 | 43 | if (((absMap) v).index == index) |
160 | 43 | return ((absMap) v).object; |
161 | |
else |
162 | 0 | return elementAt(index, ((absMap) v).vector); |
163 | |
} |
164 | |
|
165 | |
} |
166 | |
|
167 | |
static final Object firstElement(absVector v) { |
168 | 345 | if (v instanceof absEmpty) { |
169 | 0 | if (((absEmpty) v).capacity > 0) return null; |
170 | 0 | else throw new RuntimeException ("absVector firstElement CONSISTENCY"); |
171 | |
} else { |
172 | 345 | if (((absMap) v).index == 0) return ((absMap) v).object; |
173 | 309 | else return firstElement (((absMap) v).vector); |
174 | |
} |
175 | |
} |
176 | |
|
177 | |
static final Object lastElement(absVector v) { |
178 | 43 | return elementAt (size(v)-1, v); |
179 | |
} |
180 | |
|
181 | |
static final absVector setElementAt (Object o, int i, absVector v) { |
182 | 0 | if (v instanceof absEmpty) { |
183 | 0 | if (i < 0 || i >= ((absEmpty) v).capacity) |
184 | 0 | throw new RuntimeException ("absVector setElementAt CONSISTENCY"); |
185 | 0 | else return new absMap (i, o, v); |
186 | |
} else { |
187 | 0 | absVector tmp = ((absMap) v).vector; |
188 | 0 | int j = ((absMap) v).index; |
189 | 0 | if (j == i) return new absMap (i, o, tmp); |
190 | 0 | else return new absMap (j, ((absMap) v).object, setElementAt (o, i, tmp)); |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
static final absVector removeElementAt (int i, absVector v) { |
195 | 191 | if (v instanceof absEmpty) { |
196 | 15 | if (i < 0 || i >= ((absEmpty) v).capacity) |
197 | |
|
198 | 0 | throw new RuntimeException ("absVector removeElementAt CONSISTENCY"); |
199 | 15 | else return new absEmpty ( |
200 | |
((absEmpty) v).size-1, |
201 | |
((absEmpty) v).increment, |
202 | |
((absEmpty) v).capacity); |
203 | |
} else { |
204 | 176 | absVector tmp = ((absMap) v).vector; |
205 | 176 | int j = ((absMap) v).index; |
206 | 176 | if (i > j) return new absMap (j, ((absMap) v).object, removeElementAt (i, tmp)); |
207 | 117 | else if (i == j) return removeElementAt (i, tmp); |
208 | 102 | else return new absMap (j-1, ((absMap) v).object, removeElementAt (i, tmp)); |
209 | |
} |
210 | |
} |
211 | |
|
212 | |
static final absVector insertElementAt (Object o, int i, absVector v) { |
213 | 0 | if (v instanceof absEmpty) { |
214 | 0 | int tmp = ((absEmpty) v).size+1; |
215 | 0 | if (i < 0 || i >= tmp) |
216 | 0 | throw new RuntimeException ("absVector insertElementAt CONSISTENCY"); |
217 | |
else |
218 | 0 | return new absMap (i, o, ensureCapacity (tmp, |
219 | |
new absEmpty (tmp, |
220 | |
((absEmpty) v).increment, |
221 | |
((absEmpty) v).capacity))); |
222 | |
} else { |
223 | 0 | absVector tmp = ((absMap) v).vector; |
224 | 0 | Object obj = ((absMap) v).object; |
225 | 0 | int j = ((absMap) v).index; |
226 | 0 | if (j >= i) return new absMap (j+1, obj, insertElementAt (o, i, tmp)); |
227 | 0 | else return new absMap (j, obj, insertElementAt (o, i, tmp)); |
228 | |
} |
229 | |
} |
230 | |
|
231 | |
static final absVector addElement (Object o, absVector v) { |
232 | 312 | if (v instanceof absEmpty) { |
233 | 31 | int tmp = ((absEmpty) v).size; |
234 | 31 | return new absMap (tmp, o, |
235 | |
ensureCapacity(tmp+1, new absEmpty ( |
236 | |
tmp+1, |
237 | |
((absEmpty) v).increment, |
238 | |
((absEmpty) v).capacity))); |
239 | |
} else { |
240 | 281 | return new absMap ( |
241 | |
((absMap) v).index, |
242 | |
((absMap) v).object, |
243 | |
addElement (o, ((absMap) v).vector)); |
244 | |
|
245 | |
} |
246 | |
} |
247 | |
|
248 | |
static final boolean removeElement (Object o, absVector v) { |
249 | 41 | return indexOf(o, v) >= 0; |
250 | |
} |
251 | |
|
252 | |
static final absVector removeElementSide (Object o, absVector v) { |
253 | 26 | int index = indexOf(o, v); |
254 | 26 | if (index >= 0) return removeElementAt(index, v); |
255 | 26 | else return v; |
256 | |
} |
257 | |
|
258 | |
static final absVector removeAllElements (absVector v) { |
259 | 0 | if (v instanceof absEmpty) { |
260 | 0 | return new absEmpty ( |
261 | |
0, |
262 | |
((absEmpty) v).increment, |
263 | |
((absEmpty) v).capacity); |
264 | |
} else { |
265 | 0 | return removeAllElements (((absMap) v).vector); |
266 | |
} |
267 | |
} |
268 | |
|
269 | |
static final absVector clone (absVector v) { |
270 | 0 | return v; |
271 | |
} |
272 | |
|
273 | |
static final String toString (absVector v) { |
274 | 0 | if (v instanceof absEmpty) { |
275 | 0 | return "empty(" |
276 | |
+ ((absEmpty) v).size + "," |
277 | |
+ ((absEmpty) v).increment + "," |
278 | |
+ ((absEmpty) v).capacity + ")"; |
279 | |
} else { |
280 | 0 | Object tmpo = ((absMap) v).object; |
281 | 0 | String tmps = tmpo == null ? "null" : tmpo.toString(); |
282 | 0 | return "map(" |
283 | |
+ ((absMap) v).index + "," |
284 | |
+ tmps + "," |
285 | |
+ toString(((absMap) v).vector) + ")"; |
286 | |
} |
287 | |
} |
288 | |
|
289 | |
static final String toConcrString (absVector v) { |
290 | |
|
291 | |
|
292 | |
|
293 | 42 | StringBuffer buf = new StringBuffer(); |
294 | 42 | buf.append("["); |
295 | 42 | boolean needComma = false; |
296 | 400 | while (v instanceof absMap) { |
297 | 358 | if (needComma) buf.insert(1, ", "); |
298 | 358 | Object tmpo = ((absMap) v).object; |
299 | 358 | String tmps = tmpo == null ? "null" : tmpo.toString(); |
300 | 358 | buf.insert(1, tmps); |
301 | 358 | needComma = true; |
302 | 358 | v = ((absMap) v).vector; |
303 | 358 | } |
304 | 42 | buf.append("]"); |
305 | 42 | return buf.toString(); |
306 | |
} |
307 | |
|
308 | |
} |