Debian Codegraph @

current/structures/IntList3.java

raw symbols SCIP
1 package structures;
2
3 import java.util.Arrays;
4
5 import shared.KillSwitch;
6
7
8 /**
9 * Like IntList2 but has a size for each array independent of its length.
10 * This is similar to a list of IntList but more efficient.
11 * Designed for use with HashArrayHybridFast.
12 * Assumes each entry is a set with all adds either ascending (Seal)
13 * or unique (Sketch).
14 *
15 * IntList3 does not extend IntList2 because virtual functions might be slower,
16 * but it would make the code more concise.
17 *
18 * @author Brian Bushnell
19 * @date Dec 10, 2018
20 *
21 */
22 public final class IntList3{
23
24 /*--------------------------------------------------------------*/
25 /*---------------- Initialization ----------------*/
26 /*--------------------------------------------------------------*/
27
28 /** Re-call with default initial size and mode. */
29 public IntList3(){this(defaultInitialSize, defaultMode);}
30
31 /** Construct an IntList3 with this initial size.
32 * Setting the proper mode is the caller's responsibility. */
33 public IntList3(int initialSize, int mode_){
34 assert(initialSize>0);
35 entries=new int[initialSize][];
36 sizes=new int[initialSize];
37
38 mode=mode_;
39 assert(mode==ASCENDING || mode==UNIQUE) : "Unsupported mode: "+mode;
40 }
41
42 /*--------------------------------------------------------------*/
43 /*---------------- Mutation ----------------*/
44 /*--------------------------------------------------------------*/
45
46 /** Add this entry to the end of the list */
47 public final void add(int[] entry, int len){
48 set(size, entry, len);
49 }
50
51 /** Set this location to specified entry */
52 public final void set(int loc, int[] entry, int len){
53 assert((entry==null && len==0) || (entry.length>0 && len<=entry.length)) : len+", "+(entry==null ? entry : ""+entry.length);
54
55 if(loc>=entries.length){//Resize by doubling if necessary
56 resize(max(size*2, loc+1));
57 }
58 entries[loc]=entry;
59 sizes[loc]=len;
60 size=max(size, loc+1);
61 }
62
63 /**
64 * Add this value to the specified location.
65 * If an entry exists, insert the value, enlarging if necessary.
66 * Otherwise, create a new entry. */
67 public final int insertIntoList(final int v, final int loc){
68
69 //If the location is empty
70 if(loc>=size || entries[loc]==null){
71 //Add a new entry and return
72 set(loc, new int[] {v, INVALID}, 1);
73 return 1;
74 }
75
76 int[] entry=get(loc);
77 final int oldSize=sizes[loc];
78
79 //Scan the latest entries to see if v is already present.
80 for(int i=oldSize-1, lim=max(0, oldSize-slowAddLimit); i>=lim; i--){
81 if(entry[i]==v){return 0;}
82 assert(entry[i]<v || entry[i]!=v); //Ascending, the assumption for Seal; unique, the assumption for Sketch
83 assert(entry[i]!=INVALID);
84 }
85 //At this point the element was not found because it was not present or the size is too big
86
87 //If the entry is full, resize it
88 if(oldSize>=entry.length){
89 assert(oldSize==entry.length);
90 entry=KillSwitch.copyAndFill(entry, oldSize*2L, INVALID);
91 set(loc, entry, sizes[loc]);
92 }
93
94 //Quick add
95 assert(entry[oldSize]==INVALID);
96 entry[oldSize]=v;
97 sizes[loc]++;
98 return 1;
99 }
100
101 /*--------------------------------------------------------------*/
102 /*---------------- Resizing ----------------*/
103 /*--------------------------------------------------------------*/
104
105 /** Resize the array of entries */
106 public final void resize(int size2){
107 assert(size2>size);
108 entries=KillSwitch.copyOf(entries, size2);//TODO: Safe copy to prevent memory exception
109 sizes=KillSwitch.copyOf(sizes, size2);
110 }
111
112 /** Compress the list by eliminating the unused trailing space */
113 public final void shrink(){
114 if(size==entries.length){return;}
115 entries=KillSwitch.copyOf(entries, size);
116 sizes=KillSwitch.copyOf(sizes, size);
117 }
118
119 /**
120 * Provided for supporting disordered additions; not currently used.
121 * Make each entry unique and minimal-length.
122 * */
123 public final void shrinkToUnique(){
124 assert(false) : "TODO: This function has not been tested and is not expected to be used.";
125 assert(!shrunk);
126 assert(mode==DISORDERED);//Not really necessary
127 for(int i=0; i<size; i++){
128 if(sizes[i]>slowAddLimit){//Under this limit everything is already unique
129 shrinkToUnique(i);
130 }
131 }
132 shrunk=true;
133 assert(readyToUse());
134 }
135
136 /**
137 * Provided for supporting disordered additions; not currently used.
138 * I could redo this so that the sets become packed and the size changes without creating a new array.
139 * */
140 private void shrinkToUnique(int loc){
141 final int[] entry=entries[loc];
142 final int oldSize=sizes[loc];
143 assert(oldSize>1);
144 Arrays.sort(entry, 0, oldSize);
145 int unique=0;
146 int prev=INVALID;
147 for(int i=0; i<oldSize; i++){
148 assert(entry[i]>=0);
149 int current=entry[i];
150 if(current!=prev){
151 unique++;
152 }
153 prev=current;
154 }
155 if(unique==oldSize){return;} //No need to condense aside from saving a little RAM
156
157 int[] set2=new int[unique];
158 unique=0;
159 prev=INVALID;
160 for(int i=0; i<oldSize; i++){
161 int current=entry[i];
162 if(current!=prev){
163 set2[unique]=current;
164 unique++;
165 }
166 prev=current;
167 }
168 set(loc, set2, unique);
169 }
170
171 private boolean readyToUse(){
172 return shrunk || mode==ASCENDING || mode==UNIQUE;
173 }
174
175 /*--------------------------------------------------------------*/
176 /*---------------- Reading ----------------*/
177 /*--------------------------------------------------------------*/
178
179 /** Get the entry at the location */
180 public final int[] get(int loc){
181 return(loc>=size ? null : entries[loc]);
182 }
183
184 /** Added for better IntList2 compatibility */
185 public int getLen(int i) {
186 return i>=size ? 0 : sizes[i];
187 }
188
189 /*--------------------------------------------------------------*/
190 /*---------------- ToString ----------------*/
191 /*--------------------------------------------------------------*/
192
193 @Override
194 public String toString(){
195 StringBuilder sb=new StringBuilder();
196 sb.append('[');
197 String comma="";
198 for(int i=0; i<size; i++){
199 if(entries[i]!=null){//Could be improved to use sizes
200 sb.append(comma+"("+i+", "+Arrays.toString(entries[i])+")");
201 comma=", ";
202 }
203 }
204 sb.append(']');
205 return sb.toString();
206 }
207
208 /*--------------------------------------------------------------*/
209 /*---------------- Static Methods ----------------*/
210 /*--------------------------------------------------------------*/
211
212 private static final int min(int x, int y){return x<y ? x : y;}
213 private static final int max(int x, int y){return x>y ? x : y;}
214
215 /*--------------------------------------------------------------*/
216 /*---------------- Fields ----------------*/
217 /*--------------------------------------------------------------*/
218
219 /** Holds entries. Each entry is a sets of numbers in an int[].
220 * Leftmost values are valid, rightmost values are invalid. */
221 private int[][] entries;
222 /** Number of values in each entry. */
223 private int[] sizes;
224 /** Number of entries in the primary array. */
225 public int size=0;
226
227 /** True after shrinkToUnique has been called.
228 * Currently unused. */
229 private boolean shrunk=false;
230
231 /** Preconditions for adding values */
232 private final int mode;
233
234 /*--------------------------------------------------------------*/
235 /*---------------- Static Fields ----------------*/
236 /*--------------------------------------------------------------*/
237
238 /** This could be made mutable per instance, but it would be a lot of work. */
239 public static final int INVALID=-1;
240
241 /** Only scan back this far for duplicates when adding values */
242 public static final int slowAddLimit=4;
243
244 /*--------------------------------------------------------------*/
245 /*---------------- Modes ----------------*/
246 /*--------------------------------------------------------------*/
247
248 /** All adds to an entry must be nondescending */
249 public static final int ASCENDING=1;
250 /** All adds to an entry must be unique */
251 public static final int UNIQUE=2;
252 /** No requirements for adds.
253 * To ensure set functionality, shrinkToUnique should be called before use. */
254 public static final int DISORDERED=3;
255
256 /** Should be set prior to creation, e.g. by Seal or Sketch */
257 public static int defaultMode=ASCENDING;
258 public static int defaultInitialSize=256;
259
260 }