edu.rice.cs.plt.iter
Class IterUtil

java.lang.Object
  extended by edu.rice.cs.plt.iter.IterUtil

public final class IterUtil
extends Object

A collection of static methods operating on iterables and iterators.

Most classes instantiated by these methods are serializable. However, since the classes generally wrap other objects, those objects must be serializable in order for serialization to succeed.


Method Summary
static
<T> boolean
and(Iterable<? extends T> iter, Predicate<? super T> pred)
          Check whether the given predicate holds for all values in iter.
static
<T1,T2,T3,T4>
boolean
and(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4, Predicate4<? super T1,? super T2,? super T3,? super T4> pred)
          Check whether the given predicate holds for all corresponding values in the given iterables.
static
<T1,T2,T3> boolean
and(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Predicate3<? super T1,? super T2,? super T3> pred)
          Check whether the given predicate holds for all corresponding values in the given iterables.
static
<T1,T2> boolean
and(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Predicate2<? super T1,? super T2> pred)
          Check whether the given predicate holds for all corresponding values in iter1 and iter2.
static SizedIterable<?> arrayAsIterable(Object array)
          Returns an iterable that traverses the given array, which may contain primitives or references.
static
<T> Enumeration<T>
asEnumeration(Iterator<? extends T> iter)
          Make an Enumeration based on the given Iterator.
static SizedIterable<Boolean> asIterable(boolean[] values)
          Create a SizedIterable wrapping the given array.
static SizedIterable<Byte> asIterable(byte[] values)
          Create a SizedIterable wrapping the given array.
static SizedIterable<Character> asIterable(char[] values)
          Create a SizedIterable wrapping the given array.
static SizedIterable<Character> asIterable(CharSequence sequence)
          Create an iterable that wraps the given CharSequence.
static SizedIterable<Double> asIterable(double[] values)
          Create a SizedIterable wrapping the given array.
static SizedIterable<Float> asIterable(float[] values)
          Create a SizedIterable wrapping the given array.
static SizedIterable<Integer> asIterable(int[] values)
          Create a SizedIterable wrapping the given array.
static SizedIterable<Long> asIterable(long[] values)
          Create a SizedIterable wrapping the given array.
static SizedIterable<Short> asIterable(short[] values)
          Create a SizedIterable wrapping the given array.
static SizedIterable<Character> asIterable(String sequence)
          Create an iterable that wraps the given string.
static
<T> SizedIterable<T>
asIterable(T... array)
          Create a SizedIterable wrapping the given array.
static
<T> ReadOnlyIterator<T>
asIterator(Enumeration<? extends T> en)
          Make an iterator based on a (legacy-style) Enumeration.
static ReadOnlyIterator<Byte> asIterator(InputStream in)
          Make an iterator based on an InputStream.
static ReadOnlyIterator<Character> asIterator(Reader in)
          Make an iterator based on a Reader.
static ReadOnlyIterator<String> asIterator(StringTokenizer s)
          Make an iterator based on a StringTokenizer.
static
<T> SizedIterable<T>
asSizedIterable(Collection<T> coll)
          Convert the given Collection to a SizedIterable.
static
<T> CollapsedIterable<T>
collapse(Iterable<? extends Iterable<? extends T>> iters)
          Collapse a list of lists into a single list.
static
<T> ComposedIterable<T>
compose(Iterable<? extends T> i1, Iterable<? extends T> i2)
          Create a ComposedIterable with the given arguments.
static
<T> ComposedIterable<T>
compose(Iterable<? extends T> rest, T last)
          Create a ComposedIterable with the given arguments.
static
<T> ComposedIterable<T>
compose(T first, Iterable<? extends T> rest)
          Create a ComposedIterable with the given arguments.
static
<T> Lambda2<Iterable<? extends T>,Iterable<? extends T>,Iterable<T>>
composeLambda()
          Produce a lambda that invokes compose(Object, Iterable).
