1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
12 * An unsynchronized Vector implementation; same semantics as
13 * java.util.Vector. Useful for JDK1.1 platforms that don't have
14 * java.util.ArrayList.
18 * @see java.util.Vector
20 public final class Vec implements Serializable {
22 private Object[] store;
25 public Vec() { this(10); }
26 public Vec(int i) { store = new Object[i]; }
27 public Vec(int i, Object[] store) { size = i; this.store = store; }
29 private void grow() { grow(store.length * 2); }
30 private void grow(int newsize) {
31 Object[] newstore = new Object[newsize];
32 System.arraycopy(store, 0, newstore, 0, size);
36 public void removeAllElements() {
37 for(int i=0; i<size; i++) store[i] = null;
41 public void toArray(Object[] o) {
42 for(int i=0; i<size; i++)
46 public int indexOf(Object o) {
47 for(int i=0; i<size; i++)
48 if (o == null ? store[i] == null : store[i].equals(o)) return i;
53 public void addElement(Object o) {
54 if (size >= store.length - 1) grow();
58 public Object peek() {
62 public Object elementAt(int i) {
66 public Object lastElement() {
67 if (size == 0) return null;
68 return store[size - 1];
71 public void push(Object o) { addElement(o); }
73 Object ret = lastElement();
74 if (size > 0) store[size--] = null;
78 public int size() { return size; }
80 public void setSize(int newSize) {
81 if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
82 if (newSize > store.length) grow(newSize * 2);
84 for(int i=newSize; i<size; i++)
89 public void copyInto(Object[] out) {
90 for(int i=0; i<size; i++)
94 public void fromArray(Object[] in) {
96 for(int i=0; i<size; i++)
100 public void removeElementAt(int i) {
101 if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
102 for(int j=i; j<size - 1; j++)
103 store[j] = store[j + 1];
107 public void setElementAt(Object o, int i) {
108 if (i >= size) setSize(i);
112 public void removeElement(Object o) {
113 int idx = indexOf(o);
114 if (idx != -1) removeElementAt(idx);
117 public void insertElementAt(Object o, int at) {
118 if (size == store.length) grow();
119 for(int i=size; i>at; i--)
120 store[i] = store[i-1];
125 public interface CompareFunc {
126 public int compare(Object a, Object b);
129 public void sort(CompareFunc c) {
130 sort(this, null, c, 0, size-1);
133 public static void sort(Vec a, Vec b, CompareFunc c) {
134 if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec a and b must be of equal size");
135 sort(a, b, c, 0, a.size-1);
138 private static final void sort(Vec a, Vec b, CompareFunc c, int start, int end) {
139 Object tmpa, tmpb = null;
140 if(start >= end) return;
142 for(int i=start+1;i<=end;i++) {
144 if (b != null) tmpb = b.store[i];
146 for(j=i-1;j>=start;j--) {
147 if(c.compare(a.store[j],tmpa) <= 0) break;
148 a.store[j+1] = a.store[j];
149 if (b != null) b.store[j+1] = b.store[j];
152 if (b != null) b.store[j+1] = tmpb;
157 Object pivot = a.store[end];
162 while(c.compare(a.store[++lo],pivot) < 0) { }
163 while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
165 if (b != null) swap(b, lo,hi);
169 if (b != null) swap(b, lo,end);
171 sort(a, b, c, start, lo-1);
172 sort(a, b, c, lo+1, end);
175 private static final void swap(Vec vec, int a, int b) {
177 Object tmp = vec.store[a];
178 vec.store[a] = vec.store[b];
183 public static final void sortInts(int[] a, int start, int end) {
185 if(start >= end) return;
187 for(int i=start+1;i<=end;i++) {
190 for(j=i-1;j>=start;j--) {
191 if(a[j] <= tmpa) break;
202 while(a[++lo] < pivot) { }
203 while((hi > lo) && a[--hi] > pivot) { }
206 swapInts(a, lo, end);
207 sortInts(a, start, lo-1);
208 sortInts(a, lo+1, end);
210 private static final void swapInts(int[] vec, int a, int b) {
220 // just a cut-and-paste copy of Vec with int storage
221 public static class Int {
223 private int size = 0;
225 public Int() { this(10); }
226 public Int(int i) { store = new int[i]; }
227 public Int(int i, int[] store) { size = i; this.store = store; }
229 private void grow() { grow(store.length * 2); }
230 private void grow(int newsize) {
231 int[] newstore = new int[newsize];
232 System.arraycopy(store, 0, newstore, 0, size);
236 public void removeAllElements() {
237 for(int i=0; i<size; i++) store[i] = 0;
241 public void toArray(int[] o) {
242 for(int i=0; i<size; i++)
246 public int[] dump() { int[] o = new int[size]; toArray(o); return o; }
248 public int indexOf(int o) {
249 for(int i=0; i<size; i++)
250 if (o == store[i]) return i;
255 public void addElement(int o) {
256 if (size >= store.length - 1) grow();
261 return lastElement();
264 public int elementAt(int i) {
268 public int lastElement() {
269 if (size == 0) return 0;
270 return store[size - 1];
273 public void push(int o) { addElement(o); }
275 int ret = lastElement();
276 if (size > 0) store[size--] = 0;
280 public int size() { return size; }
282 public void setSize(int newSize) {
283 if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
284 if (newSize > store.length) grow(newSize * 2);
286 for(int i=newSize; i<size; i++)
291 public void copyInto(int[] out) {
292 for(int i=0; i<size; i++)
296 public void fromArray(int[] in) {
298 for(int i=0; i<size; i++)
302 public void removeElementAt(int i) {
303 if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
304 for(int j=i; j<size - 1; j++)
305 store[j] = store[j + 1];
309 public void setElementAt(int o, int i) {
310 if (i >= size) setSize(i);
314 public void removeElement(int o) {
315 int idx = indexOf(o);
316 if (idx != -1) removeElementAt(idx);
319 public void insertElementAt(int o, int at) {
320 if (size == store.length) grow();
321 for(int i=size; i>at; i--)
322 store[i] = store[i-1];
327 public void sort() { sort(this, null, 0, size-1); }
329 public static void sort(Vec.Int a, Vec.Int b) {
330 if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec.Int a and b must be of equal size");
331 sort(a, b, 0, a.size-1);
334 private static final void sort(Vec.Int a, Vec.Int b, int start, int end) {
336 if(start >= end) return;
338 for(int i=start+1;i<=end;i++) {
340 if (b != null) tmpb = b.store[i];
342 for(j=i-1;j>=start;j--) {
343 if((a.store[j]-tmpa) <= 0) break;
344 a.store[j+1] = a.store[j];
345 if (b != null) b.store[j+1] = b.store[j];
348 if (b != null) b.store[j+1] = tmpb;
353 int pivot = a.store[end];
358 while((a.store[++lo]-pivot) < 0) { }
359 while((hi > lo) && (a.store[--hi]-pivot) > 0) { }
361 if (b != null) swap(b, lo,hi);
365 if (b != null) swap(b, lo,end);
367 sort(a, b, start, lo-1);
368 sort(a, b, lo+1, end);
371 private static final void swap(Vec.Int vec, int a, int b) {
373 int tmp = vec.store[a];
374 vec.store[a] = vec.store[b];
379 public static final void sortInts(int[] a, int start, int end) {
381 if(start >= end) return;
383 for(int i=start+1;i<=end;i++) {
386 for(j=i-1;j>=start;j--) {
387 if(a[j] <= tmpa) break;
400 while(a[++lo] < pivot) { }
401 while((hi > lo) && a[--hi] > pivot) { }
404 swapInts(a, lo, end);
405 sortInts(a, start, lo-1);
406 sortInts(a, lo+1, end);
409 private static final void swapInts(int[] vec, int a, int b) {
420 public static final void sortFloats(float[] a, int start, int end) {
422 if(start >= end) return;
424 for(int i=start+1;i<=end;i++) {
427 for(j=i-1;j>=start;j--) {
428 if(a[j] <= tmpa) break;
435 float pivot = a[end];
439 while(a[++lo] < pivot) { }
440 while((hi > lo) && a[--hi] > pivot) { }
441 swapFloats(a, lo, hi);
443 swapFloats(a, lo, end);
444 sortFloats(a, start, lo-1);
445 sortFloats(a, lo+1, end);
447 private static final void swapFloats(float[] vec, int a, int b) {