/** * 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; }
/** * 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; }
/** * 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; }
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(); } }
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; }