1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
9 * An unsynchronized Vector implementation; same semantics as
10 * java.util.Vector. Useful for JDK1.1 platforms that don't have
11 * java.util.ArrayList.
15 * @see java.util.Vector
17 public final class Vec implements Serializable, Cloneable {
19 private Object[] store;
22 public Vec() { this(10); }
23 public Vec(int i) { store = new Object[i]; }
24 public Vec(int i, Object[] store) { size = i; this.store = store; }
26 store = new Object[old.store.length];
27 System.arraycopy(old.store, 0, store, 0, old.store.length);
31 public Object clone() { return new Vec(this); }
32 private void grow() { grow(store.length * 2); }
33 private void grow(int newsize) {
34 Object[] newstore = new Object[newsize];
35 System.arraycopy(store, 0, newstore, 0, size);
39 public void removeAllElements() {
40 for(int i=0; i<size; i++) store[i] = null;
44 public void toArray(Object[] o) {
45 for(int i=0; i<size; i++)
49 public int indexOf(Object o) {
50 for(int i=0; i<size; i++)
51 if (o == null ? store[i] == null : store[i].equals(o)) return i;
56 public void add(Object o) { addElement(o); }
57 public void addElement(Object o) {
58 if (size >= store.length - 1) grow();
62 public Object peek() {
66 public Object get(int i) { return elementAt(i); }
67 public Object elementAt(int i) {
71 public Object lastElement() {
72 if (size == 0) return null;
73 return store[size - 1];
76 public void push(Object o) { addElement(o); }
78 Object ret = lastElement();
79 if (size > 0) store[--size] = null;
83 public int size() { return size; }
85 public void setSize(int newSize) {
86 if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
87 if (newSize > store.length) grow(newSize * 2);
89 for(int i=newSize; i<size; i++)
94 public Object[] copyInto(Object[] out) {
95 for(int i=0; i<size; i++)
100 public void fromArray(Object[] in) {
102 for(int i=0; i<size; i++)
106 public void remove(int i) { removeElementAt(i); }
107 public void removeElementAt(int i) {
108 if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
109 for(int j=i; j<size - 1; j++)
110 store[j] = store[j + 1];
114 public void setElementAt(Object o, int i) {
115 if (i >= size) setSize(i);
119 public void removeElement(Object o) {
120 int idx = indexOf(o);
121 if (idx != -1) removeElementAt(idx);
124 public void insertElementAt(Object o, int at) {
125 if (size == store.length) grow();
126 for(int i=size; i>at; i--)
127 store[i] = store[i-1];
132 public interface CompareFunc {
133 public int compare(Object a, Object b);
136 public void sort(CompareFunc c) {
137 sort(this, null, c, 0, size-1);
140 public static void sort(Vec a, Vec b, CompareFunc c) {
141 if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec a and b must be of equal size");
142 sort(a, b, c, 0, a.size-1);
145 private static final void sort(Vec a, Vec b, CompareFunc c, int start, int end) {
146 Object tmpa, tmpb = null;
147 if(start >= end) return;
149 for(int i=start+1;i<=end;i++) {
151 if (b != null) tmpb = b.store[i];
153 for(j=i-1;j>=start;j--) {
154 if(c.compare(a.store[j],tmpa) <= 0) break;
155 a.store[j+1] = a.store[j];
156 if (b != null) b.store[j+1] = b.store[j];
159 if (b != null) b.store[j+1] = tmpb;
164 Object pivot = a.store[end];
169 while(c.compare(a.store[++lo],pivot) < 0) { }
170 while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
172 if (b != null) swap(b, lo,hi);
176 if (b != null) swap(b, lo,end);
178 sort(a, b, c, start, lo-1);
179 sort(a, b, c, lo+1, end);
182 private static final void swap(Vec vec, int a, int b) {
184 Object tmp = vec.store[a];
185 vec.store[a] = vec.store[b];
190 public static final void sortInts(int[] a, int start, int end) {
192 if(start >= end) return;
194 for(int i=start+1;i<=end;i++) {
197 for(j=i-1;j>=start;j--) {
198 if(a[j] <= tmpa) break;
209 while(a[++lo] < pivot) { }
210 while((hi > lo) && a[--hi] > pivot) { }
213 swapInts(a, lo, end);
214 sortInts(a, start, lo-1);
215 sortInts(a, lo+1, end);
217 private static final void swapInts(int[] vec, int a, int b) {
227 // just a cut-and-paste copy of Vec with int storage
228 public static class Int {
230 private int size = 0;
232 public Int() { this(10); }
233 public Int(int i) { store = new int[i]; }
234 public Int(int i, int[] store) { size = i; this.store = store; }
236 private void grow() { grow(store.length * 2); }
237 private void grow(int newsize) {
238 int[] newstore = new int[newsize];
239 System.arraycopy(store, 0, newstore, 0, size);
243 public void removeAllElements() {
244 for(int i=0; i<size; i++) store[i] = 0;
248 public void toArray(int[] o) {
249 for(int i=0; i<size; i++)
253 public int[] dump() { int[] o = new int[size]; toArray(o); return o; }
255 public int indexOf(int o) {
256 for(int i=0; i<size; i++)
257 if (o == store[i]) return i;
262 public void addElement(int o) {
263 if (size >= store.length - 1) grow();
268 return lastElement();
271 public int elementAt(int i) {
275 public int lastElement() {
276 if (size == 0) return 0;
277 return store[size - 1];
280 public void push(int o) { addElement(o); }
282 int ret = lastElement();
283 if (size > 0) store[size--] = 0;
287 public int size() { return size; }
289 public void setSize(int newSize) {
290 if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
291 if (newSize > store.length) grow(newSize * 2);
293 for(int i=newSize; i<size; i++)
298 public int[] copyInto(int[] out) {
299 for(int i=0; i<size; i++)
304 public void fromArray(int[] in) {
306 for(int i=0; i<size; i++)
310 public void removeElementAt(int i) {
311 if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
312 for(int j=i; j<size - 1; j++)
313 store[j] = store[j + 1];
317 public void setElementAt(int o, int i) {
318 if (i >= size) setSize(i);
322 public void removeElement(int o) {
323 int idx = indexOf(o);
324 if (idx != -1) removeElementAt(idx);
327 public void insertElementAt(int o, int at) {
328 if (size == store.length) grow();
329 for(int i=size; i>at; i--)
330 store[i] = store[i-1];
335 public void sort() { sort(this, null, 0, size-1); }
337 public static void sort(Vec.Int a, Vec.Int b) {
338 if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec.Int a and b must be of equal size");
339 sort(a, b, 0, a.size-1);
342 private static final void sort(Vec.Int a, Vec.Int b, int start, int end) {
344 if(start >= end) return;
346 for(int i=start+1;i<=end;i++) {
348 if (b != null) tmpb = b.store[i];
350 for(j=i-1;j>=start;j--) {
351 if((a.store[j]-tmpa) <= 0) break;
352 a.store[j+1] = a.store[j];
353 if (b != null) b.store[j+1] = b.store[j];
356 if (b != null) b.store[j+1] = tmpb;
361 int pivot = a.store[end];
366 while((a.store[++lo]-pivot) < 0) { }
367 while((hi > lo) && (a.store[--hi]-pivot) > 0) { }
369 if (b != null) swap(b, lo,hi);
373 if (b != null) swap(b, lo,end);
375 sort(a, b, start, lo-1);
376 sort(a, b, lo+1, end);
379 private static final void swap(Vec.Int vec, int a, int b) {
381 int tmp = vec.store[a];
382 vec.store[a] = vec.store[b];
387 public static final void sortInts(int[] a, int start, int end) {
389 if(start >= end) return;
391 for(int i=start+1;i<=end;i++) {
394 for(j=i-1;j>=start;j--) {
395 if(a[j] <= tmpa) break;
408 while(a[++lo] < pivot) { }
409 while((hi > lo) && a[--hi] > pivot) { }
412 swapInts(a, lo, end);
413 sortInts(a, start, lo-1);
414 sortInts(a, lo+1, end);
417 private static final void swapInts(int[] vec, int a, int b) {
428 public static final void sortFloats(float[] a, int start, int end) {
430 if(start >= end) return;
432 for(int i=start+1;i<=end;i++) {
435 for(j=i-1;j>=start;j--) {
436 if(a[j] <= tmpa) break;
443 float pivot = a[end];
447 while(a[++lo] < pivot) { }
448 while((hi > lo) && a[--hi] > pivot) { }
449 swapFloats(a, lo, hi);
451 swapFloats(a, lo, end);
452 sortFloats(a, start, lo-1);
453 sortFloats(a, lo+1, end);
455 private static final void swapFloats(float[] vec, int a, int b) {