Debian Codegraph @

current/structures/HeapLoc.java

raw symbols SCIP
1 package structures;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5
6 /** A heap that tracks element location */
7 public final class HeapLoc<T extends SetLoc<? super T>> implements Iterable<T> {
8
9 //A good value for maxSize would be (2^N)-1
10 @SuppressWarnings("unchecked")
11 public HeapLoc(int maxSize, boolean rollover_){
12
13 int len=maxSize+1;
14 if((len&1)==1){len++;} //Array size is always even.
15
16 CAPACITY=maxSize;
17 array=(T[])new SetLoc[len];
18 rollover=rollover_;
19 // queue=new PriorityQueue<T>(maxSize);
20 }
21
22 public HeapLoc<T> resizeNew(int newCapacity){
23 HeapLoc<T> heap=new HeapLoc<T>(newCapacity, rollover);
24 //Technically I could just do a limited array copy,
25 //since all the positions will remain the same.
26 //TODO Add resizing the current one as an option.
27 for(T t : this) {
28 t.setLoc(-1);
29 heap.add(t);
30 }
31 assert(size()==heap.size());
32 this.clear();
33 return heap;
34 }
35
36 public boolean add(T t){
37 return addAndReturnLocation(t)>=0;
38 }
39
40 public int addAndReturnLocation(T t){
41 assert(t.loc()<0);
42 assert(size==0 || array[size]!=null);
43 assert(rollover || size<CAPACITY);
44
45 if(size>=CAPACITY){
46
47 if(t.compareTo(array[1])<=0){return -1;}
48
49 poll(); //Turns into a rolling buffer by removing smallest value.
50
51 // {//This is a more efficient alternative to poll() and percDown(), but the result is slightly different.
52 // array[1]=t;
53 // percUp(1);
54 // return true;
55 // }
56 }
57 assert(size<CAPACITY);
58
59 //assert(testForDuplicates());
60 // assert(queue.size()==size);
61 // queue.add(t);
62 assert(size==0 || array[size]!=null);
63 size++;
64 array[size]=t;
65 t.setLoc(size);
66 return percDown(size);
67 // assert(queue.size()==size);
68 // assert(queue.peek()==peek());
69 //assert(testForDuplicates());
70 }
71
72 public T peek(){
73 //assert(testForDuplicates());
74 // assert(queue.size()==size);
75 if(size==0){return null;}
76 // assert(array[1]==queue.peek()) : size+", "+queue.size()+"\n"+
77 // array[1]+"\n"+
78 // array[2]+" , "+array[3]+"\n"+
79 // array[4]+" , "+array[5]+" , "+array[6]+" , "+array[7]+"\n"+
80 // queue.peek()+"\n";
81 //assert(testForDuplicates());
82 return array[1];
83 }
84
85 public T poll(){
86 //assert(testForDuplicates());
87 // assert(queue.size()==size);
88 if(size==0){return null;}
89 T t=array[1];
90 // assert(t==queue.poll());
91 array[1]=array[size];
92 array[size]=null;
93 size--;
94 if(size>0){
95 array[1].setLoc(1);
96 percUp(1);
97 }
98 // assert(queue.size()==size);
99 // assert(queue.peek()==peek());
100 //assert(testForDuplicates());
101 t.setLoc(-1);
102 return t;
103 }
104
105 /** Returns the new location */
106 public int jiggle(T t){
107 final int loc=t.loc();
108 assert(array[loc]==t);
109 int x=percDown(loc);
110 if(x!=loc) {return x;}
111 return percUp(loc);
112 }
113
114 /** Returns the new location */
115 public int jiggleDown(T t){
116 final int loc=t.loc();
117 assert(array[loc]==t);
118 return percDown(loc);
119 }
120
121 /** Returns the new location */
122 public int jiggleUp(T t){
123 final int loc=t.loc();
124 assert(array[loc]==t);
125 return percDown(loc);
126 }
127
128 /** Returns the new location */
129 private int percDown(int loc){
130 //assert(testForDuplicates());
131 assert(loc>0);
132 if(loc==1){return loc;}
133 int next=loc/2;
134 T a=array[loc];
135 T b=array[next];
136 assert(a!=b && a!=null);
137 if(a.compareTo(b)<0){
138 array[next]=a;
139 array[loc]=b;
140 a.setLoc(next);
141 b.setLoc(loc);
142 return percDown(next);
143 }
144 return loc;
145 }
146
147 /** Returns the new location */
148 private int percUp(int loc){
149 //assert(testForDuplicates());
150 assert(loc>0 && loc<=size) : loc+", "+size;
151 int next1=loc*2;
152 int next2=next1+1;
153 if(next1>size){return loc;}
154 T a=array[loc];
155 T b=array[next1];
156 T c=array[next2];
157 assert(a!=b);
158 assert(b!=c);
159 assert(b!=null && a!=null);
160 //assert(testForDuplicates());
161 if(c==null || b.compareTo(c)<1){
162 if(a.compareTo(b)>0){
163 array[next1]=a;
164 array[loc]=b;
165 a.setLoc(next1);
166 b.setLoc(loc);
167 //assert(testForDuplicates());
168 return percUp(next1);
169 }
170 }else{
171 if(a.compareTo(c)>0){
172 array[next2]=a;
173 array[loc]=c;
174 a.setLoc(next2);
175 c.setLoc(loc);
176 //assert(testForDuplicates());
177 return percUp(next2);
178 }
179 }
180 return loc;
181 }
182
183 public boolean isEmpty(){
184 // assert((size==0) == queue.isEmpty());
185 return size==0;
186 }
187
188 public boolean hasRoom(){
189 return size<CAPACITY;
190 }
191
192 public void clear(){
193 // queue.clear();
194 for(int i=1; i<=size; i++){array[i]=null;}
195 size=0;
196 }
197
198 public int size(){
199 return size;
200 }
201
202 public static int tier(int x){
203 int leading=Integer.numberOfLeadingZeros(x);
204 return 31-leading;
205 }
206
207 public boolean testForDuplicates(){
208 for(int i=0; i<array.length; i++){
209 for(int j=i+1; j<array.length; j++){
210 if(array[i]!=null && array[i]==array[j]){return false;}
211 if(array[i]!=null && array[i].loc()!=i){return false;}
212 }
213 }
214 return true;
215 }
216
217 public ArrayList<T> toList(){
218 ArrayList<T> list=new ArrayList<T>(size);
219 for(int i=0, lim=size; i<lim; i++){
220 list.add(poll());
221 }
222 assert(isEmpty());
223 return list;
224 }
225
226 @Override
227 public Iterator<T> iterator() {
228 return new HeapIterator();
229 }
230
231 private class HeapIterator implements Iterator<T> {
232
233 @Override
234 public boolean hasNext() {return loc<=size;}
235
236 @Override
237 public T next() {return array[loc++];}
238
239 int loc=1;
240
241 }
242
243 private final T[] array;
244 public final int CAPACITY;
245 public final boolean rollover;
246 private int size=0;
247
248 // private PriorityQueue<T> queue;
249
250 }