aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/SplitVector.h6
-rw-r--r--test/unit/testSplitVector.cxx21
2 files changed, 25 insertions, 2 deletions
diff --git a/src/SplitVector.h b/src/SplitVector.h
index fcd8b0d0e..e805c50dc 100644
--- a/src/SplitVector.h
+++ b/src/SplitVector.h
@@ -206,11 +206,12 @@ public:
/// Add some new empty elements.
/// InsertValue is good for value objects but not for unique_ptr objects
/// since they can only be moved from once.
- void InsertEmpty(ptrdiff_t position, ptrdiff_t insertLength) {
+ /// Callers can write to the returned pointer to transform inputs without copies.
+ T *InsertEmpty(ptrdiff_t position, ptrdiff_t insertLength) {
PLATFORM_ASSERT((position >= 0) && (position <= lengthBody));
if (insertLength > 0) {
if ((position < 0) || (position > lengthBody)) {
- return;
+ return nullptr;
}
RoomFor(insertLength);
GapTo(position);
@@ -222,6 +223,7 @@ public:
part1Length += insertLength;
gapLength -= insertLength;
}
+ return body.data() + position;
}
/// Ensure at least length elements allocated,
diff --git a/test/unit/testSplitVector.cxx b/test/unit/testSplitVector.cxx
index a1426d38a..8a688cd5c 100644
--- a/test/unit/testSplitVector.cxx
+++ b/test/unit/testSplitVector.cxx
@@ -168,6 +168,27 @@ TEST_CASE("SplitVector") {
}
}
+ SECTION("InsertEmpty") {
+ sv.InsertEmpty(0, 0);
+ REQUIRE(0 == sv.Length());
+ int *pi = sv.InsertEmpty(0, 2);
+ REQUIRE(2 == sv.Length());
+ REQUIRE(0 == sv.ValueAt(0));
+ REQUIRE(0 == sv.ValueAt(1));
+ pi[0] = 4;
+ pi[1] = 5;
+ REQUIRE(4 == sv.ValueAt(0));
+ REQUIRE(5 == sv.ValueAt(1));
+ pi = sv.InsertEmpty(1, 2);
+ pi[0] = 6;
+ pi[1] = 7;
+ REQUIRE(4 == sv.Length());
+ REQUIRE(4 == sv.ValueAt(0));
+ REQUIRE(6 == sv.ValueAt(1));
+ REQUIRE(7 == sv.ValueAt(2));
+ REQUIRE(5 == sv.ValueAt(3));
+ }
+
SECTION("SetValue") {
sv.InsertValue(0, 10, 0);
sv.SetValueAt(5, 3);