Debian Codegraph @

current/structures/LongListSet.java

raw symbols SCIP
1 package structures;
2
3 import java.util.Arrays;
4
5 import shared.Shared;
6 import shared.Tools;
7
8 /**
9 * A set of LongLists, designed to increase LongList capacity beyond 2B.
10 * Auto-condenses; e.g., not intended to represent multiple copies of a value.
11 *
12 * @author Brian Bushnell
13 * @date January 8, 2021
14 *
15 */
16 public class LongListSet{
17
18 public static void main(String[] args){
19 LongListSet set=new LongListSet();
20 set.add(1);
21 set.add(2);
22 set.add(3);
23 set.add(4);
24 set.add(5);
25 set.add(2);
26 set.add(2);
27 set.add(5);
28 System.err.println(set);
29 set.sort();
30 set.condense();
31 System.err.println(set);
32 }
33
34 public String toString(){
35 LongListSetIterator iter=iterator();
36 ByteBuilder bb=new ByteBuilder();
37 bb.append('[');
38 while(iter.hasMore()){
39 long x=iter.next();
40 bb.append(x);
41 bb.append(',');
42 }
43 if(bb.endsWith(',')){bb.setLength(bb.length-1);}
44 bb.append(']');
45 return bb.toString();
46 }
47
48 public LongListSet(){
49 array=new LongList[mod];
50 for(int i=0; i<mod; i++){
51 array[i]=new LongList(32);
52 }
53 nextCondense=new int[mod];
54 Arrays.fill(nextCondense, 64);
55 }
56
57 public void add(long x){
58 int y=(int)((x&Long.MAX_VALUE)%mod);
59 LongList list=array[y];
60 list.add(x);
61 if(list.size>=nextCondense[y]){
62 list.sort();
63 list.condense();
64 nextCondense[y]=(int)Tools.mid(nextCondense[y], list.size*2L, Shared.MAX_ARRAY_LEN);
65 }else{
66 sorted=false;
67 }
68 }
69
70 public void sort(){
71 if(sorted){return;}
72 for(LongList list : array){list.sort();}
73 sorted=true;
74 }
75
76 public void condense(){
77 assert(sorted) : "Sort first.";
78 for(LongList list : array){list.condense();}
79 }
80
81 public void shrinkToUnique(){
82 for(LongList list : array){list.shrinkToUnique();}
83 }
84
85 public LongListSetIterator iterator(){
86 return new LongListSetIterator();
87 }
88
89 private boolean sorted=false;
90
91 public final LongList[] array;
92 public final int[] nextCondense;
93
94 public static final int mod=3;
95
96 public class LongListSetIterator{
97
98 //Assumes hasMore() has already been called and returned true
99 public long next(){
100 long x=array[i].get(j);
101 j++;
102 return x;
103 }
104
105 public boolean hasMore(){
106 return findNextValid();
107 }
108
109 /**
110 * Increment and point to next valid element.
111 * @return true if there is a valid element.
112 */
113 boolean advance(){
114 j++;
115 return findNextValid();
116 }
117
118 /**
119 * Point to next valid element.
120 * If already valid, do nothing.
121 * @return true if there is a valid element.
122 */
123 boolean findNextValid(){
124 if(i<mod && j<array[i].size){return true;}//common case
125 while(i<mod){
126 if(j<array[i].size){return true;}
127 i++;
128 j=0;
129 }
130 return false;
131 }
132 private int i=0, j=0;
133
134 }
135
136 }