| 1 |
package structures; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Iterator; |
| 5 |
|
| 6 |
|
| 7 |
public final class HeapLoc<T extends SetLoc<? super T>> implements Iterable<T> { |
| 8 |
|
| 9 |
|
| 10 |
@SuppressWarnings("unchecked") |
| 11 |
public HeapLoc(int maxSize, boolean rollover_){ |
| 12 |
|
| 13 |
int len=maxSize+1; |
| 14 |
if((len&1)==1){len++;} |
| 15 |
|
| 16 |
CAPACITY=maxSize; |
| 17 |
array=(T[])new SetLoc[len]; |
| 18 |
rollover=rollover_; |
| 19 |
|
| 20 |
} |
| 21 |
|
| 22 |
public HeapLoc<T> resizeNew(int newCapacity){ |
| 23 |
HeapLoc<T> heap=new HeapLoc<T>(newCapacity, rollover); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
for(T t : this) { |
| 28 |
t.setLoc(-1); |
| 29 |
heap.add(t); |
| 30 |
} |
| 31 |
assert(size()==heap.size()); |
| 32 |
this.clear(); |
| 33 |
return heap; |
| 34 |
} |
| 35 |
|
| 36 |
public boolean add(T t){ |
| 37 |
return addAndReturnLocation(t)>=0; |
| 38 |
} |
| 39 |
|
| 40 |
public int addAndReturnLocation(T t){ |
| 41 |
assert(t.loc()<0); |
| 42 |
assert(size==0 || array[size]!=null); |
| 43 |
assert(rollover || size<CAPACITY); |
| 44 |
|
| 45 |
if(size>=CAPACITY){ |
| 46 |
|
| 47 |
if(t.compareTo(array[1])<=0){return -1;} |
| 48 |
|
| 49 |
poll(); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
} |
| 57 |
assert(size<CAPACITY); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
assert(size==0 || array[size]!=null); |
| 63 |
size++; |
| 64 |
array[size]=t; |
| 65 |
t.setLoc(size); |
| 66 |
return percDown(size); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
public T peek(){ |
| 73 |
|
| 74 |
|
| 75 |
if(size==0){return null;} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
return array[1]; |
| 83 |
} |
| 84 |
|
| 85 |
public T poll(){ |
| 86 |
|
| 87 |
|
| 88 |
if(size==0){return null;} |
| 89 |
T t=array[1]; |
| 90 |
|
| 91 |
array[1]=array[size]; |
| 92 |
array[size]=null; |
| 93 |
size--; |
| 94 |
if(size>0){ |
| 95 |
array[1].setLoc(1); |
| 96 |
percUp(1); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
t.setLoc(-1); |
| 102 |
return t; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
public int jiggle(T t){ |
| 107 |
final int loc=t.loc(); |
| 108 |
assert(array[loc]==t); |
| 109 |
int x=percDown(loc); |
| 110 |
if(x!=loc) {return x;} |
| 111 |
return percUp(loc); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
public int jiggleDown(T t){ |
| 116 |
final int loc=t.loc(); |
| 117 |
assert(array[loc]==t); |
| 118 |
return percDown(loc); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
public int jiggleUp(T t){ |
| 123 |
final int loc=t.loc(); |
| 124 |
assert(array[loc]==t); |
| 125 |
return percDown(loc); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
private int percDown(int loc){ |
| 130 |
|
| 131 |
assert(loc>0); |
| 132 |
if(loc==1){return loc;} |
| 133 |
int next=loc/2; |
| 134 |
T a=array[loc]; |
| 135 |
T b=array[next]; |
| 136 |
assert(a!=b && a!=null); |
| 137 |
if(a.compareTo(b)<0){ |
| 138 |
array[next]=a; |
| 139 |
array[loc]=b; |
| 140 |
a.setLoc(next); |
| 141 |
b.setLoc(loc); |
| 142 |
return percDown(next); |
| 143 |
} |
| 144 |
return loc; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
private int percUp(int loc){ |
| 149 |
|
| 150 |
assert(loc>0 && loc<=size) : loc+", "+size; |
| 151 |
int next1=loc*2; |
| 152 |
int next2=next1+1; |
| 153 |
if(next1>size){return loc;} |
| 154 |
T a=array[loc]; |
| 155 |
T b=array[next1]; |
| 156 |
T c=array[next2]; |
| 157 |
assert(a!=b); |
| 158 |
assert(b!=c); |
| 159 |
assert(b!=null && a!=null); |
| 160 |
|
| 161 |
if(c==null || b.compareTo(c)<1){ |
| 162 |
if(a.compareTo(b)>0){ |
| 163 |
array[next1]=a; |
| 164 |
array[loc]=b; |
| 165 |
a.setLoc(next1); |
| 166 |
b.setLoc(loc); |
| 167 |
|
| 168 |
return percUp(next1); |
| 169 |
} |
| 170 |
}else{ |
| 171 |
if(a.compareTo(c)>0){ |
| 172 |
array[next2]=a; |
| 173 |
array[loc]=c; |
| 174 |
a.setLoc(next2); |
| 175 |
c.setLoc(loc); |
| 176 |
|
| 177 |
return percUp(next2); |
| 178 |
} |
| 179 |
} |
| 180 |
return loc; |
| 181 |
} |
| 182 |
|
| 183 |
public boolean isEmpty(){ |
| 184 |
|
| 185 |
return size==0; |
| 186 |
} |
| 187 |
|
| 188 |
public boolean hasRoom(){ |
| 189 |
return size<CAPACITY; |
| 190 |
} |
| 191 |
|
| 192 |
public void clear(){ |
| 193 |
|
| 194 |
for(int i=1; i<=size; i++){array[i]=null;} |
| 195 |
size=0; |
| 196 |
} |
| 197 |
|
| 198 |
public int size(){ |
| 199 |
return size; |
| 200 |
} |
| 201 |
|
| 202 |
public static int tier(int x){ |
| 203 |
int leading=Integer.numberOfLeadingZeros(x); |
| 204 |
return 31-leading; |
| 205 |
} |
| 206 |
|
| 207 |
public boolean testForDuplicates(){ |
| 208 |
for(int i=0; i<array.length; i++){ |
| 209 |
for(int j=i+1; j<array.length; j++){ |
| 210 |
if(array[i]!=null && array[i]==array[j]){return false;} |
| 211 |
if(array[i]!=null && array[i].loc()!=i){return false;} |
| 212 |
} |
| 213 |
} |
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
public ArrayList<T> toList(){ |
| 218 |
ArrayList<T> list=new ArrayList<T>(size); |
| 219 |
for(int i=0, lim=size; i<lim; i++){ |
| 220 |
list.add(poll()); |
| 221 |
} |
| 222 |
assert(isEmpty()); |
| 223 |
return list; |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public Iterator<T> iterator() { |
| 228 |
return new HeapIterator(); |
| 229 |
} |
| 230 |
|
| 231 |
private class HeapIterator implements Iterator<T> { |
| 232 |
|
| 233 |
@Override |
| 234 |
public boolean hasNext() {return loc<=size;} |
| 235 |
|
| 236 |
@Override |
| 237 |
public T next() {return array[loc++];} |
| 238 |
|
| 239 |
int loc=1; |
| 240 |
|
| 241 |
} |
| 242 |
|
| 243 |
private final T[] array; |
| 244 |
public final int CAPACITY; |
| 245 |
public final boolean rollover; |
| 246 |
private int size=0; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
} |