partitionDedupBy method

(Slice<T>, Slice<T>) partitionDedupBy(
  1. bool sameBucket(
    1. T,
    2. T
    )
)

Moves all consecutive repeated elements to the end of the slice according to Comparable. Returns two slices. The first contains no consecutive repeated elements. The second contains all the duplicates in no specified order. The sameBucket function is passed the to two elements from the slice and must determine if the elements compare equal. The elements are passed in opposite order from their order in the slice, so if same_bucket(a, b) returns true, a is moved at the end of the slice. If the slice is sorted, the first returned slice contains no duplicates.

Implementation

(Slice<T> dedup, Slice<T> duplicates) partitionDedupBy(
    bool Function(T, T) sameBucket) {
  final length = len();
  if (length <= 1) {
    return (slice(0, length), slice(0, 0));
  }

  int nextRead = 1;
  int nextWrite = 1;

  while (nextRead < length) {
    if (!sameBucket(getUnchecked(nextRead), getUnchecked(nextWrite - 1))) {
      if (nextRead != nextWrite) {
        T temp = getUnchecked(nextRead);
        setUnchecked(nextRead, getUnchecked(nextWrite));
        setUnchecked(nextWrite, temp);
      }
      nextWrite += 1;
    }
    nextRead += 1;
  }

  return (slice(0, nextWrite), slice(nextWrite));
}