package structures; import java.util.ArrayList; import java.util.Iterator; /** A heap that tracks element location */ public final class HeapLoc> implements Iterable { //A good value for maxSize would be (2^N)-1 @SuppressWarnings("unchecked") public HeapLoc(int maxSize, boolean rollover_){ int len=maxSize+1; if((len&1)==1){len++;} //Array size is always even. CAPACITY=maxSize; array=(T[])new SetLoc[len]; rollover=rollover_; // queue=new PriorityQueue(maxSize); } public HeapLoc resizeNew(int newCapacity){ HeapLoc heap=new HeapLoc(newCapacity, rollover); //Technically I could just do a limited array copy, //since all the positions will remain the same. //TODO Add resizing the current one as an option. for(T t : this) { t.setLoc(-1); heap.add(t); } assert(size()==heap.size()); this.clear(); return heap; } public boolean add(T t){ return addAndReturnLocation(t)>=0; } public int addAndReturnLocation(T t){ assert(t.loc()<0); assert(size==0 || array[size]!=null); assert(rollover || size=CAPACITY){ if(t.compareTo(array[1])<=0){return -1;} poll(); //Turns into a rolling buffer by removing smallest value. // {//This is a more efficient alternative to poll() and percDown(), but the result is slightly different. // array[1]=t; // percUp(1); // return true; // } } assert(size0){ array[1].setLoc(1); percUp(1); } // assert(queue.size()==size); // assert(queue.peek()==peek()); //assert(testForDuplicates()); t.setLoc(-1); return t; } /** Returns the new location */ public int jiggle(T t){ final int loc=t.loc(); assert(array[loc]==t); int x=percDown(loc); if(x!=loc) {return x;} return percUp(loc); } /** Returns the new location */ public int jiggleDown(T t){ final int loc=t.loc(); assert(array[loc]==t); return percDown(loc); } /** Returns the new location */ public int jiggleUp(T t){ final int loc=t.loc(); assert(array[loc]==t); return percDown(loc); } /** Returns the new location */ private int percDown(int loc){ //assert(testForDuplicates()); assert(loc>0); if(loc==1){return loc;} int next=loc/2; T a=array[loc]; T b=array[next]; assert(a!=b && a!=null); if(a.compareTo(b)<0){ array[next]=a; array[loc]=b; a.setLoc(next); b.setLoc(loc); return percDown(next); } return loc; } /** Returns the new location */ private int percUp(int loc){ //assert(testForDuplicates()); assert(loc>0 && loc<=size) : loc+", "+size; int next1=loc*2; int next2=next1+1; if(next1>size){return loc;} T a=array[loc]; T b=array[next1]; T c=array[next2]; assert(a!=b); assert(b!=c); assert(b!=null && a!=null); //assert(testForDuplicates()); if(c==null || b.compareTo(c)<1){ if(a.compareTo(b)>0){ array[next1]=a; array[loc]=b; a.setLoc(next1); b.setLoc(loc); //assert(testForDuplicates()); return percUp(next1); } }else{ if(a.compareTo(c)>0){ array[next2]=a; array[loc]=c; a.setLoc(next2); c.setLoc(loc); //assert(testForDuplicates()); return percUp(next2); } } return loc; } public boolean isEmpty(){ // assert((size==0) == queue.isEmpty()); return size==0; } public boolean hasRoom(){ return size toList(){ ArrayList list=new ArrayList(size); for(int i=0, lim=size; i iterator() { return new HeapIterator(); } private class HeapIterator implements Iterator { @Override public boolean hasNext() {return loc<=size;} @Override public T next() {return array[loc++];} int loc=1; } private final T[] array; public final int CAPACITY; public final boolean rollover; private int size=0; // private PriorityQueue queue; }