Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/libraries/Common/src/System/Collections/Generic/BitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Numerics;

namespace System.Collections.Generic
{
Expand Down Expand Up @@ -33,6 +34,26 @@ internal void MarkBit(int bitPosition)
}
}

internal bool TryMarkBit(int bitPosition)
{
Debug.Assert(bitPosition >= 0);

uint bitArrayIndex = (uint)bitPosition / IntSize;

// Workaround for https://github.com/dotnet/runtime/issues/72004
Span<int> span = _span;
if (bitArrayIndex < (uint)span.Length)
{
int bits = span[(int)bitArrayIndex];
if ((bits & (1 << ((int)((uint)bitPosition % IntSize)))) == 0)
{
span[(int)bitArrayIndex] = bits | (1 << ((int)((uint)bitPosition % IntSize)));
return true;
}
}
return false;
}

internal bool IsMarked(int bitPosition)
{
Debug.Assert(bitPosition >= 0);
Expand All @@ -46,7 +67,26 @@ internal bool IsMarked(int bitPosition)
(span[(int)bitArrayIndex] & (1 << ((int)((uint)bitPosition % IntSize)))) != 0;
}

internal bool IsUnmarked(int bitPosition)
{
Debug.Assert(bitPosition >= 0);

uint bitArrayIndex = (uint)bitPosition / IntSize;

// Workaround for https://github.com/dotnet/runtime/issues/72004
ReadOnlySpan<int> span = _span;
return
bitArrayIndex < (uint)span.Length &&
(span[(int)bitArrayIndex] & (1 << ((int)((uint)bitPosition % IntSize)))) == 0;
}

internal int FindFirstUnmarked()
{
int i = _span.IndexOfAnyExcept(~0);
return i < 0 ? -1 : i * IntSize + BitOperations.TrailingZeroCount(~_span[i]);
}

/// <summary>How many ints must be allocated to represent n bits. Returns (n+31)/32, but avoids overflow.</summary>
internal static int ToIntArrayLength(int n) => n > 0 ? ((n - 1) / IntSize + 1) : 0;
internal static int ToIntArrayLength(int n) => (int)(((uint)n + 31) / IntSize);
}
}
Loading
Loading