2003/07/28 23:10:27
[org.ibex.core.git] / src / org / xwt / util / Vec.java
index 23f196f..a602089 100644 (file)
@@ -47,7 +47,7 @@ public final class Vec implements Serializable {
     }
 
     public Object peek() {
-       return lastElement();
+        return lastElement();
     }
 
     public Object elementAt(int i) {
@@ -61,9 +61,9 @@ public final class Vec implements Serializable {
 
     public void push(Object o) { addElement(o); }
     public Object pop() {
-       Object ret = lastElement();
-       if (size > 0) store[size--] = null;
-       return ret;
+        Object ret = lastElement();
+        if (size > 0) store[size--] = null;
+        return ret;
     }
 
     public int size() { return size; }
@@ -116,4 +116,47 @@ public final class Vec implements Serializable {
         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);
+    }
 }