/** * Returns true if the priority queue is empty, false otherwise. * * @return True if empty, false otherwise */ public boolean isEmpty() { return _values.isEmpty(); }
/** * Returns the key+value pair with the max priority (min for minHeap mode) * <ul> * <li> key+value pair is not removed from the priority queue. </li> * <li> Throws runtime exception if the priority queue is empty. </li> * <li> Runtime complexity of O(1). </li> * </ul> * * @return Key+Value pair */ public IntDoublePair peek() { if (_values.isEmpty()) { throw new RuntimeException("Empty collection"); } _reusablePair.setIntValue(_indexToKeyMap.get(0)); _reusablePair.setDoubleValue(_values.getDouble(0)); return _reusablePair; }
/** * Returns the key+value pair with the max priority (min for minHeap mode) * <ul> * <li> key+value pair is removed from the priority queue. </li> * <li> Returns null if the priority queue is empty. </li> * <li> Runtime complexity of O(1). </li> * </ul> * * @return Key+Value pair */ public IntDoublePair poll() { if (isEmpty()) { return null; } IntDoublePair poll = peek(); int lastIndex = _values.size() - 1; swapValues(0, lastIndex); _values.removeDouble(lastIndex); _keyToIndexMap.remove(_indexToKeyMap.get(lastIndex)); _indexToKeyMap.remove(lastIndex); if (!_values.isEmpty()) { siftDown(0); } return poll; }
@Override public boolean isEmpty() { return data.isEmpty(); }