/** * 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; } }
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++; }
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); } }
/** * 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; } }
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++; }
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); } }