@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<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); }
@Override public Vector<T> reverse() { HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { trie = trie.put(i, get(length() - 1 - i)); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public Vector<T> appendAll(java.lang.Iterable<? extends T> elements) { HashArrayMappedTrie<Integer, T> result = trie; for (T element : elements) { result = result.put(result.size(), element); } return new Vector<>(result); }
@Override public <U> Vector<U> map(Function<? super T, ? extends U> mapper) { Objects.requireNonNull(mapper, "mapper is null"); HashArrayMappedTrie<Integer, U> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { trie = trie.put(i, mapper.apply(get(i))); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
/** * Creates a Vector of the given elements. * * @param <T> Component type of the Vector. * @param elements Zero or more elements. * @return A vector containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SafeVarargs public static <T> Vector<T> of(T... elements) { Objects.requireNonNull(elements, "elements is null"); HashArrayMappedTrie<Integer, T> result = HashArrayMappedTrie.empty(); for (T element : elements) { result = result.put(result.size(), element); } return elements.length == 0 ? empty() : new Vector<>(result); }
@Override public Vector<T> replaceAll(UnaryOperator<T> operator) { Objects.requireNonNull(operator, "operator is null"); HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { trie = trie.put(trie.size(), operator.apply(get(i))); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public Vector<T> tail() { if (isEmpty()) { throw new UnsupportedOperationException("tail of empty vector"); } HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 1; i < length(); i++) { trie = trie.put(i - 1, get(i)); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public Option<Vector<T>> tailOption() { if (isEmpty()) { return None.instance(); } HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 1; i < length(); i++) { trie = trie.put(i - 1, get(i)); } return new Some<>(trie.isEmpty() ? empty() : new Vector<>(trie)); }
@Override public Vector<T> takeWhile(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { T value = get(i); if (!predicate.test(value)) { break; } trie = trie.put(i, get(i)); } return trie.size() == length() ? this : trie.isEmpty() ? empty() : new Vector<>(trie); }
/** * Creates a Vector of the given elements. * * <p>The resulting vector has the same iteration order as the given iterable of elements if the * iteration order of the elements is stable. * * @param <T> Component type of the Vector. * @param elements An java.lang.Iterable of elements. * @return A vector containing the given elements in the same order. * @throws NullPointerException if {@code elements} is null */ @SuppressWarnings("unchecked") public static <T> Vector<T> ofAll(java.lang.Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (elements instanceof Vector) { return (Vector<T>) elements; } else { HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (T element : elements) { trie = trie.put(trie.size(), element); } return new Vector<>(trie); } }
@Override public <T1, T2> Tuple2<Vector<T1>, Vector<T2>> unzip( Function<? super T, Tuple2<? extends T1, ? extends T2>> unzipper) { Objects.requireNonNull(unzipper, "unzipper is null"); HashArrayMappedTrie<Integer, T1> xs = HashArrayMappedTrie.empty(); HashArrayMappedTrie<Integer, T2> ys = HashArrayMappedTrie.empty(); for (T element : this) { final Tuple2<? extends T1, ? extends T2> t = unzipper.apply(element); xs = xs.put(xs.size(), t._1); ys = ys.put(ys.size(), t._2); } return Tuple.of( xs.isEmpty() ? empty() : new Vector<>(xs), ys.isEmpty() ? empty() : new Vector<>(ys)); }
@Override public Vector<T> dropRight(int n) { if (n <= 0) { return this; } if (n >= length()) { return empty(); } HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length() - n; i++) { trie = trie.put(trie.size(), get(i)); } return new Vector<>(trie); }
@Override public Vector<T> drop(int n) { if (n <= 0) { return this; } if (n >= length()) { return empty(); } HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = n; i < length(); i++) { trie = trie.put(i - n, get(i)); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public T get(int index) { if (index < 0 || index >= length()) { throw new IndexOutOfBoundsException("get(" + index + ")"); } return trie.get(index).get(); }
@Override public <U> Vector<U> flatMap( Function<? super T, ? extends java.lang.Iterable<? extends U>> mapper) { Objects.requireNonNull(mapper, "mapper is null"); if (isEmpty()) { return empty(); } else { HashArrayMappedTrie<Integer, U> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { for (U u : mapper.apply(get(i))) { trie = trie.put(trie.size(), u); } } return trie.isEmpty() ? empty() : new Vector<>(trie); } }
@Override public Vector<T> removeAt(int indx) { if (indx < 0) { throw new IndexOutOfBoundsException("removeAt(" + indx + ")"); } if (indx >= length()) { throw new IndexOutOfBoundsException("removeAt(" + indx + ")"); } HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { if (i != indx) { trie = trie.put(trie.size(), get(i)); } } return trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public Vector<T> init() { if (isEmpty()) { throw new UnsupportedOperationException("init of empty vector"); } return new Vector<>(trie.remove(length() - 1)); }
@Override public Vector<Tuple2<T, Integer>> zipWithIndex() { HashArrayMappedTrie<Integer, Tuple2<T, Integer>> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { trie = trie.put(i, Tuple.of(get(i), i)); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public Vector<T> update(int index, T element) { if (index < 0) { throw new IndexOutOfBoundsException("update(" + index + ")"); } if (index >= length()) { throw new IndexOutOfBoundsException("update(" + index + ")"); } return new Vector<>(trie.put(index, element)); }
@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 Vector<T> removeLast(Predicate<T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = length() - 1; i >= 0; i--) { if (predicate.test(get(i))) { return removeAt(i); } } return this; }
@Override public Vector<T> intersperse(T element) { HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { if (i > 0) { trie = trie.put(trie.size(), element); } trie = trie.put(trie.size(), get(i)); } return trie.isEmpty() ? empty() : new Vector<>(trie); }
@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> removeAll(java.lang.Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); HashArrayMappedTrie<T, T> removed = HashArrayMappedTrie.empty(); for (T element : elements) { removed = removed.put(element, element); } HashArrayMappedTrie<Integer, T> result = HashArrayMappedTrie.empty(); boolean found = false; for (int i = 0; i < length(); i++) { T element = get(i); if (removed.get(element).isDefined()) { found = true; } else { result = result.put(result.size(), element); } } return found ? (result.isEmpty() ? empty() : new Vector<>(result)) : this; }
@Override public Vector<T> remove(T element) { HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); boolean found = false; for (int i = 0; i < length(); i++) { final T value = get(i); if (found) { trie = trie.put(trie.size(), value); } else { if (element.equals(value)) { found = true; } else { trie = trie.put(trie.size(), value); } } } return trie.size() == length() ? this : trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public Vector<T> removeAll(T element) { HashArrayMappedTrie<Integer, T> result = HashArrayMappedTrie.empty(); for (int i = 0; i < length(); i++) { final T value = get(i); if (!element.equals(value)) { result = result.put(result.size(), value); } } return result.size() == length() ? this : (result.isEmpty() ? empty() : new Vector<>(result)); }
@Override public Vector<T> replaceAll(T currentElement, T newElement) { HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); boolean changed = false; for (int i = 0; i < length(); i++) { final T value = get(i); if (currentElement.equals(value)) { trie = trie.put(trie.size(), newElement); changed = true; } else { trie = trie.put(trie.size(), value); } } return changed ? (trie.isEmpty() ? empty() : new Vector<>(trie)) : this; }
@Override public Vector<T> removeFirst(Predicate<T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); boolean found = false; for (int i = 0; i < length(); i++) { final T value = get(i); if (found) { trie = trie.put(trie.size(), value); } else { if (predicate.test(value)) { found = true; } else { trie = trie.put(trie.size(), value); } } } return trie.size() == length() ? this : trie.isEmpty() ? empty() : new Vector<>(trie); }
@Override public Vector<T> filter(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (T t : this) { if (predicate.test(t)) { trie = trie.put(trie.size(), t); } } if (trie.size() == length()) { return this; } else { return trie.isEmpty() ? empty() : new Vector<>(trie); } }