Fix BitArray bit indexing
This commit is contained in:
@@ -16,6 +16,7 @@ API_CLASS(InBuild) class BitArray
|
|||||||
public:
|
public:
|
||||||
using ItemType = uint64;
|
using ItemType = uint64;
|
||||||
using AllocationData = typename AllocationType::template Data<ItemType>;
|
using AllocationData = typename AllocationType::template Data<ItemType>;
|
||||||
|
static constexpr int32 ItemBitCount = 64;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int32 _count;
|
int32 _count;
|
||||||
@@ -209,8 +210,8 @@ public:
|
|||||||
bool Get(const int32 index) const
|
bool Get(const int32 index) const
|
||||||
{
|
{
|
||||||
ASSERT(index >= 0 && index < _count);
|
ASSERT(index >= 0 && index < _count);
|
||||||
const ItemType offset = index / sizeof(ItemType);
|
const ItemType offset = index / ItemBitCount;
|
||||||
const ItemType bitMask = (ItemType)(int32)(1 << (index & ((int32)sizeof(ItemType) - 1)));
|
const ItemType bitMask = (ItemType)(1 << (index & (ItemBitCount - 1)));
|
||||||
const ItemType item = ((ItemType*)_allocation.Get())[offset];
|
const ItemType item = ((ItemType*)_allocation.Get())[offset];
|
||||||
return (item & bitMask) != 0;
|
return (item & bitMask) != 0;
|
||||||
}
|
}
|
||||||
@@ -223,13 +224,13 @@ public:
|
|||||||
void Set(const int32 index, const bool value)
|
void Set(const int32 index, const bool value)
|
||||||
{
|
{
|
||||||
ASSERT(index >= 0 && index < _count);
|
ASSERT(index >= 0 && index < _count);
|
||||||
const ItemType offset = index / sizeof(ItemType);
|
const ItemType offset = index / ItemBitCount;
|
||||||
const ItemType bitMask = (ItemType)(int32)(1 << (index & ((int32)sizeof(ItemType) - 1)));
|
const ItemType bitMask = (ItemType)(1 << (index & (ItemBitCount - 1)));
|
||||||
ItemType& item = ((ItemType*)_allocation.Get())[offset];
|
ItemType& item = ((ItemType*)_allocation.Get())[offset];
|
||||||
if (value)
|
if (value)
|
||||||
item |= bitMask;
|
item |= bitMask; // Set the bit
|
||||||
else
|
else
|
||||||
item &= ~bitMask; // Clear the bit
|
item &= ~bitMask; // Unset the bit
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -78,6 +78,32 @@ TEST_CASE("Array")
|
|||||||
|
|
||||||
TEST_CASE("BitArray")
|
TEST_CASE("BitArray")
|
||||||
{
|
{
|
||||||
|
SECTION("Test Access")
|
||||||
|
{
|
||||||
|
BitArray<> a1;
|
||||||
|
CHECK(a1.Count() == 0);
|
||||||
|
for (int32 i = 0; i < 310; i++)
|
||||||
|
{
|
||||||
|
a1.Add(false);
|
||||||
|
}
|
||||||
|
CHECK(a1.Count() == 310);
|
||||||
|
a1.Resize(300);
|
||||||
|
CHECK(a1.Count() == 300);
|
||||||
|
CHECK(a1.Capacity() >= 300);
|
||||||
|
a1.SetAll(true);
|
||||||
|
a1.SetAll(false);
|
||||||
|
for (int32 i = 0; i < 300; i++)
|
||||||
|
{
|
||||||
|
a1.Set(i, true);
|
||||||
|
for (int32 j = 0; j < 300; j++)
|
||||||
|
{
|
||||||
|
bool expected = j == i;
|
||||||
|
CHECK(a1.Get(j) == expected);
|
||||||
|
}
|
||||||
|
a1.Set(i, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("Test Allocators")
|
SECTION("Test Allocators")
|
||||||
{
|
{
|
||||||
BitArray<> a1;
|
BitArray<> a1;
|
||||||
@@ -142,7 +168,7 @@ TEST_CASE("BitArray")
|
|||||||
|
|
||||||
// Generate some random data for testing
|
// Generate some random data for testing
|
||||||
BitArray<> testData;
|
BitArray<> testData;
|
||||||
testData.Resize(32);
|
testData.Resize(128);
|
||||||
RandomStream rand(101);
|
RandomStream rand(101);
|
||||||
for (int32 i = 0; i < testData.Count(); i++)
|
for (int32 i = 0; i < testData.Count(); i++)
|
||||||
testData.Set(i, rand.GetBool());
|
testData.Set(i, rand.GetBool());
|
||||||
@@ -151,8 +177,8 @@ TEST_CASE("BitArray")
|
|||||||
{
|
{
|
||||||
const BitArray<> a1(testData);
|
const BitArray<> a1(testData);
|
||||||
const BitArray<InlinedAllocation<8>> a2(testData);
|
const BitArray<InlinedAllocation<8>> a2(testData);
|
||||||
const BitArray<InlinedAllocation<64>> a3(testData);
|
const BitArray<InlinedAllocation<256>> a3(testData);
|
||||||
const BitArray<FixedAllocation<64>> a4(testData);
|
const BitArray<FixedAllocation<256>> a4(testData);
|
||||||
CHECK(a1 == testData);
|
CHECK(a1 == testData);
|
||||||
CHECK(a2 == testData);
|
CHECK(a2 == testData);
|
||||||
CHECK(a3 == testData);
|
CHECK(a3 == testData);
|
||||||
|
|||||||
Reference in New Issue
Block a user