[llvm] e9e2e31 - [STLExtras] Add append_range helper.

Sean Silva via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 5 16:20:17 PST 2020


Author: Sean Silva
Date: 2020-11-05T16:20:02-08:00
New Revision: e9e2e3107d6b4fb1bfdd877a83f1e214fcefea76

URL: https://github.com/llvm/llvm-project/commit/e9e2e3107d6b4fb1bfdd877a83f1e214fcefea76
DIFF: https://github.com/llvm/llvm-project/commit/e9e2e3107d6b4fb1bfdd877a83f1e214fcefea76.diff

LOG: [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

Added: 
    

Modified: 
    llvm/include/llvm/ADT/STLExtras.h
    llvm/unittests/ADT/STLExtrasTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index f3f9f20dd4b5..5a5d47b783c2 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 <typename Container, typename Range>
+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<typename Container, typename RandomAccessIterator>

diff  --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index aaef46a2cf05..a4e151cfcc54 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<int> 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<int> data;


        


More information about the llvm-commits mailing list