static
<T> Lambda2<T,Iterable<? extends T>,Iterable<T>>
composeLeftLambda()
          Produce a lambda that invokes compose(Object, Iterable).
static
<T> Lambda2<Iterable<? extends T>,T,Iterable<T>>
composeRightLambda()
          Produce a lambda that invokes compose(Iterable, Object).
static
<T> Iterable<T>
conditionalSnapshot(Iterable<T> iter, int threshold)
          Produce a snapshot of iter if its composite size is greater than the given threshold.
static boolean contains(Iterable<?> iter, Object o)
          Test whether the given object appears in an iteration of iter.
static
<T> FiniteSequenceIterable<T>
copy(T value, int copies)
          Create a sequence containing copies instances of the given value.
static
<T> SizedIterable<Iterable<T>>
cross(Iterable<? extends Iterable<? extends T>> iters)
          Lazily produce the cartesian (cross) product of an arbitrary number of iterables.
static
<T1,T2> SizedIterable<Pair<T1,T2>>
cross(Iterable<? extends T1> left, Iterable<? extends T2> right)
          Lazily produce the cartesian (cross) product of two iterables, wrapping each combination of elements in a Pair.
static
<T1,T2,T3> SizedIterable<Triple<T1,T2,T3>>
cross(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3)
          Lazily produce the cartesian (cross) product of three iterables, wrapping each combination of elements in a Triple.
static
<T1,T2,T3,T4>
SizedIterable<Quad<T1,T2,T3,T4>>
cross(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4)
          Lazily produce the cartesian (cross) product of four iterables, wrapping each combination of elements in a Quad.
static
<T1,T2,T3,T4,R>
SizedIterable<R>
cross(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4, Lambda4<? super T1,? super T2,? super T3,? super T4,? extends R> combiner)
          Lazily produce the cartesian (cross) product of four iterables.
static
<T1,T2,T3,R>
SizedIterable<R>
cross(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Lambda3<? super T1,? super T2,? super T3,? extends R> combiner)
          Lazily produce the cartesian (cross) product of three iterables.
static
<T1,T2,R> SizedIterable<R>
cross(Iterable<? extends T1> left, Iterable<? extends T2> right, Lambda2<? super T1,? super T2,? extends R> combiner)
          Lazily produce the cartesian (cross) product of two iterables.
static
<T,R> SizedIterable<R>
crossFold(Iterable<? extends Iterable<? extends T>> iters, R base, Lambda2<? super R,? super T,? extends R> combiner)
          Lazily apply the given folding function to each tuple in the cartesian (cross) product of the given iterables.
static
<T> SizedIterable<Iterable<T>>
diagonalCross(Iterable<? extends Iterable<? extends T>> iters)
          Lazily produce the cartesian (cross) product of an arbitrary number of iterables.
static
<T1,T2> SizedIterable<Pair<T1,T2>>
diagonalCross(Iterable<? extends T1> left, Iterable<? extends T2> right)
          Lazily produce the cartesian (cross) product of two iterables, wrapping each combination of elements in a Pair.
static
<T1,T2,T3> SizedIterable<Triple<T1,T2,T3>>
diagonalCross(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3)
          Lazily produce the cartesian (cross) product of three iterables, wrapping each combination of elements in a Triple.
static
<T1,T2,T3,T4>
SizedIterable<Quad<T1,T2,T3,T4>>
diagonalCross(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4)
          Lazily produce the cartesian (cross) product of four iterables, wrapping each combination of elements in a Quad.
static
<T1,T2,T3,T4,R>
SizedIterable<R>
diagonalCross(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4, Lambda4<? super T1,? super T2,? super T3,? super T4,? extends R> combiner)
          Lazily produce the cartesian (cross) product of four iterables.
static
<T1,T2,T3,R>
SizedIterable<R>
diagonalCross(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Lambda3<? super T1,? super T2,? super T3,? extends R> combiner)
          Lazily produce the cartesian (cross) product of three iterables.
