javaslang.collection.IndexedSeq Maven / Gradle / Ivy
Show all versions of javaslang Show documentation
/* / \____ _ _ ____ ______ / \ ____ __ _ _____
* / / \/ \ / \/ \ / /\__\/ // \/ \ / / _ \ Javaslang
* _/ / /\ \ \/ / /\ \\__\\ \ // /\ \ /\\/ \__/ / Copyright 2014-now Daniel Dietrich
* /___/\_/ \_/\____/\_/ \_/\__\/__/___\_/ \_// \__/_____/ Licensed under the Apache License, Version 2.0
*/
package javaslang.collection;
import javaslang.Tuple2;
import javaslang.Tuple3;
import javaslang.control.Match;
import javaslang.control.Option;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* Interface for immutable, indexed sequences.
*
* Efficient random access is characteristic for indexed sequences.
*
* @param component type
* @author Daniel Dietrich
* @since 2.0.0
*/
public interface IndexedSeq extends Seq {
long serialVersionUID = 1L;
// -- Adjusted return types of Seq methods
@Override
IndexedSeq append(T element);
@Override
IndexedSeq appendAll(Iterable extends T> elements);
@Override
IndexedSeq clear();
@Override
IndexedSeq extends IndexedSeq> combinations();
@Override
IndexedSeq extends IndexedSeq> combinations(int k);
@Override
IndexedSeq> crossProduct();
@Override
IndexedSeq extends IndexedSeq> crossProduct(int power);
@Override
IndexedSeq> crossProduct(Iterable extends U> that);
@Override
IndexedSeq distinct();
@Override
IndexedSeq distinctBy(Comparator super T> comparator);
@Override
IndexedSeq distinctBy(Function super T, ? extends U> keyExtractor);
@Override
IndexedSeq drop(long n);
@Override
IndexedSeq dropRight(long n);
@Override
IndexedSeq dropUntil(Predicate super T> predicate);
@Override
IndexedSeq dropWhile(Predicate super T> predicate);
@Override
default boolean endsWith(Seq extends T> that) {
Objects.requireNonNull(that, "that is null");
if (that instanceof IndexedSeq) {
int i = length() - 1;
int j = that.length() - 1;
if (j > i) {
return false;
} else {
while (j >= 0) {
if (!Objects.equals(this.get(i), that.get(j))) {
return false;
}
i--;
j--;
}
return true;
}
} else {
return Seq.super.endsWith(that);
}
}
@Override
IndexedSeq filter(Predicate super T> predicate);
@Override
IndexedSeq flatMap(Function super T, ? extends Iterable extends U>> mapper);
@Override
Map> groupBy(Function super T, ? extends C> classifier);
@Override
default int indexWhere(Predicate super T> predicate, int from) {
Objects.requireNonNull(predicate, "predicate is null");
int start = Math.max(from, 0);
int n = start + segmentLength(predicate.negate(), start);
return (n >= length()) ? -1 : n;
}
@Override
Iterator extends IndexedSeq> grouped(long size);
@Override
IndexedSeq init();
@Override
Option extends IndexedSeq> initOption();
@Override
IndexedSeq insert(int index, T element);
@Override
IndexedSeq insertAll(int index, Iterable extends T> elements);
@Override
IndexedSeq intersperse(T element);
@Override
default T last() {
if (isEmpty()) {
throw new NoSuchElementException("last of empty IndexedSeq");
} else {
return get(length() - 1);
}
}
@Override
default int lastIndexOfSlice(Iterable extends T> that, int end) {
return IndexedSeqModule.LastIndexOfSlice.lastIndexOfSlice(this, unit(that), end);
}
@Override
default int lastIndexWhere(Predicate super T> predicate, int end) {
Objects.requireNonNull(predicate, "predicate is null");
int i = Math.min(end, length() - 1);
while (i >= 0 && !predicate.test(this.get(i))) {
i--;
}
return i;
}
@Override
IndexedSeq map(Function super T, ? extends U> mapper);
@Override
Match.MatchValue.Of extends IndexedSeq> match();
@Override
IndexedSeq padTo(int length, T element);
@Override
IndexedSeq patch(int from, Iterable extends T> that, int replaced);
@Override
Tuple2 extends IndexedSeq, ? extends IndexedSeq> partition(Predicate super T> predicate);
@Override
IndexedSeq peek(Consumer super T> action);
@Override
IndexedSeq extends IndexedSeq> permutations();
@Override
IndexedSeq prepend(T element);
@Override
IndexedSeq prependAll(Iterable extends T> elements);
@Override
IndexedSeq remove(T element);
@Override
IndexedSeq removeFirst(Predicate predicate);
@Override
IndexedSeq removeLast(Predicate predicate);
@Override
IndexedSeq removeAt(int index);
@Override
IndexedSeq removeAll(T element);
@Override
IndexedSeq removeAll(Iterable extends T> elements);
@Override
IndexedSeq replace(T currentElement, T newElement);
@Override
IndexedSeq replaceAll(T currentElement, T newElement);
@Override
IndexedSeq retainAll(Iterable extends T> elements);
@Override
IndexedSeq reverse();
@Override
default Iterator reverseIterator() {
return new AbstractIterator() {
private int i = IndexedSeq.this.length();
@Override
public boolean hasNext() {
return i > 0;
}
@Override
public T getNext() {
return IndexedSeq.this.get(--i);
}
};
}
@Override
IndexedSeq scan(T zero, BiFunction super T, ? super T, ? extends T> operation);
@Override
IndexedSeq scanLeft(U zero, BiFunction super U, ? super T, ? extends U> operation);
@Override
IndexedSeq scanRight(U zero, BiFunction super T, ? super U, ? extends U> operation);
@Override
default int segmentLength(Predicate super T> predicate, int from) {
Objects.requireNonNull(predicate, "predicate is null");
int len = length();
int i = from;
while (i < len && predicate.test(this.get(i))) {
i++;
}
return i - from;
}
@Override
IndexedSeq slice(long beginIndex, long endIndex);
@Override
Iterator extends IndexedSeq> sliding(long size);
@Override
Iterator extends IndexedSeq> sliding(long size, long step);
@Override
IndexedSeq sort();
@Override
IndexedSeq sort(Comparator super T> comparator);
@Override
> IndexedSeq sortBy(Function super T, ? extends U> mapper);
@Override
IndexedSeq sortBy(Comparator super U> comparator, Function super T, ? extends U> mapper);
@Override
Tuple2 extends IndexedSeq, ? extends IndexedSeq> span(Predicate super T> predicate);
@Override
default boolean startsWith(Iterable extends T> that, int offset) {
Objects.requireNonNull(that, "that is null");
if (offset < 0) return false;
if (that instanceof IndexedSeq) {
IndexedSeq extends T> dhat = (IndexedSeq extends T>) that;
int i = offset;
int j = 0;
int thisLen = length();
int thatLen = dhat.length();
while (i < thisLen && j < thatLen && Objects.equals(this.get(i), dhat.get(j))) {
i++;
j++;
}
return j == thatLen;
} else {
int i = offset;
int thisLen = length();
java.util.Iterator extends T> thatElems = that.iterator();
while (i < thisLen && thatElems.hasNext()) {
if (!Objects.equals(this.get(i), thatElems.next())) {
return false;
}
i++;
}
return !thatElems.hasNext();
}
}
@Override
IndexedSeq subSequence(int beginIndex);
@Override
IndexedSeq subSequence(int beginIndex, int endIndex);
@Override
IndexedSeq tail();
@Override
Option extends IndexedSeq> tailOption();
@Override
IndexedSeq take(long n);
@Override
IndexedSeq takeRight(long n);
@Override
IndexedSeq takeUntil(Predicate super T> predicate);
@Override
IndexedSeq takeWhile(Predicate super T> predicate);
@Override
IndexedSeq unit(Iterable extends U> iterable);
@Override
Tuple2 extends IndexedSeq, ? extends IndexedSeq> unzip(Function super T, Tuple2 extends T1, ? extends T2>> unzipper);
@Override
Tuple3 extends IndexedSeq, ? extends IndexedSeq, ? extends IndexedSeq> unzip3(Function super T, Tuple3 extends T1, ? extends T2, ? extends T3>> unzipper);
@Override
IndexedSeq update(int index, T element);
@Override
IndexedSeq> zip(Iterable that);
@Override
IndexedSeq> zipAll(Iterable that, T thisElem, U thatElem);
@Override
IndexedSeq> zipWithIndex();
}
interface IndexedSeqModule {
interface LastIndexOfSlice {
static int lastIndexOfSlice(IndexedSeq t, IndexedSeq slice, int end) {
if (end < 0) {
return -1;
}
if (t.isEmpty()) {
return slice.isEmpty() ? 0 : -1;
}
if (slice.isEmpty()) {
int len = t.length();
return len < end ? len : end;
}
int p = 0;
int result = -1;
final int maxPtr = t.length() - slice.length();
while (p <= maxPtr) {
int r = findSlice(t, p, maxPtr, slice);
if (r < 0) {
return result;
}
if (r <= end) {
result = r;
p = r + 1;
} else {
return result;
}
}
return result;
}
static int findSlice(IndexedSeq t, int p, int maxPtr, IndexedSeq slice) {
while (p <= maxPtr) {
if (t.startsWith(slice, p)) {
return p;
}
p++;
}
return -1;
}
}
}