Debian Codegraph @

current/align2/Quad64Heap.java

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