private static boolean anm()

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


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

        if (m == 0) {
            // All n items are selected
            List<T> tmpResult = new ArrayList<>();
            for (int i : selected) {
                tmpResult.add(all.get(i));
            }
            return callback.apply(tmpResult);
        }

        for (int i = 0; i < all.size(); i++) {
            if (selected.contains(i)) {
                continue;
            }
            int index = selected.size();
            selected.add(i);

            // Select current item, continue to select A(m-1, n-1)
            if (anm(all, n - 1, m - 1, selected, callback)) {
                return true;
            }

            selected.remove(index);
            assert selected.size() == index : selected;
        }
        return false;
    }