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
13 * An unsynchronized Vector implementation; same semantics as
14 * java.util.Vector. Useful for JDK1.1 platforms that don't have
15 * java.util.ArrayList.
19 * @see java.util.Vector
21 public final class Vec implements Serializable {
23 // FIXME: ommitting this crashes jc1
24 private final static boolean NANOGOAT_KEEP_ALL_METHODS = true;
26 private Object[] store;
29 public Vec() { this(10); }
30 public Vec(int i) { store = new Object[i]; }
31 public Vec(int i, Object[] store) { size = i; this.store = store; }
33 private void grow() { grow(store.length * 2); }
34 private void grow(int newsize) {
35 Object[] newstore = new Object[newsize];
36 System.arraycopy(store, 0, newstore, 0, size);
40 public void removeAllElements() {
41 for(int i=0; i<size; i++) store[i] = null;
45 public void toArray(Object[] o) {
46 for(int i=0; i<size; i++)
50 public int indexOf(Object o) {
51 for(int i=0; i<size; i++)
52 if (o == null ? store[i] == null : store[i].equals(o)) return i;
57 public void addElement(Object o) {
58 if (size >= store.length - 1) grow();
62 public Object peek() {
66 public Object elementAt(int i) {
70 public Object lastElement() {
71 if (size == 0) return null;
72 return store[size - 1];
75 public void push(Object o) { addElement(o); }
77 Object ret = lastElement();
78 if (size > 0) store[size--] = null;
82 public int size() { return size; }
84 public void setSize(int newSize) {
85 if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
86 if (newSize > store.length) grow(newSize * 2);
88 for(int i=newSize; i<size; i++)
93 public void copyInto(Object[] out) {
94 for(int i=0; i<size; i++)
98 public void fromArray(Object[] in) {
100 for(int i=0; i<size; i++)
104 public void removeElementAt(int i) {
105 if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
106 for(int j=i; j<size - 1; j++)
107 store[j] = store[j + 1];
111 public void setElementAt(Object o, int i) {
112 if (i >= size) setSize(i);
116 public void removeElement(Object o) {
117 int idx = indexOf(o);
118 if (idx != -1) removeElementAt(idx);
121 public void insertElementAt(Object o, int at) {
122 if (size == store.length) grow();
123 for(int i=size; i>at; i--)
124 store[i] = store[i-1];
129 public interface CompareFunc {
130 public int compare(Object a, Object b);
133 public void sort(CompareFunc c) {
134 sort(this, null, c, 0, size-1);
137 public static void sort(Vec a, Vec b, CompareFunc c) {
138 if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec a and b must be of equal size");
139 sort(a, b, c, 0, a.size-1);
142 private static final void sort(Vec a, Vec b, CompareFunc c, int start, int end) {
143 Object tmpa, tmpb = null;
144 if(start >= end) return;
146 for(int i=start+1;i<=end;i++) {
148 if (b != null) tmpb = b.store[i];
150 for(j=i-1;j>=start;j--) {
151 if(c.compare(a.store[j],tmpa) <= 0) break;
152 a.store[j+1] = a.store[j];
153 if (b != null) b.store[j+1] = b.store[j];
156 if (b != null) b.store[j+1] = tmpb;
161 Object pivot = a.store[end];
166 while(c.compare(a.store[++lo],pivot) < 0) { }
167 while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
169 if (b != null) swap(b, lo,hi);
173 if (b != null) swap(b, lo,end);
175 sort(a, b, c, start, lo-1);
176 sort(a, b, c, lo+1, end);
179 private static final void swap(Vec vec, int a, int b) {
181 Object tmp = vec.store[a];
182 vec.store[a] = vec.store[b];