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 Object[] copyInto(Object[] out) {
90 for(int i=0; i<size; i++)
95 public void fromArray(Object[] in) {
97 for(int i=0; i<size; i++)
101 public void removeElementAt(int i) {
102 if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
103 for(int j=i; j<size - 1; j++)
104 store[j] = store[j + 1];
108 public void setElementAt(Object o, int i) {
109 if (i >= size) setSize(i);
113 public void removeElement(Object o) {
114 int idx = indexOf(o);
115 if (idx != -1) removeElementAt(idx);
118 public void insertElementAt(Object o, int at) {
119 if (size == store.length) grow();
120 for(int i=size; i>at; i--)
121 store[i] = store[i-1];
126 public interface CompareFunc {
127 public int compare(Object a, Object b);
130 public void sort(CompareFunc c) {
131 sort(this, null, c, 0, size-1);
134 public static void sort(Vec a, Vec b, CompareFunc c) {
135 if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec a and b must be of equal size");
136 sort(a, b, c, 0, a.size-1);
139 private static final void sort(Vec a, Vec b, CompareFunc c, int start, int end) {
140 Object tmpa, tmpb = null;
141 if(start >= end) return;
143 for(int i=start+1;i<=end;i++) {
145 if (b != null) tmpb = b.store[i];
147 for(j=i-1;j>=start;j--) {
148 if(c.compare(a.store[j],tmpa) <= 0) break;
149 a.store[j+1] = a.store[j];
150 if (b != null) b.store[j+1] = b.store[j];
153 if (b != null) b.store[j+1] = tmpb;
158 Object pivot = a.store[end];
163 while(c.compare(a.store[++lo],pivot) < 0) { }
164 while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
166 if (b != null) swap(b, lo,hi);
170 if (b != null) swap(b, lo,end);
172 sort(a, b, c, start, lo-1);
173 sort(a, b, c, lo+1, end);
176 private static final void swap(Vec vec, int a, int b) {
178 Object tmp = vec.store[a];
179 vec.store[a] = vec.store[b];
184 public static final void sortInts(int[] a, int start, int end) {
186 if(start >= end) return;
188 for(int i=start+1;i<=end;i++) {
191 for(j=i-1;j>=start;j--) {
192 if(a[j] <= tmpa) break;
203 while(a[++lo] < pivot) { }
204 while((hi > lo) && a[--hi] > pivot) { }
207 swapInts(a, lo, end);
208 sortInts(a, start, lo-1);
209 sortInts(a, lo+1, end);
211 private static final void swapInts(int[] vec, int a, int b) {
221 // just a cut-and-paste copy of Vec with int storage
222 public static class Int {
224 private int size = 0;
226 public Int() { this(10); }
227 public Int(int i) { store = new int[i]; }
228 public Int(int i, int[] store) { size = i; this.store = store; }
230 private void grow() { grow(store.length * 2); }
231 private void grow(int newsize) {
232 int[] newstore = new int[newsize];
233 System.arraycopy(store, 0, newstore, 0, size);
237 public void removeAllElements() {
238 for(int i=0; i<size; i++) store[i] = 0;
242 public void toArray(int[] o) {
243 for(int i=0; i<size; i++)
247 public int[] dump() { int[] o = new int[size]; toArray(o); return o; }
249 public int indexOf(int o) {
250 for(int i=0; i<size; i++)
251 if (o == store[i]) return i;
256 public void addElement(int o) {
257 if (size >= store.length - 1) grow();
262 return lastElement();
265 public int elementAt(int i) {
269 public int lastElement() {
270 if (size == 0) return 0;
271 return store[size - 1];
274 public void push(int o) { addElement(o); }
276 int ret = lastElement();
277 if (size > 0) store[size--] = 0;
281 public int size() { return size; }
283 public void setSize(int newSize) {
284 if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
285 if (newSize > store.length) grow(newSize * 2);
287 for(int i=newSize; i<size; i++)
292 public int[] copyInto(int[] out) {
293 for(int i=0; i<size; i++)
298 public void fromArray(int[] in) {
300 for(int i=0; i<size; i++)
304 public void removeElementAt(int i) {
305 if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
306 for(int j=i; j<size - 1; j++)
307 store[j] = store[j + 1];
311 public void setElementAt(int o, int i) {
312 if (i >= size) setSize(i);
316 public void removeElement(int o) {
317 int idx = indexOf(o);
318 if (idx != -1) removeElementAt(idx);
321 public void insertElementAt(int o, int at) {
322 if (size == store.length) grow();
323 for(int i=size; i>at; i--)
324 store[i] = store[i-1];
329 public void sort() { sort(this, null, 0, size-1); }
331 public static void sort(Vec.Int a, Vec.Int b) {
332 if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec.Int a and b must be of equal size");
333 sort(a, b, 0, a.size-1);
336 private static final void sort(Vec.Int a, Vec.Int b, int start, int end) {
338 if(start >= end) return;
340 for(int i=start+1;i<=end;i++) {
342 if (b != null) tmpb = b.store[i];
344 for(j=i-1;j>=start;j--) {
345 if((a.store[j]-tmpa) <= 0) break;
346 a.store[j+1] = a.store[j];
347 if (b != null) b.store[j+1] = b.store[j];
350 if (b != null) b.store[j+1] = tmpb;
355 int pivot = a.store[end];
360 while((a.store[++lo]-pivot) < 0) { }
361 while((hi > lo) && (a.store[--hi]-pivot) > 0) { }
363 if (b != null) swap(b, lo,hi);
367 if (b != null) swap(b, lo,end);
369 sort(a, b, start, lo-1);
370 sort(a, b, lo+1, end);
373 private static final void swap(Vec.Int vec, int a, int b) {
375 int tmp = vec.store[a];
376 vec.store[a] = vec.store[b];
381 public static final void sortInts(int[] a, int start, int end) {
383 if(start >= end) return;
385 for(int i=start+1;i<=end;i++) {
388 for(j=i-1;j>=start;j--) {
389 if(a[j] <= tmpa) break;
402 while(a[++lo] < pivot) { }
403 while((hi > lo) && a[--hi] > pivot) { }
406 swapInts(a, lo, end);
407 sortInts(a, start, lo-1);
408 sortInts(a, lo+1, end);
411 private static final void swapInts(int[] vec, int a, int b) {
422 public static final void sortFloats(float[] a, int start, int end) {
424 if(start >= end) return;
426 for(int i=start+1;i<=end;i++) {
429 for(j=i-1;j>=start;j--) {
430 if(a[j] <= tmpa) break;
437 float pivot = a[end];
441 while(a[++lo] < pivot) { }
442 while((hi > lo) && a[--hi] > pivot) { }
443 swapFloats(a, lo, hi);
445 swapFloats(a, lo, end);
446 sortFloats(a, start, lo-1);
447 sortFloats(a, lo+1, end);
449 private static final void swapFloats(float[] vec, int a, int b) {