All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onebusaway.collections.combinations.Combinations Maven / Gradle / Ivy

/*
 * Copyright 2008 Brian Ferris
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.onebusaway.collections.combinations;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.onebusaway.collections.tuple.Pair;

public abstract class Combinations {

    public static  Iterable> getCombinationsReflexive(Iterable objects) {
        return getCombinations(objects, true);
    }

    public static  Iterable> getCombinationsNonReflexive(Iterable objects) {
        return getCombinations(objects, false);
    }

    public static  Iterable> getCombinations(Iterable objects, boolean includeReflexive) {
        List elements = new ArrayList();
        for (T element : objects)
            elements.add(element);
        return new CombinationsIterable(elements, includeReflexive);
    }

    public static  Iterable> getPermutations(final Iterable objects) {
        return new Iterable>() {
            public Iterator> iterator() {
                return new PermutationIterator(objects);
            }
        };
    }

    public static  Iterable> getPermutations(final Iterable objectsA, final Iterable objectsB) {
        return new Iterable>() {
            public Iterator> iterator() {
                return new PermutationIterator(objectsA, objectsB);
            }
        };
    }

    public static  Iterable> getSequentialPairs(final Iterable objects) {
        return new Iterable>() {
            public Iterator> iterator() {
                return new SequentialPairIterator(objects);
            }
        };
    }

    public static  List> getGroupCombinations(List elements, int groupSize) {
        if (groupSize > elements.size())
            throw new IllegalStateException("group size is larger than number of available elements");
        List> lists = new ArrayList>();
        List current = new ArrayList();
        getGroupCombinations(elements, groupSize, 0, lists, current);
        return lists;
    }

    /*******************************************************************************************************************
     * Private Methods
     ******************************************************************************************************************/

    private static  void getGroupCombinations(List elements, int groupSize, int index, List> lists,
            List current) {

        if (current.size() == groupSize) {
            lists.add(current);
            return;
        }

        int g = groupSize - current.size();

        for (int i = index; i < elements.size() - g + 1; i++) {
            List c = new ArrayList(current.size() + 1);
            c.addAll(current);
            c.add(elements.get(i));
            getGroupCombinations(elements, groupSize, i + 1, lists, c);
        }
    }

    private static class CombinationsIterable implements Iterable>, Serializable {

        private static final long serialVersionUID = 1L;

        private List _elements;

        private boolean _includeReflexive;

        public CombinationsIterable(List elements, boolean includeReflexive) {
            _elements = elements;
            _includeReflexive = includeReflexive;
        }

        public Iterator> iterator() {
            return new CombinationIterator(_elements, _includeReflexive);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy