diff options
author | Brad King <brad.king@kitware.com> | 2015-03-12 19:26:37 (GMT) |
---|---|---|
committer | CMake Topic Stage <kwrobot@kitware.com> | 2015-03-12 19:26:37 (GMT) |
commit | 18e85253c3d232d56768316a1b26a0646ad6d233 (patch) | |
tree | 89549dcf9a303a073d86bb0cb5e6a414bf9f58fc /Source/cmAlgorithms.h | |
parent | 15b06bc01278bafda15ff1fcece808959422d927 (diff) | |
parent | 8701a3f468a4fb684442a8a9c5d4c8d15c72eb7b (diff) | |
download | CMake-18e85253c3d232d56768316a1b26a0646ad6d233.zip CMake-18e85253c3d232d56768316a1b26a0646ad6d233.tar.gz CMake-18e85253c3d232d56768316a1b26a0646ad6d233.tar.bz2 |
Merge topic 'cmRemoveDuplicates-improvement'
8701a3f4 cmRemoveDuplicates: Partially specialize the API for pointer types.
eec7091d cmRemoveDuplicates: Type-parameterize all uniq-operations
7cbafa8c cmRemoveDuplicates: Store unique iterators instead of values.
Diffstat (limited to 'Source/cmAlgorithms.h')
-rw-r--r-- | Source/cmAlgorithms.h | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/Source/cmAlgorithms.h b/Source/cmAlgorithms.h index 3b3590b..f117475 100644 --- a/Source/cmAlgorithms.h +++ b/Source/cmAlgorithms.h @@ -265,11 +265,40 @@ typename Range::const_iterator cmRemoveMatching(Range &r, MatchRange const& m) ContainerAlgorithms::BinarySearcher<MatchRange>(m)); } +namespace ContainerAlgorithms { + +template<typename Range, typename T = typename Range::value_type> +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, typename T> +struct RemoveDuplicatesAPI<Range, T*> +{ + typedef typename Range::const_iterator const_iterator; + typedef T* 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> typename Range::const_iterator cmRemoveDuplicates(Range& r) { - typedef std::vector<typename Range::value_type> UniqueVector; - UniqueVector unique; + typedef typename ContainerAlgorithms::RemoveDuplicatesAPI<Range> API; + typedef typename API::value_type T; + std::vector<T> unique; unique.reserve(r.size()); std::vector<size_t> indices; size_t count = 0; @@ -277,11 +306,12 @@ typename Range::const_iterator cmRemoveDuplicates(Range& r) for(typename Range::const_iterator it = r.begin(); it != end; ++it, ++count) { - const typename UniqueVector::iterator low = - std::lower_bound(unique.begin(), unique.end(), *it); - if (low == unique.end() || *low != *it) + const typename std::vector<T>::iterator low = + 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 { |