Debian Codegraph @

current/structures/Heap.java

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