static
<T1,T2,R> SizedIterable<R>
diagonalCross(Iterable<? extends T1> left, Iterable<? extends T2> right, Lambda2<? super T1,? super T2,? extends R> combiner)
          Lazily produce the cartesian (cross) product of two iterables.
static
<T,R> SizedIterable<R>
diagonalCrossFold(Iterable<? extends Iterable<? extends T>> iters, R base, Lambda2<? super R,? super T,? extends R> combiner)
          Lazily apply the given folding function to each tuple in the cartesian (cross) product of the given iterables.
static
<T> EmptyIterable<T>
empty()
          Create an EmptyIterable; equivalent to make().
static
<T> FilteredIterable<T>
filter(Iterable<? extends T> iter, Predicate<? super T> pred)
          Produce an iterable that only contains values from the given iterable that satisfy a predicate.
static
<T> FilteredIterable<T>
filterInstances(Iterable<? super T> iter, Class<? extends T> c)
          Cast all instances of the given type appropriately; filter out any non-instances.
static
<T> SnapshotIterable<T>
filterSnapshot(Iterable<? extends T> iter, Predicate<? super T> pred)
          Produce an iterable that only contains values from the given iterable that satisfy a predicate.
static
<T> FiniteSequenceIterable<T>
finiteSequence(T initial, Lambda<? super T,? extends T> successor, int size)
          Create a finite sequence of the given size defined by an initial value and a successor function.
static
<T> T
first(Iterable<? extends T> iter)
          Access the first value in the given iterable.
static
<T,R> R
fold(Iterable<? extends T> iter, R base, Lambda2<? super R,? super T,? extends R> combiner)
          Compute the left fold of the given list.
static boolean hasFixedSize(Iterable<?> iter)
          Return true iff the given iterable is known to have a fixed size.
static int hashCode(Iterable<?> iter)
          Return a hash code computed by xoring shifted copies of each element's hash code; the result is consistent with isEqual(java.lang.Iterable, java.lang.Iterable), but may not be consistent with the input's equals and hashCode methods; invokes RecurUtil#safeHashCode(Object) on each element.
static
<T> ImmutableIterable<T>
immutable(Iterable<? extends T> iter)
          Produce an ImmutableIterable with the given iterable.
static
<T> SequenceIterable<T>
infiniteSequence(T initial, Lambda<? super T,? extends T> successor)
          Create an infinite sequence defined by an initial value and a successor function.
static FiniteSequenceIterable<Integer> integerSequence(int start, int end)
          Create a simple sequence containing the numbers between start and end (inclusive).
static boolean isEmpty(Iterable<?> iter)
           
static boolean isEqual(Iterable<?> iter1, Iterable<?> iter2)
          Return true iff the lists are identical (according to ==), or they have the same size (according to sizeOf(java.lang.Iterable)) and each corresponding element is equal (according to LambdaUtil.EQUAL).
static boolean isInfinite(Iterable<?> iter)
          Return true iff the given iterable is known to have an infinite size.
static boolean isStatic(Iterable<?> iter)
          Return true iff the given iterable is known to be immutable.
static
<T> T
last(Iterable<? extends T> iter)
          Access the last value in the given iterable.
static
<T> SizedIterable<T>
make()
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2, T v3)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2, T v3, T v4)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2, T v3, T v4, T v5)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2, T v3, T v4, T v5, T v6)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2, T v3, T v4, T v5, T v6, T v7)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9)
          Create an immutable SizedIterable containing the given values.
static
<T> SizedIterable<T>
make(T v1, T v2, T v3, T v4, T v5, T v6, T v7, T v8, T v9, T v10)
          Create an immutable SizedIterable containing the given values.
static
<T> Octet<T,T,T,T,T,T,T,T>
makeOctet(Iterable<? extends T> iter)
          Convert an iterable of 8 elements to an Octet.
