cmRemoveDuplicates: Type-parameterize all uniq-operations

This commit is contained in:
Stephen Kelly 2015-03-01 21:57:16 +01:00
parent 7cbafa8c65
commit eec7091d76
1 changed files with 22 additions and 11 deletions

View File

@ -176,12 +176,6 @@ private:
Range const& m_range; Range const& m_range;
}; };
struct IterLess
{
template<typename It>
bool operator()(It const& a, It const& b) const { return *a < *b; }
};
} }
template<typename Iter1, typename Iter2> template<typename Iter1, typename Iter2>
@ -267,10 +261,27 @@ typename Range::const_iterator cmRemoveMatching(Range &r, MatchRange const& m)
ContainerAlgorithms::BinarySearcher<MatchRange>(m)); ContainerAlgorithms::BinarySearcher<MatchRange>(m));
} }
namespace ContainerAlgorithms {
template<typename Range>
struct RemoveDuplicatesAPI
{
typedef typename Range::const_iterator const_iterator;
typedef typename Range::const_iterator value_type;
static bool lessThan(value_type a, value_type b) { return *a < *b; }
static value_type uniqueValue(const_iterator a) { return a; }
template<typename It>
static bool valueCompare(It it, const_iterator it2) { return **it != *it2; }
};
}
template<typename Range> template<typename Range>
typename Range::const_iterator cmRemoveDuplicates(Range& r) typename Range::const_iterator cmRemoveDuplicates(Range& r)
{ {
typedef typename Range::const_iterator T; typedef typename ContainerAlgorithms::RemoveDuplicatesAPI<Range> API;
typedef typename API::value_type T;
std::vector<T> unique; std::vector<T> unique;
unique.reserve(r.size()); unique.reserve(r.size());
std::vector<size_t> indices; std::vector<size_t> indices;
@ -280,11 +291,11 @@ typename Range::const_iterator cmRemoveDuplicates(Range& r)
it != end; ++it, ++count) it != end; ++it, ++count)
{ {
const typename std::vector<T>::iterator low = const typename std::vector<T>::iterator low =
std::lower_bound(unique.begin(), unique.end(), it, std::lower_bound(unique.begin(), unique.end(),
ContainerAlgorithms::IterLess()); API::uniqueValue(it), API::lessThan);
if (low == unique.end() || **low != *it) if (low == unique.end() || API::valueCompare(low, it))
{ {
unique.insert(low, it); unique.insert(low, API::uniqueValue(it));
} }
else else
{ {