diff --git a/Source/Engine/Core/Collections/BitArray.h b/Source/Engine/Core/Collections/BitArray.h index 01238d434..78e4f94f4 100644 --- a/Source/Engine/Core/Collections/BitArray.h +++ b/Source/Engine/Core/Collections/BitArray.h @@ -22,6 +22,16 @@ private: int32 _capacity; AllocationData _allocation; + FORCE_INLINE static int32 ToItemCount(int32 size) + { + return Math::DivideAndRoundUp(size, sizeof(ItemType)); + } + + FORCE_INLINE static int32 ToItemCapacity(int32 size) + { + return Math::Max(Math::DivideAndRoundUp(size, sizeof(ItemType)), 1); + } + public: /// /// Initializes a new instance of the class. @@ -41,7 +51,7 @@ public: , _capacity(capacity) { if (capacity > 0) - _allocation.Allocate(Math::Max(capacity / sizeof(ItemType), 1)); + _allocation.Allocate(ToItemCapacity(capacity)); } /// @@ -53,7 +63,7 @@ public: _count = _capacity = other.Count(); if (_capacity > 0) { - const uint64 itemsCapacity = Math::Max(_capacity / sizeof(ItemType), 1); + const uint64 itemsCapacity = ToItemCapacity(_capacity); _allocation.Allocate(itemsCapacity); Platform::MemoryCopy(Get(), other.Get(), itemsCapacity * sizeof(ItemType)); } @@ -69,7 +79,7 @@ public: _count = _capacity = other.Count(); if (_capacity > 0) { - const uint64 itemsCapacity = Math::Max(_capacity / sizeof(ItemType), 1); + const uint64 itemsCapacity = ToItemCapacity(_capacity); _allocation.Allocate(itemsCapacity); Platform::MemoryCopy(Get(), other.Get(), itemsCapacity * sizeof(ItemType)); } @@ -101,7 +111,7 @@ public: { _allocation.Free(); _capacity = other._count; - const uint64 itemsCapacity = Math::Max(_capacity / sizeof(ItemType), 1); + const uint64 itemsCapacity = ToItemCapacity(_capacity); _allocation.Allocate(itemsCapacity); Platform::MemoryCopy(Get(), other.Get(), itemsCapacity * sizeof(ItemType)); } @@ -246,7 +256,7 @@ public: return; ASSERT(capacity >= 0); const int32 count = preserveContents ? (_count < capacity ? _count : capacity) : 0; - _allocation.Relocate(Math::Max(capacity / sizeof(ItemType), 1), _count / sizeof(ItemType), count / sizeof(ItemType)); + _allocation.Relocate(ToItemCapacity(capacity), ToItemCount(_count), ToItemCount(count)); _capacity = capacity; _count = count; } @@ -272,7 +282,7 @@ public: { if (_capacity < minCapacity) { - const int32 capacity = _allocation.CalculateCapacityGrow(Math::Max(_capacity / sizeof(ItemType), 1), minCapacity); + const int32 capacity = _allocation.CalculateCapacityGrow(ToItemCapacity(_capacity), minCapacity); SetCapacity(capacity, preserveContents); } } @@ -284,7 +294,7 @@ public: void SetAll(const bool value) { if (_count != 0) - Platform::MemorySet(_allocation.Get(), Math::Max(_count / sizeof(ItemType), 1), value ? MAX_int32 : 0); + Platform::MemorySet(_allocation.Get(), ToItemCount(_count) * sizeof(ItemType), value ? MAX_uint64 : 0); } /// diff --git a/Source/Engine/Tests/TestCollections.cpp b/Source/Engine/Tests/TestCollections.cpp index 24c971860..ff5f34bf6 100644 --- a/Source/Engine/Tests/TestCollections.cpp +++ b/Source/Engine/Tests/TestCollections.cpp @@ -109,6 +109,19 @@ TEST_CASE("BitArray") a1 = testData; CHECK(a1 == testData); } + + SECTION("Test Set All") + { + BitArray<> a1; + a1.Resize(9); + CHECK(a1.Count() == 9); + a1.SetAll(true); + for (int32 i = 0; i < a1.Count(); i++) + CHECK(a1[i] == true); + a1.SetAll(false); + for (int32 i = 0; i < a1.Count(); i++) + CHECK(a1[i] == false); + } } TEST_CASE("HashSet")