private static boolean cnm()

in hugegraph-common/src/main/java/org/apache/hugegraph/util/CollectionUtil.java [322:362]


    private static <T> boolean cnm(List<T> all, int n, int m,
                                   int current, List<T> selected,
                                   Function<List<T>, Boolean> callback) {
        assert n <= all.size();
        assert m <= n;
        assert current <= all.size();
        if (selected == null) {
            selected = new ArrayList<>(m);
        }

        if (m == 0) {
            assert selected.size() > 0 : selected;
            // All n items are selected
            List<T> tmpResult = Collections.unmodifiableList(selected);
            return callback.apply(tmpResult);
        }
        if (n == m) {
            // No choice, select all n items, we don't update the `result` here
            List<T> tmpResult = new ArrayList<>(selected);
            tmpResult.addAll(all.subList(current, all.size()));
            return callback.apply(tmpResult);
        }

        if (current >= all.size()) {
            // Reach the end of items
            return false;
        }

        // Select current item, continue to select C(m-1, n-1)
        int index = selected.size();
        selected.add(all.get(current));
        ++current;
        if (cnm(all, n - 1, m - 1, current, selected, callback)) {
            // NOTE: we can pop the tailing items if want to keep result clear
            return true;
        }
        // Not select current item, pop it and continue to select C(m-1, n)
        selected.remove(index);
        assert selected.size() == index : selected;
        return cnm(all, n - 1, m, current, selected, callback);
    }