/* * (c) Copyright 2007, 2008, 2009 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package structure.ttree; import java.util.Iterator ; import java.util.NoSuchElementException ; import org.openjena.atlas.iterator.Iter ; import org.openjena.atlas.iterator.IteratorArray ; import org.openjena.atlas.lib.Alg ; public class TTreeIterator> implements Iterator { public static > Iterator iterator(TTree ttree, T min, T max) { if ( ttree.root == null ) return Iter.nullIterator() ; return new TTreeIterator(ttree, min, max) ; } boolean finished = false ; Iterator nodeIter ; TTreeNode node ; T slot ; T max ; TTreeIterator(TTree ttree, T min, T max) { this.max = max ; this.finished = false ; this.slot = null ; if ( min != null ) { node = TTree.findBoundingNode(ttree.root, min) ; int idx = node.find(min) ; // Does not short-cut min being larger than all elements in the tree. if ( idx < 0 ) { idx = Alg.decodeIndex(idx) ; // idx may actually be out of the array - that means this node is "finished" with if ( idx > node.nodeSize ) nodeIter = null ; else nodeIter = IteratorArray.create(node.elements, idx, node.nodeSize) ; } else nodeIter = IteratorArray.create(node.elements, idx, node.nodeSize) ; } else { //min == null node = ttree.root ; while(node.left != null) node = node.left ; nodeIter = IteratorArray.create(node.elements, 0, node.nodeSize) ; } } @Override public boolean hasNext() { if ( finished ) return false ; if ( slot != null ) return true ; if ( nodeIter != null && nodeIter.hasNext() ) { T item = nodeIter.next() ; return testAndSetSlot(item) ; } // End of current node elements. // Slot == null // Have yielded the value for this tree node (and hence all relevant left subtree) // Move the left-est node of the right subtree. // If no right subtree // Go up to parent. // Check we were the left subtree of parent, not right. // If no parent (this is the root), and we were left of parent, // go down to min of left. TTreeNode nextNode = node.right ; // There is a right if ( nextNode != null ) nextNode = TTreeNode.getLeftDeep(nextNode) ; else //if ( nextNode == null ) { // No right subtree from here. // Walk up tree until we were not the right node of our parent. TTreeNode n2 = node ; TTreeNode n3 = n2.parent ; while( n3 != null ) { if ( n3.right != n2 ) // Same as n3.left == n2 { n2 = n3 ; break ; } n2 = n3 ; n3 = n2.parent ; } if ( n3 == null ) { finished = true ; return false ; } // Now at the first node upwards when we're the left // (i.e. less than the value) nextNode = n2 ; } // On exit nextNode is the node of interest. // Yield it's elements node = nextNode ; nodeIter = IteratorArray.create(node.elements, 0, node.nodeSize) ; // And try again. // Rafactor. if ( ! nodeIter.hasNext() ) return false ; T item = nodeIter.next() ; return testAndSetSlot(item) ; } private boolean testAndSetSlot(T item) { if ( max != null ) { int x = max.compareTo(item) ; if ( x <= 0 ) { // End finished = true ; slot = null ; return false ; } } slot = item ; return true ; } @Override public T next() { if ( ! hasNext()) throw new NoSuchElementException("TTreeIterator") ; T rc = slot ; slot = null ; return rc ; } @Override public void remove() { throw new UnsupportedOperationException("TTreeIterator") ; } // // // // Iterator of zero // static > Iter nothing() // { // return Iter.nullIter() ; // } // // // Iterator of one // static > Iter singleton(R singleton) // { // return Iter.singletonIter(singleton) ; // } // // // Iterator of a pair of iterators concatenated // static > Iter concat(Iter iter1, Iter iter2) // { // return Iter.concat(iter1, iter2) ; // } // // // Calculate the iterator. For testing. // static > List calcIter(TTreeNode node, R min, R max) // { // List x = new ArrayList() ; // if ( node == null ) // return x ; // node.records(x) ; // Sorted. // if ( min != null ) // while ( x.size() > 0 && x.get(0).compareTo(min) < 0 ) // x.remove(0) ; // // if ( max != null ) // while ( x.size() > 0 && x.get(x.size()-1).compareTo(max) >= 0 ) // x.remove(x.size()-1) ; // return x ; // } // } /* * (c) Copyright 2007, 2008, 2009 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */