import classgen's Sort utility class, better to replicate 20 lines of quicksort than...
authorDavid Crawshaw <david@zentus.com>
Mon, 20 Nov 2006 06:44:32 +0000 (22:44 -0800)
committerDavid Crawshaw <david@zentus.com>
Mon, 20 Nov 2006 06:44:32 +0000 (22:44 -0800)
darcs-hash:20061120064432-0c629-34144a892c2e6e1c0619953e31c6bc063831e1ae.gz

src/org/ibex/nestedvm/UnixRuntime.java
src/org/ibex/nestedvm/util/Sort.java [new file with mode: 0644]

index 2852f35..8031f58 100644 (file)
@@ -5,8 +5,6 @@
 package org.ibex.nestedvm;
 
 import org.ibex.nestedvm.util.*;
 package org.ibex.nestedvm;
 
 import org.ibex.nestedvm.util.*;
-// HACK: This is ugly, this stuff needs to be in org.ibex.util or something
-import org.ibex.classgen.util.Sort;
 import java.io.*;
 import java.util.*;
 import java.net.*;
 import java.io.*;
 import java.util.*;
 import java.net.*;
diff --git a/src/org/ibex/nestedvm/util/Sort.java b/src/org/ibex/nestedvm/util/Sort.java
new file mode 100644 (file)
index 0000000..79fe029
--- /dev/null
@@ -0,0 +1,47 @@
+package org.ibex.nestedvm.util;
+
+public final class Sort {
+    private Sort() { }
+
+    public interface Comparable { public int compareTo(Object o); }
+    public interface CompareFunc { public int compare(Object a, Object b); }
+
+    private static final CompareFunc comparableCompareFunc = new CompareFunc() {
+        public int compare(Object a,Object b) { return ((Comparable)a).compareTo(b); }
+    };
+
+    public static void sort(Comparable[] a) { sort(a,comparableCompareFunc); }
+    public static void sort(Object[] a, CompareFunc c) { sort(a,c,0,a.length-1); }
+
+    private static void sort(Object[] a, 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 = a[i];
+                int j;
+                for(j=i-1;j>=start;j--) {
+                    if(c.compare(a[j],tmp) <= 0) break;
+                    a[j+1] = a[j];
+                }
+                a[j+1] = tmp;
+            }
+            return;
+        }
+
+        Object pivot = a[end];
+        int lo = start - 1;
+        int hi = end;
+
+        do {
+            while((lo < hi) && c.compare(a[++lo],pivot) < 0) { }
+            while((hi > lo) && c.compare(a[--hi],pivot) > 0) { }
+            tmp = a[lo]; a[lo] = a[hi]; a[hi] = tmp;
+        } while(lo < hi);
+
+        tmp = a[lo]; a[lo] = a[end]; a[end] = tmp;
+
+        sort(a, c, start, lo-1);
+        sort(a, c, lo+1, end);
+    }
+}