@SuppressWarnings("unchecked") @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.bucketList = new DoublyLinkedList<Bucket>(); this.capacity = in.readInt(); int size = in.readInt(); this.counterMap = new HashMap<T, ListNode2<Counter<T>>>(size); Bucket currentBucket = null; ListNode2<Bucket> currentBucketNode = null; for (int i = 0; i < size; i++) { Counter<T> c = (Counter<T>) in.readObject(); if (currentBucket == null || c.count != currentBucket.count) { currentBucket = new Bucket(c.count); currentBucketNode = bucketList.add(currentBucket); } c.bucketNode = currentBucketNode; counterMap.put(c.item, currentBucket.counterList.add(c)); } }
@Test public void testAddAfter() { DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); list.add(1); ListNode2<Integer> node2 = list.add(2); ListNode2<Integer> node4 = list.add(4); list.addAfter(node2, 3); assertEquals(4, list.size()); assertArrayEquals(new Integer[]{1, 2, 3, 4}, list.toArray()); ListNode2<Integer> node5 = list.addAfter(node4, 5); assertEquals(5, list.size()); assertArrayEquals(new Integer[]{1, 2, 3, 4, 5}, list.toArray()); assertEquals(new Integer(5), list.last()); assertEquals(node5, list.head()); }
/** * @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); }
@Test public void testRemove() { DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); ListNode2<Integer> node1 = list.add(1); list.remove(node1); node1 = list.add(1); ListNode2<Integer> node2 = list.add(2); list.remove(node1); assertEquals(1, list.size()); assertIsEmpty(list); node1 = list.add(1); node2 = list.add(2); list.remove(node2); assertEquals(1, list.size()); assertArrayEquals(new Integer[]{1}, list.toArray()); node2 = list.add(2); list.add(3); assertEquals(3, list.size()); assertArrayEquals(new Integer[]{1, 2, 3}, list.toArray());
@Test public void testAdd() { DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>(); list.add(1); assertFalse(list.isEmpty()); assertEquals(1, list.size()); assertArrayEquals(new Integer[]{1}, list.toArray()); list.add(2); assertFalse(list.isEmpty()); assertEquals(2, list.size()); assertArrayEquals(new Integer[]{1, 2}, list.toArray()); list.add(3); assertFalse(list.isEmpty()); assertEquals(3, list.size()); assertArrayEquals(new Integer[]{1, 2, 3}, list.toArray()); assertEquals(new Integer(1), list.first()); }
@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()); }
DoublyLinkedList listTwo = new DoublyLinkedList(); for (int x = (localDoublyLinkedList.size() / 2) + 1, offset = x; x < localDoublyLinkedList.size(); x++) { listTwo.add(x - offset, localDoublyLinkedList.getValue(x)); }
DoublyLinkedList listTwo = new DoublyLinkedList(); for (int x = (localDoublyLinkedList.size() / 2) + 1; x < localDoublyLinkedList.size(); x++) { listTwo.add(x, localDoublyLinkedList.getValue(x)); }
@SuppressWarnings("unchecked") @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.bucketList = new DoublyLinkedList<Bucket>(); this.capacity = in.readInt(); int size = in.readInt(); this.counterMap = new HashMap<T, ListNode2<Counter<T>>>(size); Bucket currentBucket = null; ListNode2<Bucket> currentBucketNode = null; for (int i = 0; i < size; i++) { Counter<T> c = (Counter<T>) in.readObject(); if (currentBucket == null || c.count != currentBucket.count) { currentBucket = new Bucket(c.count); currentBucketNode = bucketList.add(currentBucket); } c.bucketNode = currentBucketNode; counterMap.put(c.item, currentBucket.counterList.add(c)); } }
public static <T> StreamSummary<T> read(Kryo kryo, Input input) { int capacity = input.readInt(); int size = input.readInt(); StreamSummary<T> deser = new StreamSummary<>(capacity); StreamSummary<T>.Bucket currentBucket = null; ListNode2<StreamSummary<T>.Bucket> currentBucketNode = null; for (int i = 0; i < size; i++) { @SuppressWarnings("unchecked") Counter<T> c = (Counter<T>)kryo.readObject(input, Counter.class); if (currentBucket == null || c.count != currentBucket.count) { currentBucket = deser.new Bucket(c.count); currentBucketNode = deser.bucketList.add(currentBucket); } c.bucketNode = currentBucketNode; deser.counterMap.put(c.item, currentBucket.counterList.add(c)); } return deser; } }
/** * @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); }
/** * @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<>(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<>(isNewItem, droppedItem); }