@Override public int compare(Object a, Object b) { return compare((WritableComparable) a, (WritableComparable) b); }
/** * Compute hash for binary data. */ public static int hashBytes(byte[] bytes, int length) { return hashBytes(bytes, 0, length); }
/** * Get a comparator for a {@link WritableComparable} implementation. */ public static WritableComparator get(Class<? extends WritableComparable> c) { WritableComparator comparator = comparators.get(c); if (comparator == null) { // force the static initializers to run forceInit(c); // look to see if it is defined now comparator = comparators.get(c); // if not, use the generic one if (comparator == null) { comparator = new WritableComparator(c, true); } } return comparator; }
@Override public int compare(Node a, Node b) { int diff = a.type.ordinal() - b.type.ordinal(); if (diff != 0) return diff; switch (a.type) { case EMPTY: return 0; // All empty nodes are the same case TERM: return WritableComparator.compareBytes(text, a.start, a.end - a.start, text, b.start, b.end - b.start); case OR: case AND: diff = a.children.size() - b.children.size(); if (diff != 0) return diff; for (int i = 0; i < a.children.size(); i++) { diff = compare(a.children.get(i), b.children.get(i)); if (diff != 0) return diff; } } return 0; } }
/** * Parse a double from a byte array. */ public static double readDouble(byte[] bytes, int start) { return Double.longBitsToDouble(readLong(bytes, start)); }
/** * Reads a zero-compressed encoded integer from a byte array and returns it. * * @param bytes byte array with the encoded integer * @param start start index * @return deserialized integer * @throws java.io.IOException */ public static int readVInt(byte[] bytes, int start) throws IOException { return (int) readVLong(bytes, start); } }
/** * Get a comparator for a {@link WritableComparable} implementation. */ public static WritableComparator get(Class<? extends WritableComparable> c) { WritableComparator comparator = comparators.get(c); if (comparator == null) { // force the static initializers to run forceInit(c); // look to see if it is defined now comparator = comparators.get(c); // if not, use the generic one if (comparator == null) { comparator = new WritableComparator(c, true); } } return comparator; }
@Override public int compare(Node a, Node b) { int diff = a.type.ordinal() - b.type.ordinal(); if (diff != 0) return diff; switch (a.type) { case EMPTY: return 0; // All empty nodes are the same case TERM: return WritableComparator.compareBytes(text, a.start, a.end - a.start, text, b.start, b.end - b.start); case OR: case AND: diff = a.children.size() - b.children.size(); if (diff != 0) return diff; for (int i = 0; i < a.children.size(); i++) { diff = compare(a.children.get(i), b.children.get(i)); if (diff != 0) return diff; } } return 0; } }
/** * Parse a double from a byte array. */ public static double readDouble(byte[] bytes, int start) { return Double.longBitsToDouble(readLong(bytes, start)); }
/** * Reads a zero-compressed encoded integer from a byte array and returns it. * * @param bytes byte array with the encoded integer * @param start start index * @return deserialized integer * @throws java.io.IOException */ public static int readVInt(byte[] bytes, int start) throws IOException { return (int) readVLong(bytes, start); } }
/** * Parse a float from a byte array. */ public static float readFloat(byte[] bytes, int start) { return Float.intBitsToFloat(readInt(bytes, start)); }
@Override public int compare(Object a, Object b) { return compare((WritableComparable) a, (WritableComparable) b); }
/** * Compares this byte sequence to another. * * @param obs byte sequence to compare * @return comparison result * @see #compareBytes(ByteSequence, ByteSequence) */ public int compareTo(ByteSequence obs) { if (isBackedByArray() && obs.isBackedByArray()) { return WritableComparator.compareBytes(getBackingArray(), offset(), length(), obs.getBackingArray(), obs.offset(), obs.length()); } return compareBytes(this, obs); }
/** * Compute hash for binary data. */ public static int hashBytes(byte[] bytes, int length) { return hashBytes(bytes, 0, length); }
/** * Parse a float from a byte array. */ public static float readFloat(byte[] bytes, int start) { return Float.intBitsToFloat(readInt(bytes, start)); }
/** * Optimization hook. Override this to make SequenceFile.Sorter's scream. * <p/> * <p>The default implementation reads the data into two {@link * WritableComparable}s (using {@link * Writable#readFields(DataInput)}, then calls {@link * #compare(WritableComparable, WritableComparable)}. */ @Override public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { try { buffer.reset(b1, s1, l1); // parse key1 key1.readFields(buffer); buffer.reset(b2, s2, l2); // parse key2 key2.readFields(buffer); } catch (IOException e) { throw new RuntimeException(e); } return compare(key1, key2); // compare them }