From e9e2e3107d6b4fb1bfdd877a83f1e214fcefea76 Mon Sep 17 00:00:00 2001 From: Sean Silva Date: Thu, 5 Nov 2020 16:00:38 -0800 Subject: [PATCH] [STLExtras] Add append_range helper. This is convenient in a lot of cases, such as when the thing you want to append is `someReallyLongFunctionName()` that you'd rather not write twice or assign to a variable for the paired begin/end calls. Differential Revision: https://reviews.llvm.org/D90894 --- llvm/include/llvm/ADT/STLExtras.h | 8 ++++++++ llvm/unittests/ADT/STLExtrasTest.cpp | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index f3f9f20..5a5d47b 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -1676,6 +1676,14 @@ void erase_value(Container &C, ValueType V) { C.erase(std::remove(C.begin(), C.end(), V), C.end()); } +/// Wrapper function to append a range to a container. +/// +/// C.insert(C.end(), R.begin(), R.end()); +template +inline void append_range(Container &C, Range &&R) { + C.insert(C.end(), R.begin(), R.end()); +} + /// Given a sequence container Cont, replace the range [ContIt, ContEnd) with /// the range [ValIt, ValEnd) (which is not from the same container). template diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp index aaef46a..a4e151c 100644 --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -323,6 +323,15 @@ TEST(STLExtrasTest, EraseIf) { EXPECT_EQ(7, V[3]); } +TEST(STLExtrasTest, AppendRange) { + auto AppendVals = {3}; + std::vector V = {1, 2}; + append_range(V, AppendVals); + EXPECT_EQ(1, V[0]); + EXPECT_EQ(2, V[1]); + EXPECT_EQ(3, V[2]); +} + namespace some_namespace { struct some_struct { std::vector data; -- 2.7.4