/// 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.
/// 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)
{
CheckArgsUlong(pos,numBits);
returnBitwise.GetBits(Ptr,Length,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>
/// 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>The index of the first occurrence in this array of `numBits` contiguous 0 bits. Range is pos up to (but not including) the length of this array. Returns int.MaxValue if no occurrence is found.</returns>
/// 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>The index of the first occurrence in this array of `numBits` contiguous 0 bits. Range is pos up to (but not including) `pos + count`. Returns int.MaxValue if no occurrence is found.</returns>
/// 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>
publicboolTestNone(intpos,intnumBits=1)
{
CheckArgs(pos,numBits);
returnBitwise.TestNone(Ptr,Length,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 or 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>
publicboolTestAny(intpos,intnumBits=1)
{
CheckArgs(pos,numBits);
returnBitwise.TestAny(Ptr,Length,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>
publicboolTestAll(intpos,intnumBits=1)
{
CheckArgs(pos,numBits);
returnBitwise.TestAll(Ptr,Length,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>
publicintCountBits(intpos,intnumBits=1)
{
CheckArgs(pos,numBits);
returnBitwise.CountBits(Ptr,Length,pos,numBits);
}
/// <summary>
/// Returns a readonly version of this UnsafeBitArray instance.
/// </summary>
/// <remarks>ReadOnly containers point to the same underlying data as the UnsafeBitArray it is made from.</remarks>
/// <returns>ReadOnly instance for this.</returns>
publicReadOnlyAsReadOnly()
{
returnnewReadOnly(Ptr,Length);
}
/// <summary>
/// A read-only alias for the value of a UnsafeBitArray. Does not have its own allocated storage.
/// </summary>
publicstructReadOnly
{
/// <summary>
/// Pointer to the data.
/// </summary>
/// <value>Pointer to the data.</value>
[NativeDisableUnsafePtrRestriction]
publicreadonlyulong*Ptr;
/// <summary>
/// The number of bits.
/// </summary>
/// <value>The number of bits.</value>
publicreadonlyintLength;
/// <summary>
/// Whether this array has been allocated (and not yet deallocated).
/// </summary>
/// <value>True if this array has been allocated (and not yet deallocated).</value>
publicreadonlyboolIsCreated=>Ptr!=null;
/// <summary>
/// Whether the container is empty.
/// </summary>
/// <value>True if the container is empty or the container has not been constructed.</value>
publicreadonlyboolIsEmpty=>!IsCreated||Length==0;
internalReadOnly(ulong*ptr,intlength)
{
Ptr=ptr;
Length=length;
}
/// <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>
publicreadonlyulongGetBits(intpos,intnumBits=1)
{
CheckArgsUlong(pos,numBits);
returnBitwise.GetBits(Ptr,Length,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)
{
CheckArgs(pos,1);
returnBitwise.IsSet(Ptr,pos);
}
/// <summary>
/// Returns the index of the first occurrence in this array of *N* contiguous 0 bits.
/// </summary>
/// <remarks>The search is linear.</remarks>
/// <param name="pos">Index of the bit at which to start searching.</param>
/// <param name="numBits">Number of contiguous 0 bits to look for.</param>
/// <returns>The index of the first occurrence in this array of `numBits` contiguous 0 bits. Range is pos up to (but not including) the length of this array. Returns int.MaxValue if no occurrence is found.</returns>
/// <returns>The index of the first occurrence in this array of `numBits` contiguous 0 bits. Range is pos up to (but not including) `pos + count`. Returns int.MaxValue if no occurrence is found.</returns>
thrownewArgumentException($"BitArray invalid arguments: Out of bounds - source position {srcPos}, numBits {numBits}, source bit array Length {srcBitArray.Length}.");
}
if(dstPos+numBits>dstBitArray.Length)
{
thrownewArgumentException($"BitArray invalid arguments: Out of bounds - destination position {dstPos}, numBits {numBits}, destination bit array Length {dstBitArray.Length}.");