diff options
author | Chandler Carruth <chandlerc@gmail.com> | 2012-10-03 01:04:07 +0000 |
---|---|---|
committer | Chandler Carruth <chandlerc@gmail.com> | 2012-10-03 01:04:07 +0000 |
commit | de2fae4c7bfaedb95705b272015592895e05fd9c (patch) | |
tree | 811062f8705fa1d038cc9ae5629aeb4fd754121e | |
parent | 0d141f867d49dadc2f7580b149464f1e8e095f03 (diff) |
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
-rw-r--r-- | include/llvm/ADT/SetVector.h | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/include/llvm/ADT/SetVector.h b/include/llvm/ADT/SetVector.h index 0ff0f46f88..d2f7286c25 100644 --- a/include/llvm/ADT/SetVector.h +++ b/include/llvm/ADT/SetVector.h @@ -141,15 +141,12 @@ public: /// \returns true if any element is removed. template <typename UnaryPredicate> bool remove_if(UnaryPredicate P) { - typename vector_type::iterator B = std::partition(vector_.begin(), - vector_.end(), - std::not1(P)), - E = vector_.end(); - if (B == E) + typename vector_type::iterator I + = std::remove_if(vector_.begin(), vector_.end(), + TestAndEraseFromSet<UnaryPredicate>(P, set_)); + if (I == vector_.end()) return false; - for (typename vector_type::iterator I = B; I != E; ++I) - set_.erase(*I); - vector_.erase(B, E); + vector_.erase(I, vector_.end()); return true; } @@ -188,6 +185,29 @@ public: } private: + /// \brief A wrapper predicate designed for use with std::remove_if. + /// + /// This predicate wraps a predicate suitable for use with std::remove_if to + /// call set_.erase(x) on each element which is slated for removal. + template <typename UnaryPredicate> + class TestAndEraseFromSet { + UnaryPredicate P; + set_type &set_; + + public: + typedef typename UnaryPredicate::argument_type argument_type; + + TestAndEraseFromSet(UnaryPredicate P, set_type &set_) : P(P), set_(set_) {} + + bool operator()(argument_type Arg) { + if (P(Arg)) { + set_.erase(Arg); + return true; + } + return false; + } + }; + set_type set_; ///< The set. vector_type vector_; ///< The vector. }; |