public ListMinBucketQueue(int maxNumberOfIds) { this.buckets = DoubleLinkedList.getArray(NUMBER_OF_BUCKETS); this.handles = new DoubleLinkedIntListItem[maxNumberOfIds]; for (int i = 0; i < maxNumberOfIds; i++) { handles[i] = new DoubleLinkedIntListItem(i); } }
@Override public void mergeInto(AbstractMaterialRequestPriorityQueue newAbstractQueue) { assert newAbstractQueue instanceof SimpleMaterialRequestPriorityQueue : "can't move positions between diffrent types of queues."; SimpleMaterialRequestPriorityQueue newQueue = (SimpleMaterialRequestPriorityQueue) newAbstractQueue; for (int queueIdx = 0; queueIdx < queues.length; queueIdx++) { DoubleLinkedList<MaterialRequestObject> currList = queues[queueIdx]; DoubleLinkedList<MaterialRequestObject> newList = newQueue.queues[queueIdx]; for (MaterialRequestObject request : currList) { request.requestQueue = newQueue; } currList.mergeInto(newList); } }
@Test public void testPushEndAndPopFrontWithClear() { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); } assertEquals(TEST_NUMBERS, list.size()); list.clear(); assertEquals(0, list.size()); testPushEndAndPopFront(); }
@Test public void testPushFrontAndPopFrontWithClear() { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushFront(new DoubleLinkedIntListItem(i)); } assertEquals(TEST_NUMBERS, list.size()); list.clear(); assertEquals(0, list.size()); testPushFrontAndPopFront(); }
@Test public void testPushFrontAndPopFront() { assertEquals(0, list.size()); for (int i = 0; i < TEST_NUMBERS; i++) { list.pushFront(new DoubleLinkedIntListItem(i)); assertEquals(i + 1, list.size()); } for (int i = TEST_NUMBERS - 1; i >= 0; i--) { assertEquals(i + 1, list.size()); assertEquals(i, list.popFront().value); } assertEquals(0, list.size()); }
@Test public void testSerialization() throws IOException, ClassNotFoundException { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); } DoubleLinkedList<DoubleLinkedIntListItem> readList = TestUtils.serializeAndDeserialize(list); assertListEquals(list, readList); }
@Test public void testIteratorLoop() { for (int i = 0; i < TEST_NUMBERS; i++) { list.pushEnd(new DoubleLinkedIntListItem(i)); } int i = 0; for (DoubleLinkedIntListItem curr : list) { assertEquals(i, curr.value); i++; } }
private void assertListEquals(DoubleLinkedList<DoubleLinkedIntListItem> list0, DoubleLinkedList<DoubleLinkedIntListItem> list1) { assertEquals(list0.size(), list1.size()); int size = list0.size(); for (int i = 0; i < size; i++) { assertEquals(list0.popFront().value, list1.popFront().value); } assertEquals(0, list0.size()); assertEquals(0, list1.size()); }
@Override public void clear() { for (int i = 0; i < NUMBER_OF_BUCKETS; i++) { this.buckets[i].clear(); } size = 0; }
@Override public void remove() { list.remove(next.prev); } }
/** * Generates a new array of {@link DoubleLinkedList}s of the given length. The array will be filled with new {@link DoubleLinkedList} objects. * * @param length * Length of the resulting array. * @return */ public static <T extends DoubleLinkedListItem<T>> DoubleLinkedList<T>[] getArray(final int length) { @SuppressWarnings("unchecked") DoubleLinkedList<T>[] array = new DoubleLinkedList[length]; for (int i = 0; i < length; i++) { array[i] = new DoubleLinkedList<>(); } return array; }
@Override public void insert(int elementId, float rank) { buckets[getRankIdx(rank)].pushFront(handles[elementId]); size++; }
@Override public Iterator<T> iterator() { return new DoubleLinkedListIterator<>(this); }
@SuppressWarnings("unchecked") private void initHead() { head = (T) new DoubleLinkedListItem<T>(); head.next = head; head.prev = head; }
public DoubleLinkedList() { initHead(); }
@Test public void testPushEndAndPopFrontMulti() { for (int i = 0; i < 5; i++) { testPushEndAndPopFront(); } }
/** * Adds all elements of this list to the given {@link DoubleLinkedList}. After this operation this list will not contain any elements. * * @param newList * The list to append all the elements of this list. */ public void mergeInto(DoubleLinkedList<T> newList) { newList.head.prev.next = this.head.next; this.head.next.prev = newList.head.prev; this.head.prev.next = newList.head; newList.head.prev = this.head.prev; newList.size += size; clear(); }
private final void remove(int elementId, int rankIdx) { buckets[rankIdx].remove(handles[elementId]); size--; }
@Override public void mergeInto(AbstractMaterialRequestPriorityQueue newAbstractQueue) { assert newAbstractQueue instanceof MaterialsForBuildingsRequestPriorityQueue : "can't move positions between different types of queues."; MaterialsForBuildingsRequestPriorityQueue newQueue = (MaterialsForBuildingsRequestPriorityQueue) newAbstractQueue; for (int priorityIndex = 0; priorityIndex < queues.length; priorityIndex++) { for (int queueIdx = 0; queueIdx < numberOfConfigurableBuildings; queueIdx++) { DoubleLinkedList<MaterialRequestObject> currList = queues[priorityIndex][queueIdx]; DoubleLinkedList<MaterialRequestObject> newList = newQueue.queues[priorityIndex][queueIdx]; for (MaterialRequestObject request : currList) { request.requestQueue = newQueue; } currList.mergeInto(newList); } } }