static
<T> Option<T>
makeOption(Iterable<? extends T> iter)
          Convert an iterable of 0 or 1 elements to an Option.
static
<T> Pair<T,T>
makePair(Iterable<? extends T> iter)
          Convert an iterable of 2 elements to a Pair.
static
<T> Quad<T,T,T,T>
makeQuad(Iterable<? extends T> iter)
          Convert an iterable of 4 elements to a Quad.
static
<T> Quint<T,T,T,T,T>
makeQuint(Iterable<? extends T> iter)
          Convert an iterable of 5 elements to a Quint.
static
<T> Septet<T,T,T,T,T,T,T>
makeSeptet(Iterable<? extends T> iter)
          Convert an iterable of 7 elements to a Septet.
static
<T> Sextet<T,T,T,T,T,T>
makeSextet(Iterable<? extends T> iter)
          Convert an iterable of 6 elements to a Sextet.
static
<T> Triple<T,T,T>
makeTriple(Iterable<? extends T> iter)
          Convert an iterable of 3 elements to a Triple.
static
<T> Wrapper<T>
makeWrapper(Iterable<? extends T> iter)
          Convert an iterable of 1 element to a Wrapper.
static
<T,R> SizedIterable<R>
map(Iterable<? extends T> source, Lambda<? super T,? extends R> map)
          Lazily apply a map function to each element in an iterable.
static
<T1,T2,T3,T4,R>
SizedIterable<R>
map(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4, Lambda4<? super T1,? super T2,? super T3,? super T4,? extends R> map)
          Lazily apply a map function to each corresponding quadruple of elements in the given iterables.
static
<T1,T2,T3,R>
SizedIterable<R>
map(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Lambda3<? super T1,? super T2,? super T3,? extends R> map)
          Lazily apply a map function to each corresponding triple of elements in the given iterables.
static
<T1,T2,R> SizedIterable<R>
map(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Lambda2<? super T1,? super T2,? extends R> map)
          Lazily apply a map function to each corresponding pair of elements in the given iterables.
static
<T,R> SnapshotIterable<R>
mapSnapshot(Iterable<? extends T> source, Lambda<? super T,? extends R> map)
          Immediately apply a map function to each element in an iterable.
static
<T1,T2,T3,T4,R>
SnapshotIterable<R>
mapSnapshot(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4, Lambda4<? super T1,? super T2,? super T3,? super T4,? extends R> map)
          Immediately apply a map function to each corresponding quadruple of elements in the given iterables.
static
<T1,T2,T3,R>
SnapshotIterable<R>
mapSnapshot(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Lambda3<? super T1,? super T2,? super T3,? extends R> map)
          Immediately apply a map function to each corresponding triple of elements in the given iterables.
static
<T1,T2,R> SnapshotIterable<R>
mapSnapshot(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Lambda2<? super T1,? super T2,? extends R> map)
          Immediately apply a map function to each corresponding pair of elements in the given iterables.
static String multilineToString(Iterable<?> iter)
          Generate a string representation of the given iterable where each element is listed on a separate line.
static
<T> boolean
or(Iterable<? extends T> iter, Predicate<? super T> pred)
          Check whether the given predicate holds for some value in iter.
static
<T1,T2,T3,T4>
boolean
or(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4, Predicate4<? super T1,? super T2,? super T3,? super T4> pred)
          Check whether the given predicate holds for some corresponding values in the given iterables.
static
<T1,T2,T3> boolean
or(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Predicate3<? super T1,? super T2,? super T3> pred)
          Check whether the given predicate holds for some corresponding values in the given iterables.
static
<T1,T2> boolean
or(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Predicate2<? super T1,? super T2> pred)
          Check whether the given predicate holds for some corresponding values in iter1 and iter2.
static
<T> Iterable<T>
pairFirsts(Iterable<? extends Pair<? extends T,?>> iter)
          Lazily create an iterable containing the first values of the given tuples.
