Debian Codegraph @

current/structures/SuperLongList.java

raw symbols SCIP
1 package structures;
2
3 import shared.Tools;
4
5 /**
6 * Holds counts of numbers for histograms.
7 * Small numbers are stored as counts in an array.
8 * Large numbers are stored individually in a LongList.
9 * @author Brian Bushnell
10 *
11 */
12 public class SuperLongList {
13
14 /*--------------------------------------------------------------*/
15 /*---------------- Initialization ----------------*/
16 /*--------------------------------------------------------------*/
17
18 public SuperLongList(){
19 this(100000);
20 }
21
22 /**
23 * @param limit_ Length of array used to store small numbers.
24 */
25 public SuperLongList(int limit_){
26 limit=limit_;
27 array=new long[limit];
28 list=new LongList();
29 }
30
31 /*--------------------------------------------------------------*/
32 /*---------------- Getters ----------------*/
33 /*--------------------------------------------------------------*/
34
35 public long[] array(){return array;}
36 public LongList list(){return list;}
37
38 public void addTo(long[] ca){
39 final int max=ca.length-1;
40 {
41 for(int i=0; i<array.length; i++){
42 ca[Tools.min(i, max)]+=array[i];
43 }
44 }
45 {
46 final int listSize=list.size;
47 final long[] listArray=list.array;
48 for(int i=0; i<listSize; i++){
49 long value=listArray[i];
50 ca[(int)Tools.min(value, max)]++;
51 }
52 }
53 }
54
55 /*--------------------------------------------------------------*/
56 /*---------------- Mutation ----------------*/
57 /*--------------------------------------------------------------*/
58
59 /** Add or increment these keys. */
60 public void add(LongList ll){
61 for(int i=0; i<ll.size; i++){
62 add(ll.get(i));
63 }
64 }
65
66 /** Add or increment these counts. */
67 public void addCounts(long[] counts){
68 for(int i=0; i<counts.length; i++){
69 long x=counts[i];
70 assert(x>=0);
71 if(x>0){increment(i, x);}
72 }
73 }
74
75 /** Add or increment this key.
76 * May result in duplicate copies appearing, which is fine. */
77 public void add(long x){
78 if(x<limit){array[(int)x]++;}
79 else{list.add(x);}
80 sum+=x;
81 count++;
82 }
83 public void increment(long x){add(x);}
84
85 public void increment(long x, long amt){
86 assert(amt>=0) : "SLL does not support decrements.";
87 if(x<limit){array[(int)x]+=amt;}
88 else{
89 for(int i=0; i<amt; i++){list.add(x);}
90 }
91 sum+=x*amt;
92 count+=amt;
93 }
94
95 public void add(SuperLongList sllT){
96 if(array.length==sllT.array.length){//Fast, expected case
97 assert(array.length==sllT.array.length) : "Array lengths must match.";
98 for(int i=0; i<sllT.array.length; i++){
99 array[i]+=sllT.array[i];
100 }
101 list.append(sllT.list);
102 count+=sllT.count;
103 sum+=sllT.sum;
104 }else{//Slower generic case of unequal SLLs
105 addCounts(sllT.array);
106 add(sllT.list);
107 }
108 }
109 public void incrementBy(SuperLongList sllT) {add(sllT);}
110
111 public void sort() {
112 list.sort();
113 // sorted=true;
114 }
115
116 /*--------------------------------------------------------------*/
117 /*---------------- Statistics ----------------*/
118 /*--------------------------------------------------------------*/
119
120 public double stdev(){
121 final long div=Tools.max(1, count);
122 double avg=sum/(double)div;
123 double sumdev2=0;
124 for(int i=0; i<array.length; i++){
125 double dev=avg-i;
126 double dev2=dev*dev;
127 sumdev2+=(array[i]*dev2);
128 }
129 for(int i=0; i<list.size; i++){
130 long x=list.get(i);
131 double dev=avg-x;
132 double dev2=dev*dev;
133 sumdev2+=dev2;
134 }
135 return Math.sqrt(sumdev2/div);
136 }
137
138 /** Returns value such that percentile of values are below that value */
139 public long percentileValueByCount(double percentile){
140 // assert(sorted);
141 long thresh=(long)(count*percentile);
142 long currentSum=0;
143 long currentCount=0;
144 for(int i=0; i<array.length; i++){
145 long x=array[i];
146 currentSum+=(x*i);
147 currentCount+=i;
148 if(currentCount>=thresh){return i;}
149 }
150 long prev=-1;
151 for(int i=0; i<list.size; i++){
152 long x=list.get(i);
153 assert(x>=prev) : "Needs to be sorted ascending.";
154 currentSum+=x;
155 currentCount++;
156 if(currentCount>=thresh){return x;}
157 prev=x;
158 }
159 assert(false) : percentile+", "+count+", "+sum;
160 return 0;
161 }
162
163 /** Returns value such that percentile of sum of values are below that value */
164 public long percentileValueBySum(double percentile){
165 // assert(sorted);
166 long thresh=(long)(sum*percentile);
167 long currentSum=0;
168 long currentCount=0;
169 for(int i=0; i<array.length; i++){
170 long x=array[i];
171 currentSum+=(x*i);
172 currentCount+=i;
173 if(currentSum>=thresh){return i;}
174 }
175 long prev=-1;
176 for(int i=0; i<list.size; i++){
177 long x=list.get(i);
178 assert(x>=prev) : "Needs to be sorted ascending.";
179 currentSum+=x;
180 currentCount++;
181 if(currentSum>=thresh){return x;}
182 prev=x;
183 }
184 assert(false) : percentile+", "+count+", "+sum;
185 return 0;
186 }
187
188 /** Returns the sum of the lower percentile of values */
189 public long percentileSumByCount(double percentile){
190 // assert(sorted);
191 long thresh=(long)(count*percentile);
192 long currentSum=0;
193 long currentCount=0;
194 for(int i=0; i<array.length; i++){
195 long x=array[i];
196 currentSum+=(x*i);
197 currentCount+=i;
198 if(currentCount>=thresh){
199 currentSum-=(x*i);
200 currentCount-=i;
201 while(currentCount<thresh){
202 currentSum+=i;
203 currentCount++;
204 }
205 return currentSum;
206 }
207 }
208 long prev=-1;
209 for(int i=0; i<list.size; i++){
210 long x=list.get(i);
211 assert(x>=prev) : "Needs to be sorted ascending.";
212 currentSum+=x;
213 currentCount++;
214 if(currentCount>=thresh){return currentSum;}
215 prev=x;
216 }
217 assert(false) : percentile+", "+count+", "+sum;
218 return 0;
219 }
220
221 /** Returns the number of lower values needed to sum to this percentile of the total sum */
222 public long percentileCountBySum(double percentile){
223 // assert(sorted);
224 long thresh=(long)(sum*percentile);
225 long currentSum=0;
226 long currentCount=0;
227 for(int i=0; i<array.length; i++){
228 long x=array[i];
229 currentSum+=(x*i);
230 currentCount+=i;
231 if(currentSum>=thresh){
232 currentSum-=(x*i);
233 currentCount-=i;
234 while(currentSum<thresh){
235 currentSum+=i;
236 currentCount++;
237 }
238 return currentCount;
239 }
240 }
241 long prev=-1;
242 for(int i=0; i<list.size; i++){
243 long x=list.get(i);
244 assert(x>=prev) : "Needs to be sorted ascending.";
245 currentSum+=x;
246 currentCount++;
247 if(currentSum>=thresh){return currentCount;}
248 prev=x;
249 }
250 assert(false) : percentile+", "+count+", "+sum;
251 return 0;
252 }
253
254 //Slow, avoid using
255 public long max(){
256 if(list.size>0){return list.max();}
257 for(int i=array.length-1; i>=0; i--){
258 if(array[i]>0){return i;}
259 }
260 return 0;
261 }
262
263 public double mean(){
264 return sum/Tools.max(1.0, count);
265 }
266
267 public long median(){
268 return percentileValueByCount(0.5);
269 }
270
271 public long mode(){
272 long maxCount=0;
273 long maxValue=0;
274 for(int i=0; i<array.length; i++){
275 long x=array[i];
276 if(x>maxCount){
277 maxCount=x;
278 maxValue=i;
279 }
280 }
281
282 long prev=-1;
283 long currentCount=0;
284 for(int i=0; i<list.size; i++){
285 long x=list.get(i);
286 if(x==prev){
287 currentCount++;
288 if(currentCount>maxCount){
289 maxCount=currentCount;
290 maxValue=x;
291 }
292 }else{
293 assert(x>prev) : "Needs to be sorted ascending.";
294 prev=x;
295 currentCount=1;
296 }
297 }
298 return maxValue;
299 }
300
301 /*--------------------------------------------------------------*/
302 /*---------------- toString ----------------*/
303 /*--------------------------------------------------------------*/
304
305 @Override
306 public String toString(){
307 ByteBuilder bb=new ByteBuilder();
308 bb.append('[');
309 String comma="";
310 for(int i=0; i<array.length; i++){
311 long value=array[i];
312 for(long j=0; j<value; j++){
313 bb.append(comma).append(i);
314 comma=", ";
315 }
316 }
317 for(int i=0; i<list.size; i++){
318 bb.append(comma).append(list.get(i));
319 comma=", ";
320 }
321 bb.append(']');
322 return bb.toString();
323 }
324
325 /*--------------------------------------------------------------*/
326 /*---------------- Fields ----------------*/
327 /*--------------------------------------------------------------*/
328
329 public long count() {return count;}
330 public long sum() {return sum;}
331
332 private long count;
333 private long sum;
334
335 /*--------------------------------------------------------------*/
336 /*---------------- Final Fields ----------------*/
337 /*--------------------------------------------------------------*/
338
339 final long[] array;
340 final LongList list;
341 final int limit;
342
343 }