llvm.org GIT mirror llvm / de2fae4
Third try at fixing this. ;] Go back to using std::remove_if, which has most of the behavior we want, but wrap the predicate in one which erases elements from the set if they pass the predicate. Oh what I wouldn't give for a lambda here. Let me know if the predicate wrapping is too much magic. ;] git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@165076 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 7 years ago
1 changed file(s) with 28 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
140140 /// \returns true if any element is removed.
141141 template
142142 bool remove_if(UnaryPredicate P) {
143 typename vector_type::iterator B = std::partition(vector_.begin(),
144 vector_.end(),
145 std::not1(P)),
146 E = vector_.end();
147 if (B == E)
143 typename vector_type::iterator I
144 = std::remove_if(vector_.begin(), vector_.end(),
145 TestAndEraseFromSet(P, set_));
146 if (I == vector_.end())
148147 return false;
149 for (typename vector_type::iterator I = B; I != E; ++I)
150 set_.erase(*I);
151 vector_.erase(B, E);
148 vector_.erase(I, vector_.end());
152149 return true;
153150 }
154151
187184 }
188185
189186 private:
187 /// \brief A wrapper predicate designed for use with std::remove_if.
188 ///
189 /// This predicate wraps a predicate suitable for use with std::remove_if to
190 /// call set_.erase(x) on each element which is slated for removal.
191 template
192 class TestAndEraseFromSet {
193 UnaryPredicate P;
194 set_type &set_;
195
196 public:
197 typedef typename UnaryPredicate::argument_type argument_type;
198
199 TestAndEraseFromSet(UnaryPredicate P, set_type &set_) : P(P), set_(set_) {}
200
201 bool operator()(argument_type Arg) {
202 if (P(Arg)) {
203 set_.erase(Arg);
204 return true;
205 }
206 return false;
207 }
208 };
209
190210 set_type set_; ///< The set.
191211 vector_type vector_; ///< The vector.
192212 };