Struct UnsafeBitArray
An arbitrarily-sized array of bits.
Implements
Namespace: Unity.Collections.LowLevel.Unsafe
Assembly: Unity.Collections.dll
Syntax
public struct UnsafeBitArray : INativeDisposableRemarks
The number of allocated bytes is always a multiple of 8. For example, a 65-bit array could fit in 9 bytes, but its allocation is actually 16 bytes.
Constructors
UnsafeBitArray(int, AllocatorHandle, NativeArrayOptions)
Initializes and returns an instance of UnsafeBitArray.
Declaration
public UnsafeBitArray(int numBits, AllocatorManager.AllocatorHandle allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)Parameters
| Type | Name | Description | 
|---|---|---|
| int | numBits | Number of bits. | 
| AllocatorManager.AllocatorHandle | allocator | The allocator to use. | 
| NativeArrayOptions | options | Whether newly allocated bytes should be zeroed out. | 
UnsafeBitArray(void*, int, AllocatorHandle)
Initializes and returns an instance of UnsafeBitArray which aliases an existing buffer.
Declaration
public UnsafeBitArray(void* ptr, int sizeInBytes, AllocatorManager.AllocatorHandle allocator = default)Parameters
| Type | Name | Description | 
|---|---|---|
| void* | ptr | An existing buffer. | 
| int | sizeInBytes | The number of bytes. The length will be  | 
| AllocatorManager.AllocatorHandle | allocator | The allocator that was used to allocate the bytes. Needed to dispose this array. | 
Fields
Allocator
The allocator to use.
Declaration
public AllocatorManager.AllocatorHandle AllocatorField Value
| Type | Description | 
|---|---|
| AllocatorManager.AllocatorHandle | The allocator to use. | 
Capacity
The capacity number of bits.
Declaration
public int CapacityField Value
| Type | Description | 
|---|---|
| int | The capacity number of bits. | 
Length
The number of bits.
Declaration
public int LengthField Value
| Type | Description | 
|---|---|
| int | The number of bits. | 
Ptr
Pointer to the data.
Declaration
[NativeDisableUnsafePtrRestriction]
public ulong* PtrField Value
| Type | Description | 
|---|---|
| ulong* | Pointer to the data. | 
Properties
IsCreated
Whether this array has been allocated (and not yet deallocated).
Declaration
public readonly bool IsCreated { get; }Property Value
| Type | Description | 
|---|---|
| bool | True if this array has been allocated (and not yet deallocated). | 
IsEmpty
Whether the container is empty.
Declaration
public readonly bool IsEmpty { get; }Property Value
| Type | Description | 
|---|---|
| bool | True if the container is empty or the container has not been constructed. | 
Methods
AsReadOnly()
Returns a readonly version of this UnsafeBitArray instance.
Declaration
public UnsafeBitArray.ReadOnly AsReadOnly()Returns
| Type | Description | 
|---|---|
| UnsafeBitArray.ReadOnly | ReadOnly instance for this. | 
Remarks
ReadOnly containers point to the same underlying data as the UnsafeBitArray it is made from.
Clear()
Sets all the bits to 0.
Declaration
public void Clear()Copy(int, int, int)
Copies a range of bits from this array to another range in this array.
Declaration
public void Copy(int dstPos, int srcPos, int numBits)Parameters
| Type | Name | Description | 
|---|---|---|
| int | dstPos | Index of the first bit to set. | 
| int | srcPos | Index of the first bit to copy. | 
| int | numBits | Number of bits to copy. | 
Remarks
The bits to copy run from index srcPos up to (but not including) srcPos + numBits.
The bits to set run from index dstPos up to (but not including) dstPos + numBits.
The ranges may overlap, but the result in the overlapping region is undefined.
Exceptions
| Type | Condition | 
|---|---|
| ArgumentException | Thrown if either  | 
Copy(int, ref UnsafeBitArray, int, int)
Copies a range of bits from an array to a range of bits in this array.
Declaration
public void Copy(int dstPos, ref UnsafeBitArray srcBitArray, int srcPos, int numBits)Parameters
| Type | Name | Description | 
|---|---|---|
| int | dstPos | Index of the first bit to set. | 
| UnsafeBitArray | srcBitArray | The source array. | 
| int | srcPos | Index of the first bit to copy. | 
| int | numBits | The number of bits to copy. | 
Remarks
The bits to copy in the source array run from index srcPos up to (but not including) srcPos + numBits.
The bits to set in the destination array run from index dstPos up to (but not including) dstPos + numBits.
It's fine if source and destination array are one and the same, even if the ranges overlap, but the result in the overlapping region is undefined.
Exceptions
| Type | Condition | 
|---|---|
| ArgumentException | Thrown if either  | 
CountBits(int, int)
Returns the number of bits in a range that are 1.
Declaration
public int CountBits(int pos, int numBits = 1)Parameters
| Type | Name | Description | 
|---|---|---|
| int | pos | Index of the bit at which to start searching. | 
| int | numBits | Number of bits to test. Defaults to 1. | 
Returns
| Type | Description | 
|---|---|
| int | The number of bits in a range of bits that are 1. | 
Exceptions
| Type | Condition | 
|---|---|
| ArgumentException | Thrown if  | 
Dispose()
Releases all resources (memory and safety handles).
Declaration
public void Dispose()Dispose(JobHandle)
Creates and schedules a job that will dispose this array.
Declaration
public JobHandle Dispose(JobHandle inputDeps)Parameters
| Type | Name | Description | 
|---|---|---|
| JobHandle | inputDeps | The handle of a job which the new job will depend upon. | 
Returns
| Type | Description | 
|---|---|
| JobHandle | The handle of a new job that will dispose this array. The new job depends upon inputDeps. | 
Find(int, int)
Returns the index of the first occurrence in this array of N contiguous 0 bits.
Declaration
public int Find(int pos, int numBits)Parameters
| Type | Name | Description | 
|---|---|---|
| int | pos | Index of the bit at which to start searching. | 
| int | numBits | Number of contiguous 0 bits to look for. | 
Returns
| Type | Description | 
|---|---|
| int | The index of the first occurrence in this array of  | 
Remarks
The search is linear.
Find(int, int, int)
Returns the index of the first occurrence in this array of a given number of contiguous 0 bits.
Declaration
public int Find(int pos, int count, int numBits)Parameters
| Type | Name | Description | 
|---|---|---|
| int | pos | Index of the bit at which to start searching. | 
| int | count | Number of indexes to consider as the return value. | 
| int | numBits | Number of contiguous 0 bits to look for. | 
Returns
| Type | Description | 
|---|---|
| int | The index of the first occurrence in this array of  | 
Remarks
The search is linear.
GetBits(int, int)
Returns a ulong which has bits copied from this array.
Declaration
public ulong GetBits(int pos, int numBits = 1)Parameters
| Type | Name | Description | 
|---|---|---|
| int | pos | Index of the first bit to get. | 
| int | numBits | Number of bits to get (must be between 1 and 64). | 
Returns
| Type | Description | 
|---|---|
| ulong | A ulong which has bits copied from this array. | 
Remarks
The source bits in this array run from index pos up to (but not including) pos + numBits.
No exception is thrown if pos + numBits exceeds the length.
The first source bit is copied to the lowest bit of the ulong; the second source bit is copied to the second-lowest bit of the ulong; and so forth. Any remaining bits in the ulong will be 0.
Exceptions
| Type | Condition | 
|---|---|
| ArgumentException | Thrown if pos is out of bounds or if numBits is not between 1 and 64. | 
IsSet(int)
Returns true if the bit at an index is 1.
Declaration
public bool IsSet(int pos)Parameters
| Type | Name | Description | 
|---|---|---|
| int | pos | Index of the bit to test. | 
Returns
| Type | Description | 
|---|---|
| bool | True if the bit at the index is 1. | 
Exceptions
| Type | Condition | 
|---|---|
| ArgumentException | Thrown if  | 
Resize(int, NativeArrayOptions)
Sets the length, expanding the capacity if necessary.
Declaration
public void Resize(int numBits, NativeArrayOptions options = NativeArrayOptions.UninitializedMemory)Parameters
| Type | Name | Description | 
|---|---|---|
| int | numBits | The new length in bits. | 
| NativeArrayOptions | options | Whether newly allocated data should be zeroed out. | 
Set(int, bool)
Sets the bit at an index to 0 or 1.
Declaration
public void Set(int pos, bool value)Parameters
| Type | Name | Description | 
|---|---|---|
| int | pos | Index of the bit to set. | 
| bool | value | True for 1, false for 0. | 
Set(ulong*, int, bool)
Sets the bit at an index to 0 or 1.
Declaration
public static void Set(ulong* ptr, int pos, bool value)Parameters
| Type | Name | Description | 
|---|---|---|
| ulong* | ptr | pointer to the bit buffer | 
| int | pos | Index of the bit to set. | 
| bool | value | True for 1, false for 0. | 
SetBits(int, bool, int)
Sets a range of bits to 0 or 1.
Declaration
public void SetBits(int pos, bool value, int numBits)Parameters
| Type | Name | Description | 
|---|---|---|
| int | pos | Index of the first bit to set. | 
| bool | value | True for 1, false for 0. | 
| int | numBits | Number of bits to set. | 
Remarks
The range runs from index pos up to (but not including) pos + numBits.
No exception is thrown if pos + numBits exceeds the length.
Exceptions
| Type | Condition | 
|---|---|
| ArgumentException | Thrown if pos is out of bounds or if numBits is less than 1. | 
SetBits(int, ulong, int)
Copies bits of a ulong to bits in this array.
Declaration
public void SetBits(int pos, ulong value, int numBits = 1)Parameters
| Type | Name | Description | 
|---|---|---|
| int | pos | Index of the first bit to set. | 
| ulong | value | Unsigned long from which to copy bits. | 
| int | numBits | Number of bits to set (must be between 1 and 64). | 
Remarks
The destination bits in this array run from index pos up to (but not including) pos + numBits.
No exception is thrown if pos + numBits exceeds the length.
The lowest bit of the ulong is copied to the first destination bit; the second-lowest bit of the ulong is copied to the second destination bit; and so forth.
Exceptions
| Type | Condition | 
|---|---|
| ArgumentException | Thrown if pos is out of bounds or if numBits is not between 1 and 64. | 
SetCapacity(int)
Sets the capacity.
Declaration
public void SetCapacity(int capacityInBits)Parameters
| Type | Name | Description | 
|---|---|---|
| int | capacityInBits | The new capacity. | 
TrimExcess()
Sets the capacity to match what it would be if it had been originally initialized with all its entries.
Declaration
public void TrimExcess()