Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create BitArray.HasAllSet() and .HasAnySet(). #81527

2 changes: 2 additions & 0 deletions src/libraries/System.Collections/ref/System.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public sealed partial class BitArray : System.Collections.ICollection, System.Co
public void Set(int index, bool value) { }
public void SetAll(bool value) { }
public System.Collections.BitArray Xor(System.Collections.BitArray value) { throw null; }
public bool HasAllSet() { throw null; }
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
public bool HasAnySet() { throw null; }
}
public static partial class StructuralComparisons
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,56 @@ public unsafe void CopyTo(Array array, int index)
}
}

public bool HasAllSet()
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
Div32Rem(m_length, out int extraBits);
int intCount = GetInt32ArrayLengthFromBitLength(m_length);
if (extraBits != 0)
{
intCount--;
}

const int allSetInt = -1; // 0xFF_FF_FF_FF
if (m_array.AsSpan(0, intCount).IndexOfAnyExcept(allSetInt) != -1)
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
}
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
if (extraBits == 0)
{
return true;
}

Debug.Assert(GetInt32ArrayLengthFromBitLength(m_length) > 0);
Debug.Assert(intCount == GetInt32ArrayLengthFromBitLength(m_length) - 1);

int mask = (1 << extraBits) - 1;
return (m_array[intCount] & mask) == mask;
}

public bool HasAnySet()
{
Div32Rem(m_length, out int extraBits);
int intCount = GetInt32ArrayLengthFromBitLength(m_length);
if (extraBits != 0)
{
intCount--;
}

if (m_array.AsSpan(0, intCount).IndexOfAnyExcept(0) != -1)
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
return true;
}
if (extraBits == 0)
{
return false;
}

Debug.Assert(GetInt32ArrayLengthFromBitLength(m_length) > 0);
Debug.Assert(intCount == GetInt32ArrayLengthFromBitLength(m_length) - 1);

return (m_array[intCount] & (1 << extraBits) - 1) != 0;
}

stephentoub marked this conversation as resolved.
Show resolved Hide resolved
public int Count => m_length;

public object SyncRoot => this;
Expand Down