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