| 1 |
package align2; |
| 2 |
|
| 3 |
public final class PackedHeap { |
| 4 |
|
| 5 |
public PackedHeap(int maxSize){ |
| 6 |
|
| 7 |
int len=maxSize+1; |
| 8 |
if((len&1)==1){len++;} |
| 9 |
|
| 10 |
CAPACITY=maxSize; |
| 11 |
array=new long[len]; |
| 12 |
|
| 13 |
} |
| 14 |
|
| 15 |
public boolean add(long t){ |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
assert(size==0 || array[size]!=-1L); |
| 20 |
size++; |
| 21 |
array[size]=t; |
| 22 |
percDown(size); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
return true; |
| 27 |
} |
| 28 |
|
| 29 |
public long peek(){ |
| 30 |
|
| 31 |
|
| 32 |
if(size==0){return -1L;} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
return array[1]; |
| 40 |
} |
| 41 |
|
| 42 |
public long poll(){ |
| 43 |
|
| 44 |
|
| 45 |
if(size==0){return -1L;} |
| 46 |
long t=array[1]; |
| 47 |
|
| 48 |
array[1]=array[size]; |
| 49 |
array[size]=-1L; |
| 50 |
size--; |
| 51 |
if(size>0){percUp(1);} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
return t; |
| 56 |
} |
| 57 |
|
| 58 |
private void percDown(int loc){ |
| 59 |
|
| 60 |
assert(loc>0); |
| 61 |
if(loc==1){return;} |
| 62 |
|
| 63 |
int next=loc/2; |
| 64 |
final long a=array[loc]; |
| 65 |
long b=array[next]; |
| 66 |
|
| 67 |
|
| 68 |
while(loc>1 && a<b){ |
| 69 |
array[loc]=b; |
| 70 |
loc=next; |
| 71 |
next=next/2; |
| 72 |
b=array[next]; |
| 73 |
} |
| 74 |
|
| 75 |
array[loc]=a; |
| 76 |
} |
| 77 |
|
| 78 |
private void percUp(int loc){ |
| 79 |
|
| 80 |
assert(loc>0 && loc<=size) : loc+", "+size; |
| 81 |
int next1=loc*2; |
| 82 |
int next2=next1+1; |
| 83 |
if(next1>size){return;} |
| 84 |
long a=array[loc]; |
| 85 |
long b=array[next1]; |
| 86 |
long c=array[next2]; |
| 87 |
assert(a!=b); |
| 88 |
assert(b!=c); |
| 89 |
assert(b!=-1L); |
| 90 |
|
| 91 |
if(c==-1L || b<=c){ |
| 92 |
if(a>b){ |
| 93 |
|
| 94 |
array[next1]=a; |
| 95 |
array[loc]=b; |
| 96 |
|
| 97 |
percUp(next1); |
| 98 |
} |
| 99 |
}else{ |
| 100 |
if(a>c){ |
| 101 |
|
| 102 |
array[next2]=a; |
| 103 |
array[loc]=c; |
| 104 |
|
| 105 |
percUp(next2); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
private void percUpIter(int loc){ |
| 111 |
|
| 112 |
assert(loc>0 && loc<=size) : loc+", "+size; |
| 113 |
final long a=array[loc]; |
| 114 |
|
| 115 |
|
| 116 |
int next1=loc*2; |
| 117 |
int next2=next1+1; |
| 118 |
|
| 119 |
while(next1<=size){ |
| 120 |
|
| 121 |
long b=array[next1]; |
| 122 |
long c=array[next2]; |
| 123 |
assert(a!=b); |
| 124 |
assert(b!=c); |
| 125 |
assert(b!=-1L); |
| 126 |
|
| 127 |
if(c==-1L || b<=c){ |
| 128 |
|
| 129 |
if(a>b){ |
| 130 |
|
| 131 |
|
| 132 |
array[loc]=b; |
| 133 |
loc=next1; |
| 134 |
}else{ |
| 135 |
break; |
| 136 |
} |
| 137 |
}else{ |
| 138 |
if(a>c){ |
| 139 |
|
| 140 |
|
| 141 |
array[loc]=c; |
| 142 |
loc=next2; |
| 143 |
}else{ |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
next1=loc*2; |
| 148 |
next2=next1+1; |
| 149 |
} |
| 150 |
array[loc]=a; |
| 151 |
} |
| 152 |
|
| 153 |
public boolean isEmpty(){ |
| 154 |
|
| 155 |
return size==0; |
| 156 |
} |
| 157 |
|
| 158 |
public void clear(){ |
| 159 |
|
| 160 |
|
| 161 |
size=0; |
| 162 |
} |
| 163 |
|
| 164 |
public int size(){ |
| 165 |
return size; |
| 166 |
} |
| 167 |
|
| 168 |
public static int tier(int x){ |
| 169 |
int leading=Integer.numberOfLeadingZeros(x); |
| 170 |
return 31-leading; |
| 171 |
} |
| 172 |
|
| 173 |
public boolean testForDuplicates(){ |
| 174 |
for(int i=0; i<array.length; i++){ |
| 175 |
for(int j=i+1; j<array.length; j++){ |
| 176 |
if(array[i]!=-1L && array[i]==array[j]){return false;} |
| 177 |
} |
| 178 |
} |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
final long[] array; |
| 183 |
private final int CAPACITY; |
| 184 |
private int size=0; |
| 185 |
|
| 186 |
} |