C# Class Nez.BitSet

This class can be thought of in two ways. You can see it as a vector of bits or as a set of non-negative integers. The name BitSet is a bit misleading. It is implemented by a bit vector, but its equally possible to see it as set of non-negative integer; each integer in the set is represented by a set bit at the corresponding index. The size of this structure is determined by the highest integer in the set. You can union, intersect and build (symmetric) remainders, by invoking the logical operations and, or, andNot, resp. xor. This implementation is NOT synchronized against concurrent access from multiple threads. Specifically, if one thread is reading from a bitset while another thread is simultaneously modifying it, the results are undefined. author Jochen Hoenicke author Tom Tromey ([email protected]) author Eric Blake ([email protected]) status updated to 1.4
Show file Open project: prime31/Nez Class Usage Examples

Public Methods

Method Description
BitSet ( ) : System

Create a new empty bit set. All bits are initially false.

BitSet ( int nbits ) : System

Create a new empty bit set, with a given size. This constructor reserves enough space to represent the integers from 0 to nbits-1.

Equals ( object obj ) : bool

Returns true if the obj is a bit set that contains exactly the same elements as this bit set, otherwise false.

GetHashCode ( ) : int

Returns a hash code value for this bit set. The hash code of two bit sets containing the same integers is identical. The algorithm used to compute it is as follows: Suppose the bits in the BitSet were to be stored in an array of long integers called bits, in such a manner that bit k is set in the BitSet (for non-negative values of k) if and only if ((k/64) < bits.length) && ((bits[k/64] & (1L << (bit % 64))) != 0) Then the following definition of the GetHashCode method would be a correct implementation of the actual algorithm:

public override int GetHashCode() { long h = 1234; for (int i = bits.length-1; i >= 0; i--) { h ^= bits[i] * (i + 1); } return (int)((h >> 32) ^ h); }
Note that the hash code values changes, if the set is changed.

ToString ( ) : string

Returns the string representation of this bit set. This consists of a comma separated list of the integers in this set surrounded by curly braces. There is a space after each comma. A sample string is thus "{1, 3, 53}".

and ( BitSet bs ) : void

Performs the logical AND operation on this bit set and the given set. This means it builds the intersection of the two sets. The result is stored into this bit set.

andNot ( BitSet bs ) : void

Performs the logical AND operation on this bit set and the complement of the given bs. This means it selects every element in the first set, that isn't in the second set. The result is stored into this bit set and is effectively the set difference of the two.

cardinality ( ) : int

Returns the number of bits set to true.

clear ( ) : void

Sets all bits in the set to false.

clear ( int pos ) : void

Removes the integer pos from this set. That is the corresponding bit is cleared. If the index is not in the set, this method does nothing.

clear ( int from, int to ) : void

Sets the bits between from (inclusive) and to (exclusive) to false.

clone ( ) : object

Create a clone of this bit set, that is an instance of the same class and contains the same elements. But it doesn't change when this bit set changes.

containsAll ( BitSet other ) : bool
flip ( int index ) : void

Sets the bit at the index to the opposite value.

flip ( int from, int to ) : void

Sets a range of bits to the opposite value.

get ( int from, int to ) : BitSet

Returns a new BitSet composed of a range of bits from this one.

get ( int pos ) : System.Boolean

Returns true if the integer bitIndex is in this bit set, otherwise false.

intersects ( BitSet set ) : bool

Returns true if the specified BitSet and this one share at least one common true bit.

isEmpty ( ) : bool

Returns true if this set contains no true bits.

nextClearBit ( int from ) : int

Returns the index of the next false bit, from the specified bit (inclusive).

nextSetBit ( int from ) : int

Returns the index of the next true bit, from the specified bit (inclusive). If there is none, -1 is returned. You can iterate over all true bits with this loop:

