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 private Object[] store;
26 public Vec() { this(10); }
27 public Vec(int i) { store = new Object[i]; }
28 public Vec(int i, Object[] store) { size = i; this.store = store; }
30 private void grow() { grow(store.length * 2); }
31 private void grow(int newsize) {
32 Object[] newstore = new Object[newsize];
33 System.arraycopy(store, 0, newstore, 0, size);
37 public void removeAllElements() {
38 for(int i=0; i<size; i++) store[i] = null;
42 public void toArray(Object[] o) {
43 for(int i=0; i<size; i++)
47 public int indexOf(Object o) {
48 for(int i=0; i<size; i++)
49 if (o == null ? store[i] == null : store[i].equals(o)) return i;
54 public void addElement(Object o) {
55 if (size >= store.length - 1) grow();
59 public Object peek() {
63 public Object elementAt(int i) {
67 public Object lastElement() {
68 if (size == 0) return null;
69 return store[size - 1];
72 public void push(Object o) { addElement(o); }
74 Object ret = lastElement();
75 if (size > 0) store[size--] = null;
79 public int size() { return size; }
81 public void setSize(int newSize) {
82 if (newSize < 0) throw new RuntimeException("tried to set size to negative value");
83 if (newSize > store.length) grow(newSize * 2);
85 for(int i=newSize; i<size; i++)
90 public void copyInto(Object[] out) {
91 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];