private NBSI help_copy() { // Pick some words to help with - but only help copy the top-level NBSI. // Nested NBSI waits until the top is done before we start helping. NBSI top_nbsi = _non_blocking_set_int._nbsi; final int HELP = 8; // Tuning number: how much copy pain are we willing to inflict? // We "help" by forcing individual bit indices to copy. However, bits // come in lumps of 64 per word, so we just advance the bit counter by 64's. int idx = top_nbsi._copyIdx.getAndAdd(64*HELP); for( int i=0; i<HELP; i++ ) { int j = idx+i*64; j %= (top_nbsi._bits.length<<6); // Limit, wrap to array size; means we retry indices top_nbsi.help_copy_impl(j ); top_nbsi.help_copy_impl(j+63); // Also force the nested-by-64 bit } // Top level guy ready to promote? // Note: WE may not be the top-level guy! if( top_nbsi._copyDone.get() == top_nbsi._sum_bits_length ) // One shot CAS to promote - it may fail since we are racing; others // may promote as well if( _non_blocking_set_int.CAS_nbsi( top_nbsi, top_nbsi._new ) ) { //System.out.println("Promote at top level to size "+(_non_blocking_set_int._nbsi._bits.length<<6)); } // Return the new bitvector for 'fluid' programming style return _new; }
private NBSI help_copy() { // Pick some words to help with - but only help copy the top-level NBSI. // Nested NBSI waits until the top is done before we start helping. NBSI top_nbsi = _non_blocking_set_int._nbsi; final int HELP = 8; // Tuning number: how much copy pain are we willing to inflict? // We "help" by forcing individual bit indices to copy. However, bits // come in lumps of 64 per word, so we just advance the bit counter by 64's. int idx = top_nbsi._copyIdx.getAndAdd(64*HELP); for( int i=0; i<HELP; i++ ) { int j = idx+i*64; j %= (top_nbsi._bits.length<<6); // Limit, wrap to array size; means we retry indices top_nbsi.help_copy_impl(j ); top_nbsi.help_copy_impl(j+63); // Also force the nested-by-64 bit } // Top level guy ready to promote? // Note: WE may not be the top-level guy! if( top_nbsi._copyDone.get() == top_nbsi._sum_bits_length ) // One shot CAS to promote - it may fail since we are racing; others // may promote as well if( _non_blocking_set_int.CAS_nbsi( top_nbsi, top_nbsi._new ) ) { //System.out.println("Promote at top level to size "+(_non_blocking_set_int._nbsi._bits.length<<6)); } // Return the new bitvector for 'fluid' programming style return _new; }
public boolean add( final int i ) { // Check for out-of-range for the current size bit vector. // If so we need to grow the bit vector. if( (i>>6) >= _bits.length ) return install_larger_new_bits(i). // Install larger pile-o-bits (duh) help_copy().add(i); // Finally, add to the new table // Handle every 64th bit via using a nested array NBSI nbsi = this; // The bit array being added into int j = i; // The bit index being added while( (j&63) == 63 ) { // Bit 64? (low 6 bits are all set) nbsi = nbsi._nbsi64; // Recurse j = j>>6; // Strip off low 6 bits (all set) } final long mask = mask(j); long old; do { old = nbsi._bits[j>>6]; // Read old bits if( old < 0 ) // Not mutable? // Not mutable: finish copy of word, and retry on copied word return help_copy_impl(i).help_copy().add(i); if( (old & mask) != 0 ) return false; // Bit is already set? } while( !nbsi.CAS( j>>6, old, old | mask ) ); _size.add(1); return true; }
public boolean remove( final int i ) { if( (i>>6) >= _bits.length ) // Out of bounds? Not in this array! return _new != null && help_copy().remove(i); // Handle every 64th bit via using a nested array NBSI nbsi = this; // The bit array being added into int j = i; // The bit index being added while( (j&63) == 63 ) { // Bit 64? (low 6 bits are all set) nbsi = nbsi._nbsi64; // Recurse j = j>>6; // Strip off low 6 bits (all set) } final long mask = mask(j); long old; do { old = nbsi._bits[j>>6]; // Read old bits if( old < 0 ) // Not mutable? // Not mutable: finish copy of word, and retry on copied word return help_copy_impl(i).help_copy().remove(i); if( (old & mask) == 0 ) return false; // Bit is already clear? } while( !nbsi.CAS( j>>6, old, old & ~mask ) ); _size.add(-1); return true; }
public boolean contains( final int i ) { if( (i>>6) >= _bits.length ) // Out of bounds? Not in this array! return _new != null && help_copy().contains(i); // Handle every 64th bit via using a nested array NBSI nbsi = this; // The bit array being added into int j = i; // The bit index being added while( (j&63) == 63 ) { // Bit 64? (low 6 bits are all set) nbsi = nbsi._nbsi64; // Recurse j = j>>6; // Strip off low 6 bits (all set) } final long mask = mask(j); long old = nbsi._bits[j>>6]; // Read old bits if( old < 0 ) // Not mutable? // Not mutable: finish copy of word, and retry on copied word return help_copy_impl(i).help_copy().contains(i); // Yes mutable: test & return bit return (old & mask) != 0; }
public boolean add( final int i ) { // Check for out-of-range for the current size bit vector. // If so we need to grow the bit vector. if( (i>>6) >= _bits.length ) return install_larger_new_bits(i). // Install larger pile-o-bits (duh) help_copy().add(i); // Finally, add to the new table // Handle every 64th bit via using a nested array NBSI nbsi = this; // The bit array being added into int j = i; // The bit index being added while( (j&63) == 63 ) { // Bit 64? (low 6 bits are all set) nbsi = nbsi._nbsi64; // Recurse j = j>>6; // Strip off low 6 bits (all set) } final long mask = mask(j); long old; do { old = nbsi._bits[j>>6]; // Read old bits if( old < 0 ) // Not mutable? // Not mutable: finish copy of word, and retry on copied word return help_copy_impl(i).help_copy().add(i); if( (old & mask) != 0 ) return false; // Bit is already set? } while( !nbsi.CAS( j>>6, old, old | mask ) ); _size.add(1); return true; }
public boolean remove( final int i ) { if( (i>>6) >= _bits.length ) // Out of bounds? Not in this array! return _new != null && help_copy().remove(i); // Handle every 64th bit via using a nested array NBSI nbsi = this; // The bit array being added into int j = i; // The bit index being added while( (j&63) == 63 ) { // Bit 64? (low 6 bits are all set) nbsi = nbsi._nbsi64; // Recurse j = j>>6; // Strip off low 6 bits (all set) } final long mask = mask(j); long old; do { old = nbsi._bits[j>>6]; // Read old bits if( old < 0 ) // Not mutable? // Not mutable: finish copy of word, and retry on copied word return help_copy_impl(i).help_copy().remove(i); if( (old & mask) == 0 ) return false; // Bit is already clear? } while( !nbsi.CAS( j>>6, old, old & ~mask ) ); _size.add(-1); return true; }
public boolean contains( final int i ) { if( (i>>6) >= _bits.length ) // Out of bounds? Not in this array! return _new != null && help_copy().contains(i); // Handle every 64th bit via using a nested array NBSI nbsi = this; // The bit array being added into int j = i; // The bit index being added while( (j&63) == 63 ) { // Bit 64? (low 6 bits are all set) nbsi = nbsi._nbsi64; // Recurse j = j>>6; // Strip off low 6 bits (all set) } final long mask = mask(j); long old = nbsi._bits[j>>6]; // Read old bits if( old < 0 ) // Not mutable? // Not mutable: finish copy of word, and retry on copied word return help_copy_impl(i).help_copy().contains(i); // Yes mutable: test & return bit return (old & mask) != 0; }