2003/12/25 08:09:21
[org.ibex.core.git] / src / org / xwt / util / Vec.java
index e04be25..846473b 100644 (file)
@@ -122,48 +122,62 @@ public final class Vec implements Serializable {
         store[at] = o;
         size++;
     }
-    
+
     public interface CompareFunc {
         public int compare(Object a, Object b);
     }
-    
+
     public void sort(CompareFunc c) {
-        sort(c,0,size-1);
+        sort(this, null, 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;
-        }
+
+    public static void sort(Vec a, Vec b, CompareFunc c) {
+        if (b != null && a.size != b.size) throw new IllegalArgumentException("Vec a and b must be of equal size");
+        sort(a, b, c, 0, a.size-1);
     }
-    
-    private void sort(CompareFunc c, int start, int end) {
-        Object tmp;
+
+    private static final void sort(Vec a, Vec b, CompareFunc c, int start, int end) {
+        Object tmpa, tmpb = null;
         if(start >= end) return;
         if(end-start <= 6) {
             for(int i=start+1;i<=end;i++) {
-                tmp = store[i];
+                tmpa = a.store[i];
+                if (b != null) tmpb = b.store[i];
                 int j;
                 for(j=i-1;j>=start;j--) {
-                    if(c.compare(store[j],tmp) <= 0) break;
-                    store[j+1] = store[j];
+                    if(c.compare(a.store[j],tmpa) <= 0) break;
+                    a.store[j+1] = a.store[j];
+                    if (b != null) b.store[j+1] = b.store[j];
                 }
-                store[j+1] = tmp;
+                a.store[j+1] = tmpa;
+                if (b != null) b.store[j+1] = tmpb;
             }
             return;
         }
-        Object pivot = store[end];
+
+        Object pivot = a.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(c.compare(a.store[++lo],pivot) < 0) { }
+            while((hi > lo) && c.compare(a.store[--hi],pivot) > 0) { }
+            swap(a, lo,hi);
+            if (b != null) swap(b, lo,hi);
         } while(lo < hi);
-        swap(lo,end);
-        sort(c,start,lo-1);
-        sort(c,lo+1,end);
+
+        swap(a, lo,end);
+        if (b != null) swap(b, lo,end);
+
+        sort(a, b, c, start, lo-1);
+        sort(a, b, c, lo+1, end);
+    }
+
+    private static final void swap(Vec vec, int a, int b) {
+        if(a != b) {
+            Object tmp = vec.store[a];
+            vec.store[a] = vec.store[b];
+            vec.store[b] = tmp;
+        }
     }
 }