From eec7091d76fc3db6535eec3f78fd2585b9c0c38a Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Sun, 1 Mar 2015 21:57:16 +0100 Subject: [PATCH] cmRemoveDuplicates: Type-parameterize all uniq-operations --- Source/cmAlgorithms.h | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/Source/cmAlgorithms.h b/Source/cmAlgorithms.h index 1b7029beb..5504fee71 100644 --- a/Source/cmAlgorithms.h +++ b/Source/cmAlgorithms.h @@ -176,12 +176,6 @@ private: Range const& m_range; }; -struct IterLess -{ - template - bool operator()(It const& a, It const& b) const { return *a < *b; } -}; - } template @@ -267,10 +261,27 @@ typename Range::const_iterator cmRemoveMatching(Range &r, MatchRange const& m) ContainerAlgorithms::BinarySearcher(m)); } +namespace ContainerAlgorithms { + +template +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 + static bool valueCompare(It it, const_iterator it2) { return **it != *it2; } +}; + +} + template typename Range::const_iterator cmRemoveDuplicates(Range& r) { - typedef typename Range::const_iterator T; + typedef typename ContainerAlgorithms::RemoveDuplicatesAPI API; + typedef typename API::value_type T; std::vector unique; unique.reserve(r.size()); std::vector indices; @@ -280,11 +291,11 @@ typename Range::const_iterator cmRemoveDuplicates(Range& r) it != end; ++it, ++count) { const typename std::vector::iterator low = - std::lower_bound(unique.begin(), unique.end(), it, - ContainerAlgorithms::IterLess()); - if (low == unique.end() || **low != *it) + std::lower_bound(unique.begin(), unique.end(), + API::uniqueValue(it), API::lessThan); + if (low == unique.end() || API::valueCompare(low, it)) { - unique.insert(low, it); + unique.insert(low, API::uniqueValue(it)); } else {