@Override public Vector<Object> flatten() { return flatMap( t -> (t instanceof java.lang.Iterable) ? Vector.ofAll((java.lang.Iterable<?>) t).flatten() : Vector.of(t)); }
Vector<Vector<T>> combinations(Vector<T> elements, int k) { return (k == 0) ? Vector.of(Vector.empty()) : elements .zipWithIndex() .flatMap( t -> combinations(elements.drop(t._2 + 1), (k - 1)) .map((Vector<T> c) -> c.prepend(t._1))); }
@Override public Tuple2<Vector<T>, Vector<T>> partition(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); final java.util.List<T> left = new ArrayList<>(), right = new ArrayList<>(); for (int i = 0; i < length(); i++) { T t = get(i); (predicate.test(t) ? left : right).add(t); } return Tuple.of(Vector.ofAll(left), Vector.ofAll(right)); }
@Override public Vector<T> retainAll(java.lang.Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); final Vector<T> keeped = Vector.ofAll(elements).distinct(); HashArrayMappedTrie<Integer, T> result = HashArrayMappedTrie.empty(); for (T element : this) { if (keeped.contains(element)) { result = result.put(result.size(), element); } } return result.isEmpty() ? empty() : new Vector<>(result); }
@Override public Vector<Vector<T>> permutations() { if (isEmpty()) { return Vector.empty(); } else { final Vector<T> tail = tail(); if (tail.isEmpty()) { return Vector.of(this); } else { final Vector<Vector<T>> zero = empty(); // TODO: IntelliJ IDEA 14.1.1 needs a redundant cast here, jdk 1.8.0_40 compiles fine return distinct() .foldLeft( zero, (xs, x) -> xs.appendAll( remove(x) .permutations() .map((Function<Vector<T>, Vector<T>>) l -> l.prepend(x)))); } } }
@Override public Vector<T> slice(int beginIndex, int endIndex) { if (beginIndex < 0 || beginIndex > endIndex || endIndex > length()) { throw new IndexOutOfBoundsException( String.format("slice(%s, %s) on List of length %s", beginIndex, endIndex, length())); } if (beginIndex == endIndex) { return Vector.empty(); } HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = beginIndex; i < endIndex; i++) { trie = trie.put(trie.size(), get(i)); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
/** * Creates a Vector of long numbers starting from {@code from}, extending to {@code toInclusive}. * * <p>Examples: * * <pre> * <code> * Vector.rangeClosed(0L, 0L) // = Vector(0L) * Vector.rangeClosed(2L, 0L) // = Vector() * Vector.rangeClosed(-2L, 2L) // = Vector(-2L, -1L, 0L, 1L, 2L) * </code> * </pre> * * @param from the first number * @param toInclusive the last number * @return a range of long values as specified or the empty range if {@code from > toInclusive} */ public static Vector<Long> rangeClosed(long from, long toInclusive) { return Vector.ofAll(Iterator.rangeClosed(from, toInclusive)); }
/** * Creates a Vector of long numbers starting from {@code from}, extending to {@code toExclusive - * 1}. * * <p>Examples: * * <pre> * <code> * Vector.range(0L, 0L) // = Vector() * Vector.range(2L, 0L) // = Vector() * Vector.range(-2L, 2L) // = Vector(-2L, -1L, 0L, 1L) * </code> * </pre> * * @param from the first number * @param toExclusive the last number + 1 * @return a range of long values as specified or the empty range if {@code from >= toExclusive} */ public static Vector<Long> range(long from, long toExclusive) { return Vector.ofAll(Iterator.range(from, toExclusive)); }
/** * Creates a Vector of int numbers starting from {@code from}, extending to {@code toExclusive - * 1}. * * <p>Examples: * * <pre> * <code> * Vector.range(0, 0) // = Vector() * Vector.range(2, 0) // = Vector() * Vector.range(-2, 2) // = Vector(-2, -1, 0, 1) * </code> * </pre> * * @param from the first number * @param toExclusive the last number + 1 * @return a range of int values as specified or the empty range if {@code from >= toExclusive} */ public static Vector<Integer> range(int from, int toExclusive) { return Vector.ofAll(Iterator.range(from, toExclusive)); }
@Override public <C> Map<C, Vector<T>> groupBy(Function<? super T, ? extends C> classifier) { Objects.requireNonNull(classifier, "classifier is null"); return iterator().groupBy(classifier).map((c, it) -> Map.Entry.of(c, Vector.ofAll(it))); }
@Override public Vector<T> clear() { return Vector.empty(); }
@Override public Vector<T> patch(int from, java.lang.Iterable<? extends T> that, int replaced) { from = from < 0 ? 0 : from; replaced = replaced < 0 ? 0 : replaced; Vector<T> result = take(from).appendAll(that); from += replaced; result = result.appendAll(drop(from)); return result; }
@Override public boolean equals(Object o) { if (o == this) { return true; } else if (o instanceof Vector) { Vector<?> vector1 = this; Vector<?> vector2 = (Vector<?>) o; while (!vector1.isEmpty() && !vector2.isEmpty()) { final boolean isEqual = Objects.equals(vector1.head(), vector2.head()); if (!isEqual) { return false; } vector1 = vector1.tail(); vector2 = vector2.tail(); } return vector1.isEmpty() && vector2.isEmpty(); } else { return false; } }
/** * Creates a Vector based on the elements of a short array. * * @param array a short array * @return A new Vector of Short values */ public static Vector<Short> ofAll(short[] array) { Objects.requireNonNull(array, "array is null"); return Vector.ofAll(Iterator.ofAll(array)); }
/** * Creates a Vector of long numbers starting from {@code from}, extending to {@code toInclusive}, * with {@code step}. * * <p>Examples: * * <pre> * <code> * Vector.rangeClosedBy(1L, 3L, 1L) // = Vector(1L, 2L, 3L) * Vector.rangeClosedBy(1L, 4L, 2L) // = Vector(1L, 3L) * Vector.rangeClosedBy(4L, 1L, -2L) // = Vector(4L, 2L) * Vector.rangeClosedBy(4L, 1L, 2L) // = Vector() * </code> * </pre> * * @param from the first number * @param toInclusive the last number * @param step the step * @return a range of int values as specified or the empty range if<br> * {@code from > toInclusive} and {@code step > 0} or<br> * {@code from < toInclusive} and {@code step < 0} * @throws IllegalArgumentException if {@code step} is zero */ public static Vector<Long> rangeClosedBy(long from, long toInclusive, long step) { return Vector.ofAll(Iterator.rangeClosedBy(from, toInclusive, step)); }
/** * Creates a Vector based on the elements of a boolean array. * * @param array a boolean array * @return A new Vector of Boolean values */ public static Vector<Boolean> ofAll(boolean[] array) { Objects.requireNonNull(array, "array is null"); return Vector.ofAll(Iterator.ofAll(array)); }
/** * Creates a Vector of int numbers starting from {@code from}, extending to {@code toInclusive}. * * <p>Examples: * * <pre> * <code> * Vector.rangeClosed(0, 0) // = Vector(0) * Vector.rangeClosed(2, 0) // = Vector() * Vector.rangeClosed(-2, 2) // = Vector(-2, -1, 0, 1, 2) * </code> * </pre> * * @param from the first number * @param toInclusive the last number * @return a range of int values as specified or the empty range if {@code from > toInclusive} */ public static Vector<Integer> rangeClosed(int from, int toInclusive) { return Vector.ofAll(Iterator.rangeClosed(from, toInclusive)); }
@Override public Vector<Vector<T>> combinations() { return Vector.rangeClosed(0, length()).map(this::combinations).flatMap(Function.identity()); }
/** * Creates a Vector based on the elements of a char array. * * @param array a char array * @return A new Vector of Character values */ public static Vector<Character> ofAll(char[] array) { Objects.requireNonNull(array, "array is null"); return Vector.ofAll(Iterator.ofAll(array)); }
@Override public <U> Vector<Tuple2<T, U>> crossProduct(java.lang.Iterable<? extends U> that) { Objects.requireNonNull(that, "that is null"); final Vector<? extends U> other = Vector.ofAll(that); return flatMap(a -> other.map(b -> Tuple.of(a, b))); }
/** * Creates a Vector based on the elements of a long array. * * @param array a long array * @return A new Vector of Long values */ public static Vector<Long> ofAll(long[] array) { Objects.requireNonNull(array, "array is null"); return Vector.ofAll(Iterator.ofAll(array)); }
@Override public <U> Vector<U> unit(java.lang.Iterable<? extends U> iterable) { return Vector.ofAll(iterable); }
@Override public Tuple2<Vector<T>, Vector<T>> splitAt(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); final Vector<T> init = takeWhile(predicate.negate()); return Tuple.of(init, drop(init.length())); }
/** * Creates a Vector based on the elements of a double array. * * @param array a double array * @return A new Vector of Double values */ public static Vector<Double> ofAll(double[] array) { Objects.requireNonNull(array, "array is null"); return Vector.ofAll(Iterator.ofAll(array)); }
/** * Creates a Vector of int numbers starting from {@code from}, extending to {@code toInclusive}, * with {@code step}. * * <p>Examples: * * <pre> * <code> * Vector.rangeClosedBy(1, 3, 1) // = Vector(1, 2, 3) * Vector.rangeClosedBy(1, 4, 2) // = Vector(1, 3) * Vector.rangeClosedBy(4, 1, -2) // = Vector(4, 2) * Vector.rangeClosedBy(4, 1, 2) // = Vector() * </code> * </pre> * * @param from the first number * @param toInclusive the last number * @param step the step * @return a range of int values as specified or the empty range if<br> * {@code from > toInclusive} and {@code step > 0} or<br> * {@code from < toInclusive} and {@code step < 0} * @throws IllegalArgumentException if {@code step} is zero */ public static Vector<Integer> rangeClosedBy(int from, int toInclusive, int step) { return Vector.ofAll(Iterator.rangeClosedBy(from, toInclusive, step)); }
/** * Creates a Vector based on the elements of an int array. * * @param array an int array * @return A new Vector of Integer values */ public static Vector<Integer> ofAll(int[] array) { Objects.requireNonNull(array, "array is null"); return Vector.ofAll(Iterator.ofAll(array)); }
@Override public Vector<T> sort() { return isEmpty() ? this : toJavaStream().sorted().collect(Vector.collector()); }
@Override public Vector<T> sort(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); return isEmpty() ? this : toJavaStream().sorted(comparator).collect(Vector.collector()); }