Ejemplo n.º 1
0
 /**
  * Binary search of a key in a list. If there are several hits the last one is returned.
  *
  * @param a array to search into
  * @param e key to search for
  * @return index of the found hit or where the key ought to be inserted
  */
 private int sortedLastIndexOf(final int[] a, final int e) {
   int i = Arrays.binarySearch(a, 0, rows, e);
   if (i >= 0) {
     while (++i < rows && a[i] == e) ;
     return i - 1;
   }
   return i;
 }
Ejemplo n.º 2
0
  /**
   * Inserts a new record.
   *
   * @param pre record PRE
   * @param id record ID
   * @param c number of inserted records
   */
  public void insert(final int pre, final int id, final int c) {
    if (rows == 0 && pre == id && id == baseid + 1) {
      // no mapping and we append at the end => nothing to do
      baseid += c;
      return;
    }

    int pos = 0;
    int inc = c;
    int oid = pre;

    if (rows > 0) {
      pos = Arrays.binarySearch(pres, 0, rows, pre);
      if (pos < 0) {
        pos = -pos - 1;
        if (pos != 0) {
          // check if inserting into an existing id interval
          final int prev = pos - 1;
          final int prevcnt = nids[prev] - fids[prev] + 1;
          final int prevpre = pres[prev];

          if (pre < prevpre + prevcnt) {
            // split the id interval
            final int split = pre - prevpre;
            final int fid = fids[prev] + split;

            // add a new next interval
            add(pos, pre, fid, nids[prev], incs[prev], oids[prev]);

            // shrink the previous interval
            nids[prev] = fid - 1;
            incs[prev] -= prevcnt - split;

            oid = oids[prev];
            inc += incs[prev];
          } else {
            oid = pre - incs[prev];
            inc += incs[prev];
          }
        }
      } else if (pos > 0) {
        oid = oids[pos];
        inc += incs[pos - 1];
      }

      increment(pos, c);
    }

    // add the new interval
    add(pos, pre, id, id + c - 1, inc, oid);
  }