2003/09/19 05:01:38
[org.ibex.core.git] / src / org / xwt / util / Vec.java
index 7b55023..70be7d9 100644 (file)
@@ -1,4 +1,4 @@
-// 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.*;
@@ -10,7 +10,7 @@ import java.io.*;
  *  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;
@@ -42,10 +42,14 @@ public class Vec implements Serializable {
     }
     
     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];
     }
@@ -55,6 +59,13 @@ public class Vec implements Serializable {
         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) {
@@ -71,6 +82,12 @@ public class Vec implements Serializable {
             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++)
@@ -99,4 +116,47 @@ public 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);
+    }
 }