Browse Source

cmAlgorithms: Add cmRemoveMatching algorithm.

Implement it in terms of std::remove_if with a binary search through
a matching range.
Stephen Kelly 10 years ago
parent
commit
050958a328
1 changed files with 24 additions and 0 deletions
  1. 24 0
      Source/cmAlgorithms.h

+ 24 - 0
Source/cmAlgorithms.h

@@ -154,6 +154,23 @@ Iter RemoveN(Iter i1, Iter i2, size_t n)
   return ContainerAlgorithms::Rotate(i1, i1 + n, i2);
 }
 
+template<typename Range>
+struct BinarySearcher
+{
+  typedef typename Range::value_type argument_type;
+  BinarySearcher(Range const& r)
+    : m_range(r)
+  {
+  }
+
+  bool operator()(argument_type const& item)
+  {
+    return std::binary_search(m_range.begin(), m_range.end(), item);
+  }
+private:
+  Range const& m_range;
+};
+
 }
 
 template<typename Iter1, typename Iter2>
@@ -226,4 +243,11 @@ typename Range::const_iterator cmRemoveIndices(Range& r, InputRange const& rem)
   return writer;
 }
 
+template<typename Range, typename MatchRange>
+typename Range::const_iterator cmRemoveMatching(Range &r, MatchRange const& m)
+{
+  return std::remove_if(r.begin(), r.end(),
+                        ContainerAlgorithms::BinarySearcher<MatchRange>(m));
+}
+
 #endif