2525package jdk .graal .compiler .lir .alloc .lsra ;
2626
2727import java .util .Arrays ;
28- import java .util .BitSet ;
29-
30- import jdk .graal .compiler .debug .Assertions ;
3128
3229/**
3330 * This class implements a very simple sparse bitset that's optimized for the measured usage by
3431 * linear scan. In practice the bitset cardinality is at most 60 elements while the range of values
3532 * in the set can be quite large, reaching over 100000 in extreme cases. Since lifetime analysis
3633 * uses 4 bitsets per block and the number of blocks can also be very large, space efficiency can
37- * plays a major role in performance. A simple sorted list of integers performs significantly better
34+ * play a major role in performance. A simple sorted list of integers performs significantly better
3835 * for large numbers of intervals and blocks while only modestly affecting the perform for small
3936 * values.
4037 */
4138public class SparseBitSet {
4239
43- /**
44- * A bitset used to verify the operation of this set.
45- */
46- private BitSet sanity ;
47-
4840 /**
4941 * The number of elements in the set.
5042 */
@@ -55,51 +47,25 @@ public class SparseBitSet {
5547 */
5648 protected int [] elements ;
5749
58- private boolean checkBitSet () {
59- if (sanity != null ) {
60- assert sanity .cardinality () == cardinality () : sanity + " " + this ;
61- for (int bit = sanity .nextSetBit (0 ); bit >= 0 ; bit = sanity .nextSetBit (bit + 1 )) {
62- boolean found = false ;
63- for (int i = 0 ; i < size ; i ++) {
64- if (elements [i ] == bit ) {
65- found = true ;
66- break ;
67- }
68- }
69- assert found : sanity + " " + this ;
70- }
71- }
72- return true ;
73- }
50+ /**
51+ * Internal element iterator value used by {@link #iterateValues(int)}.
52+ */
53+ private int iterator = 0 ;
7454
7555 public SparseBitSet () {
7656 elements = new int [4 ];
77- if (Assertions .assertionsEnabled ()) {
78- sanity = new BitSet ();
79- }
8057 }
8158
8259 public SparseBitSet (SparseBitSet other ) {
8360 elements = other .elements .clone ();
8461 size = other .size ;
85- if (other .sanity != null ) {
86- sanity = (BitSet ) other .sanity .clone ();
87- }
8862 iterator = 0 ;
8963 }
9064
91- private boolean checkOr (SparseBitSet other ) {
92- if (sanity != null ) {
93- sanity .or (other .sanity );
94- checkBitSet ();
95- }
96- return true ;
97- }
98-
99- public void or (SparseBitSet other ) {
100- assert checkBitSet ();
101- assert other .checkBitSet ();
102-
65+ /**
66+ * Adds all elements from {@code other} to this set.
67+ */
68+ public void addAll (SparseBitSet other ) {
10369 if (size == 0 ) {
10470 if (elements .length < other .size ) {
10571 elements = other .elements .clone ();
@@ -138,8 +104,6 @@ public void or(SparseBitSet other) {
138104 size += newElements ;
139105 }
140106 }
141-
142- assert checkOr (other );
143107 }
144108
145109 private void ensureCapacity (int minCapacity ) {
@@ -148,30 +112,15 @@ private void ensureCapacity(int minCapacity) {
148112 }
149113 }
150114
151- /**
152- * Increases the capacity to ensure that it can hold at least the number of elements specified
153- * by the minimum capacity argument.
154- *
155- * @param minCapacity the desired minimum capacity
156- * @throws OutOfMemoryError if minCapacity is less than zero
157- */
158- private int [] grow (int minCapacity ) {
115+ private void grow (int minCapacity ) {
159116 int oldCapacity = elements .length ;
160- /* minimum growth */
161- /* preferred growth */
162- int newCapacity = oldCapacity + Math .max (minCapacity - oldCapacity , oldCapacity >> 1 );
163- return elements = Arrays .copyOf (elements , newCapacity );
164- }
165-
166- private boolean checkAndNot (SparseBitSet other ) {
167- if (sanity != null ) {
168- sanity .andNot (other .sanity );
169- checkBitSet ();
170- }
171- return true ;
117+ elements = Arrays .copyOf (elements , oldCapacity + Math .max (minCapacity - oldCapacity , oldCapacity >> 1 ));
172118 }
173119
174- public void andNot (SparseBitSet other ) {
120+ /**
121+ * Remove all the value that are seting {@ocde other} from this set.
122+ */
123+ public void removeAll (SparseBitSet other ) {
175124 int otherIndex = 0 ;
176125 int firstDeleted = -1 ;
177126 for (int index = 0 ; index < size && otherIndex < other .size ;) {
@@ -202,48 +151,47 @@ public void andNot(SparseBitSet other) {
202151 }
203152 size -= shift ;
204153 }
205- assert checkAndNot (other );
206154 }
207155
156+ /**
157+ * Returns the number of bits set to {@code true} in this set.
158+ */
208159 public int cardinality () {
209160 return size ;
210161 }
211162
212- public boolean get (int value ) {
213- return Arrays .binarySearch (elements , 0 , size , value ) >= 0 ;
214- }
215-
216- private boolean checkSet (int value ) {
217- if (value < 0 ) {
218- throw new IllegalArgumentException ();
219- }
220- if (sanity != null ) {
221- sanity .set (value );
222- checkBitSet ();
223- }
224- return true ;
163+ /**
164+ * Returns the bitIndex of the bit with the specified index. The bitIndex is {@code true} if the
165+ * bit with the index {@code bitIndex} is currently set in this {@code BitSet}; otherwise, the
166+ * result is {@code false}.
167+ */
168+ public boolean get (int bitIndex ) {
169+ return Arrays .binarySearch (elements , 0 , size , bitIndex ) >= 0 ;
225170 }
226171
227- public void set (int value ) {
228- int location = Arrays .binarySearch (elements , 0 , size , value );
172+ /**
173+ * Sets the bit at the specified index to {@code true}.
174+ */
175+ public void set (int bitIndex ) {
176+ int location = Arrays .binarySearch (elements , 0 , size , bitIndex );
229177 if (location < 0 ) {
230- insertAt (value , -(location + 1 ));
231- }
232- assert checkSet (value );
233- }
234-
235- private void insertAt (int value , int insertPoint ) {
236- if (size == elements .length ) {
237- grow (size + 1 );
178+ int insertPoint = -(location + 1 );
179+ if (size == elements .length ) {
180+ grow (size + 1 );
181+ }
182+ System .arraycopy (elements , insertPoint , elements , insertPoint + 1 , size - insertPoint );
183+ elements [insertPoint ] = bitIndex ;
184+ size ++;
238185 }
239- System .arraycopy (elements , insertPoint , elements , insertPoint + 1 , size - insertPoint );
240- elements [insertPoint ] = value ;
241- size ++;
242186 }
243187
244- private int iterator = 0 ;
245-
246- public int nextSetBit (int i ) {
188+ /**
189+ * Uses an internal iterator to visit the values in the set in order. {@code iterateValues(0)}
190+ * starts the iteration and returns the first value. Repeated calls of {@code iterateValues}
191+ * with non-zero values return the next element in the set. It returns -1 when there are no more
192+ * elements to visit.
193+ */
194+ public int iterateValues (int i ) {
247195 if (i == 0 ) {
248196 iterator = 0 ;
249197 }
@@ -253,11 +201,11 @@ public int nextSetBit(int i) {
253201 return -1 ;
254202 }
255203
204+ /**
205+ * Remove all values from the bit set.
206+ */
256207 public void clear () {
257208 size = 0 ;
258- if (Assertions .assertionsEnabled () && sanity != null ) {
259- sanity .clear ();
260- }
261209 }
262210
263211 @ Override
@@ -266,24 +214,22 @@ public boolean equals(Object obj) {
266214 if (size != bs .size ) {
267215 return false ;
268216 }
269- int result = Arrays .compare (elements , 0 , size , bs .elements , 0 , size );
270- assert sanity == null || sanity .equals (bs .sanity ) == (result == 0 ) : sanity + " " + bs .sanity ;
271- return result == 0 ;
217+ return Arrays .compare (elements , 0 , size , bs .elements , 0 , size ) == 0 ;
272218 }
273219 return false ;
274220 }
275221
276222 @ Override
277223 public String toString () {
278224 StringBuilder sb = new StringBuilder ();
279- sb .append ("set(size=" ). append ( size ). append ( ", {" );
225+ sb .append ("{" );
280226 for (int i = 0 ; i < size ; i ++) {
281227 if (i > 0 ) {
282228 sb .append (',' );
283229 }
284230 sb .append (elements [i ]);
285231 }
286- sb .append ("}) " );
232+ sb .append ("}" );
287233 return sb .toString ();
288234 }
289235
0 commit comments