Пример #1
0
  /**
   * Adds an element to the start of the array.
   *
   * @param elem element to add
   * @return self reference for convenience
   */
  public ArrayBuilder prepend(final Value elem) {
    if (inLeft < Array.MAX_DIGIT) {
      // just insert the element
      vals[(mid - inLeft + CAP - 1) % CAP] = elem;
      inLeft++;
    } else if (tree.isEmpty() && inRight < Array.MAX_DIGIT) {
      // move the middle to the left
      mid = (mid + CAP - 1) % CAP;
      vals[(mid - inLeft + CAP) % CAP] = elem;
      inRight++;
    } else {
      // push leaf node into the tree
      final Value[] leaf = new Value[NODE_SIZE];
      final int start = (mid - NODE_SIZE + CAP) % CAP;
      for (int i = 0; i < NODE_SIZE; i++) leaf[i] = vals[(start + i) % CAP];
      tree.prepend(new LeafNode(leaf));

      // move rest of the nodes to the right
      final int rest = inLeft - NODE_SIZE;
      final int p0 = (mid - inLeft + CAP) % CAP;
      for (int i = 0; i < rest; i++) {
        final int from = (p0 + i) % CAP, to = (from + NODE_SIZE) % CAP;
        vals[to] = vals[from];
      }

      // insert the element
      vals[(mid - rest + CAP - 1) % CAP] = elem;
      inLeft = rest + 1;
    }
    return this;
  }
 /**
  * Prepends a single element to the array.
  *
  * @param elem element to prepend
  * @return this builder for convenience
  */
 public ObjectArrayBuilder<E> prepend(final E elem) {
   builder.prepend(new ObjectArray.Leaf<>(elem));
   return this;
 }