[PATCH] D18281: [SetVector] Add erase() method

Jun Bum Lim via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 18 14:14:10 PDT 2016

junbuml created this revision.
junbuml added reviewers: MatzeB, qcolombet, mcrosier.
junbuml added a subscriber: llvm-commits.
Herald added a subscriber: mcrosier.

Add erase() which returns an iterator pointing to the next element after the
erased one. This makes it possible to erase selected elements while iterating
over the SetVector :    
  while (I != E)
    if (test(*I))
      I = SetVector.erase(I);



Index: include/llvm/ADT/SetVector.h
--- include/llvm/ADT/SetVector.h
+++ include/llvm/ADT/SetVector.h
@@ -151,6 +151,21 @@
     return false;
+  /// \brief Erase a single element from the set vector.
+  /// \returns an iterator pointing to the next element that followed the
+  /// element erased. This is the end of the SetVector if the last element is
+  /// erased.
+  iterator erase(iterator I) {
+    const key_type &V = *I;
+    typename vector_type::iterator VI =
+        std::find(vector_.begin(), vector_.end(), V);
+    assert(VI != vector_.end() && "Iterator to erase is out of bounds.");
+    if (!set_.erase(V)) {
+      assert(false && "Corrupted SetVector instances!");
+    }
+    return vector_.erase(VI);
+  }
   /// \brief Remove items from the set vector based on a predicate function.
   /// This is intended to be equivalent to the following code, if we could

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18281.51068.patch
Type: text/x-patch
Size: 967 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160318/ab88fd3b/attachment-0001.bin>

More information about the llvm-commits mailing list