Struct BitVector
A 32-bit vector with simple operations for getting and setting bits.
Implements
Inherited Members
Namespace: SudokuSpice
Assembly: SudokuSpice.dll
Syntax
public struct BitVector : IEquatable<BitVector>
Constructors
| Improve this Doc View SourceBitVector(UInt32)
Constructs a bit vector with the given data.
Declaration
public BitVector(uint data)
Parameters
Type | Name | Description |
---|---|---|
System.UInt32 | data | The data to use for this bit vector. |
Fields
| Improve this Doc View SourceNumBits
The number of bits (i.e. the capacity) contained by a BitVector.
Declaration
public const int NumBits = 32
Field Value
Type | Description |
---|---|
System.Int32 |
Properties
| Improve this Doc View SourceData
Gets the data stored in this bit vector as an unsigned int.
Declaration
public uint Data { get; }
Property Value
Type | Description |
---|---|
System.UInt32 |
IsEmpty
Checks if this vector is empty (i.e. no bits are set).
Declaration
public bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
System.Boolean | True if empty. |
Methods
| Improve this Doc View SourceComputeCount()
Gets the count of bits that are set.
Declaration
public int ComputeCount()
Returns
Type | Description |
---|---|
System.Int32 |
Remarks
This relies on the System.Runtime.Intrinsics.X86.Popcnt hardware intrinsic to be efficient. If this operation is not available on your hardware, then this function falls back to a less efficient software-based approach.
ComputeLastSetBit()
Computes the last bit that is set, or -1 if none are set.
Declaration
public int ComputeLastSetBit()
Returns
Type | Description |
---|---|
System.Int32 |
CreateWithSize(Int32)
Creates a bit vector with only the first size
bits set to true.
Declaration
public static BitVector CreateWithSize(int size)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | size | The number of bits to set. |
Returns
Type | Description |
---|---|
BitVector |
Equals(BitVector)
Declaration
public bool Equals(BitVector other)
Parameters
Type | Name | Description |
---|---|---|
BitVector | other |
Returns
Type | Description |
---|---|
System.Boolean |
Equals(Object)
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj |
Returns
Type | Description |
---|---|
System.Boolean |
Overrides
FindDifference(BitVector, BitVector)
Creates a bit vector that is the difference of the given vectors.
Declaration
public static BitVector FindDifference(BitVector a, BitVector b)
Parameters
Type | Name | Description |
---|---|---|
BitVector | a | One bit vector. |
BitVector | b | The other bit vector. |
Returns
Type | Description |
---|---|
BitVector | A new |
FindIntersect(BitVector, BitVector)
Creates a bit vector that is the intersect of the given vectors.
Declaration
public static BitVector FindIntersect(BitVector a, BitVector b)
Parameters
Type | Name | Description |
---|---|---|
BitVector | a | One bit vector. |
BitVector | b | The other bit vector. |
Returns
Type | Description |
---|---|
BitVector | A new |
FindUnion(BitVector, BitVector)
Creates a bit vector that is the union of the given vectors.
Declaration
public static BitVector FindUnion(BitVector a, BitVector b)
Parameters
Type | Name | Description |
---|---|---|
BitVector | a | One bit vector. |
BitVector | b | The other bit vector. |
Returns
Type | Description |
---|---|
BitVector | A new |
GetHashCode()
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 |
Overrides
IsBitSet(Int32)
Checks if the bit is true at the given index.
Declaration
public bool IsBitSet(int bit)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | bit | The zero-based index of the bit to check. |
Returns
Type | Description |
---|---|
System.Boolean | True if set. |
IsSubsetOf(BitVector)
Determines if this vector is a subset of the given vector (i.e. all bits set in this
vector are also set in other
).
Declaration
public bool IsSubsetOf(BitVector other)
Parameters
Type | Name | Description |
---|---|---|
BitVector | other | The possible superset. |
Returns
Type | Description |
---|---|
System.Boolean | True if this is equal to or a subset of the given vector. |
PopulateSetBits(Span<Int32>)
Populates a provided Span with the indices of set bits, and returns the number of set bits it populated.
This method will return if it reaches the end of the provided
setIndices
span, so if setIndices
has size N,
then this will only populate the indices for the first N set bits, and will return N.
If there are less setBits than N, say M, the values of setIndices
from setIndices[M] to setIndices[N-1] are left unchanged.
Declaration
public int PopulateSetBits(Span<int> setIndices)
Parameters
Type | Name | Description |
---|---|---|
System.Span<System.Int32> | setIndices | A span to fill with the indices of set bits. All values will be written to |
Returns
Type | Description |
---|---|
System.Int32 | The number of set bits that have been populated into |
Remarks
This operation is slightly more efficient on average when System.Runtime.Intrinsics.X86.Popcnt is supported. Worst case performance is roughly the same.
SetBit(Int32)
Sets the given bit index in the given vector. Leaves other bits unchanged.
Declaration
public void SetBit(int bit)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | bit | The zero-based index of the bit to set. |
ToString()
Returns this bitvector as a binary-formatted string (eg. 1011).
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String |
Overrides
UnsetBit(Int32)
Unsets the given bit index in the given vector. Leaves other bits unchanged.
Declaration
public void UnsetBit(int bit)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | bit | The zero-based index of the bit to unset. |
Operators
| Improve this Doc View SourceEquality(BitVector, BitVector)
Declaration
public static bool operator ==(BitVector a, BitVector b)
Parameters
Type | Name | Description |
---|---|---|
BitVector | a | |
BitVector | b |
Returns
Type | Description |
---|---|
System.Boolean |
Inequality(BitVector, BitVector)
Declaration
public static bool operator !=(BitVector a, BitVector b)
Parameters
Type | Name | Description |
---|---|---|
BitVector | a | |
BitVector | b |
Returns
Type | Description |
---|---|
System.Boolean |