-// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
package org.xwt.util;
import java.util.*;
* java.util.ArrayList.
* @see java.util.Vector
*/
-public class Vec implements Serializable {
+public final class Vec implements Serializable {
private Object[] store;
private int size = 0;
}
public void addElement(Object o) {
- if (size >= store.length) grow();
+ if (size >= store.length - 1) grow();
store[size++] = o;
}
+ public Object peek() {
+ return lastElement();
+ }
+
public Object elementAt(int i) {
return store[i];
}
return store[size - 1];
}
+ public void push(Object o) { addElement(o); }
+ public Object pop() {
+ Object ret = lastElement();
+ if (size > 0) store[size--] = null;
+ return ret;
+ }
+
public int size() { return size; }
public void setSize(int newSize) {
out[i] = store[i];
}
+ public void fromArray(Object[] in) {
+ setSize(in.length);
+ for(int i=0; i<size; i++)
+ store[i] = in[i];
+ }
+
public void removeElementAt(int i) {
if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
for(int j=i; j<size - 1; j++)
size++;
}
+ public interface CompareFunc {
+ public int compare(Object a, Object b);
+ }
+
+ public void sort(CompareFunc c) {
+ sort(c,0,size-1);
+ }
+
+ private final void swap(int a, int b) {
+ if(a != b) {
+ Object tmp = store[a];
+ store[a] = store[b];
+ store[b] = tmp;
+ }
+ }
+
+ private void sort(CompareFunc c, int start, int end) {
+ Object tmp;
+ if(start >= end) return;
+ if(end-start <= 6) {
+ for(int i=start+1;i<=end;i++) {
+ tmp = store[i];
+ int j;
+ for(j=i-1;j>=start;j--) {
+ if(c.compare(store[j],tmp) <= 0) break;
+ store[j+1] = store[j];
+ }
+ store[j+1] = tmp;
+ }
+ return;
+ }
+ Object pivot = store[end];
+ int lo = start - 1;
+ int hi = end;
+ do {
+ while(c.compare(store[++lo],pivot) < 0) { }
+ while((hi > lo) && c.compare(store[--hi],pivot) > 0) { }
+ swap(lo,hi);
+ } while(lo < hi);
+ swap(lo,end);
+ sort(c,start,lo-1);
+ sort(c,lo+1,end);
+ }
}