[PATCH] D90894: [STLExtras] Add append_range helper.

Sean Silva via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 5 16:00:47 PST 2020


silvas created this revision.
Herald added subscribers: llvm-commits, dexonsmith.
Herald added a project: LLVM.
silvas requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90894

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


Index: llvm/unittests/ADT/STLExtrasTest.cpp
===================================================================
--- llvm/unittests/ADT/STLExtrasTest.cpp
+++ llvm/unittests/ADT/STLExtrasTest.cpp
@@ -323,6 +323,15 @@
   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;
Index: llvm/include/llvm/ADT/STLExtras.h
===================================================================
--- llvm/include/llvm/ADT/STLExtras.h
+++ llvm/include/llvm/ADT/STLExtras.h
@@ -1676,6 +1676,14 @@
   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>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90894.303288.patch
Type: text/x-patch
Size: 1252 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201106/bbd1b53c/attachment.bin>


More information about the llvm-commits mailing list