java.lang.Object
com.github.basking2.sdsai.itrex.iterators.Iterators

public class Iterators extends Object
  • Field Details

    • EMPTY_ITERATOR

      public static Iterator<?> EMPTY_ITERATOR
  • Constructor Details

    • Iterators

      public Iterators()
  • Method Details

    • toIterator

      public static <T> Iterator<T> toIterator(Object o)
      Map iterables and arrays to iterators or returns null.

      If an iterator is passed it, it is returned.

      This will not wrap a non-iterable object (or array) into a single element iterator. See wrap(Object[]) for that.

      Type Parameters:
      T - The type returned by the Iterator.
      Parameters:
      o - An object we would like to try and convert to an Iterator.
      Returns:
      An iterator that walks over the elements in o, or null if we cannot convert o to an Iterator.
    • isIter

      public static boolean isIter(Object o)
      Return true if the object is an iterator or can be made into an iterator via toIterator(Object).
      Parameters:
      o - The object to check.
      Returns:
      true if the object is an iterator or can be made into an iterator via toIterator(Object).
    • wrap

      @SafeVarargs public static <T> Iterator<T> wrap(T... ts)
      Wrap one or more values into an iterator.
      Type Parameters:
      T - The type returned by the returned iterator.
      Parameters:
      ts - The t values to wrap into an interator.
      Returns:
      An interator that will walk through ts values.
    • skipNulls

      public static <T> Iterator<T> skipNulls(Iterator<T> iterator)
      Construct and return a NullSkippingIterator.
      Type Parameters:
      T - A type for iterator.
      Parameters:
      iterator - The iterator.
      Returns:
      A NullSkippingIterator.
    • mapIterator

      public static <T, R> MappingIterator<T,R> mapIterator(Iterator<T> iterator, MappingIterator.Mapper<T,R> f)
      Construct and return a MappingIterator.
      Type Parameters:
      T - The input type.
      R - The output type.
      Parameters:
      iterator - The iterator that provides the input T values.
      f - The mapping function.
      Returns:
      a MappingIterator.
    • flatten

      public static <T> IteratorIterator<T> flatten(Iterator<Iterator<T>> iterator)
      Build an IteratorIterator that will flatten the given iterator of iterators of T.
      Type Parameters:
      T - The type to iterate.
      Parameters:
      iterator - An iterator of iterators of type T.
      Returns:
      An iterator that has flattened the iterator of iterators.
    • toList

      public static <T> List<T> toList(Iterator<T> itr)
      Collect the elements of an iterator into a List.
      Type Parameters:
      T - The type of list elements.
      Parameters:
      itr - The iterator to materialize.
      Returns:
      a list for the iterator.
    • filterIterator

      public static <T> FilterIterator<T> filterIterator(Iterator<T> iterator, Predicate<T> p)
    • splitMapJoinIterator

      public static <R, T, K> Iterator<R> splitMapJoinIterator(Executor executor, Iterator<T> inputs, Function<T,K> splitFunction, MappingIterator.Mapper<T,R> mapper)
      Build an iterator that will concurrently process elements split from the source iterator.
      Type Parameters:
      R - The result type.
      T - The input type.
      K - The key type to group iterations by.
      Parameters:
      executor - The executor service.
      inputs - Inputs to be split into many sub-iterations based on the split function.
      splitFunction - Map input types to keys that will group like items together for mapping in separate threads.
      mapper - The mapper that maps sub-iterations in individual tasks in the executor.
      Returns:
      An iterator that will manage all the complexities of splitting, mapping, and joining.
    • emptyIterator

      public static <T> Iterator<T> emptyIterator()
    • zip

      public static <T1, T2> Iterator<TwoTuple<T1,T2>> zip(Iterator<T1> i1, Iterator<T2> i2)
    • zip

      public static <T1, T2> Iterator<TwoTuple<T1,T2>> zip(Iterator<T1> i1, boolean pad1, T1 pad1Value, Iterator<T2> i2, boolean pad2, T2 pad2Value)
    • mergeSorted

      public static <K extends Comparable<K>, T1, T2> long[] mergeSorted(Iterator<T1> itr1, Function<T1,K> toKey1, Iterator<T2> itr2, Function<T2,K> toKey2, BiConsumer<T1,T2> consumer)
      Given two sorted iterators, merge them. This is essentially the algorithm used by Merge Sort to merge two sorted list segments into a final list, but when two keys are equal (Comparable.compareTo(Object) is 0) then the consumer function is called.
      Type Parameters:
      K - The key type.
      T1 - The type of elements of iterator 1.
      T2 - The type of elements of iterator 2.
      Parameters:
      itr1 - An iterator that provides values for which toKey1 will return ascending keys.
      toKey1 - A mapping function from values to keys.
      itr2 - An iterator that provides values for which toKey2 will return ascending keys.
      toKey2 - A mapping function from values to keys.
      consumer - The user's method of consuming matching values.
      Returns:
      An array that reports the statistics of the merge operation. This array is 3 elements long. The first value is the number of left-values that have no match on the right (T1). The third value is the number of the right-values that have no match on the left (T2). The second value, the middle value, is the number of elemnts for which a match is found.
    • mergeSortedDescending

      public static <K extends Comparable<K>, T1, T2> long[] mergeSortedDescending(Iterator<T1> itr1, Function<T1,K> toKey1, Iterator<T2> itr2, Function<T2,K> toKey2, BiConsumer<T1,T2> consumer)
      Given two sorted iterators, merge them. This is essentially the algorithm used by Merge Sort to merge two sorted list segments into a final list, but when two keys are equal (Comparable.compareTo(Object) is 0) then the consumer function is called.
      Type Parameters:
      K - The key type.
      T1 - The type of elements of iterator 1.
      T2 - The type of elements of iterator 2.
      Parameters:
      itr1 - An iterator that provides values for which toKey1 will return descending keys.
      toKey1 - A mapping function from values to keys.
      itr2 - An iterator that provides values for which toKey2 will return descending keys.
      toKey2 - A mapping function from values to keys.
      consumer - The user's method of consuming matching values.
      Returns:
      An array that reports the statistics of the merge operation. This array is 3 elements long. The first value is the number of left-values that have no match on the right (T1). The third value is the number of the right-values that have no match on the left (T2). The second value, the middle value, is the number of elemnts for which a match is found.
    • mergeOrHandleSorted

      public static <K extends Comparable<K>, T1, T2> long[] mergeOrHandleSorted(Iterator<T1> itr1, Function<T1,K> toKey1, Iterator<T2> itr2, Function<T2,K> toKey2, BiConsumer<T1,T2> consumer)
      Given two sorted iterators, merge them. This is essentially the algorithm used by Merge Sort to merge two sorted list segments into a final list, but when two keys are equal (Comparable.compareTo(Object) is 0) then the consumer function is called. If the keys are not equal, the lower one iks passed to the function with the other value as null.
      Type Parameters:
      K - The key type.
      T1 - The type of elements of iterator 1.
      T2 - The type of elements of iterator 2.
      Parameters:
      itr1 - An iterator that provides values for which toKey1 will return descending keys.
      toKey1 - A mapping function from values to keys.
      itr2 - An iterator that provides values for which toKey2 will return descending keys.
      toKey2 - A mapping function from values to keys.
      consumer - The user's method of consuming matching values.
      Returns:
      An array that reports the statistics of the merge operation. This array is 3 elements long. The first value is the number of left-values that have no match on the right (T1). The third value is the number of the right-values that have no match on the left (T2). The second value, the middle value, is the number of elemnts for which a match is found.