Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 114   Methods: 6
NCLOC: 51   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DiagonalCartesianIterator.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    import java.util.LinkedList;
 39    import edu.rice.cs.plt.lambda.Lambda2;
 40    import edu.rice.cs.plt.object.Composite;
 41    import edu.rice.cs.plt.object.ObjectUtil;
 42   
 43    /**
 44    * <p>Enumerates the elements of a cartesian (or cross) product in diagonal order. Where the
 45    * "index" of the ith item in an iterator is i, this class produces all pairs of values with
 46    * indices that sum to n before proceding to those with indices that sum to n+1. This allows
 47    * the cartesian product of two infinite iterators to be methodically traversed. Within the
 48    * set of pairs with indices summing to n, the order is lexographical in terms of the respective
 49    * indices. For example, {@code [0, 1, 2]} crossed with itself will produce (under string concatenation)
 50    * {@code [00, 01, 10, 02, 11, 20, 12, 21, 22]}. The {@code combiner} function is used, rather than simply
 51    * producing {@code Pair}s, in order to provide a greater degree of flexibility. {@link Iterator#remove}
 52    * is not supported.</p>
 53    * <p>In order to support this traversal, the set of previously-seen values must be cached. The amount
 54    * of space required by this iterator after n invocations of {@code next()} is in O(sqrt(n)).</p>
 55    */
 56    public class DiagonalCartesianIterator<T1, T2, R> extends ReadOnlyIterator<R> implements Composite {
 57   
 58    private final Lambda2<? super T1, ? super T2, ? extends R> _combiner;
 59    private final Iterator<? extends T1> _left;
 60    private final Iterator<? extends T2> _right;
 61    private LinkedList<T1> _leftCache;
 62    private Iterator<T1> _leftCacheIter;
 63    private LinkedList<T2> _rightCache;
 64    private Iterator<T2> _rightCacheIter;
 65   
 66  0 public DiagonalCartesianIterator(Iterator<? extends T1> left, Iterator<? extends T2> right,
 67    Lambda2<? super T1, ? super T2, ? extends R> combiner) {
 68  0 _combiner = combiner;
 69  0 _left = left;
 70  0 _right = right;
 71  0 _leftCache = new LinkedList<T1>();
 72  0 _rightCache = new LinkedList<T2>();
 73  0 _leftCacheIter = _leftCache.iterator();
 74  0 _rightCacheIter = _rightCache.iterator();
 75    }
 76   
 77  0 public int compositeHeight() { return ObjectUtil.compositeHeight(_left, _right) + 1; }
 78  0 public int compositeSize() { return ObjectUtil.compositeSize(_left, _right) + 1; }
 79   
 80  0 public boolean hasNext() {
 81  0 if (_left.hasNext()) { return _right.hasNext() || !_rightCache.isEmpty(); }
 82  0 else if (_right.hasNext()) { return !_leftCache.isEmpty(); }
 83    else {
 84  0 return _leftCacheIter.hasNext() && _rightCacheIter.hasNext() || // there's more in the current traversal
 85    _leftCache.size() > 1 && _rightCache.size() > 1; // there are leftovers to create a new traversal
 86    }
 87    // return (_left.hasNext() || !_leftCache.isEmpty()) && (_right.hasNext() || !_rightCache.isEmpty()) ||
 88    // ;
 89    // return _leftCacheIter.hasNext() && _rightCacheIter.hasNext() ||
 90    // _left.hasNext() && (_right.hasNext() || !_rightCache.isEmpty()) ||
 91    // _right.hasNext() && !_leftCache.isEmpty();
 92    }
 93   
 94  0 public R next() {
 95  0 if (!_leftCacheIter.hasNext() || !_rightCacheIter.hasNext()) {
 96  0 if (_left.hasNext()) { _leftCache.addLast(_left.next()); }
 97  0 else if (!_rightCache.isEmpty()) { _rightCache.removeLast(); }
 98  0 if (_right.hasNext()) { _rightCache.addFirst(_right.next()); }
 99  0 else if (!_leftCache.isEmpty()) { _leftCache.removeFirst(); }
 100  0 _leftCacheIter = _leftCache.iterator();
 101  0 _rightCacheIter = _rightCache.iterator();
 102    }
 103    // This may cause an exception, which is OK.
 104  0 return _combiner.value(_leftCacheIter.next(), _rightCacheIter.next());
 105    }
 106   
 107    /** Call the constructor (allows the type arguments to be inferred) */
 108  0 public static <T1, T2, R>
 109    DiagonalCartesianIterator<T1, T2, R> make(Iterator<? extends T1> left, Iterator<? extends T2> right,
 110    Lambda2<? super T1, ? super T2, ? extends R> combiner) {
 111  0 return new DiagonalCartesianIterator<T1, T2, R>(left, right, combiner);
 112    }
 113   
 114    }