private void minHeapify(int index, int lastIndex) { int left = leftChild(index); int right = rightChild(index); int smallest; if (left <= lastIndex && compare(left, index) < 0) { smallest = left; } else { smallest = index; } if (right <= lastIndex && compare(right, smallest) < 0) { smallest = right; } if (smallest != index) { swap(index, smallest); minHeapify(smallest, lastIndex); } }
public synchronized void add(long item1, long item2) { if (this.size >= this.capacity) { expandArray(); } int lastIndex = this.size << 1; data[lastIndex] = item1; data[lastIndex + 1] = item2; int loc = lastIndex; // Swap with parent until parent not larger while (loc > 0 && compare(loc, parent(loc)) < 0) { swap(loc, parent(loc)); loc = parent(loc); } this.size++; }
/** * @return a new list of keys with max provided numberOfItems (makes a copy) */ public Set<LongPair> items(int numberOfItems) { Set<LongPair> items = new HashSet<>(this.size); forEach((item1, item2) -> { if (items.size() < numberOfItems) { items.add(new LongPair(item1, item2)); } }); return items; }
/** * it is not a thread-safe method and it should be called before acquiring a lock by a caller. * * @param index * @return */ private LongPair removeAtWithoutLock(int index) { if (this.size > 0) { LongPair item = new LongPair(data[index], data[index + 1]); data[index] = EmptyItem; data[index + 1] = EmptyItem; --this.size; int lastIndex = this.size << 1; swap(index, lastIndex); minHeapify(index, lastIndex - 2); return item; } else { return null; } }
private synchronized LongPair removeAt(int index) { return removeAtWithoutLock(index); }
@Override public int hashCode() { return (int) hash(first, second); }
/** * Removes min element from the heap * * @return */ public LongPair remove() { return removeAt(0); }
/** * it is not a thread-safe method and it should be called before acquiring a lock by a caller. * * @param index * @return */ private LongPair removeAtWithoutLock(int index) { if (this.size > 0) { LongPair item = new LongPair(data[index], data[index + 1]); data[index] = EmptyItem; data[index + 1] = EmptyItem; --this.size; int lastIndex = this.size << 1; swap(index, lastIndex); minHeapify(index, lastIndex - 2); return item; } else { return null; } }
/** * It removes all occurrence of given pair from the queue. * * @param item1 * @param item2 * @return */ public synchronized boolean remove(long item1, long item2) { boolean removed = false; int index = 0; for (int i = 0; i < this.size; i++) { if (data[index] == item1 && data[index + 1] == item2) { removeAtWithoutLock(index); removed = true; } index = index + 2; } return removed; }
@Override public int hashCode() { return (int) hash(first, second); }
/** * Removes min element from the heap * * @return */ public LongPair remove() { return removeAt(0); }
private void minHeapify(int index, int lastIndex) { int left = leftChild(index); int right = rightChild(index); int smallest; if (left <= lastIndex && compare(left, index) < 0) { smallest = left; } else { smallest = index; } if (right <= lastIndex && compare(right, smallest) < 0) { smallest = right; } if (smallest != index) { swap(index, smallest); minHeapify(smallest, lastIndex); } }
public synchronized void add(long item1, long item2) { if (this.size >= this.capacity) { expandArray(); } int lastIndex = this.size << 1; data[lastIndex] = item1; data[lastIndex + 1] = item2; int loc = lastIndex; // Swap with parent until parent not larger while (loc > 0 && compare(loc, parent(loc)) < 0) { swap(loc, parent(loc)); loc = parent(loc); } this.size++; }
if (data[index] == deletedItems[deleteItemsIndex] && data[index + 1] == deletedItems[deleteItemsIndex + 1]) { removeAtWithoutLock(index);
/** * @return a new list of all keys (makes a copy) */ public Set<LongPair> items() { Set<LongPair> items = new HashSet<>(this.size); forEach((item1, item2) -> items.add(new LongPair(item1, item2))); return items; }
private synchronized LongPair removeAt(int index) { return removeAtWithoutLock(index); }
/** * @return a new list of all keys (makes a copy) */ public Set<LongPair> items() { Set<LongPair> items = new HashSet<>(this.size); forEach((item1, item2) -> items.add(new LongPair(item1, item2))); return items; }
/** * It removes all occurrence of given pair from the queue. * * @param item1 * @param item2 * @return */ public synchronized boolean remove(long item1, long item2) { boolean removed = false; int index = 0; for (int i = 0; i < this.size; i++) { if (data[index] == item1 && data[index + 1] == item2) { removeAtWithoutLock(index); removed = true; } index = index + 2; } return removed; }
/** * @return a new list of keys with max provided numberOfItems (makes a copy) */ public Set<LongPair> items(int numberOfItems) { Set<LongPair> items = new HashSet<>(this.size); forEach((item1, item2) -> { if (items.size() < numberOfItems) { items.add(new LongPair(item1, item2)); } }); return items; }
if (data[index] == deletedItems[deleteItemsIndex] && data[index + 1] == deletedItems[deleteItemsIndex + 1]) { removeAtWithoutLock(index);