Esempio n. 1
0
  /**
   * Removes the given value from the Sequence set, splitting a contained sequence if necessary.
   *
   * @param value The value that should be removed from the SequenceSet.
   * @return true if the value was removed from the set, false if there was no sequence in the set
   *     that contained the given value.
   */
  public boolean remove(long value) {
    Sequence sequence = getHead();
    while (sequence != null) {
      if (sequence.contains(value)) {
        if (sequence.range() == 1) {
          sequence.unlink();
          return true;
        } else if (sequence.getFirst() == value) {
          sequence.setFirst(value + 1);
          return true;
        } else if (sequence.getLast() == value) {
          sequence.setLast(value - 1);
          return true;
        } else {
          sequence.linkBefore(new Sequence(sequence.first, value - 1));
          sequence.linkAfter(new Sequence(value + 1, sequence.last));
          sequence.unlink();
          return true;
        }
      }

      sequence = sequence.getNext();
    }

    return false;
  }
Esempio n. 2
0
 /**
  * Computes the size of this Sequence by summing the values of all the contained sequences.
  *
  * @return the total number of values contained in this set if it were to be iterated over like an
  *     array.
  */
 public long rangeSize() {
   long result = 0;
   Sequence sequence = getHead();
   while (sequence != null) {
     result += sequence.range();
     sequence = sequence.getNext();
   }
   return result;
 }
Esempio n. 3
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;
  }
Esempio n. 4
0
 public void writePayload(SequenceSet value, DataOutput out) throws IOException {
   out.writeInt(value.size());
   Sequence sequence = value.getHead();
   while (sequence != null) {
     if (sequence.range() > 1) {
       out.writeBoolean(true);
       out.writeLong(sequence.first);
       out.writeLong(sequence.last);
     } else {
       out.writeBoolean(false);
       out.writeLong(sequence.first);
     }
     sequence = sequence.getNext();
   }
 }
Esempio n. 5
0
    public Long next() {
      if (currentEntry == null) {
        throw new NoSuchElementException();
      }

      if (lastReturned < currentEntry.first) {
        lastReturned = currentEntry.first;
        if (currentEntry.range() == 1) {
          currentEntry = currentEntry.getNext();
        }
      } else {
        lastReturned++;
        if (lastReturned == currentEntry.last) {
          currentEntry = currentEntry.getNext();
        }
      }

      return lastReturned;
    }