Skip to content

Commit b467d29

Browse files
committed
Fix LGTM warning, potential int overflow bug
1 parent 7e4eb24 commit b467d29

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

jctools-core/src/main/java/org/jctools/maps/NonBlockingSetInt.java

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -63,42 +63,42 @@ private final boolean CAS_nbsi( NBSI old, NBSI nnn ) {
6363
private transient NBSI _nbsi;
6464

6565
/** Create a new empty bit-vector */
66-
public NonBlockingSetInt( ) {
66+
public NonBlockingSetInt( ) {
6767
_nbsi = new NBSI(63, new ConcurrentAutoTable(), this); // The initial 1-word set
6868
}
6969

70-
/**
70+
/**
7171
* Add {@code i} to the set. Uppercase {@link Integer} version of add,
7272
* requires auto-unboxing. When possible use the {@code int} version of
7373
* {@link #add(int)} for efficiency.
7474
* @throws IllegalArgumentException if i is negative.
7575
* @return <tt>true</tt> if i was added to the set.
7676
*/
77-
public boolean add ( final Integer i ) {
78-
return add(i.intValue());
77+
public boolean add ( final Integer i ) {
78+
return add(i.intValue());
7979
}
80-
/**
80+
/**
8181
* Test if {@code o} is in the set. This is the uppercase {@link Integer}
8282
* version of contains, requires a type-check and auto-unboxing. When
8383
* possible use the {@code int} version of {@link #contains(int)} for
8484
* efficiency.
8585
* @return <tt>true</tt> if i was in the set.
8686
*/
87-
public boolean contains( final Object o ) {
87+
public boolean contains( final Object o ) {
8888
return o instanceof Integer && contains(((Integer) o).intValue());
8989
}
90-
/**
90+
/**
9191
* Remove {@code o} from the set. This is the uppercase {@link Integer}
9292
* version of remove, requires a type-check and auto-unboxing. When
9393
* possible use the {@code int} version of {@link #remove(int)} for
9494
* efficiency.
9595
* @return <tt>true</tt> if i was removed to the set.
9696
*/
97-
public boolean remove( final Object o ) {
97+
public boolean remove( final Object o ) {
9898
return o instanceof Integer && remove(((Integer) o).intValue());
9999
}
100100

101-
/**
101+
/**
102102
* Add {@code i} to the set. This is the lower-case '{@code int}' version
103103
* of {@link #add} - no autoboxing. Negative values throw
104104
* IllegalArgumentException.
@@ -109,20 +109,20 @@ public boolean add( final int i ) {
109109
RangeUtil.checkPositiveOrZero(i, "i");
110110
return _nbsi.add(i);
111111
}
112-
/**
112+
/**
113113
* Test if {@code i} is in the set. This is the lower-case '{@code int}'
114114
* version of {@link #contains} - no autoboxing.
115115
* @return <tt>true</tt> if i was int the set.
116116
*/
117117
public boolean contains( final int i ) { return i >= 0 && _nbsi.contains(i); }
118-
/**
118+
/**
119119
* Remove {@code i} from the set. This is the fast lower-case '{@code int}'
120120
* version of {@link #remove} - no autoboxing.
121121
* @return <tt>true</tt> if i was added to the set.
122122
*/
123123
public boolean remove ( final int i ) { return i >= 0 && _nbsi.remove(i); }
124-
125-
/**
124+
125+
/**
126126
* Current count of elements in the set. Due to concurrent racing updates,
127127
* the size is only ever approximate. Updates due to the calling thread are
128128
* immediately visible to calling thread.
@@ -132,7 +132,7 @@ public boolean add( final int i ) {
132132
/** Approx largest element in set; at least as big (but max might be smaller). */
133133
public int length() { return _nbsi._bits.length<<6; }
134134
/** Empty the bitvector. */
135-
public void clear ( ) {
135+
public void clear ( ) {
136136
NBSI cleared = new NBSI(63, new ConcurrentAutoTable(), this); // An empty initial NBSI
137137
while( !CAS_nbsi( _nbsi, cleared ) ) // Spin until clear works
138138
;
@@ -153,26 +153,26 @@ private class iter implements Iterator<Integer> {
153153
int _prev = -1;
154154
iter() { _nbsi2 = _nbsi; advance(); }
155155
public boolean hasNext() { return _idx != -2; }
156-
private void advance() {
156+
private void advance() {
157157
while( true ) {
158158
_idx++; // Next index
159159
while( (_idx>>6) >= _nbsi2._bits.length ) { // Index out of range?
160160
if( _nbsi2._new == null ) { // New table?
161161
_idx = -2; // No, so must be all done
162-
return; //
162+
return; //
163163
}
164164
_nbsi2 = _nbsi2._new; // Carry on, in the new table
165165
}
166166
if( _nbsi2.contains(_idx) ) return;
167167
}
168168
}
169-
public Integer next() {
169+
public Integer next() {
170170
if( _idx == -1 ) throw new NoSuchElementException();
171171
_prev = _idx;
172172
advance();
173173
return _prev;
174174
}
175-
public void remove() {
175+
public void remove() {
176176
if( _prev == -1 ) throw new IllegalStateException();
177177
_nbsi2.remove(_prev);
178178
_prev = -1;
@@ -186,10 +186,10 @@ private void writeObject(java.io.ObjectOutputStream s) throws IOException {
186186
final NBSI nbsi = _nbsi; // The One Field is transient
187187
final int len = _nbsi._bits.length<<6;
188188
s.writeInt(len); // Write max element
189-
for( int i=0; i<len; i++ )
189+
for( int i=0; i<len; i++ )
190190
s.writeBoolean( _nbsi.contains(i) );
191191
}
192-
192+
193193
// --- readObject --------------------------------------------------------
194194
// Read a CHM from a stream
195195
private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
@@ -216,7 +216,7 @@ private static final class NBSI {
216216
private static final int _Lscale = UNSAFE.arrayIndexScale(long[].class);
217217
private static long rawIndex(final long[] ary, final int idx) {
218218
assert idx >= 0 && idx < ary.length;
219-
return _Lbase + idx * _Lscale;
219+
return _Lbase + (idx * (long)_Lscale);
220220
}
221221
private final boolean CAS( int idx, long old, long nnn ) {
222222
return UNSAFE.compareAndSwapLong( _bits, rawIndex(_bits, idx), old, nnn );
@@ -246,9 +246,9 @@ private final boolean CAS_new( NBSI nnn ) {
246246
// Every 64th bit is put in it's own recursive bitvector. If the low 6 bits
247247
// are all set, we shift them off and recursively operate on the _nbsi64 set.
248248
private final NBSI _nbsi64;
249-
250-
private NBSI( int max_elem, ConcurrentAutoTable ctr, NonBlockingSetInt nonb ) {
251-
super();
249+
250+
private NBSI( int max_elem, ConcurrentAutoTable ctr, NonBlockingSetInt nonb ) {
251+
super();
252252
_non_blocking_set_int = nonb;
253253
_size = ctr;
254254
_copyIdx = ctr == null ? null : new AtomicInteger();
@@ -260,13 +260,13 @@ private NBSI( int max_elem, ConcurrentAutoTable ctr, NonBlockingSetInt nonb ) {
260260
_nbsi64 = ((max_elem+1)>>>6) == 0 ? null : new NBSI((max_elem+1)>>>6, null, null);
261261
_sum_bits_length = _bits.length + (_nbsi64==null ? 0 : _nbsi64._sum_bits_length);
262262
}
263-
263+
264264
// Lower-case 'int' versions - no autoboxing, very fast.
265265
// 'i' is known positive.
266266
public boolean add( final int i ) {
267267
// Check for out-of-range for the current size bit vector.
268268
// If so we need to grow the bit vector.
269-
if( (i>>6) >= _bits.length )
269+
if( (i>>6) >= _bits.length )
270270
return install_larger_new_bits(i). // Install larger pile-o-bits (duh)
271271
help_copy().add(i); // Finally, add to the new table
272272

@@ -280,7 +280,7 @@ public boolean add( final int i ) {
280280

281281
final long mask = mask(j);
282282
long old;
283-
do {
283+
do {
284284
old = nbsi._bits[j>>6]; // Read old bits
285285
if( old < 0 ) // Not mutable?
286286
// Not mutable: finish copy of word, and retry on copied word
@@ -305,7 +305,7 @@ public boolean remove( final int i ) {
305305

306306
final long mask = mask(j);
307307
long old;
308-
do {
308+
do {
309309
old = nbsi._bits[j>>6]; // Read old bits
310310
if( old < 0 ) // Not mutable?
311311
// Not mutable: finish copy of word, and retry on copied word
@@ -315,8 +315,8 @@ public boolean remove( final int i ) {
315315
_size.add(-1);
316316
return true;
317317
}
318-
319-
public boolean contains( final int i ) {
318+
319+
public boolean contains( final int i ) {
320320
if( (i>>6) >= _bits.length ) // Out of bounds? Not in this array!
321321
return _new != null && help_copy().contains(i);
322322

@@ -334,9 +334,9 @@ public boolean contains( final int i ) {
334334
// Not mutable: finish copy of word, and retry on copied word
335335
return help_copy_impl(i).help_copy().contains(i);
336336
// Yes mutable: test & return bit
337-
return (old & mask) != 0;
337+
return (old & mask) != 0;
338338
}
339-
339+
340340
public int size() { return (int)_size.get(); }
341341

342342
// Must grow the current array to hold an element of size i
@@ -385,7 +385,7 @@ private NBSI help_copy() {
385385
return _new;
386386
}
387387

388-
// Help copy this one word. State Machine.
388+
// Help copy this one word. State Machine.
389389
// (1) If not "made immutable" in the old array, set the sign bit to make
390390
// it immutable.
391391
// (2) If non-zero in old array & zero in new, CAS new from 0 to copy-of-old
@@ -413,7 +413,7 @@ private NBSI help_copy_impl( int i ) {
413413
if( old.CAS( j>>6, oldbits, bits ) ) {
414414
if( oldbits == 0 ) _copyDone.addAndGet(1);
415415
break; // Success - old array word is now immutable
416-
}
416+
}
417417
bits = old._bits[j>>6]; // Retry if CAS failed
418418
}
419419

@@ -434,7 +434,7 @@ private NBSI help_copy_impl( int i ) {
434434
if( old.CAS( j>>6, bits, mask(63) ) )
435435
_copyDone.addAndGet(1); // One more word finished copying
436436
}
437-
437+
438438
// Now in state 4: zero (and immutable) in old
439439

440440
// Return the self bitvector for 'fluid' programming style
@@ -456,7 +456,7 @@ private void print(int d) {
456456
x = x._nbsi64;
457457
}
458458
print(d,buf.toString());
459-
459+
460460
x = this;
461461
while( x != null ) {
462462
for( int i=0; i<x._bits.length; i++ )
@@ -472,5 +472,5 @@ private void print(int d) {
472472
_new.print(d+1);
473473
}
474474
}
475-
}
475+
}
476476
}

0 commit comments

Comments
 (0)