using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Unity.Burst;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
namespace Unity.Collections
{
///
/// An arbitrarily-sized array of bits.
///
///
/// 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.
///
[StructLayout(LayoutKind.Sequential)]
[NativeContainer]
[DebuggerDisplay("Length = {Length}, IsCreated = {IsCreated}")]
[BurstCompatible]
public unsafe struct NativeBitArray
: INativeDisposable
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
internal AtomicSafetyHandle m_Safety;
static readonly SharedStatic s_staticSafetyId = SharedStatic.GetOrCreate();
#if REMOVE_DISPOSE_SENTINEL
#else
[NativeSetClassTypeToNullOnSchedule]
DisposeSentinel m_DisposeSentinel;
#endif
#endif
[NativeDisableUnsafePtrRestriction]
internal UnsafeBitArray m_BitArray;
///
/// Initializes and returns an instance of NativeBitArray.
///
/// The number of bits.
/// The allocator to use.
/// Whether newly allocated bytes should be zeroed out.
public NativeBitArray(int numBits, AllocatorManager.AllocatorHandle allocator, NativeArrayOptions options = NativeArrayOptions.ClearMemory)
: this(numBits, allocator, options, 2)
{
}
NativeBitArray(int numBits, AllocatorManager.AllocatorHandle allocator, NativeArrayOptions options, int disposeSentinelStackDepth)
{
CollectionHelper.CheckAllocator(allocator);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
#if REMOVE_DISPOSE_SENTINEL
m_Safety = CollectionHelper.CreateSafetyHandle(allocator);
#else
if (allocator.IsCustomAllocator)
{
m_Safety = AtomicSafetyHandle.Create();
m_DisposeSentinel = null;
}
else
{
DisposeSentinel.Create(out m_Safety, out m_DisposeSentinel, disposeSentinelStackDepth, allocator.ToAllocator);
}
#endif
CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeBitArray");
#endif
m_BitArray = new UnsafeBitArray(numBits, allocator, options);
}
///
/// Whether this array has been allocated (and not yet deallocated).
///
/// True if this array has been allocated (and not yet deallocated).
public bool IsCreated => m_BitArray.IsCreated;
///
/// Releases all resources (memory and safety handles).
///
public void Dispose()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
#if REMOVE_DISPOSE_SENTINEL
CollectionHelper.DisposeSafetyHandle(ref m_Safety);
#else
DisposeSentinel.Dispose(ref m_Safety, ref m_DisposeSentinel);
#endif
#endif
m_BitArray.Dispose();
}
///
/// Creates and schedules a job that will dispose this array.
///
/// The handle of a job which the new job will depend upon.
/// The handle of a new job that will dispose this array. The new job depends upon inputDeps.
[NotBurstCompatible /* This is not burst compatible because of IJob's use of a static IntPtr. Should switch to IJobBurstSchedulable in the future */]
public JobHandle Dispose(JobHandle inputDeps)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
#if REMOVE_DISPOSE_SENTINEL
#else
// [DeallocateOnJobCompletion] is not supported, but we want the deallocation
// to happen in a thread. DisposeSentinel needs to be cleared on main thread.
// AtomicSafetyHandle can be destroyed after the job was scheduled (Job scheduling
// will check that no jobs are writing to the container).
DisposeSentinel.Clear(ref m_DisposeSentinel);
#endif
#endif
var jobHandle = m_BitArray.Dispose(inputDeps);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.Release(m_Safety);
#endif
return jobHandle;
}
///
/// Returns the number of bits.
///
/// The number of bits.
public int Length
{
get
{
CheckRead();
return CollectionHelper.AssumePositive(m_BitArray.Length);
}
}
///
/// Sets all the bits to 0.
///
public void Clear()
{
CheckWrite();
m_BitArray.Clear();
}
///
/// Returns a native array that aliases the content of this array.
///
/// The type of elements in the aliased array.
/// Thrown if the number of bits in this array
/// is not evenly divisible by the size of T in bits (`sizeof(T) * 8`).
/// A native array that aliases the content of this array.
[BurstCompatible(GenericTypeArguments = new [] { typeof(int) })]
public NativeArray AsNativeArray() where T : unmanaged
{
CheckReadBounds();
var bitsPerElement = UnsafeUtility.SizeOf() * 8;
var length = m_BitArray.Length / bitsPerElement;
var array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray(m_BitArray.Ptr, length, Allocator.None);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.UseSecondaryVersion(ref m_Safety);
NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref array, m_Safety);
#endif
return array;
}
///
/// Sets the bit at an index to 0 or 1.
///
/// Index of the bit to set.
/// True for 1, false for 0.
public void Set(int pos, bool value)
{
CheckWrite();
m_BitArray.Set(pos, value);
}
///
/// Sets a range of bits to 0 or 1.
///
///
/// The range runs from index `pos` up to (but not including) `pos + numBits`.
/// No exception is thrown if `pos + numBits` exceeds the length.
///
/// Index of the first bit to set.
/// True for 1, false for 0.
/// Number of bits to set.
/// Thrown if pos is out of bounds or if numBits is less than 1.
public void SetBits(int pos, bool value, int numBits)
{
CheckWrite();
m_BitArray.SetBits(pos, value, numBits);
}
///
/// Copies bits of a ulong to bits in this array.
///
///
/// 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.
///
/// Index of the first bit to set.
/// Unsigned long from which to copy bits.
/// Number of bits to set (must be between 1 and 64).
/// Thrown if pos is out of bounds or if numBits is not between 1 and 64.
public void SetBits(int pos, ulong value, int numBits = 1)
{
CheckWrite();
m_BitArray.SetBits(pos, value, numBits);
}
///
/// Returns a ulong which has bits copied from this array.
///
///
/// 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.
///
/// Index of the first bit to get.
/// Number of bits to get (must be between 1 and 64).
/// Thrown if pos is out of bounds or if numBits is not between 1 and 64.
/// A ulong which has bits copied from this array.
public ulong GetBits(int pos, int numBits = 1)
{
CheckRead();
return m_BitArray.GetBits(pos, numBits);
}
///
/// Returns true if the bit at an index is 1.
///
/// Index of the bit to test.
/// True if the bit at the index is 1.
/// Thrown if `pos` is out of bounds.
public bool IsSet(int pos)
{
CheckRead();
return m_BitArray.IsSet(pos);
}
///
/// Copies a range of bits from this array to another range in this array.
///
///
/// 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.
///
/// Index of the first bit to set.
/// Index of the first bit to copy.
/// Number of bits to copy.
/// Thrown if either `dstPos + numBits` or `srcPos + numBits` exceed the length of this array.
public void Copy(int dstPos, int srcPos, int numBits)
{
CheckWrite();
m_BitArray.Copy(dstPos, srcPos, numBits);
}
///
/// Copies a range of bits from an array to a range of bits in this array.
///
///
/// 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.
///
/// Index of the first bit to set.
/// The source array.
/// Index of the first bit to copy.
/// The number of bits to copy.
/// Thrown if either `dstPos + numBits` or `srcBitArray + numBits` exceed the length of this array.
public void Copy(int dstPos, ref NativeBitArray srcBitArray, int srcPos, int numBits)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckReadAndThrow(srcBitArray.m_Safety);
#endif
CheckWrite();
m_BitArray.Copy(dstPos, ref srcBitArray.m_BitArray, srcPos, numBits);
}
///
/// Finds the first length-*N* contiguous sequence of 0 bits in this bit array.
///
/// Index at which to start searching.
/// Number of contiguous 0 bits to look for.
/// 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.
public int Find(int pos, int numBits)
{
CheckRead();
return m_BitArray.Find(pos, numBits);
}
///
/// Finds the first length-*N* contiguous sequence of 0 bits in this bit array. Searches only a subsection.
///
/// Index at which to start searching.
/// Number of contiguous 0 bits to look for.
/// Number of bits to search.
/// 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.
public int Find(int pos, int count, int numBits)
{
CheckRead();
return m_BitArray.Find(pos, count, numBits);
}
///
/// Returns true if none of the bits in a range are 1 (*i.e.* all bits in the range are 0).
///
/// Index of the bit at which to start searching.
/// Number of bits to test. Defaults to 1.
/// Returns true if none of the bits in range `pos` up to (but not including) `pos + numBits` are 1.
/// Thrown if `pos` is out of bounds or `numBits` is less than 1.
public bool TestNone(int pos, int numBits = 1)
{
CheckRead();
return m_BitArray.TestNone(pos, numBits);
}
///
/// Returns true if at least one of the bits in a range is 1.
///
/// Index of the bit at which to start searching.
/// Number of bits to test. Defaults to 1.
/// True if one ore more of the bits in range `pos` up to (but not including) `pos + numBits` are 1.
/// Thrown if `pos` is out of bounds or `numBits` is less than 1.
public bool TestAny(int pos, int numBits = 1)
{
CheckRead();
return m_BitArray.TestAny(pos, numBits);
}
///
/// Returns true if all of the bits in a range are 1.
///
/// Index of the bit at which to start searching.
/// Number of bits to test. Defaults to 1.
/// True if all of the bits in range `pos` up to (but not including) `pos + numBits` are 1.
/// Thrown if `pos` is out of bounds or `numBits` is less than 1.
public bool TestAll(int pos, int numBits = 1)
{
CheckRead();
return m_BitArray.TestAll(pos, numBits);
}
///
/// Returns the number of bits in a range that are 1.
///
/// Index of the bit at which to start searching.
/// Number of bits to test. Defaults to 1.
/// The number of bits in a range of bits that are 1.
/// Thrown if `pos` is out of bounds or `numBits` is less than 1.
public int CountBits(int pos, int numBits = 1)
{
CheckRead();
return m_BitArray.CountBits(pos, numBits);
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckRead()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckReadBounds() where T : unmanaged
{
CheckRead();
var bitsPerElement = UnsafeUtility.SizeOf() * 8;
var length = m_BitArray.Length / bitsPerElement;
if (length == 0)
{
throw new InvalidOperationException($"Number of bits in the NativeBitArray {m_BitArray.Length} is not sufficient to cast to NativeArray {UnsafeUtility.SizeOf() * 8}.");
}
else if (m_BitArray.Length != bitsPerElement* length)
{
throw new InvalidOperationException($"Number of bits in the NativeBitArray {m_BitArray.Length} couldn't hold multiple of T {UnsafeUtility.SizeOf()}. Output array would be truncated.");
}
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckWrite()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
#endif
}
}
}
namespace Unity.Collections.LowLevel.Unsafe
{
///
/// Unsafe helper methods for NativeBitArray.
///
[BurstCompatible]
public static class NativeBitArrayUnsafeUtility
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
///
/// Returns an array's atomic safety handle.
///
/// Array from which to get an AtomicSafetyHandle.
/// This array's atomic safety handle.
[BurstCompatible(RequiredUnityDefine = "ENABLE_UNITY_COLLECTIONS_CHECKS", CompileTarget = BurstCompatibleAttribute.BurstCompatibleCompileTarget.Editor)]
public static AtomicSafetyHandle GetAtomicSafetyHandle(in NativeBitArray container)
{
return container.m_Safety;
}
///
/// Sets an array's atomic safety handle.
///
/// Array which the AtomicSafetyHandle is for.
/// Atomic safety handle for this array.
[BurstCompatible(RequiredUnityDefine = "ENABLE_UNITY_COLLECTIONS_CHECKS", CompileTarget = BurstCompatibleAttribute.BurstCompatibleCompileTarget.Editor)]
public static void SetAtomicSafetyHandle(ref NativeBitArray container, AtomicSafetyHandle safety)
{
container.m_Safety = safety;
}
#endif
///
/// Returns a bit array with content aliasing a buffer.
///
/// A buffer.
/// Size of the buffer in bytes. Must be a multiple of 8.
/// The allocator that was used to create the buffer.
/// A bit array with content aliasing a buffer.
public static unsafe NativeBitArray ConvertExistingDataToNativeBitArray(void* ptr, int sizeInBytes, AllocatorManager.AllocatorHandle allocator)
{
return new NativeBitArray
{
m_BitArray = new UnsafeBitArray(ptr, sizeInBytes, allocator),
};
}
}
}