/** * 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; }
@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); } } }
@Override public List<T> peek(int k) { List<T> topK = new ArrayList<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.item); } } return topK; }
public T last() { return head == null ? null : head.getValue(); }
public T first() { return tail == null ? null : tail.getValue(); }
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; }
protected void incrementCounter(ListNode2<Counter<T>> counterNode, int incrementCount) { Counter<T> counter = counterNode.getValue(); // count_i ListNode2<Bucket> oldNode = counter.bucketNode; Bucket bucket = oldNode.getValue(); // Let Bucket_i be the bucket of count_i ListNode2<Bucket> bucketNodeNext = bucketNodePrev.getNext(); while (bucketNodeNext != null) { Bucket bucketNext = bucketNodeNext.getValue(); // Let Bucket_i^+ be Bucket_i's neighbor of larger value if (counter.count == bucketNext.count) { } else if (counter.count > bucketNext.count) { bucketNodePrev = bucketNodeNext; bucketNodeNext = bucketNodePrev.getNext(); // Continue hunting for an appropriate bucket } else {
/** * @param item stream element (<i>e</i>) * @return Pair<isNewItem, itemDropped> where isNewItem is the return value of offer() and itemDropped is null if no item was dropped */ public Pair<Boolean, T> offerReturnAll(T item, int incrementCount) { ListNode2<Counter<T>> counterNode = counterMap.get(item); boolean isNewItem = (counterNode == null); T droppedItem = null; if (isNewItem) { if (size() < capacity) { counterNode = bucketList.enqueue(new Bucket(0)).getValue().counterList.add(new Counter<T>(bucketList.tail(), item)); } else { Bucket min = bucketList.first(); counterNode = min.counterList.tail(); Counter<T> counter = counterNode.getValue(); droppedItem = counter.item; counterMap.remove(droppedItem); counter.item = item; counter.error = min.count; } counterMap.put(item, counterNode); } incrementCounter(counterNode, incrementCount); return new Pair<Boolean, T>(isNewItem, droppedItem); }
/** * 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; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append('['); for (ListNode2<Bucket> bNode = bucketList.head(); bNode != null; bNode = bNode.getPrev()) { Bucket b = bNode.getValue(); sb.append('{'); sb.append(b.count); sb.append(":["); for (Counter<T> c : b.counterList) { sb.append('{'); sb.append(c.item); sb.append(':'); sb.append(c.error); sb.append("},"); } if (b.counterList.size() > 0) { sb.deleteCharAt(sb.length() - 1); } sb.append("]},"); } if (bucketList.size() > 0) { sb.deleteCharAt(sb.length() - 1); } sb.append(']'); return sb.toString(); }
@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); } } }
public T first() { return tail == null ? null : tail.getValue(); }
public ListNode2<T> addAfter(ListNode2<T> node, T value) { ListNode2<T> newNode = new ListNode2<T>(value); addAfter(node, newNode); return newNode; }
public List<Counter<T>> topK(int k) { List<Counter<T>> topK = new ArrayList<>(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; }
public static <T> void write(Kryo kryo, Output output, StreamSummary<T> obj) { output.writeInt(obj.capacity); output.writeInt(obj.size()); for (ListNode2<StreamSummary<T>.Bucket> bNode = obj.bucketList.tail(); bNode != null; bNode = bNode.getNext()) { StreamSummary<T>.Bucket b = bNode.getValue(); for (Counter<T> c : b.counterList) { kryo.writeObject(output, c); } } }
public T last() { return head == null ? null : head.getValue(); }
@Test public void testAddNode() { DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); list.add(new ListNode2<Integer>(1)); assertFalse(list.isEmpty()); assertEquals(1, list.size()); assertArrayEquals(new Integer[]{1}, list.toArray()); list.add(new ListNode2<Integer>(2)); assertFalse(list.isEmpty()); assertEquals(2, list.size()); assertArrayEquals(new Integer[]{1, 2}, list.toArray()); list.add(new ListNode2<Integer>(3)); assertFalse(list.isEmpty()); assertEquals(3, list.size()); assertArrayEquals(new Integer[]{1, 2, 3}, list.toArray()); assertEquals(new Integer(1), list.first()); }
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; }
protected void incrementCounter(ListNode2<Counter<T>> counterNode, int incrementCount) { Counter<T> counter = counterNode.getValue(); // count_i ListNode2<Bucket> oldNode = counter.bucketNode; Bucket bucket = oldNode.getValue(); // Let Bucket_i be the bucket of count_i ListNode2<Bucket> bucketNodeNext = bucketNodePrev.getNext(); while (bucketNodeNext != null) { Bucket bucketNext = bucketNodeNext.getValue(); // Let Bucket_i^+ be Bucket_i's neighbor of larger value if (counter.count == bucketNext.count) { } else if (counter.count > bucketNext.count) { bucketNodePrev = bucketNodeNext; bucketNodeNext = bucketNodePrev.getNext(); // Continue hunting for an appropriate bucket } else {
/** * @param item stream element (<i>e</i>) * @return Pair<isNewItem, itemDropped> where isNewItem is the return value of offer() and itemDropped is null if no item was dropped */ public Pair<Boolean, T> offerReturnAll(T item, int incrementCount) { ListNode2<Counter<T>> counterNode = counterMap.get(item); boolean isNewItem = (counterNode == null); T droppedItem = null; if (isNewItem) { if (size() < capacity) { counterNode = bucketList.enqueue(new Bucket(0)).getValue().counterList.add(new Counter<T>(bucketList.tail(), item)); } else { Bucket min = bucketList.first(); counterNode = min.counterList.tail(); Counter<T> counter = counterNode.getValue(); droppedItem = counter.item; counterMap.remove(droppedItem); counter.item = item; counter.error = min.count; } counterMap.put(item, counterNode); } incrementCounter(counterNode, incrementCount); return new Pair<Boolean, T>(isNewItem, droppedItem); }