/** * Returns an iterator whose range is the k-element subsets of {0, ..., n - 1} * represented as {@code int[]} arrays. * <p> * The arrays returned by the iterator are sorted in descending order and * they are visited in lexicographic order with significance from right to * left. For example, combinationsIterator(4, 2) returns an Iterator that * will generate the following sequence of arrays on successive calls to * {@code next()}:</p><p> * {@code [0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]} * </p><p> * If {@code k == 0} an Iterator containing an empty array is returned and * if {@code k == n} an Iterator containing [0, ..., n -1] is returned.</p> * * @param n Size of the set from which subsets are selected. * @param k Size of the subsets to be enumerated. * @return an {@link Iterator iterator} over the k-sets in n. * @throws NotPositiveException if {@code n < 0}. * @throws NumberIsTooLargeException if {@code k > n}. */ public static Iterator<int[]> combinationsIterator(int n, int k) { return new Combinations(n, k).iterator(); }
@UserFunction @Description("apoc.coll.combinations(coll, minSelect, maxSelect:minSelect) - Returns collection of all combinations of list elements of selection size between minSelect and maxSelect (default:minSelect), inclusive") public List<List<Object>> combinations(@Name("coll") List<Object> coll, @Name(value="minSelect") long minSelectIn, @Name(value="maxSelect",defaultValue = "-1") long maxSelectIn) { int minSelect = (int) minSelectIn; int maxSelect = (int) maxSelectIn; maxSelect = maxSelect == -1 ? minSelect : maxSelect; if (coll == null || coll.isEmpty() || minSelect < 1 || minSelect > coll.size() || minSelect > maxSelect || maxSelect > coll.size()) { return Collections.emptyList(); } List<List<Object>> combinations = new ArrayList<>(); for (int i = minSelect; i <= maxSelect; i++) { Iterator<int[]> itr = new Combinations(coll.size(), i).iterator(); while (itr.hasNext()) { List<Object> entry = new ArrayList<>(i); int[] indexes = itr.next(); if (indexes.length > 0) { for (int index : indexes) { entry.add(coll.get(index)); } combinations.add(entry); } } } return combinations; }
/** * Returns an iterator whose range is the k-element subsets of {0, ..., n - 1} * represented as {@code int[]} arrays. * <p> * The arrays returned by the iterator are sorted in descending order and * they are visited in lexicographic order with significance from right to * left. For example, combinationsIterator(4, 2) returns an Iterator that * will generate the following sequence of arrays on successive calls to * {@code next()}:</p><p> * {@code [0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]} * </p><p> * If {@code k == 0} an Iterator containing an empty array is returned and * if {@code k == n} an Iterator containing [0, ..., n -1] is returned.</p> * * @param n Size of the set from which subsets are selected. * @param k Size of the subsets to be enumerated. * @return an {@link Iterator iterator} over the k-sets in n. * @throws NotPositiveException if {@code n < 0}. * @throws NumberIsTooLargeException if {@code k > n}. */ public static Iterator<int[]> combinationsIterator(int n, int k) { return new Combinations(n, k).iterator(); }
/** * Returns an iterator whose range is the k-element subsets of {0, ..., n - 1} * represented as {@code int[]} arrays. * <p> * The arrays returned by the iterator are sorted in descending order and * they are visited in lexicographic order with significance from right to * left. For example, combinationsIterator(4, 2) returns an Iterator that * will generate the following sequence of arrays on successive calls to * {@code next()}:</p><p> * {@code [0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]} * </p><p> * If {@code k == 0} an Iterator containing an empty array is returned and * if {@code k == n} an Iterator containing [0, ..., n -1] is returned.</p> * * @param n Size of the set from which subsets are selected. * @param k Size of the subsets to be enumerated. * @return an {@link Iterator iterator} over the k-sets in n. * @throws NotPositiveException if {@code n < 0}. * @throws NumberIsTooLargeException if {@code k > n}. */ public static Iterator<int[]> combinationsIterator(int n, int k) { return new Combinations(n, k).iterator(); }