Debian Codegraph @

current/structures/LongHeap.java

raw symbols SCIP
1 package structures;
2
3 import shared.KillSwitch;
4
5 /**
6 * @author Brian Bushnell
7 * @date June 30, 2016
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++;} //Array size is always even.
18
19 CAPACITY=maxSize;
20 array=KillSwitch.allocLong1D(len);
21 rollover=rollover_;
22 // queue=new PriorityQueue<T>(maxSize);
23 }
24
25 public boolean add(long t){
26 //assert(testForDuplicates());
27 // assert(queue.size()==size);
28 // queue.add(t);
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(); //Turns into a rolling buffer by removing smallest value.
37
38 // {//This is a more efficient alternative to poll() and percDown(), but the result is slightly different.
39 // array[1]=t;
40 // percUp(1);
41 // return true;
42 // }
43 }
44 assert(size<CAPACITY);
45
46 size++;
47 array[size]=t;
48 percDown(size);
49 // assert(queue.size()==size);
50 // assert(queue.peek()==peek());
51 //assert(testForDuplicates());
52 return true;
53 }
54
55 public long peek(){
56 //assert(testForDuplicates());
57 // assert(queue.size()==size);
58 if(size==0){return EMPTY;}
59 // assert(array[1]==queue.peek()) : size+", "+queue.size()+"\n"+
60 // array[1]+"\n"+
61 // array[2]+" , "+array[3]+"\n"+
62 // array[4]+" , "+array[5]+" , "+array[6]+" , "+array[7]+"\n"+
63 // queue.peek()+"\n";
64 //assert(testForDuplicates());
65 return array[1];
66 }
67
68 public long poll(){
69 //assert(testForDuplicates());
70 // assert(queue.size()==size);
71 if(size==0){return EMPTY;}
72 long t=array[1];
73 // assert(t==queue.poll());
74 array[1]=array[size];
75 array[size]=EMPTY;
76 size--;
77 if(size>0){percUp(1);}
78 // assert(queue.size()==size);
79 // assert(queue.peek()==peek());
80 //assert(testForDuplicates());
81 return t;
82 }
83
84 // private void percDownRecursive(int loc){
85 // //assert(testForDuplicates());
86 // assert(loc>0);
87 // if(loc==1){return;}
88 // int next=loc/2;
89 // long a=array[loc];
90 // long b=array[next];
91 // assert(a!=b);
92 // if(a.compareTo(b)<0){
93 // array[next]=a;
94 // array[loc]=b;
95 // percDown(next);
96 // }
97 // }
98 //
99 // private void percDown_old(int loc){
100 // //assert(testForDuplicates());
101 // assert(loc>0);
102 //
103 // final long a=array[loc];
104 //
105 // while(loc>1){
106 // int next=loc/2;
107 // long b=array[next];
108 // assert(a!=b);
109 // if(a.compareTo(b)<0){
110 // array[next]=a;
111 // array[loc]=b;
112 // loc=next;
113 // }else{return;}
114 // }
115 // }
116
117 private void percDown(int loc){
118 //assert(testForDuplicates());
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 // while(loc>1 && (a.site<b.site || (a.site==b.site && a.column<b.column))){
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 //assert(testForDuplicates());
139 // assert(loc>0 && loc<=size+1) : loc+", "+size; //Allows use of more-efficient sketch creation, but gives different result...
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 //assert(testForDuplicates());
151 if(c==EMPTY || b<=c){
152 if(a>b){
153 // if((a.site>b.site || (a.site==b.site && a.column>b.column))){
154 array[next1]=a;
155 array[loc]=b;
156 //assert(testForDuplicates());
157 percUp(next1);
158 }
159 }else{
160 if(a>c){
161 // if((a.site>c.site || (a.site==c.site && a.column>c.column))){
162 array[next2]=a;
163 array[loc]=c;
164 //assert(testForDuplicates());
165 percUp(next2);
166 }
167 }
168 }
169
170 private void percUpIter(int loc){
171 //assert(testForDuplicates());
172 assert(loc>0 && loc<=size) : loc+", "+size;
173 final long a=array[loc];
174 //assert(testForDuplicates());
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 // if(c==EMPTY || (b.site<c.site || (b.site==c.site && b.column<c.column))){
189 if(a>b){
190 // if((a.site>b.site || (a.site==b.site && a.column>b.column))){
191 // array[next1]=a;
192 array[loc]=b;
193 loc=next1;
194 }else{
195 break;
196 }
197 }else{
198 if(a>c){
199 // if((a.site>c.site || (a.site==c.site && a.column>c.column))){
200 // array[next2]=a;
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 // assert((size==0) == queue.isEmpty());
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 // queue.clear();
228 // for(int i=1; i<=size; i++){array[i]=EMPTY;}
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 }