Debian Codegraph @

current/structures/IntList.java

raw symbols SCIP
1 package structures;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.LinkedList;
7 import java.util.Random;
8
9 import shared.KillSwitch;
10 import shared.Shared;
11 import shared.Timer;
12 import shared.Tools;
13
14
15 /**
16 * Efficient equivalent of ArrayList for int
17 * @author Brian Bushnell
18 * @date Sep 20, 2014
19 *
20 */
21 public final class IntList{
22
23 /*--------------------------------------------------------------*/
24 /*---------------- Main ----------------*/
25 /*--------------------------------------------------------------*/
26
27 /** For benchmarking.
28 * Accepts an optional argument for list length. */
29 public static void main(String[] args){
30 int length=args.length>0 ? Integer.parseInt(args[0]) : 100000000;
31 benchmark(length);
32 }
33
34 /** For benchmarking */
35 private static void benchmark(final int length){
36 Timer t=new Timer();
37
38 System.gc();
39
40 {
41 System.err.println("\nIntList:");
42 Shared.printMemory();
43 t.start();
44 IntList list=new IntList();
45 for(int i=0; i<length; i++){
46 list.add(i);
47 }
48 t.stop("Time: \t");
49 System.gc();
50 Shared.printMemory();
51 list=null;
52 System.gc();
53 }
54
55 {
56 System.err.println("\nArrayList:");
57 Shared.printMemory();
58 t.start();
59 ArrayList<Integer> list=new ArrayList<Integer>();
60 for(int i=0; i<length; i++){
61 list.add(i);
62 }
63 t.stop("Time: \t");
64 System.gc();
65 Shared.printMemory();
66 list=null;
67 System.gc();
68 }
69
70 {
71 System.err.println("\nLinkedList:");
72 Shared.printMemory();
73 t.start();
74 LinkedList<Integer> list=new LinkedList<Integer>();
75 for(int i=0; i<length; i++){
76 list.add(i);
77 }
78 t.stop("Time: \t");
79 System.gc();
80 Shared.printMemory();
81 list=null;
82 System.gc();
83 }
84
85
86
87 {
88 System.err.println("\nIntList:");
89 Shared.printMemory();
90 t.start();
91 IntList list=new IntList();
92 for(int i=0; i<length; i++){
93 list.add(i);
94 }
95 t.stop("Time: \t");
96 t.start();
97 System.gc();
98 t.stop("GC Time: \t");
99 Shared.printMemory();
100 t.start();
101 list.shuffle();
102 t.stop("Shuf Time: \t");
103 t.start();
104 list.sort();
105 t.stop("Sort Time: \t");
106 list=null;
107 System.gc();
108 }
109
110 {
111 System.err.println("\nArrayList:");
112 Shared.printMemory();
113 t.start();
114 ArrayList<Integer> list=new ArrayList<Integer>();
115 for(int i=0; i<length; i++){
116 list.add(i);
117 }
118 t.stop("Time: \t");
119 t.start();
120 System.gc();
121 t.stop("GC Time: \t");
122 Shared.printMemory();
123 t.start();
124 Collections.shuffle(list);
125 t.stop("Shuf Time: \t");
126 t.start();
127 Collections.sort(list);
128 t.stop("Sort Time: \t");
129 list=null;
130 System.gc();
131 }
132
133 {
134 System.err.println("\nLinkedList:");
135 Shared.printMemory();
136 t.start();
137 LinkedList<Integer> list=new LinkedList<Integer>();
138 for(int i=0; i<length; i++){
139 list.add(i);
140 }
141 t.stop("Time: \t");
142 t.start();
143 System.gc();
144 t.stop("GC Time: \t");
145 Shared.printMemory();
146 t.start();
147 Collections.shuffle(list);
148 t.stop("Shuf Time: \t");
149 t.start();
150 Collections.sort(list);
151 t.stop("Sort Time: \t");
152 list=null;
153 System.gc();
154 }
155
156 }
157
158 /*--------------------------------------------------------------*/
159 /*---------------- Initialization ----------------*/
160 /*--------------------------------------------------------------*/
161
162 public IntList(){this(256);}
163
164 public IntList(int initial){
165 // assert(initial>0) : initial+"\n"+this;
166 initial=Tools.max(initial, 1);
167 array=KillSwitch.allocInt1D(initial);
168 }
169
170 public IntList copy() {
171 IntList copy=new IntList(size);
172 copy.addAll(this);
173 return copy;
174 }
175
176 /*--------------------------------------------------------------*/
177 /*---------------- Mutation ----------------*/
178 /*--------------------------------------------------------------*/
179
180 public void clear(){size=0;}
181 public void clearFull(){
182 Arrays.fill(array, 0);
183 size=0;
184 }
185
186 public final void set(int loc, int value){
187 if(loc>=array.length){
188 resize(loc*2L+1);
189 }
190 array[loc]=value;
191 size=max(size, loc+1);
192 }
193
194 public final void setLast(int value){
195 assert(size>0);
196 array[size-1]=value;
197 }
198
199 public final void increment(int loc){increment(loc, 1);}
200
201 public final void increment(int loc, int value){
202 if(loc>=array.length){
203 resize(loc*2L+1);
204 }
205 array[loc]+=value;
206 size=max(size, loc+1);
207 }
208
209 public void subtractFrom(int value){
210 for(int i=0; i<size; i++){
211 array[i]=value-array[i];
212 }
213 }
214
215 public final void add(int x){
216 if(size>=array.length){
217 resize(size*2L+1);
218 }
219 array[size]=x;
220 size++;
221 }
222
223 /*--------------------------------------------------------------*/
224 /*---------------- Bulk Operations ----------------*/
225 /*--------------------------------------------------------------*/
226
227 public void addAll(IntList counts) {
228 final int[] array2=counts.array;
229 final int size2=counts.size;
230 for(int i=0; i<size2; i++){add(array2[i]);}
231 }
232
233 public void sort() {
234 if(size>1){Shared.sort(array, 0, size);}
235 }
236
237 public void shuffle() {
238 if(size<2){return;}
239 Random randy=Shared.threadLocalRandom();
240 for(int i=0; i<size; i++){
241 int j=randy.nextInt(size);
242 int temp=array[i];
243 array[i]=array[j];
244 array[j]=temp;
245 }
246 }
247
248 public void reverse() {
249 if(size>1){Tools.reverseInPlace(array, 0, size);}
250 }
251
252 /*--------------------------------------------------------------*/
253 /*---------------- Resizing ----------------*/
254 /*--------------------------------------------------------------*/
255
256 private final void resize(final long size2){
257 assert(size2>size) : size+", "+size2;
258 final int size3=(int)Tools.min(Shared.MAX_ARRAY_LEN, size2);
259 assert(size3>size) : "Overflow: "+size+", "+size2+" -> "+size3;
260 array=KillSwitch.copyOf(array, size3);
261 }
262
263 public final void setSize(final int size2) {
264 if(size2<array.length){resize(size2);}
265 size=size2;
266 }
267
268 public final IntList shrink(){
269 if(size==array.length){return this;}
270 array=KillSwitch.copyOf(array, size);
271 return this;
272 }
273
274 public final long sumLong(){
275 long sum=0;
276 for(int i=0; i<size; i++){
277 sum+=array[i];
278 }
279 return sum;
280 }
281
282 public final double sum(){
283 double sum=0;
284 for(int i=0; i<size; i++){
285 sum+=array[i];
286 }
287 return sum;
288 }
289
290 public double percentile(double fraction){
291 if(size<1){return 0;}
292 int idx=percentileIndex(fraction);
293 return array[idx];
294 }
295
296 public int percentileIndex(double fraction){
297 if(size<2){return size-1;}
298 assert(sorted());
299 double target=(sum()*fraction);
300 double sum=0;
301 for(int i=0; i<size; i++){
302 sum+=array[i];
303 if(sum>=target){
304 return i;
305 }
306 }
307 return size-1;
308 }
309
310
311
312 public final void shrinkToUnique(){
313 condense();
314 shrink();
315 }
316
317 //In-place.
318 //Assumes sorted.
319 public final void condense(){
320 if(size<=1){return;}
321
322 int i=0, j=1;
323 for(; j<size && array[i]<array[j]; i++, j++){}//skip while strictly ascending
324
325 int dupes=0;
326 for(; j<size; j++){//This only enters at the first nonascending pair
327 int a=array[i], b=array[j];
328 assert(a<=b) : "Unsorted: "+i+", "+j+", "+a+", "+b;
329 if(b>a){
330 i++;
331 array[i]=b;
332 }else{
333 //do nothing
334 dupes++;
335 assert(a==b);
336 }
337 }
338 assert(dupes==(size-(i+1)));
339 assert(size>=(i+1));
340 size=i+1;
341 }
342
343 /*--------------------------------------------------------------*/
344 /*---------------- Reading ----------------*/
345 /*--------------------------------------------------------------*/
346
347 public final int get(int loc){
348 return(loc>=size ? 0 : array[loc]);//TODO: Shouldn't this crash instead of returning 0?
349 }
350
351 public int lastElement() {
352 assert(size>0);
353 return array[size-1];
354 }
355
356 //Slow; for validation
357 //Does not assume sorted
358 public boolean containsDuplicates(){
359 for(int i=0; i<size; i++){
360 for(int j=i+1; j<size; j++){
361 if(array[i]==array[j]){return true;}
362 }
363 }
364 return false;
365 }
366
367 public boolean contains(int x) {
368 for(int i=0; i<size; i++){
369 if(array[i]==x){return true;}
370 }
371 return false;
372 }
373
374 public int[] toArray(){
375 return KillSwitch.copyOf(array, size);
376 }
377
378 /** Assumes this is sorted.
379 * Reduces the list to a set of unique values;
380 * stores their counts in a second list. */
381 public void getUniqueCounts(IntList counts) {
382 counts.size=0;
383 if(size<=0){return;}
384
385 int unique=1;
386 int count=1;
387
388 for(int i=1; i<size; i++){
389 assert(array[i]>=array[i-1]);
390 if(array[i]==array[i-1]){
391 count++;
392 }else{
393 array[unique]=array[i];
394 unique++;
395 counts.add(count);
396 count=1;
397 }
398 }
399 if(count>0){
400 counts.add(count);
401 }
402 size=unique;
403 assert(counts.size==size);
404 }
405
406 public boolean sorted(){//slow
407 for(int i=1; i<size; i++){
408 if(array[i]<array[i-1]){return false;}
409 }
410 return true;
411 }
412
413 public int size() {
414 return size;
415 }
416
417 public boolean isEmpty() {
418 return size<1;
419 }
420
421 public int capacity() {
422 return array.length;
423 }
424
425 public int freeSpace() {
426 return array.length-size;
427 }
428
429 /*--------------------------------------------------------------*/
430 /*---------------- ToString ----------------*/
431 /*--------------------------------------------------------------*/
432
433 @Override
434 public String toString(){
435 return toStringListView();
436 }
437
438 public String toStringSetView(){
439 StringBuilder sb=new StringBuilder();
440 sb.append('[');
441 String comma="";
442 for(int i=0; i<size; i++){
443 if(array[i]!=0){
444 sb.append(comma+"("+i+", "+array[i]+")");
445 comma=", ";
446 }
447 }
448 sb.append(']');
449 return sb.toString();
450 }
451
452 public String toStringListView(){
453 StringBuilder sb=new StringBuilder();
454 sb.append('[');
455 String comma="";
456 for(int i=0; i<size; i++){
457 sb.append(comma+array[i]);
458 comma=", ";
459 }
460 sb.append(']');
461 return sb.toString();
462 }
463
464 /*--------------------------------------------------------------*/
465 /*---------------- Static Methods ----------------*/
466 /*--------------------------------------------------------------*/
467
468 private static final int min(int x, int y){return x<y ? x : y;}
469 private static final int max(int x, int y){return x>y ? x : y;}
470
471 /*--------------------------------------------------------------*/
472 /*---------------- Fields ----------------*/
473 /*--------------------------------------------------------------*/
474
475 /** Holds values. */
476 public int[] array;
477 /** Number of values in the primary array. */
478 public int size=0;
479
480 }