cmAlgorithms: Add cmRemoveMatching algorithm.

Implement it in terms of std::remove_if with a binary search through
a matching range.
This commit is contained in:
Stephen Kelly 2015-02-15 18:58:36 +01:00
parent a77af8f130
commit 050958a328
1 changed files with 24 additions and 0 deletions

View File

@ -154,6 +154,23 @@ Iter RemoveN(Iter i1, Iter i2, size_t n)
return ContainerAlgorithms::Rotate(i1, i1 + n, i2); 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> template<typename Iter1, typename Iter2>
@ -226,4 +243,11 @@ typename Range::const_iterator cmRemoveIndices(Range& r, InputRange const& rem)
return writer; 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 #endif