Class Iterators
java.lang.Object
com.github.basking2.sdsai.itrex.iterators.Iterators
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Iterator<T>
static <T> FilterIterator<T>
filterIterator
(Iterator<T> iterator, Predicate<T> p) static <T> IteratorIterator<T>
Build anIteratorIterator
that will flatten the given iterator of iterators of T.static boolean
Return true if the object is an iterator or can be made into an iterator viatoIterator(Object)
.static <T,
R> MappingIterator<T, R> mapIterator
(Iterator<T> iterator, MappingIterator.Mapper<T, R> f) Construct and return aMappingIterator
.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.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.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.static <T> Iterator<T>
Construct and return aNullSkippingIterator
.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.static <T> Iterator<T>
toIterator
(Object o) Map iterables and arrays to iterators or returns null.static <T> List<T>
Collect the elements of an iterator into aList
.static <T> Iterator<T>
wrap
(T... ts) Wrap one or more values into an iterator.
-
Field Details
-
EMPTY_ITERATOR
-
-
Constructor Details
-
Iterators
public Iterators()
-
-
Method Details
-
toIterator
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. -
isIter
Return true if the object is an iterator or can be made into an iterator viatoIterator(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
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
Construct and return aNullSkippingIterator
.- 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 aMappingIterator
.- 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
Build anIteratorIterator
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
Collect the elements of an iterator into aList
.- Type Parameters:
T
- The type of list elements.- Parameters:
itr
- The iterator to materialize.- Returns:
- a list for the iterator.
-
filterIterator
-
splitMapJoinIterator
public static <R,T, Iterator<R> splitMapJoinIteratorK> (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
-
zip
-
zip
-
mergeSorted
public static <K extends Comparable<K>,T1, long[] mergeSortedT2> (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, long[] mergeSortedDescendingT2> (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, long[] mergeOrHandleSortedT2> (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.
-