Debian Codegraph @

current/align2/PackedHeap.java

raw symbols SCIP
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++;} //Array size is always even.
9
10 CAPACITY=maxSize;
11 array=new long[len];
12 // queue=new PriorityQueue<T>(maxSize);
13 }
14
15 public boolean add(long t){
16 //assert(testForDuplicates());
17 // assert(queue.size()==size);
18 // queue.add(t);
19 assert(size==0 || array[size]!=-1L);
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 long peek(){
30 //assert(testForDuplicates());
31 // assert(queue.size()==size);
32 if(size==0){return -1L;}
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 long poll(){
43 //assert(testForDuplicates());
44 // assert(queue.size()==size);
45 if(size==0){return -1L;}
46 long t=array[1];
47 // assert(t==queue.poll());
48 array[1]=array[size];
49 array[size]=-1L;
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 percDown(int loc){
59 //assert(testForDuplicates());
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 // while(loc>1 && (a.site<b.site || (a.site==b.site && a.column<b.column))){
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 //assert(testForDuplicates());
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 //assert(testForDuplicates());
91 if(c==-1L || b<=c){
92 if(a>b){
93 // if((a.site>b.site || (a.site==b.site && a.column>b.column))){
94 array[next1]=a;
95 array[loc]=b;
96 //assert(testForDuplicates());
97 percUp(next1);
98 }
99 }else{
100 if(a>c){
101 // if((a.site>c.site || (a.site==c.site && a.column>c.column))){
102 array[next2]=a;
103 array[loc]=c;
104 //assert(testForDuplicates());
105 percUp(next2);
106 }
107 }
108 }
109
110 private void percUpIter(int loc){
111 //assert(testForDuplicates());
112 assert(loc>0 && loc<=size) : loc+", "+size;
113 final long a=array[loc];
114 //assert(testForDuplicates());
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 // if(c==-1L || (b.site<c.site || (b.site==c.site && b.column<c.column))){
129 if(a>b){
130 // if((a.site>b.site || (a.site==b.site && a.column>b.column))){
131 // array[next1]=a;
132 array[loc]=b;
133 loc=next1;
134 }else{
135 break;
136 }
137 }else{
138 if(a>c){
139 // if((a.site>c.site || (a.site==c.site && a.column>c.column))){
140 // array[next2]=a;
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 // assert((size==0) == queue.isEmpty());
155 return size==0;
156 }
157
158 public void clear(){
159 // queue.clear();
160 // for(int i=1; i<=size; i++){array[i]=-1L;}
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 }