Add rdcarray helper for "resize array up to this index if smaller"

* This is a common enough operation to warrant a helper, as an alternative to
  vec.resize(RDCMAX(index + 1, vec.size())
This commit is contained in:
baldurk
2020-06-01 13:34:08 +01:00
parent ae38a10299
commit 0f8ad41359
2 changed files with 47 additions and 0 deletions
+10
View File
@@ -251,6 +251,16 @@ public:
allocatedCount = s;
}
void resize_for_index(size_t s)
{
// do nothing if we're already big enough
if(size() >= s + 1)
return;
// otherwise resize so that [s] is valid
resize(s + 1);
}
void resize(size_t s)
{
// do nothing if we're already this size
+37
View File
@@ -463,6 +463,43 @@ TEST_CASE("Test array type", "[basictypes]")
CHECK(vec[1] == 5);
};
SECTION("resize_for_index")
{
rdcarray<int> test;
CHECK(test.empty());
test.resize_for_index(0);
CHECK(test.size() == 1);
CHECK(test.capacity() >= 1);
test.resize_for_index(5);
CHECK(test.size() == 5);
CHECK(test.capacity() >= 5);
test.resize_for_index(5);
CHECK(test.size() == 5);
CHECK(test.capacity() >= 5);
test.resize_for_index(3);
CHECK(test.size() == 5);
CHECK(test.capacity() >= 5);
test.resize_for_index(0);
CHECK(test.size() == 5);
CHECK(test.capacity() >= 5);
test.resize_for_index(9);
CHECK(test.size() == 9);
CHECK(test.capacity() >= 9);
};
SECTION("Check construction")
{
rdcarray<ConstructorCounter> test;