diff --git a/source/de/anomic/kelondro/kelondroAbstractOrder.java b/source/de/anomic/kelondro/kelondroAbstractOrder.java index b1456cf70..613ff48fe 100644 --- a/source/de/anomic/kelondro/kelondroAbstractOrder.java +++ b/source/de/anomic/kelondro/kelondroAbstractOrder.java @@ -63,25 +63,13 @@ public abstract class kelondroAbstractOrder implements kelondroOrder { return cardinal(key) / d; } - public int compareI(byte[] a, byte[] b) { - if (zero == null) return compare(a, b); - // we have an artificial start point. check all combinations - int az = compare(a, zero); // -1 if a < z; 0 if a == z; 1 if a > z - int bz = compare(b, zero); // -1 if b < z; 0 if b == z; 1 if b > z - if ((az == 0) && (bz == 0)) return 0; - if (az == 0) return -1; - if (bz == 0) return 1; - if (az == bz) return compare(a, b); - return bz; - } - public int compare(Object a, Object b) { if ((a instanceof byte[]) && (b instanceof byte[])) { - return compareI((byte[]) a, (byte[]) b); + return compare((byte[]) a, (byte[]) b); } else if ((a instanceof Node) && (b instanceof Node)) { - return compareI(((Node) a).getKey(), ((Node) b).getKey()); + return compare(((Node) a).getKey(), ((Node) b).getKey()); } else if ((a instanceof String) && (b instanceof String)) { - return compareI(((String) a).getBytes(), ((String) b).getBytes()); + return compare(((String) a).getBytes(), ((String) b).getBytes()); } else throw new IllegalArgumentException("Object type or Object type combination not supported: a=" + a + ", b=" + b); } diff --git a/source/de/anomic/kelondro/kelondroBase64Order.java b/source/de/anomic/kelondro/kelondroBase64Order.java index 66dd13117..2d94e6af5 100644 --- a/source/de/anomic/kelondro/kelondroBase64Order.java +++ b/source/de/anomic/kelondro/kelondroBase64Order.java @@ -244,15 +244,31 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond if (keyCardinal > zeroCardinal) return keyCardinal - zeroCardinal; return Long.MAX_VALUE - keyCardinal + zeroCardinal + 1; } - + public int compare(byte[] a, byte[] b) { + return (asc) ? compare0(a, b) : compare0(b, a); + } + + public int compare0(byte[] a, byte[] b) { + if (zero == null) return compares(a, b); + // we have an artificial start point. check all combinations + int az = compares(a, zero); // -1 if a < z; 0 if a == z; 1 if a > z + int bz = compares(b, zero); // -1 if b < z; 0 if b == z; 1 if b > z + if ((az == 0) && (bz == 0)) return 0; + if (az == 0) return -1; + if (bz == 0) return 1; + if (az == bz) return compares(a, b); + return bz; + } + + public int compares(byte[] a, byte[] b) { int i = 0; final int al = a.length; final int bl = b.length; final int len = (al > bl) ? bl : al; while (i < len) { - if (ahpla[a[i]] > ahpla[b[i]]) return (asc) ? 1 : -1; - if (ahpla[a[i]] < ahpla[b[i]]) return (asc) ? -1 : 1; + if (ahpla[a[i]] > ahpla[b[i]]) return 1; + if (ahpla[a[i]] < ahpla[b[i]]) return -1; // else the bytes are equal and it may go on yet undecided i++; } @@ -260,8 +276,8 @@ public class kelondroBase64Order extends kelondroAbstractOrder implements kelond if ((i == al) && (i < bl) && (b[i] == 0)) return 0; if ((i == bl) && (i < al) && (a[i] == 0)) return 0; // no, decide by length - if (al > bl) return (asc) ? 1 : -1; - if (al < bl) return (asc) ? -1 : 1; + if (al > bl) return 1; + if (al < bl) return -1; // no, they are equal return 0; } diff --git a/source/de/anomic/kelondro/kelondroNaturalOrder.java b/source/de/anomic/kelondro/kelondroNaturalOrder.java index 0b60d7e46..c7c12e075 100644 --- a/source/de/anomic/kelondro/kelondroNaturalOrder.java +++ b/source/de/anomic/kelondro/kelondroNaturalOrder.java @@ -115,19 +115,29 @@ public class kelondroNaturalOrder extends kelondroAbstractOrder implements kelon // two arrays are also equal if one array is a subset of the other's array // with filled-up char(0)-values public int compare(byte[] a, byte[] b) { - return (asc) ? compares(a, b) : compares(b, a); + return (asc) ? compare0(a, b) : compare0(b, a); } + public int compare0(byte[] a, byte[] b) { + if (zero == null) return compares(a, b); + // we have an artificial start point. check all combinations + int az = compares(a, zero); // -1 if a < z; 0 if a == z; 1 if a > z + int bz = compares(b, zero); // -1 if b < z; 0 if b == z; 1 if b > z + if ((az == 0) && (bz == 0)) return 0; + if (az == 0) return -1; + if (bz == 0) return 1; + if (az == bz) return compares(a, b); + return bz; + } + public static final int compares(byte[] a, byte[] b) { int i = 0; final int al = a.length; final int bl = b.length; final int len = (al > bl) ? bl : al; while (i < len) { - if (a[i] > b[i]) - return 1; - if (a[i] < b[i]) - return -1; + if (a[i] > b[i]) return 1; + if (a[i] < b[i]) return -1; // else the bytes are equal and it may go on yet undecided i++; }