Ejemplo n.º 1
0
  /**
   * Removes and returns the first sequence that is count range large.
   *
   * @return a sequence that is count range large, or null if no sequence is that large in the list.
   */
  public Sequence removeFirstSequence(long count) {
    if (isEmpty()) {
      return null;
    }

    Sequence sequence = getHead();
    while (sequence != null) {
      if (sequence.range() == count) {
        sequence.unlink();
        return sequence;
      }
      if (sequence.range() > count) {
        Sequence rc = new Sequence(sequence.first, sequence.first + count - 1);
        sequence.first += count;
        return rc;
      }
      sequence = sequence.getNext();
    }
    return null;
  }
Ejemplo n.º 2
0
  /**
   * @param value the value to add to the list
   * @return false if the value was a duplicate.
   */
  public boolean add(long value) {

    if (isEmpty()) {
      addFirst(new Sequence(value));
      return true;
    }

    // check for append
    Sequence sequence = getTail();
    if (sequence.isAdjacentToLast(value)) {
      sequence.last = value;
      return true;
    }

    sequence = getHead();
    while (sequence != null) {

      if (sequence.isAdjacentToLast(value)) {
        // grow the sequence...
        sequence.last = value;
        // it might connect us to the next sequence..
        if (sequence.getNext() != null) {
          Sequence next = sequence.getNext();
          if (next.isAdjacentToFirst(value)) {
            // Yep the sequence connected.. so join them.
            sequence.last = next.last;
            next.unlink();
          }
        }
        return true;
      }

      if (sequence.isAdjacentToFirst(value)) {
        // grow the sequence...
        sequence.first = value;

        // it might connect us to the previous
        if (sequence.getPrevious() != null) {
          Sequence prev = sequence.getPrevious();
          if (prev.isAdjacentToLast(value)) {
            // Yep the sequence connected.. so join them.
            sequence.first = prev.first;
            prev.unlink();
          }
        }
        return true;
      }

      // Did that value land before this sequence?
      if (value < sequence.first) {
        // Then insert a new entry before this sequence item.
        sequence.linkBefore(new Sequence(value));
        return true;
      }

      // Did that value land within the sequence? The it's a duplicate.
      if (sequence.contains(value)) {
        return false;
      }

      sequence = sequence.getNext();
    }

    // Then the value is getting appended to the tail of the sequence.
    addLast(new Sequence(value));
    return true;
  }