Example #1
0
  /* returns LONG_MAX iff out of memory */
  int pqInsert(Object keyNew) {
    int curr;

    if (initialized) {
      return heap.pqInsert(keyNew);
    }
    curr = size;
    if (++size >= max) {
      Object[] saveKey = keys;

      /* If the heap overflows, double its size. */
      max <<= 1;
      //            pq->keys = (PQHeapKey *)memRealloc( pq->keys,(size_t)(pq->max * sizeof(
      // pq->keys[0] )));
      Object[] pqKeys = new Object[max];
      System.arraycopy(keys, 0, pqKeys, 0, keys.length);
      keys = pqKeys;
      if (keys == null) {
        keys = saveKey; /* restore ptr to free upon return */
        return Integer.MAX_VALUE;
      }
    }
    assert curr != Integer.MAX_VALUE;
    keys[curr] = keyNew;

    /* Negative handles index the sorted array. */
    return -(curr + 1);
  }