From 0f8ad4135969bd5d720dc8bb467bff24247c1462 Mon Sep 17 00:00:00 2001 From: baldurk Date: Mon, 1 Jun 2020 13:34:08 +0100 Subject: [PATCH] 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()) --- renderdoc/api/replay/rdcarray.h | 10 +++++++ renderdoc/replay/basic_types_tests.cpp | 37 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/renderdoc/api/replay/rdcarray.h b/renderdoc/api/replay/rdcarray.h index 266e49122..f71ccb3dc 100644 --- a/renderdoc/api/replay/rdcarray.h +++ b/renderdoc/api/replay/rdcarray.h @@ -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 diff --git a/renderdoc/replay/basic_types_tests.cpp b/renderdoc/replay/basic_types_tests.cpp index 9980541a6..8a1aeb6f0 100644 --- a/renderdoc/replay/basic_types_tests.cpp +++ b/renderdoc/replay/basic_types_tests.cpp @@ -463,6 +463,43 @@ TEST_CASE("Test array type", "[basictypes]") CHECK(vec[1] == 5); }; + SECTION("resize_for_index") + { + rdcarray 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 test;