C# Класс 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
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
ensure ( int lastElt ) : void

Make sure the vector is big enough.

Описание методов

BitSet() публичный Метод

Create a new empty bit set. All bits are initially false.
public BitSet ( ) : System
Результат System

BitSet() публичный Метод

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
Результат System

Equals() публичный Метод

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
Результат bool

GetHashCode() публичный Метод

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
Результат int

ToString() публичный Метод

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
Результат string

and() публичный Метод

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
Результат void

andNot() публичный Метод

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
Результат void

cardinality() публичный Метод

Returns the number of bits set to true.
public cardinality ( ) : int
Результат int

clear() публичный Метод

Sets all bits in the set to false.
public clear ( ) : void
Результат void

clear() публичный Метод

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
Результат void

clear() публичный Метод

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)
Результат void

clone() публичный Метод

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
Результат object

containsAll() публичный Метод

public containsAll ( BitSet other ) : bool
other BitSet
Результат bool

flip() публичный Метод

Sets the bit at the index to the opposite value.
public flip ( int index ) : void
index int the index of the bit
Результат void

flip() публичный Метод

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)
Результат void

get() публичный Метод

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)
Результат BitSet

get() публичный Метод

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
Результат System.Boolean

intersects() публичный Метод

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
Результат bool

isEmpty() публичный Метод

Returns true if this set contains no true bits.
public isEmpty ( ) : bool
Результат bool

nextClearBit() публичный Метод

Returns the index of the next false bit, from the specified bit (inclusive).
public nextClearBit ( int from ) : int
from int the start location
Результат int

nextSetBit() публичный Метод

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
Результат int

or() публичный Метод

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
Результат void

set() публичный Метод

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.
Результат void

set() публичный Метод

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
Результат void

set() публичный Метод

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)
Результат void

set() публичный Метод

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
Результат void

xor() публичный Метод

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
Результат void