@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> 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 <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> 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> 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 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); }
/** * Creates a HashMap of the given entries. * * @param entries Map entries * @param <K> The key type * @param <V> The value type * @return A new Map containing the given entries */ @SafeVarargs public static <K, V> HashMap<K, V> ofAll(Tuple2<? extends K, ? extends V>... entries) { Objects.requireNonNull(entries, "entries is null"); HashArrayMappedTrie<K, V> trie = HashArrayMappedTrie.empty(); for (Tuple2<? extends K, ? extends V> entry : entries) { trie = trie.put(entry._1, entry._2); } return new HashMap<>(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> 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 <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); }
@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> 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> 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> 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 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> insert(int index, T element) { if (index < 0) { throw new IndexOutOfBoundsException("insert(" + index + ", e)"); } if (index > length()) { throw new IndexOutOfBoundsException( "insert(" + index + ", e) on Vector of length " + length()); } HashArrayMappedTrie<Integer, T> trie = HashArrayMappedTrie.empty(); for (int i = 0; i <= length(); i++) { if (i == index) { trie = trie.put(trie.size(), element); } if (i < length()) { trie = trie.put(trie.size(), get(i)); } } return 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 HashMap<K, V> retainAll(java.lang.Iterable<? extends Tuple2<K, V>> elements) { Objects.requireNonNull(elements, "elements is null"); HashArrayMappedTrie<K, V> tree = HashArrayMappedTrie.empty(); for (Tuple2<K, V> entry : elements) { if (contains(entry)) { tree = tree.put(entry._1, entry._2); } } return tree.isEmpty() ? empty() : new HashMap<>(tree); }
@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); } }
/** * Creates a HashMap of the given entries. * * @param entries Map entries * @param <K> The key type * @param <V> The value type * @return A new Map containing the given entries */ @SuppressWarnings("unchecked") public static <K, V> HashMap<K, V> ofAll( java.lang.Iterable<? extends Tuple2<? extends K, ? extends V>> entries) { Objects.requireNonNull(entries, "entries is null"); if (entries instanceof HashMap) { return (HashMap<K, V>) entries; } else { HashArrayMappedTrie<K, V> trie = HashArrayMappedTrie.empty(); for (Tuple2<? extends K, ? extends V> entry : entries) { trie = trie.put(entry._1, entry._2); } return new HashMap<>(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 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> 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); } }
@Override public Tuple2<Vector<T>, Vector<T>> splitAtInclusive(Predicate<? super T> predicate) { Objects.requireNonNull(predicate, "predicate is null"); HashArrayMappedTrie<Integer, T> init = HashArrayMappedTrie.empty(); for (T t : this) { init = init.put(init.size(), t); if (predicate.test(t)) { if (init.size() == length()) { Tuple.of(this, empty()); } else { return Tuple.of(new Vector<>(init), drop(init.size())); } } } return Tuple.of(this, empty()); }
@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 HashMap<K, V> put(K key, V value) { return new HashMap<>(trie.put(key, value)); }