Skip to content

Commit

Permalink
Cleanup BitVector32 (#54097)
Browse files Browse the repository at this point in the history
* Cleanup BitVector32

* Cleanup Section.ToString
  • Loading branch information
huoyaoyuan committed Jun 14, 2021
1 parent 4c0d2f0 commit 342e4cd
Showing 1 changed file with 12 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;

namespace System.Collections.Specialized
{
Expand Down Expand Up @@ -93,18 +92,6 @@ public int Data
}
}

private static short CountBitsSet(short mask)
{
// We assume that the bits are always right aligned, with no holes (i.e. always 00000111, never 00100011)
short value = 0;
while ((mask & 0x1) != 0)
{
value++;
mask >>= 1;
}
return value;
}

/// <devdoc>
/// <para> Creates the first mask in a series.</para>
/// </devdoc>
Expand All @@ -131,29 +118,6 @@ public static int CreateMask(int previous)
return previous << 1;
}

/// <devdoc>
/// Given a highValue, creates the mask
/// </devdoc>
private static short CreateMaskFromHighValue(short highValue)
{
short required = 16;
while ((highValue & 0x8000) == 0)
{
required--;
highValue <<= 1;
}

ushort value = 0;
while (required > 0)
{
required--;
value <<= 1;
value |= 0x1;
}

return unchecked((short)value);
}

/// <devdoc>
/// <para>Creates the first section in a series, with the specified maximum value.</para>
/// </devdoc>
Expand All @@ -177,28 +141,19 @@ private static Section CreateSectionHelper(short maxValue, short priorMask, shor
throw new ArgumentException(SR.Format(SR.Argument_InvalidValue_TooSmall, nameof(maxValue), 1), nameof(maxValue));
}

short offset = (short)(priorOffset + CountBitsSet(priorMask));
short offset = (short)(priorOffset + BitOperations.PopCount((uint)(ushort)priorMask));
if (offset >= 32)
{
throw new InvalidOperationException(SR.BitVectorFull);
}
return new Section(CreateMaskFromHighValue(maxValue), offset);
}

public override bool Equals([NotNullWhen(true)] object? o)
{
if (!(o is BitVector32))
{
return false;
}

return _data == ((BitVector32)o)._data;
short mask = (short)(BitOperations.RoundUpToPowerOf2((uint)(ushort)maxValue + 1) - 1);
return new Section(mask, offset);
}

public override int GetHashCode()
{
return base.GetHashCode();
}
public override bool Equals([NotNullWhen(true)] object? o) => o is BitVector32 other && _data == other._data;

public override int GetHashCode() => _data.GetHashCode();

public static string ToString(BitVector32 value)
{
Expand Down Expand Up @@ -238,29 +193,11 @@ internal Section(short mask, short offset)
_offset = offset;
}

public short Mask
{
get
{
return _mask;
}
}
public short Mask => _mask;

public short Offset
{
get
{
return _offset;
}
}
public short Offset => _offset;

public override bool Equals([NotNullWhen(true)] object? o)
{
if (o is Section)
return Equals((Section)o);
else
return false;
}
public override bool Equals([NotNullWhen(true)] object? o) => o is Section other && Equals(other);

public bool Equals(Section obj)
{
Expand All @@ -277,14 +214,11 @@ public bool Equals(Section obj)
return !(a == b);
}

public override int GetHashCode()
{
return base.GetHashCode();
}
public override int GetHashCode() => HashCode.Combine(_mask, _offset);

public static string ToString(Section value)
{
return "Section{0x" + Convert.ToString(value.Mask, 16) + ", 0x" + Convert.ToString(value.Offset, 16) + "}";
return $"Section{{0x{value.Mask:x}, 0x{value.Offset:x}}}";
}

public override string ToString()
Expand Down

0 comments on commit 342e4cd

Please sign in to comment.