/// 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.
/// <param name="pos">Index of the bit to set.</param>
/// <param name="value">True for 1, false for 0.</param>
publicvoidSet(intpos,boolvalue)
{
CheckWrite();
m_BitArray->Set(pos,value);
}
/// <summary>
/// Sets a range of bits to 0 or 1.
/// </summary>
/// <remarks>
/// The range runs from index `pos` up to (but not including) `pos + numBits`.
/// No exception is thrown if `pos + numBits` exceeds the length.
/// </remarks>
/// <param name="pos">Index of the first bit to set.</param>
/// <param name="value">True for 1, false for 0.</param>
/// <param name="numBits">Number of bits to set.</param>
/// <exception cref="ArgumentException">Thrown if pos is out of bounds or if numBits is less than 1.</exception>
publicvoidSetBits(intpos,boolvalue,intnumBits)
{
CheckWrite();
m_BitArray->SetBits(pos,value,numBits);
}
/// <summary>
/// Copies bits of a ulong to bits in this array.
/// </summary>
/// <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.
/// </remarks>
/// <param name="pos">Index of the first bit to set.</param>
/// <param name="value">Unsigned long from which to copy bits.</param>
/// <param name="numBits">Number of bits to set (must be between 1 and 64).</param>
/// <exception cref="ArgumentException">Thrown if pos is out of bounds or if numBits is not between 1 and 64.</exception>
publicvoidSetBits(intpos,ulongvalue,intnumBits=1)
{
CheckWrite();
m_BitArray->SetBits(pos,value,numBits);
}
/// <summary>
/// Returns a ulong which has bits copied from this array.
/// </summary>
/// <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.
/// </remarks>
/// <param name="pos">Index of the first bit to get.</param>
/// <param name="numBits">Number of bits to get (must be between 1 and 64).</param>
/// <exception cref="ArgumentException">Thrown if pos is out of bounds or if numBits is not between 1 and 64.</exception>
/// <returns>A ulong which has bits copied from this array.</returns>
publiculongGetBits(intpos,intnumBits=1)
{
CheckRead();
returnm_BitArray->GetBits(pos,numBits);
}
/// <summary>
/// Returns true if the bit at an index is 1.
/// </summary>
/// <param name="pos">Index of the bit to test.</param>
/// <returns>True if the bit at the index is 1.</returns>
/// <exception cref="ArgumentException">Thrown if `pos` is out of bounds.</exception>
publicboolIsSet(intpos)
{
CheckRead();
returnm_BitArray->IsSet(pos);
}
/// <summary>
/// Copies a range of bits from this array to another range in this array.
/// </summary>
/// <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.
/// </remarks>
/// <param name="dstPos">Index of the first bit to set.</param>
/// <param name="srcPos">Index of the first bit to copy.</param>
/// <param name="numBits">Number of bits to copy.</param>
/// <exception cref="ArgumentException">Thrown if either `dstPos + numBits` or `srcPos + numBits` exceed the length of this array.</exception>
publicvoidCopy(intdstPos,intsrcPos,intnumBits)
{
CheckWrite();
m_BitArray->Copy(dstPos,srcPos,numBits);
}
/// <summary>
/// Copies a range of bits from an array to a range of bits in this array.
/// </summary>
/// <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`.
///
/// When the source and destination are the same array, the ranges may still overlap, but the result in the overlapping region is undefined.
/// </remarks>
/// <param name="dstPos">Index of the first bit to set.</param>
/// Find a sequence of N contiguous 0 bits in a bit array. To achieve higher performance, it is not guaranteed that the found index is necessarily the first occurrence of such a sequence.
/// Find a sequence of N contiguous 0 bits in a bit array. To achieve higher performance, it is not guaranteed that the found index is necessarily the first occurrence of such a sequence.
/// Returns a ulong which has bits copied from this array.
/// </summary>
/// <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.
/// </remarks>
/// <param name="pos">Index of the first bit to get.</param>
/// <param name="numBits">Number of bits to get (must be between 1 and 64).</param>
/// <exception cref="ArgumentException">Thrown if pos is out of bounds or if numBits is not between 1 and 64.</exception>
/// <returns>A ulong which has bits copied from this array.</returns>
publicreadonlyulongGetBits(intpos,intnumBits=1)
{
CheckRead();
returnm_BitArray.GetBits(pos,numBits);
}
/// <summary>
/// Returns true if the bit at an index is 1.
/// </summary>
/// <param name="pos">Index of the bit to test.</param>
/// <returns>True if the bit at the index is 1.</returns>
/// <exception cref="ArgumentException">Thrown if `pos` is out of bounds.</exception>
publicreadonlyboolIsSet(intpos)
{
CheckRead();
returnm_BitArray.IsSet(pos);
}
/// <summary>
/// Finds the first length-*N* contiguous sequence of 0 bits in this bit array.
/// </summary>
/// <param name="pos">Index at which to start searching.</param>
/// <param name="numBits">Number of contiguous 0 bits to look for.</param>
/// <returns>The index in this array where the sequence is found. The index will be greater than or equal to `pos`.
/// Returns -1 if no occurrence is found.</returns>
publicreadonlyintFind(intpos,intnumBits)
{
CheckRead();
returnm_BitArray.Find(pos,numBits);
}
/// <summary>
/// Finds the first length-*N* contiguous sequence of 0 bits in this bit array. Searches only a subsection.
/// </summary>
/// <param name="pos">Index at which to start searching.</param>
/// <param name="numBits">Number of contiguous 0 bits to look for.</param>
/// <param name="count">Number of bits to search.</param>
/// <returns>The index in this array where the sequence is found. The index will be greater than or equal to `pos` but less than `pos + count`.
/// Returns -1 if no occurrence is found.</returns>
publicreadonlyintFind(intpos,intcount,intnumBits)
{
CheckRead();
returnm_BitArray.Find(pos,count,numBits);
}
/// <summary>
/// Returns true if none of the bits in a range are 1 (*i.e.* all bits in the range are 0).
/// </summary>
/// <param name="pos">Index of the bit at which to start searching.</param>
/// <param name="numBits">Number of bits to test. Defaults to 1.</param>
/// <returns>Returns true if none of the bits in range `pos` up to (but not including) `pos + numBits` are 1.</returns>
/// <exception cref="ArgumentException">Thrown if `pos` is out of bounds or `numBits` is less than 1.</exception>
publicreadonlyboolTestNone(intpos,intnumBits=1)
{
CheckRead();
returnm_BitArray.TestNone(pos,numBits);
}
/// <summary>
/// Returns true if at least one of the bits in a range is 1.
/// </summary>
/// <param name="pos">Index of the bit at which to start searching.</param>
/// <param name="numBits">Number of bits to test. Defaults to 1.</param>
/// <returns>True if one ore more of the bits in range `pos` up to (but not including) `pos + numBits` are 1.</returns>
/// <exception cref="ArgumentException">Thrown if `pos` is out of bounds or `numBits` is less than 1.</exception>
publicreadonlyboolTestAny(intpos,intnumBits=1)
{
CheckRead();
returnm_BitArray.TestAny(pos,numBits);
}
/// <summary>
/// Returns true if all of the bits in a range are 1.
/// </summary>
/// <param name="pos">Index of the bit at which to start searching.</param>
/// <param name="numBits">Number of bits to test. Defaults to 1.</param>
/// <returns>True if all of the bits in range `pos` up to (but not including) `pos + numBits` are 1.</returns>
/// <exception cref="ArgumentException">Thrown if `pos` is out of bounds or `numBits` is less than 1.</exception>
publicreadonlyboolTestAll(intpos,intnumBits=1)
{
CheckRead();
returnm_BitArray.TestAll(pos,numBits);
}
/// <summary>
/// Returns the number of bits in a range that are 1.
/// </summary>
/// <param name="pos">Index of the bit at which to start searching.</param>
/// <param name="numBits">Number of bits to test. Defaults to 1.</param>
/// <returns>The number of bits in a range of bits that are 1.</returns>
/// <exception cref="ArgumentException">Thrown if `pos` is out of bounds or `numBits` is less than 1.</exception>
thrownewInvalidOperationException($"Number of bits in the NativeBitArray {m_BitArray->Length} is not sufficient to cast to NativeArray<T> {UnsafeUtility.SizeOf<T>() * 8}.");
}
elseif(m_BitArray->Length!=bitsPerElement*length)
{
thrownewInvalidOperationException($"Number of bits in the NativeBitArray {m_BitArray->Length} couldn't hold multiple of T {UnsafeUtility.SizeOf<T>()}. Output array would be truncated.");
}
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
voidCheckWrite()
{
#ifENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
#endif
}
}
[NativeContainer]
[GenerateTestsForBurstCompatibility]
internalunsafestructNativeBitArrayDispose
{
[NativeDisableUnsafePtrRestriction]
publicUnsafeBitArray*m_BitArrayData;
publicAllocatorHandlem_Allocator;
#ifENABLE_UNITY_COLLECTIONS_CHECKS
publicAtomicSafetyHandlem_Safety;
#endif
publicvoidDispose()
{
UnsafeBitArray.Free(m_BitArrayData,m_Allocator);
}
}
[BurstCompile]
internalunsafestructNativeBitArrayDisposeJob:IJob
{
publicNativeBitArrayDisposeData;
publicvoidExecute()
{
Data.Dispose();
}
}
}
namespaceUnity.Collections.LowLevel.Unsafe
{
/// <summary>
/// Unsafe helper methods for NativeBitArray.
/// </summary>
[GenerateTestsForBurstCompatibility]
publicstaticclassNativeBitArrayUnsafeUtility
{
#ifENABLE_UNITY_COLLECTIONS_CHECKS
/// <summary>
/// Returns an array's atomic safety handle.
/// </summary>
/// <param name="container">Array from which to get an AtomicSafetyHandle.</param>