Debian Codegraph @

current/structures/LongList2.java

raw symbols SCIP
1 package structures;
2
3 import java.util.Arrays;
4
5 import shared.KillSwitch;
6
7
8 /**
9 * Similar to an ArrayList<long[]> but intended to hold sets.
10 * Faster insert could be handled via binary search if sorted,
11 * or binary search for the last filled position if
12 * all elements are unique. */
13 public final class LongList2{
14
15 /*--------------------------------------------------------------*/
16 /*---------------- Initialization ----------------*/
17 /*--------------------------------------------------------------*/
18
19 /** Re-call with default initial size. */
20 public LongList2(){this(256);}
21
22 /** Construct an IntList3 with this initial size.*/
23 public LongList2(int initialSize){
24 assert(initialSize>0);
25 entries=new long[initialSize][];
26 }
27
28 /*--------------------------------------------------------------*/
29 /*---------------- Mutation ----------------*/
30 /*--------------------------------------------------------------*/
31
32 /** Add this entry to the end of the list */
33 public final void add(long[] entry){
34 if(size>=entries.length){
35 resize(max(size*2, 1));
36 }
37 entries[size]=entry;
38 size++;
39 }
40
41 /** Added for better IntList3 compatibility */
42 public final void add(long[] entry, int len){
43 if(size>=entries.length){
44 resize(max(size*2, 1));
45 }
46 entries[size]=entry;
47 size++;
48 }
49
50 /** Set this location to specified entry */
51 public final void set(int loc, long[] entry){
52 if(loc>=entries.length){
53 resize((loc+1)*2);
54 }
55 entries[loc]=entry;
56 size=max(size, loc+1);
57 }
58
59 /**
60 * Add this value to the specified location.
61 * If an entry exists, insert the value, enlarging if necessary.
62 * Otherwise, create a new entry. */
63 public final int insertIntoList(final long v, final int loc){
64
65 if(loc>=size){
66 assert(loc==size);
67 add(null);
68 }
69
70 long[] entry=get(loc);
71 if(entry==null){
72 set(loc, new long[] {v, INVALID});
73 return 1;
74 }
75
76 for(int i=0; i<entry.length; i++){//This is the slow bit; accelerate by hopping to the middle
77 if(entry[i]==v){return 0;}
78 if(entry[i]==INVALID){entry[i]=v;return 1;}
79 }
80
81 final int oldSize=entry.length;
82 entry=KillSwitch.copyAndFill(entry, oldSize*2L, INVALID);
83 set(loc, entry);
84
85 //Quick add
86 assert(entry[oldSize]==INVALID);
87 entry[oldSize]=v;
88 return 1;
89
90 //Old code
91 // final int oldSize=entry.length;
92 // final int newSize=(int)Tools.min(Shared.MAX_ARRAY_LEN, oldSize*2L);
93 // assert(newSize>entry.length) : "Overflow.";
94 // entry=KillSwitch.copyOf(entry, newSize);
95 // entry[oldSize]=v;
96 // Arrays.fill(entry, oldSize+1, newSize, -1);
97 // set(loc, entry);
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);
109 }
110
111 /** Compress the list by eliminating the unused trailing space */
112 public final void shrink(){
113 if(size==entries.length){return;}
114 entries=KillSwitch.copyOf(entries, size);
115 }
116
117 /*--------------------------------------------------------------*/
118 /*---------------- Reading ----------------*/
119 /*--------------------------------------------------------------*/
120
121 /** Get the entry at the location */
122 public final long[] get(int loc){
123 return(loc>=size ? null : entries[loc]);
124 }
125
126 /** Added for better IntList3 compatibility */
127 public int getLen(int i) {
128 return i>=size ? 0 : entries[i]==null ? 0 : -1;
129 }
130
131 /*--------------------------------------------------------------*/
132 /*---------------- ToString ----------------*/
133 /*--------------------------------------------------------------*/
134
135 @Override
136 public String toString(){
137 StringBuilder sb=new StringBuilder();
138 sb.append('[');
139 String comma="";
140 for(int i=0; i<size; i++){
141 if(entries[i]!=null){
142 sb.append(comma+"("+i+", "+Arrays.toString(entries[i])+")");
143 comma=", ";
144 }
145 }
146 sb.append(']');
147 return sb.toString();
148 }
149
150 /*--------------------------------------------------------------*/
151 /*---------------- Static Methods ----------------*/
152 /*--------------------------------------------------------------*/
153
154 private static final int min(int x, int y){return x<y ? x : y;}
155 private static final int max(int x, int y){return x>y ? x : y;}
156
157 /*--------------------------------------------------------------*/
158 /*---------------- Fields ----------------*/
159 /*--------------------------------------------------------------*/
160
161 /** Holds entries. Each entry is a sets of numbers in an long[].
162 * Leftmost values are valid, rightmost values are invalid. */
163 private long[][] entries;
164 /** Number of entries in the primary array. */
165 public int size=0;
166
167 /*--------------------------------------------------------------*/
168 /*---------------- Static Fields ----------------*/
169 /*--------------------------------------------------------------*/
170
171 /** This could be made mutable per instance, but it would be a lot of work. */
172 public static final int INVALID=-1;
173
174 }