View Javadoc

1   package cz.cuni.amis.utils.heap;
2   
3   import java.util.Iterator;
4   
5   /**
6    * {@link Iterator} used by {@link Heap} inside {@link Heap#iterator()}.
7    */
8   public class HeapIterator<NODE> implements Iterator<NODE> {
9   	
10  	private NODE[] nodes;
11  	private int items;
12  	private int current;
13  	private Heap heap;
14  	
15  	public HeapIterator(NODE[] myNodes, int myItems, Heap<NODE> myHeap){
16  		nodes = myNodes;
17  		items = myItems;
18  		current = 0;
19  		heap = myHeap;
20  	}
21  
22  	@Override
23  	public boolean hasNext() {
24  		return (current < items);		
25  	}
26  
27  	@Override
28  	public NODE next() {
29  		if (current < items){
30  			return nodes[current++];
31  		} else {
32  			return null;
33  		}		
34  	}
35  
36  	@Override
37  	public void remove() {
38  		if (current == 0) 
39  			return;
40  		heap.remove(nodes[current-1]);
41  		current = current - 1;
42  		items = items - 1;
43  	}
44  
45  }