| 1 |
package structures; |
| 2 |
|
| 3 |
import java.util.Arrays; |
| 4 |
|
| 5 |
import shared.KillSwitch; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public final class IntList3{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public IntList3(){this(defaultInitialSize, defaultMode);} |
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public final void add(int[] entry, int len){ |
| 48 |
set(size, entry, len); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 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){ |
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
public final int insertIntoList(final int v, final int loc){ |
| 68 |
|
| 69 |
|
| 70 |
if(loc>=size || entries[loc]==null){ |
| 71 |
|
| 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 |
|
| 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); |
| 83 |
assert(entry[i]!=INVALID); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 95 |
assert(entry[oldSize]==INVALID); |
| 96 |
entry[oldSize]=v; |
| 97 |
sizes[loc]++; |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public final void resize(int size2){ |
| 107 |
assert(size2>size); |
| 108 |
entries=KillSwitch.copyOf(entries, size2); |
| 109 |
sizes=KillSwitch.copyOf(sizes, size2); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 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 |
|
| 121 |
|
| 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); |
| 127 |
for(int i=0; i<size; i++){ |
| 128 |
if(sizes[i]>slowAddLimit){ |
| 129 |
shrinkToUnique(i); |
| 130 |
} |
| 131 |
} |
| 132 |
shrunk=true; |
| 133 |
assert(readyToUse()); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 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;} |
| 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 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public final int[] get(int loc){ |
| 181 |
return(loc>=size ? null : entries[loc]); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
public int getLen(int i) { |
| 186 |
return i>=size ? 0 : sizes[i]; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 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){ |
| 200 |
sb.append(comma+"("+i+", "+Arrays.toString(entries[i])+")"); |
| 201 |
comma=", "; |
| 202 |
} |
| 203 |
} |
| 204 |
sb.append(']'); |
| 205 |
return sb.toString(); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 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 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private int[][] entries; |
| 222 |
|
| 223 |
private int[] sizes; |
| 224 |
|
| 225 |
public int size=0; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
private boolean shrunk=false; |
| 230 |
|
| 231 |
|
| 232 |
private final int mode; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static final int INVALID=-1; |
| 240 |
|
| 241 |
|
| 242 |
public static final int slowAddLimit=4; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public static final int ASCENDING=1; |
| 250 |
|
| 251 |
public static final int UNIQUE=2; |
| 252 |
|
| 253 |
|
| 254 |
public static final int DISORDERED=3; |
| 255 |
|
| 256 |
|
| 257 |
public static int defaultMode=ASCENDING; |
| 258 |
public static int defaultInitialSize=256; |
| 259 |
|
| 260 |
} |