for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) { // operate on i here } 

or ( BitSet bs ) : void

Performs the logical OR operation on this bit set and the given set. This means it builds the union of the two sets. The result is stored into this bit set, which grows as necessary.

set ( int pos ) : void

Add the integer bitIndex to this set. That is the corresponding bit is set to true. If the index was already in the set, this method does nothing. The size of this structure is automatically increased as necessary.

set ( int index, bool value ) : void

Sets the bit at the given index to the specified value. The size of this structure is automatically increased as necessary.

set ( int from, int to ) : void

Sets the bits between from (inclusive) and to (exclusive) to true.

set ( int from, int to, bool value ) : void

Sets the bits between from (inclusive) and to (exclusive) to the specified value.

xor ( BitSet bs ) : void

Performs the logical XOR operation on this bit set and the given set. This means it builds the symmetric remainder of the two sets (the elements that are in one set, but not in the other). The result is stored into this bit set, which grows as necessary.

Private Methods

Method Description
ensure ( int lastElt ) : void

Make sure the vector is big enough.

Method Details

BitSet() public method

Create a new empty bit set. All bits are initially false.
public BitSet ( ) : System
return System

BitSet() public method

Create a new empty bit set, with a given size. This constructor reserves enough space to represent the integers from 0 to nbits-1.
public BitSet ( int nbits ) : System
nbits int nbits the initial size of the bit set
return System

Equals() public method

Returns true if the obj is a bit set that contains exactly the same elements as this bit set, otherwise false.
public Equals ( object obj ) : bool
obj object the object to compare to
return bool

GetHashCode() public method

Returns a hash code value for this bit set. The hash code of two bit sets containing the same integers is identical. The algorithm used to compute it is as follows: Suppose the bits in the BitSet were to be stored in an array of long integers called bits, in such a manner that bit k is set in the BitSet (for non-negative values of k) if and only if ((k/64) < bits.length) && ((bits[k/64] & (1L << (bit % 64))) != 0) Then the following definition of the GetHashCode method would be a correct implementation of the actual algorithm:
public override int GetHashCode() { long h = 1234; for (int i = bits.length-1; i >= 0; i--) { h ^= bits[i] * (i + 1); } return (int)((h >> 32) ^ h); }
Note that the hash code values changes, if the set is changed.
public GetHashCode ( ) : int
return int

ToString() public method

Returns the string representation of this bit set. This consists of a comma separated list of the integers in this set surrounded by curly braces. There is a space after each comma. A sample string is thus "{1, 3, 53}".
public ToString ( ) : string
return string

and() public method

Performs the logical AND operation on this bit set and the given set. This means it builds the intersection of the two sets. The result is stored into this bit set.
public and ( BitSet bs ) : void
bs BitSet the second bit set
return void

andNot() public method

Performs the logical AND operation on this bit set and the complement of the given bs. This means it selects every element in the first set, that isn't in the second set. The result is stored into this bit set and is effectively the set difference of the two.
public andNot ( BitSet bs ) : void
bs BitSet the second bit set
return void

cardinality() public method

Returns the number of bits set to true.
public cardinality ( ) : int
return int

clear() public method

Sets all bits in the set to false.
public clear ( ) : void
return void

clear() public method

Removes the integer pos from this set. That is the corresponding bit is cleared. If the index is not in the set, this method does nothing.
public clear ( int pos ) : void
pos int a non-negative integer
return void

clear() public method

Sets the bits between from (inclusive) and to (exclusive) to false.
public clear ( int from, int to ) : void
from int the start range (inclusive)
to int the end range (exclusive)
return void

clone() public method

Create a clone of this bit set, that is an instance of the same class and contains the same elements. But it doesn't change when this bit set changes.
public clone ( ) : object
return object

containsAll() public method

public containsAll ( BitSet other ) : bool
other BitSet
return bool

flip() public method

Sets the bit at the index to the opposite value.
public flip ( int index ) : void
index int the index of the bit
return void

flip() public method

Sets a range of bits to the opposite value.
public flip ( int from, int to ) : void
from int the low index (inclusive)
to int the high index (exclusive)
return void

get() public method

Returns a new BitSet composed of a range of bits from this one.
public get ( int from, int to ) : BitSet
from int the low index (inclusive)
to int the high index (exclusive)
return BitSet

get() public method

Returns true if the integer bitIndex is in this bit set, otherwise false.
public get ( int pos ) : System.Boolean
pos int a non-negative integer
return System.Boolean

intersects() public method

Returns true if the specified BitSet and this one share at least one common true bit.
public intersects ( BitSet set ) : bool
set BitSet the set to check for intersection
return bool

isEmpty() public method

Returns true if this set contains no true bits.
public isEmpty ( ) : bool
return bool

nextClearBit() public method

Returns the index of the next false bit, from the specified bit (inclusive).
public nextClearBit ( int from ) : int
from int the start location
return int

nextSetBit() public method

Returns the index of the next true bit, from the specified bit (inclusive). If there is none, -1 is returned. You can iterate over all true bits with this loop:
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) { // operate on i here } 
public nextSetBit ( int from ) : int
from int the start location
return int

or() public method

Performs the logical OR operation on this bit set and the given set. This means it builds the union of the two sets. The result is stored into this bit set, which grows as necessary.
public or ( BitSet bs ) : void
bs BitSet the second bit set
return void

set() public method

Add the integer bitIndex to this set. That is the corresponding bit is set to true. If the index was already in the set, this method does nothing. The size of this structure is automatically increased as necessary.
public set ( int pos ) : void
pos int a non-negative integer.
return void

set() public method

Sets the bit at the given index to the specified value. The size of this structure is automatically increased as necessary.
public set ( int index, bool value ) : void
index int the position to set
value bool the value to set it to
return void

set() public method

Sets the bits between from (inclusive) and to (exclusive) to true.
public set ( int from, int to ) : void
from int the start range (inclusive)
to int the end range (exclusive)
return void

set() public method

Sets the bits between from (inclusive) and to (exclusive) to the specified value.
public set ( int from, int to, bool value ) : void
from int the start range (inclusive)
to int the end range (exclusive)
value bool the value to set it to
return void

xor() public method

Performs the logical XOR operation on this bit set and the given set. This means it builds the symmetric remainder of the two sets (the elements that are in one set, but not in the other). The result is stored into this bit set, which grows as necessary.
public xor ( BitSet bs ) : void
bs BitSet the second bit set
return void