@Override default <U> Tree<Tuple2<T, U>> zipAll(Iterable<U> that, T thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); if (isEmpty()) { return Iterator.ofAll(that).map(elem -> Tuple.of(thisElem, elem)).toTree(); } else { final java.util.Iterator<U> thatIter = that.iterator(); final Tree<Tuple2<T, U>> tree = ZipAll.apply((Node<T>) this, thatIter, thatElem); if (thatIter.hasNext()) { final Iterable<Node<Tuple2<T, U>>> remainder = Iterator.ofAll(thatIter).map(elem -> Tree.of(Tuple.of(thisElem, elem))); return new Node<>(tree.getValue(), tree.getChildren().appendAll(remainder)); } else { return tree; } } }
@Override public Array<T> leftPadTo(int length, T element) { final int actualLength = length(); if (length <= actualLength) { return this; } else { return prependAll(Iterator.continually(element).take(length - actualLength)); } }
/** * 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)); }
/** * Creates a Vector based on the elements of a float array. * * @param array a float array * @return A new Vector of Float values */ public static Vector<Float> ofAll(float[] 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}. * * <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 Array based on the elements of a long array. * * @param array a long array * @return A new Array of Long values */ public static Array<Long> ofAll(long[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
@Override default Tree<Tuple2<T, Integer>> zipWithIndex() { return zip(Iterator.from(0)); }
public static Array<Character> range(char from, char toExclusive) { return ofAll(Iterator.range(from, toExclusive)); }
/** * Creates a Array of int numbers starting from {@code from}, extending to {@code toExclusive - * 1}. * * <p>Examples: * * <pre> * <code> * Array.range(0, 0) // = Array() * Array.range(2, 0) // = Array() * Array.range(-2, 2) // = Array(-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 Array<Integer> range(int from, int toExclusive) { return ofAll(Iterator.range(from, toExclusive)); }
/** * Creates a Array of int numbers starting from {@code from}, extending to {@code toInclusive}. * * <p>Examples: * * <pre> * <code> * Array.rangeClosed(0, 0) // = Array(0) * Array.rangeClosed(2, 0) // = Array() * Array.rangeClosed(-2, 2) // = Array(-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 Array<Integer> rangeClosed(int from, int toInclusive) { return ofAll(Iterator.rangeClosed(from, toInclusive)); }
/** * Creates a Array based on the elements of a short array. * * @param array a short array * @return A new Array of Short values */ public static Array<Short> ofAll(short[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of a double array. * * @param array a double array * @return A new Array of Double values */ public static Array<Double> ofAll(double[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of an int array. * * @param array an int array * @return A new Array of Integer values */ public static Array<Integer> ofAll(int[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of a char array. * * @param array a char array * @return A new Array of Character values */ public static Array<Character> ofAll(char[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array based on the elements of a boolean array. * * @param array a boolean array * @return A new Array of Boolean values */ public static Array<Boolean> ofAll(boolean[] array) { Objects.requireNonNull(array, "array is null"); return ofAll(Iterator.ofAll(array)); }
/** * Creates a Array of long numbers starting from {@code from}, extending to {@code toInclusive}. * * <p>Examples: * * <pre> * <code> * Array.rangeClosed(0L, 0L) // = Array(0L) * Array.rangeClosed(2L, 0L) // = Array() * Array.rangeClosed(-2L, 2L) // = Array(-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 Array<Long> rangeClosed(long from, long toInclusive) { return ofAll(Iterator.rangeClosed(from, toInclusive)); }
public static Array<Character> rangeClosed(char from, char toInclusive) { return ofAll(Iterator.rangeClosed(from, toInclusive)); }
/** * Creates a Array of long numbers starting from {@code from}, extending to {@code toExclusive - * 1}. * * <p>Examples: * * <pre> * <code> * Array.range(0L, 0L) // = Array() * Array.range(2L, 0L) // = Array() * Array.range(-2L, 2L) // = Array(-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 Array<Long> range(long from, long toExclusive) { return ofAll(Iterator.range(from, toExclusive)); }
@Override public <U> Vector<Tuple2<T, U>> zip(java.lang.Iterable<U> that) { Objects.requireNonNull(that, "that is null"); HashArrayMappedTrie<Integer, Tuple2<T, U>> result = HashArrayMappedTrie.empty(); Iterator<T> list1 = iterator(); java.util.Iterator<U> list2 = that.iterator(); while (list1.hasNext() && list2.hasNext()) { result = result.put(result.size(), Tuple.of(list1.next(), list2.next())); } return result.isEmpty() ? empty() : new Vector<>(result); }
@Override public <U> Vector<Tuple2<T, U>> zipAll(java.lang.Iterable<U> that, T thisElem, U thatElem) { Objects.requireNonNull(that, "that is null"); HashArrayMappedTrie<Integer, Tuple2<T, U>> result = HashArrayMappedTrie.empty(); Iterator<T> list1 = iterator(); java.util.Iterator<U> list2 = that.iterator(); while (list1.hasNext() || list2.hasNext()) { final T elem1 = list1.hasNext() ? list1.next() : thisElem; final U elem2 = list2.hasNext() ? list2.next() : thatElem; result = result.put(result.size(), Tuple.of(elem1, elem2)); } return result.isEmpty() ? empty() : new Vector<>(result); }
@Override public Vector<T> padTo(int length, T element) { if (length <= length()) { return this; } else { return appendAll(Iterator.constant(element).take(length - length())); } }
/** * Creates a Vector of int numbers starting from {@code from}, extending to {@code toExclusive - * 1}, with {@code step}. * * <p>Examples: * * <pre> * <code> * Vector.rangeBy(1, 3, 1) // = Vector(1, 2) * Vector.rangeBy(1, 4, 2) // = Vector(1, 3) * Vector.rangeBy(4, 1, -2) // = Vector(4, 2) * Vector.rangeBy(4, 1, 2) // = Vector() * </code> * </pre> * * @param from the first number * @param toExclusive the last number + 1 * @param step the step * @return a range of long 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> rangeBy(int from, int toExclusive, int step) { return Vector.ofAll(Iterator.rangeBy(from, toExclusive, step)); }
/** * Creates a Vector of long numbers starting from {@code from}, extending to {@code toExclusive - * 1}, with {@code step}. * * <p>Examples: * * <pre> * <code> * Vector.rangeBy(1L, 3L, 1L) // = Vector(1L, 2L) * Vector.rangeBy(1L, 4L, 2L) // = Vector(1L, 3L) * Vector.rangeBy(4L, 1L, -2L) // = Vector(4L, 2L) * Vector.rangeBy(4L, 1L, 2L) // = Vector() * </code> * </pre> * * @param from the first number * @param toExclusive the last number + 1 * @param step the step * @return a range of long 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> rangeBy(long from, long toExclusive, long step) { return Vector.ofAll(Iterator.rangeBy(from, toExclusive, step)); }
/** * Creates a Array of long numbers starting from {@code from}, extending to {@code toInclusive}, * with {@code step}. * * <p>Examples: * * <pre> * <code> * Array.rangeClosedBy(1L, 3L, 1L) // = Array(1L, 2L, 3L) * Array.rangeClosedBy(1L, 4L, 2L) // = Array(1L, 3L) * Array.rangeClosedBy(4L, 1L, -2L) // = Array(4L, 2L) * Array.rangeClosedBy(4L, 1L, 2L) // = Array() * </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 Array<Long> rangeClosedBy(long from, long toInclusive, long step) { return ofAll(Iterator.rangeClosedBy(from, toInclusive, step)); }
@Override public boolean hasNext() { return iterator.hasNext(); }
/** * Creates a Array of int numbers starting from {@code from}, extending to {@code toInclusive}, * with {@code step}. * * <p>Examples: * * <pre> * <code> * Array.rangeClosedBy(1, 3, 1) // = Array(1, 2, 3) * Array.rangeClosedBy(1, 4, 2) // = Array(1, 3) * Array.rangeClosedBy(4, 1, -2) // = Array(4, 2) * Array.rangeClosedBy(4, 1, 2) // = Array() * </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 Array<Integer> rangeClosedBy(int from, int toInclusive, int step) { return ofAll(Iterator.rangeClosedBy(from, toInclusive, step)); }
public static Array<Double> rangeClosedBy(double from, double toInclusive, double step) { return ofAll(Iterator.rangeClosedBy(from, toInclusive, step)); }
public static Array<Character> rangeClosedBy(char from, char toInclusive, int step) { return ofAll(Iterator.rangeClosedBy(from, toInclusive, step)); }
@Override public int nextInt() { return iterator.next(); }