]> granicus.if.org Git - llvm/commitdiff
Add convenience utility for replacing a range within a container with a
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 17 Jun 2019 21:01:09 +0000 (21:01 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 17 Jun 2019 21:01:09 +0000 (21:01 +0000)
different range, in preparation for use in Clang.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363617 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/STLExtras.h

index de030128987a4303ffe315125b33d460544fdcdb..eb9dff3efeda2ff2c1500f0a8273d7261eaab9e8 100644 (file)
@@ -1392,6 +1392,33 @@ void erase_if(Container &C, UnaryPredicate P) {
   C.erase(remove_if(C, P), C.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>
+void replace(Container &Cont, typename Container::iterator ContIt,
+             typename Container::iterator ContEnd, RandomAccessIterator ValIt,
+             RandomAccessIterator ValEnd) {
+  while (true) {
+    if (ValIt == ValEnd) {
+      Cont.erase(ContIt, ContEnd);
+      return;
+    } else if (ContIt == ContEnd) {
+      Cont.insert(ContIt, ValIt, ValEnd);
+      return;
+    }
+    *ContIt++ = *ValIt++;
+  }
+}
+
+/// Given a sequence container Cont, replace the range [ContIt, ContEnd) with
+/// the range R.
+template<typename Container, typename Range = std::initializer_list<
+                                 typename Container::value_type>>
+void replace(Container &Cont, typename Container::iterator ContIt,
+             typename Container::iterator ContEnd, Range R) {
+  replace(Cont, ContIt, ContEnd, R.begin(), R.end());
+}
+
 //===----------------------------------------------------------------------===//
 //     Extra additions to <memory>
 //===----------------------------------------------------------------------===//