/** * Returns a {@link Collection} of all the permutations of the specified {@link Iterable}. * * <p><i>Notes:</i> This is an implementation of the algorithm for Lexicographical Permutations * Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, * Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the * first permutation will be in ascending order, and the last will be in descending order. * * <p>Duplicate elements are considered equal. For example, the list [1, 1] will have only one * permutation, instead of two. This is why the elements have to implement {@link Comparable}. * * <p>An empty iterable has only one permutation, which is an empty list. * * <p>This method is equivalent to {@code Collections2.orderedPermutations(list, * Ordering.natural())}. * * @param elements the original iterable whose elements have to be permuted. * @return an immutable {@link Collection} containing all the different permutations of the * original iterable. * @throws NullPointerException if the specified iterable is null or has any null elements. * @since 12.0 */ @Beta public static <E extends Comparable<? super E>> Collection<List<E>> orderedPermutations( Iterable<E> elements) { return orderedPermutations(elements, Ordering.natural()); }
static class TransformedCollection<F, T> extends AbstractCollection<T> { final Collection<F> fromCollection; final Function<? super F, ? extends T> function; TransformedCollection(Collection<F> fromCollection, Function<? super F, ? extends T> function) { this.fromCollection = checkNotNull(fromCollection); this.function = checkNotNull(function); } @Override public void clear() { fromCollection.clear(); } @Override public boolean isEmpty() { return fromCollection.isEmpty(); } @Override public Iterator<T> iterator() { return Iterators.transform(fromCollection.iterator(), function); } @Override public int size() { return fromCollection.size(); } }
/** * Returns a {@link Collection} of all the permutations of the specified {@link Iterable}. * * <p><i>Notes:</i> This is an implementation of the algorithm for Lexicographical Permutations * Generation, described in Knuth's "The Art of Computer Programming", Volume 4, Chapter 7, * Section 7.2.1.2. The iteration order follows the lexicographical order. This means that the * first permutation will be in ascending order, and the last will be in descending order. * * <p>Duplicate elements are considered equal. For example, the list [1, 1] will have only one * permutation, instead of two. This is why the elements have to implement {@link Comparable}. * * <p>An empty iterable has only one permutation, which is an empty list. * * <p>This method is equivalent to {@code Collections2.orderedPermutations(list, * Ordering.natural())}. * * @param elements the original iterable whose elements have to be permuted. * @return an immutable {@link Collection} containing all the different permutations of the * original iterable. * @throws NullPointerException if the specified iterable is null or has any null elements. * @since 12.0 */ @Beta public static <E extends Comparable<? super E>> Collection<List<E>> orderedPermutations( Iterable<E> elements) { return orderedPermutations(elements, Ordering.natural()); }
public void testOrderedPermutationSetRepeatedElementsSize() { List<Integer> list = newArrayList(1, 1, 1, 1, 2, 2, 3); Collection<List<Integer>> permutations = Collections2.orderedPermutations(list, Ordering.natural()); assertPermutationsCount(105, permutations); }
public void testOrderedPermutationSetOneElement() { List<Integer> list = newArrayList(1); Iterator<List<Integer>> permutations = Collections2.orderedPermutations(list).iterator(); assertNextPermutation(newArrayList(1), permutations); assertNoMorePermutations(permutations); }
public void testOrderedPermutationSetSizeOverflow() { // 12 elements won't overflow assertEquals( 479001600 /*12!*/, Collections2.orderedPermutations(newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)) .size()); // 13 elements overflow an int assertEquals( Integer.MAX_VALUE, Collections2.orderedPermutations(newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)) .size()); // 21 elements overflow a long assertEquals( Integer.MAX_VALUE, Collections2.orderedPermutations( newArrayList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)) .size()); // Almost force an overflow in the binomial coefficient calculation assertEquals( 1391975640 /*C(34,14)*/, Collections2.orderedPermutations(concat(nCopies(20, 1), nCopies(14, 2))).size()); // Do force an overflow in the binomial coefficient calculation assertEquals( Integer.MAX_VALUE, Collections2.orderedPermutations(concat(nCopies(21, 1), nCopies(14, 2))).size()); }
public void testOrderedPermutationSetContains() { List<Integer> list = newArrayList(3, 2, 1); Collection<List<Integer>> permutationSet = Collections2.orderedPermutations(list); assertTrue(permutationSet.contains(newArrayList(1, 2, 3))); assertTrue(permutationSet.contains(newArrayList(2, 3, 1))); assertFalse(permutationSet.contains(newArrayList(1, 2))); assertFalse(permutationSet.contains(newArrayList(1, 1, 2, 3))); assertFalse(permutationSet.contains(newArrayList(1, 2, 3, 4))); assertFalse(permutationSet.contains(null)); }
public void testOrderedPermutationSetEmpty() { List<Integer> list = newArrayList(); Collection<List<Integer>> permutationSet = Collections2.orderedPermutations(list); assertEquals(1, permutationSet.size()); assertThat(permutationSet).contains(list); Iterator<List<Integer>> permutations = permutationSet.iterator(); assertNextPermutation(Lists.<Integer>newArrayList(), permutations); assertNoMorePermutations(permutations); }
public void testPermutationSetSizeOverflow() { // 13 elements overflow an int assertEquals( Integer.MAX_VALUE, Collections2.permutations(newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)).size()); // 21 elements overflow a long assertEquals( Integer.MAX_VALUE, Collections2.orderedPermutations( newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)) .size()); assertEquals( Integer.MAX_VALUE, Collections2.orderedPermutations( newArrayList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)) .size()); }
public void testOrderedPermutationSetRepeatedElements() { List<Integer> list = newArrayList(1, 1, 2, 2); Iterator<List<Integer>> permutations = Collections2.orderedPermutations(list, Ordering.natural()).iterator(); assertNextPermutation(newArrayList(1, 1, 2, 2), permutations); assertNextPermutation(newArrayList(1, 2, 1, 2), permutations); assertNextPermutation(newArrayList(1, 2, 2, 1), permutations); assertNextPermutation(newArrayList(2, 1, 1, 2), permutations); assertNextPermutation(newArrayList(2, 1, 2, 1), permutations); assertNextPermutation(newArrayList(2, 2, 1, 1), permutations); assertNoMorePermutations(permutations); }
public void testOrderedPermutationSetThreeElements() { List<String> list = newArrayList("b", "a", "c"); Iterator<List<String>> permutations = Collections2.orderedPermutations(list).iterator(); assertNextPermutation(newArrayList("a", "b", "c"), permutations); assertNextPermutation(newArrayList("a", "c", "b"), permutations); assertNextPermutation(newArrayList("b", "a", "c"), permutations); assertNextPermutation(newArrayList("b", "c", "a"), permutations); assertNextPermutation(newArrayList("c", "a", "b"), permutations); assertNextPermutation(newArrayList("c", "b", "a"), permutations); assertNoMorePermutations(permutations); }
public void testOrderedPermutationSetRepeatedElementsSize() { List<Integer> list = newArrayList(1, 1, 1, 1, 2, 2, 3); Collection<List<Integer>> permutations = Collections2.orderedPermutations(list, Ordering.natural()); assertPermutationsCount(105, permutations); }
public void testOrderedPermutationSetSizeOverflow() { // 12 elements won't overflow assertEquals(479001600 /*12!*/, Collections2.orderedPermutations( newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)).size()); // 13 elements overflow an int assertEquals(Integer.MAX_VALUE, Collections2.orderedPermutations( newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)).size()); // 21 elements overflow a long assertEquals(Integer.MAX_VALUE, Collections2.orderedPermutations( newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)).size()); // Almost force an overflow in the binomial coefficient calculation assertEquals(1391975640 /*C(34,14)*/, Collections2.orderedPermutations( concat(nCopies(20, 1), nCopies(14, 2))).size()); // Do force an overflow in the binomial coefficient calculation assertEquals(Integer.MAX_VALUE, Collections2.orderedPermutations( concat(nCopies(21, 1), nCopies(14, 2))).size()); }
public void testOrderedPermutationSetOneElement() { List<Integer> list = newArrayList(1); Iterator<List<Integer>> permutations = Collections2.orderedPermutations(list).iterator(); assertNextPermutation(newArrayList(1), permutations); assertNoMorePermutations(permutations); }
public void testPermutationSetSizeOverflow() { // 13 elements overflow an int assertEquals(Integer.MAX_VALUE, Collections2.permutations(newArrayList( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)).size()); // 21 elements overflow a long assertEquals(Integer.MAX_VALUE, Collections2.orderedPermutations( newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)).size()); assertEquals(Integer.MAX_VALUE, Collections2.orderedPermutations( newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21)).size()); }
public void testOrderedPermutationSetContains() { List<Integer> list = newArrayList(3, 2, 1); Collection<List<Integer>> permutationSet = Collections2.orderedPermutations(list); assertTrue(permutationSet.contains(newArrayList(1, 2, 3))); assertTrue(permutationSet.contains(newArrayList(2, 3, 1))); assertFalse(permutationSet.contains(newArrayList(1, 2))); assertFalse(permutationSet.contains(newArrayList(1, 1, 2, 3))); assertFalse(permutationSet.contains(newArrayList(1, 2, 3, 4))); assertFalse(permutationSet.contains(null)); }
public void testOrderedPermutationSetEmpty() { List<Integer> list = newArrayList(); Collection<List<Integer>> permutationSet = Collections2.orderedPermutations(list); assertEquals(1, permutationSet.size()); assertThat(permutationSet).contains(list); Iterator<List<Integer>> permutations = permutationSet.iterator(); assertNextPermutation(Lists.<Integer>newArrayList(), permutations); assertNoMorePermutations(permutations); }
private static List<String> createTestPaths(int depth, boolean permutation){ List<String> rootPaths = Arrays.asList("a", "b", "c", "d", "e", "f", "g"); List<String> paths = new ArrayList<String>(); if (permutation){ List<String> newRoots = new ArrayList<String>(); for (List<String> permuts : Collections2.orderedPermutations(rootPaths)){ newRoots.add(Joiner.on("").join(permuts)); } rootPaths = newRoots; } for (String root : rootPaths){ List<String> pathElements = new ArrayList<String>(); pathElements.add(root); paths.add(createId(pathElements)); for (int i = 0; i < depth; i++){ pathElements.add(root + i); paths.add(createId(pathElements)); } } Set<String> idSet = new HashSet<String>(paths); assertEquals(paths.size(), idSet.size()); Collections.shuffle(paths); return paths; }
public void testOrderedPermutationSetRepeatedElements() { List<Integer> list = newArrayList(1, 1, 2, 2); Iterator<List<Integer>> permutations = Collections2.orderedPermutations(list, Ordering.natural()).iterator(); assertNextPermutation(newArrayList(1, 1, 2, 2), permutations); assertNextPermutation(newArrayList(1, 2, 1, 2), permutations); assertNextPermutation(newArrayList(1, 2, 2, 1), permutations); assertNextPermutation(newArrayList(2, 1, 1, 2), permutations); assertNextPermutation(newArrayList(2, 1, 2, 1), permutations); assertNextPermutation(newArrayList(2, 2, 1, 1), permutations); assertNoMorePermutations(permutations); }
public void testOrderedPermutationSetThreeElements() { List<String> list = newArrayList("b", "a", "c"); Iterator<List<String>> permutations = Collections2.orderedPermutations(list).iterator(); assertNextPermutation(newArrayList("a", "b", "c"), permutations); assertNextPermutation(newArrayList("a", "c", "b"), permutations); assertNextPermutation(newArrayList("b", "a", "c"), permutations); assertNextPermutation(newArrayList("b", "c", "a"), permutations); assertNextPermutation(newArrayList("c", "a", "b"), permutations); assertNextPermutation(newArrayList("c", "b", "a"), permutations); assertNoMorePermutations(permutations); }