Add TestBitSet and fix bugs
This commit is contained in:
parent
861c951d56
commit
fdb73a92b8
|
@ -51,6 +51,7 @@
|
|||
</Choose>
|
||||
<ItemGroup>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="TestBitSet.cs" />
|
||||
<Compile Include="TestGraphNodes.cs" />
|
||||
<Compile Include="TestIntervalSet.cs" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
namespace Antlr4.Runtime.Test
|
||||
{
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Sharpen;
|
||||
|
||||
[TestClass]
|
||||
public class TestBitSet
|
||||
{
|
||||
[TestMethod]
|
||||
public void TestCardinality1()
|
||||
{
|
||||
BitSet set = new BitSet();
|
||||
Assert.AreEqual(0, set.Cardinality());
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
set.Set(i);
|
||||
Assert.AreEqual(i + 1, set.Cardinality());
|
||||
Assert.AreEqual(0, set.NextSetBit(0));
|
||||
if (i > 0)
|
||||
Assert.AreEqual(i * 1 - 1, set.NextSetBit(i * 1 - 1));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestCardinality2()
|
||||
{
|
||||
BitSet set = new BitSet();
|
||||
Assert.AreEqual(0, set.Cardinality());
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
set.Set(i * 2);
|
||||
Assert.AreEqual(i + 1, set.Cardinality());
|
||||
Assert.AreEqual(0, set.NextSetBit(0));
|
||||
if (i > 0)
|
||||
Assert.AreEqual(i * 2, set.NextSetBit(i * 2 - 1));
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestCardinality7()
|
||||
{
|
||||
BitSet set = new BitSet();
|
||||
Assert.AreEqual(0, set.Cardinality());
|
||||
for (int i = 0; i < 128; i++)
|
||||
{
|
||||
set.Set(i * 7);
|
||||
Assert.AreEqual(i + 1, set.Cardinality());
|
||||
Assert.AreEqual(0, set.NextSetBit(0));
|
||||
if (i > 0)
|
||||
Assert.AreEqual(i * 7, set.NextSetBit(i * 7 - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -30,6 +30,7 @@
|
|||
namespace Sharpen
|
||||
{
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
public class BitSet
|
||||
{
|
||||
|
@ -57,7 +58,7 @@ namespace Sharpen
|
|||
private static int GetBitCount(ulong[] value)
|
||||
{
|
||||
int data = 0;
|
||||
uint size = (uint)value.Length / (sizeof(ulong) / sizeof(int));
|
||||
uint size = (uint)value.Length;
|
||||
const ulong m1 = 0x5555555555555555;
|
||||
const ulong m2 = 0x3333333333333333;
|
||||
const ulong m4 = 0x0F0F0F0F0F0F0F0F;
|
||||
|
@ -148,7 +149,7 @@ namespace Sharpen
|
|||
if (element >= _data.Length)
|
||||
return false;
|
||||
|
||||
return (_data[element] & (1U << (index % BitsPerElement))) != 0;
|
||||
return (_data[element] & (1UL << (index % BitsPerElement))) != 0;
|
||||
}
|
||||
|
||||
public void Set(int index)
|
||||
|
@ -160,7 +161,7 @@ namespace Sharpen
|
|||
if (element >= _data.Length)
|
||||
Array.Resize(ref _data, Math.Max(_data.Length * 2, element + 1));
|
||||
|
||||
_data[element] |= 1U << (index % BitsPerElement);
|
||||
_data[element] |= 1UL << (index % BitsPerElement);
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
|
@ -191,15 +192,18 @@ namespace Sharpen
|
|||
if (i >= _data.Length)
|
||||
return -1;
|
||||
|
||||
ulong current = _data[i] & ((1U << (fromIndex % BitsPerElement)) - 1);
|
||||
ulong current = _data[i] & ~((1UL << (fromIndex % BitsPerElement)) - 1);
|
||||
|
||||
for (; i < _data.Length; i++)
|
||||
while (true)
|
||||
{
|
||||
int bit = BitScanForward(current);
|
||||
if (bit >= 0)
|
||||
return bit + i * BitsPerElement;
|
||||
|
||||
i++;
|
||||
if (i >= _data.Length)
|
||||
break;
|
||||
|
||||
current = _data[i];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue