@Override public int compare(byte[] left, byte[] right) { int l = Varint.readUnsignedVarInt(left); int r = Varint.readUnsignedVarInt(right); return l - r; } }
/** * @see #writeSignedVarLong(long, DataOutput) */ public static void writeSignedVarInt(int value, DataOutput out) throws IOException { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types writeUnsignedVarInt((value << 1) ^ (value >> 31), out); }
public List<Counter<T>> topK(int k) { List<Counter<T>> topK = new ArrayList<Counter<T>>(k); for (ListNode2<Bucket> bNode = bucketList.head(); bNode != null; bNode = bNode.getPrev()) { Bucket b = bNode.getValue(); for (Counter<T> c : b.counterList) { if (topK.size() == k) { return topK; } topK.add(c); } } return topK; }
@Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(this.capacity); out.writeInt(this.size()); for (ListNode2<Bucket> bNode = bucketList.tail(); bNode != null; bNode = bNode.getNext()) { Bucket b = bNode.getValue(); for (Counter<T> c : b.counterList) { out.writeObject(c); } } }
@Test public void testDoublyLinkedList() { DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); assertIsEmpty(list); }
public static <X, Y> Pair<X, Y> create(X x, Y y) { return new Pair<X, Y>(x, y); } }
public static int decode(ByteBuffer buf) { int v = buf.get(); int z = 0x7f & v; int shift = 7; while ((v & 0x80) != 0) { Preconditions.checkState(shift <= 28); v = buf.get(); z += (v & 0x7f) << shift; shift += 7; } return z; }
@Override public byte[] getBytes() throws IOException { return ExternalizableUtil.toBytes(this); }
/** * Prepend to tail of list */ public ListNode2<T> enqueue(T value) { ListNode2<T> node = new ListNode2<T>(value); if (size++ == 0) { head = node; } else { node.next = tail; tail.prev = node; } tail = node; return node; }
public T last() { return head == null ? null : head.getValue(); }
/** * Encodes a value using the variable-length encoding from * <a href="http://code.google.com/apis/protocolbuffers/docs/encoding.html"> * Google Protocol Buffers</a>. It uses zig-zag encoding to efficiently * encode signed values. If values are known to be nonnegative, * {@link #writeUnsignedVarLong(long, DataOutput)} should be used. * * @param value value to encode * @param out to write bytes to * @throws IOException if {@link DataOutput} throws {@link IOException} */ public static void writeSignedVarLong(long value, DataOutput out) throws IOException { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types writeUnsignedVarLong((value << 1) ^ (value >> 63), out); }
@Override public Iterator<T> iterator() { return new DoublyLinkedListIterator(this); }
public static int readSignedVarInt(byte[] bytes) { int raw = readUnsignedVarInt(bytes); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }
public static byte[] writeSignedVarInt(int value) { // Great trick from http://code.google.com/apis/protocolbuffers/docs/encoding.html#types return writeUnsignedVarInt((value << 1) ^ (value >> 31)); }
public byte[] toBytes() throws IOException { return ExternalizableUtil.toBytes(this); } }
/** * Append to head of list */ public ListNode2<T> add(T value) { ListNode2<T> node = new ListNode2<T>(value); if (size++ == 0) { tail = node; } else { node.prev = head; head.next = node; } head = node; return node; }
public T first() { return tail == null ? null : tail.getValue(); }
/** * @throws IllegalArgumentException if variable-length value does not terminate * after 5 bytes have been read * @throws IOException if {@link DataInput} throws {@link IOException} * @see #readSignedVarLong(DataInput) */ public static int readSignedVarInt(DataInput in) throws IOException { int raw = readUnsignedVarInt(in); // This undoes the trick in writeSignedVarInt() int temp = (((raw << 31) >> 31) ^ raw) >> 1; // This extra step lets us deal with the largest signed values by treating // negative results from read unsigned methods as like unsigned values. // Must re-flip the top bit if the original read value had it set. return temp ^ (raw & (1 << 31)); }