add method

void add(
  1. Comparable x
)

Insert into the priority queue. Duplicates are allowed. @param x the item to insert.

Implementation

void add(Comparable x) {
  // increase the size of the items heap to create a hole for the new item
  _items.add(null);

  // Insert item at end of heap and then re-establish ordering
  _size += 1;
  int hole = _size;
  // set the item as a sentinel at the base of the heap
  _items.insert(0, x);

  // move the item up from the hole position to its correct place
  for (; x.compareTo(_items[hole ~/ 2]) < 0; hole = hole ~/ 2) {
    _items.insert(hole, _items[hole ~/ 2]);
  }
  // insert the new item in the correct place
  _items.insert(hole, x);
}