Exemple #1
1
 @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);
 }
Exemple #2
1
 @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);
 }
Exemple #3
0
 @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);
 }
Exemple #4
0
 @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 HashMap<K, V> takeRight(int n) {
   if (trie.size() <= n) {
     return this;
   } else {
     return HashMap.ofAll(trie.iterator().takeRight(n));
   }
 }
Exemple #6
0
 /**
  * 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);
 }
Exemple #7
0
 @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);
 }
 /**
  * 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);
 }
 @Override
 public HashMap<K, V> removeAll(java.lang.Iterable<? extends K> keys) {
   Objects.requireNonNull(keys, "keys is null");
   HashArrayMappedTrie<K, V> result = trie;
   for (K key : keys) {
     result = result.remove(key);
   }
   return result.isEmpty() ? empty() : new HashMap<>(result);
 }
Exemple #10
0
 @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);
 }
Exemple #11
0
 @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 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);
 }
Exemple #13
0
 @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));
 }
Exemple #14
0
 /**
  * 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);
   }
 }
Exemple #15
0
 @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 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);
   }
 }
Exemple #17
0
 @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));
 }
Exemple #18
0
 @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);
 }
Exemple #19
0
 @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);
 }
Exemple #20
0
 @Override
 public T get(int index) {
   if (index < 0 || index >= length()) {
     throw new IndexOutOfBoundsException("get(" + index + ")");
   }
   return trie.get(index).get();
 }
Exemple #21
0
 @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);
 }
Exemple #22
0
 @Override
 public Vector<T> init() {
   if (isEmpty()) {
     throw new UnsupportedOperationException("init of empty vector");
   }
   return new Vector<>(trie.remove(length() - 1));
 }
Exemple #23
0
 @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 HashMap<K, V> init() {
   if (trie.isEmpty()) {
     throw new UnsupportedOperationException("init of empty HashMap");
   } else {
     return remove(last()._1);
   }
 }
 @Override
 public Option<HashMap<K, V>> tailOption() {
   if (trie.isEmpty()) {
     return None.instance();
   } else {
     return new Some<>(tail());
   }
 }
Exemple #26
0
 @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 HashMap<K, V> tail() {
   if (trie.isEmpty()) {
     throw new UnsupportedOperationException("tail of empty HashMap");
   } else {
     return remove(head()._1);
   }
 }
Exemple #28
0
 @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));
 }
Exemple #29
0
 @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;
 }
Exemple #30
0
 @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);
 }