static
<T> Iterable<T>
pairSeconds(Iterable<? extends Pair<?,? extends T>> iter)
          Lazily create an iterable containing the second values of the given tuples.
static
<T> Iterable<T>
quadFirsts(Iterable<? extends Quad<? extends T,?,?,?>> iter)
          Lazily create an iterable containing the first values of the given tuples.
static
<T> Iterable<T>
quadFourths(Iterable<? extends Quad<?,?,?,? extends T>> iter)
          Lazily create an iterable containing the fourth values of the given tuples.
static
<T> Iterable<T>
quadSeconds(Iterable<? extends Quad<?,? extends T,?,?>> iter)
          Lazily create an iterable containing the second values of the given tuples.
static
<T> Iterable<T>
quadThirds(Iterable<? extends Quad<?,?,? extends T,?>> iter)
          Lazily create an iterable containing the third values of the given tuples.
static
<T> SizedIterable<T>
relax(Iterable<? extends T> iter)
          Allow covariance in situations where wildcards can't be used by wrapping the iterable with a less- precise type parameter.
static
<T> Iterator<T>
relax(Iterator<? extends T> iter)
          Allow covariance in situations where wildcards can't be used by wraping the iterator with a less- precise type parameter.
static
<T> SizedIterable<T>
reverse(Iterable<? extends T> iter)
          Produce a reverse-order iterable over the given elements.
static
<T> void
run(Iterable<? extends T> iter, Runnable1<? super T> runnable)
          Apply the given runnable to every element in an iterable.
static
<T1,T2,T3,T4>
void
run(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Iterable<? extends T4> iter4, Runnable4<? super T1,? super T2,? super T3,? super T4> runnable)
          Apply the given runnable to every quadruple of corresponding elements in the given iterables.
static
<T1,T2,T3> void
run(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Iterable<? extends T3> iter3, Runnable3<? super T1,? super T2,? super T3> runnable)
          Apply the given runnable to every triple of corresponding elements in the given iterables.
static
<T1,T2> void
run(Iterable<? extends T1> iter1, Iterable<? extends T2> iter2, Runnable2<? super T1,? super T2> runnable)
          Apply the given runnable to every pair of corresponding elements in the given iterables.
static
<T> SizedIterable<T>
shuffle(Iterable<T> iter)
          Produce a shuffled iterable over the given elements.
static
<T> SizedIterable<T>
shuffle(Iterable<T> iter, Random random)
          Produce a shuffled iterable over the given elements, using the specified random number generator.
static
<T> SingletonIterable<T>
singleton(T value)
          Create a SingletonIterable; equivalent to make(Object).
static int sizeOf(Iterable<?> iter)
          Compute the size of the given iterable.
static int sizeOf(Iterable<?> iter, int bound)
          Compute the size of the given iterable, or bound -- whichever is less.
static
<T> SkipFirstIterable<T>
skipFirst(Iterable<T> iter)
          Produce an iterable that skips the first element of iter (if it exists)
static
<T> SkipLastIterable<T>
skipLast(Iterable<? extends T> iter)
          Produce an iterable that skips the last element of iter (if it exists).
static
<T> SnapshotIterable<T>
snapshot(Iterable<? extends T> iter)
          Create a SnapshotIterable with the given iterable.
static
<T> SnapshotIterable<T>
snapshot(Iterator<? extends T> iter)
          Create a SnapshotIterable with the given iterator.
static
<T extends Comparable<? super T>>
SizedIterable<T>
sort(Iterable<T> iter)
          Produce a sorted iterable over the given elements.
static
<T> SizedIterable<T>
sort(Iterable<T> iter, Comparator<? super T> comp)
          Produce a sorted iterable over the given elements, using the specified comparator.
static
<T> Pair<SizedIterable<T>,SizedIterable<T>>
split(Iterable<? extends T> iter, int index)