llvm.org GIT mirror llvm / c9a0644
[SetVector] Add erase() method Summary: 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); else ++I; Reviewers: qcolombet, mcrosier, MatzeB, dblaikie Subscribers: dberlin, dblaikie, mcrosier, llvm-commits Differential Revision: http://reviews.llvm.org/D18281 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264414 91177308-0d34-0410-b5e6-96231b3b80d8 Jun Bum Lim 4 years ago
3 changed file(s) with 46 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
150150 return false;
151151 }
152152
153 /// Erase a single element from the set vector.
154 /// \returns an iterator pointing to the next element that followed the
155 /// element erased. This is the end of the SetVector if the last element is
156 /// erased.
157 iterator erase(iterator I) {
158 const key_type &V = *I;
159 assert(set_.count(V) && "Corrupted SetVector instances!");
160 set_.erase(V);
161 return vector_.erase(I);
162 }
163
153164 /// \brief Remove items from the set vector based on a predicate function.
154165 ///
155166 /// This is intended to be equivalent to the following code, if we could
3131 PostOrderIteratorTest.cpp
3232 RangeAdapterTest.cpp
3333 SCCIteratorTest.cpp
34 SetVectorTest.cpp
3435 SmallPtrSetTest.cpp
3536 SmallStringTest.cpp
3637 SmallVectorTest.cpp
0 //===- llvm/unittest/ADT/SetVector.cpp ------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // SetVector unit tests.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/ADT/SetVector.h"
14 #include "gtest/gtest.h"
15
16 using namespace llvm;
17
18 TEST(SetVector, EraseTest) {
19 SetVector S;
20 S.insert(0);
21 S.insert(1);
22 S.insert(2);
23
24 auto I = S.erase(std::next(S.begin()));
25
26 // Test that the returned iterator is the expected one-after-erase
27 // and the size/contents is the expected sequence {0, 2}.
28 EXPECT_EQ(std::next(S.begin()), I);
29 EXPECT_EQ(2u, S.size());
30 EXPECT_EQ(0, *S.begin());
31 EXPECT_EQ(2, *std::next(S.begin()));
32 }
33