/** * Projects each element of a sequence into a new form. */ public static <TSource, TResult> Enumerable<TResult> select( final Enumerable<TSource> source, final Function1<TSource, TResult> selector) { if (selector == Functions.identitySelector()) { //noinspection unchecked return (Enumerable<TResult>) source; } return new AbstractEnumerable<TResult>() { public Enumerator<TResult> enumerator() { return new Enumerator<TResult>() { final Enumerator<TSource> enumerator = source.enumerator(); public TResult current() { return selector.apply(enumerator.current()); } public boolean moveNext() { return enumerator.moveNext(); } public void reset() { enumerator.reset(); } public void close() { enumerator.close(); } }; } }; }
/** * Creates a Map<TKey, TValue> from an * Enumerable<TSource> according to a specified key selector * function. * * <p>NOTE: Called {@code toDictionary} in LINQ.NET.</p> */ public static <TSource, TKey> Map<TKey, TSource> toMap( Enumerable<TSource> source, Function1<TSource, TKey> keySelector) { return toMap(source, keySelector, Functions.identitySelector()); }
/** * Creates a Lookup<TKey, TElement> from an * Enumerable<TSource> according to a specified key selector * function. */ public static <TSource, TKey> Lookup<TKey, TSource> toLookup( Enumerable<TSource> source, Function1<TSource, TKey> keySelector) { return toLookup(source, keySelector, Functions.identitySelector()); }
/** * Creates a {@code Lookup<TKey, TElement>} from an * {@code Enumerable<TSource>} according to a specified key selector function * and key comparer. */ public static <TSource, TKey> Lookup<TKey, TSource> toLookup( Enumerable<TSource> source, Function1<TSource, TKey> keySelector, EqualityComparer<TKey> comparer) { return toLookup( source, keySelector, Functions.identitySelector(), comparer); }
/** * Creates a {@code Map<TKey, TValue>} from an * {@code Enumerable<TSource>} according to a specified key selector function * and key comparer. */ public static <TSource, TKey> Map<TKey, TSource> toMap( Enumerable<TSource> source, Function1<TSource, TKey> keySelector, EqualityComparer<TKey> comparer) { return toMap(source, keySelector, Functions.identitySelector(), comparer); }
/** * Performs binary search of the upper bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T> int upperBound(T[] a, T key, Comparator<T> comparator) { return upperBound(a, key, 0, a.length - 1, Functions.identitySelector(), comparator); }
/** * Performs binary search of the lower bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T> int lowerBound(T[] a, T key, Comparator<T> comparator) { return lowerBound(a, key, 0, a.length - 1, Functions.identitySelector(), comparator); }
/** * Performs binary search of the lower bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T> int lowerBound(T[] a, T key, Comparator<T> comparator) { return lowerBound(a, key, 0, a.length - 1, Functions.identitySelector(), comparator); }
/** * Performs binary search of the upper bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T> int upperBound(T[] a, T key, Comparator<T> comparator) { return upperBound(a, key, 0, a.length - 1, Functions.identitySelector(), comparator); }
/** * Performs binary search of the lower bound in the given section of array. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param imin the minimal index (inclusive) to look for * @param imax the maximum index (inclusive) to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T> int lowerBound(T[] a, T key, int imin, int imax, Comparator<T> comparator) { return lowerBound(a, key, imin, imax, Functions.identitySelector(), comparator); }
/** * Performs binary search of the upper bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param imin the minimal index (inclusive) to look for * @param imax the maximum index (inclusive) to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T> int upperBound(T[] a, T key, int imin, int imax, Comparator<T> comparator) { return upperBound(a, key, imin, imax, Functions.identitySelector(), comparator); }
/** * Performs binary search of the lower bound in the given section of array. * It is assumed that the array is sorted. * The method is guaranteed to return the minimal index of the element that is * greater or equal to the given key. * @param a array that holds the values * @param key element to look for * @param imin the minimal index (inclusive) to look for * @param imax the maximum index (inclusive) to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return minimal index of the element that is * greater or equal to the given key. Returns -1 when all elements exceed * the given key or the array is empty. Returns {@code a.length} when all * elements are less than the given key. */ public static <T> int lowerBound(T[] a, T key, int imin, int imax, Comparator<T> comparator) { return lowerBound(a, key, imin, imax, Functions.identitySelector(), comparator); }
/** * Performs binary search of the upper bound in the given array. * It is assumed that the array is sorted. * The method is guaranteed to return the maximal index of the element that is * less or equal to the given key. * @param a array that holds the values * @param key element to look for * @param imin the minimal index (inclusive) to look for * @param imax the maximum index (inclusive) to look for * @param comparator comparator that compares keys * @param <T> the type of elements in array * @return maximal index of the element that is * less or equal to the given key. Returns -1 when all elements are less * than the given key or the array is empty. Returns {@code a.length} when * all elements exceed the given key. */ public static <T> int upperBound(T[] a, T key, int imin, int imax, Comparator<T> comparator) { return upperBound(a, key, imin, imax, Functions.identitySelector(), comparator); }
/** * Sorts the elements of a sequence in ascending * order by using a specified comparer. */ public static <TSource, TKey> Enumerable<TSource> orderBy( Enumerable<TSource> source, Function1<TSource, TKey> keySelector, Comparator<TKey> comparator) { // NOTE: TreeMap allows null comparator. But the caller of this method // must supply a comparator if the key does not extend Comparable. // Otherwise there will be a ClassCastException while retrieving. final Map<TKey, List<TSource>> map = new TreeMap<>(comparator); LookupImpl<TKey, TSource> lookup = toLookup_(map, source, keySelector, Functions.identitySelector()); return lookup.valuesEnumerable(); }
private static <T extends Comparable<T>> Enumerable<T> intersect( List<T> list0, List<T> list1) { return EnumerableDefaults.mergeJoin( Linq4j.asEnumerable(list0), Linq4j.asEnumerable(list1), Functions.identitySelector(), Functions.identitySelector(), (v0, v1) -> v0, false, false); }
private static <T extends Comparable<T>> Enumerable<T> intersect( List<T> list0, List<T> list1) { return EnumerableDefaults.mergeJoin( Linq4j.asEnumerable(list0), Linq4j.asEnumerable(list1), Functions.identitySelector(), Functions.identitySelector(), (v0, v1) -> v0, false, false); }
@Test public void testThetaFullJoinLeftEmpty() { assertThat( EnumerableDefaults.thetaJoin(EMPS.take(0), DEPTS, EQUAL_DEPTNO, EMP_DEPT_TO_STRING, true, true) .orderBy(Functions.identitySelector()).toList().toString(), equalTo("[{null, null, 15, Marketing}, {null, null, 20, Sales}]")); }
@Test public void testThetaFullJoinLeftEmpty() { assertThat( EnumerableDefaults.thetaJoin(EMPS.take(0), DEPTS, EQUAL_DEPTNO, EMP_DEPT_TO_STRING, true, true) .orderBy(Functions.identitySelector()).toList().toString(), equalTo("[{null, null, 15, Marketing}, {null, null, 20, Sales}]")); }