Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 103   Methods: 5
NCLOC: 42   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PermutationIterator.java 0% 0% 0% 0%
coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.iter;
 36   
 37    import java.util.Iterator;
 38   
 39    /**
 40    * Enumerates all permutations of the given list. Behavior is undefined if
 41    * the given list changes during iteration. The size of the enumeration, where
 42    * the original list has size n, is n! (and is thus guaranteed to be >= 1). The order of the
 43    * results is lexicographical, assuming each element of the original list is taken to
 44    * lexicographically precede all of its successors. (Thus, the original list is
 45    * the first to be returned, and a reversed list is the last.) Of course, due to the
 46    * factorial complexity of enumerating all permutations, this class is probably not suitable
 47    * for applications in which n is unbounded (or just intractably large).
 48    *
 49    * @param <T> The element type of the permuted lists; note that {@code next()} returns
 50    * {@code Iterable<T>}s, not {@code T}s.
 51    */
 52    public class PermutationIterator<T> extends ReadOnlyIterator<Iterable<T>> {
 53   
 54    private final Iterable<? extends T> _original;
 55    private final Iterator<? extends T> _elements;
 56    private T _element;
 57    private int _elementIndex;
 58    private Iterator<Iterable<T>> _restPermutations;
 59   
 60  0 public PermutationIterator(Iterable<? extends T> original) {
 61  0 _original = original;
 62  0 _elements = _original.iterator();
 63    // _element is initialized later
 64  0 _elementIndex = -1;
 65   
 66    // to deal with the empty case, we set _restPermutations appropriately
 67  0 if (IterUtil.isEmpty(_original)) {
 68  0 _restPermutations = SingletonIterator.<Iterable<T>>make(EmptyIterable.<T>make());
 69    }
 70  0 else { _restPermutations = EmptyIterator.make(); }
 71    }
 72   
 73  0 public boolean hasNext() { return _restPermutations.hasNext() || _elements.hasNext(); }
 74   
 75  0 public Iterable<T> next() {
 76    // in the empty case, _restPermutations contians a single empty list
 77  0 if (IterUtil.isEmpty(_original)) { return _restPermutations.next(); }
 78    else {
 79  0 if (!_restPermutations.hasNext()) {
 80  0 _element = _elements.next(); // exception occurs if !elements.hasNext()
 81  0 _elementIndex++;
 82  0 _restPermutations = new PermutationIterator<T>(makeRest(_elementIndex));
 83    // restPermutations must now haveNext()
 84    }
 85  0 return new ComposedIterable<T>(_element, _restPermutations.next());
 86    }
 87    }
 88   
 89  0 private Iterable<T> makeRest(int skipIndex) {
 90  0 Iterable<T> result = EmptyIterable.make();
 91  0 int i = 0;
 92  0 for (T e : _original) {
 93  0 if (i != skipIndex) { result = new ComposedIterable<T>(result, e); }
 94  0 i++;
 95    }
 96  0 return result;
 97    }
 98   
 99    /** Call the constructor (allows {@code T} to be inferred) */
 100  0 public static <T> PermutationIterator<T> make(Iterable<? extends T> original) {
 101  0 return new PermutationIterator<T>(original);
 102    }
 103    }