Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2011 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);
}
/**
* Given elements {a,b,c} will return {{a,b},{a,c},{b,c}}.
*
* @param
* @param objects
* @return
*/
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);
}
}
}