summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2019-03-08 14:35:25 (GMT)
committerKitware Robot <kwrobot@kitware.com>2019-03-08 14:38:54 (GMT)
commit42bbde4193537ac9e9e955304ef63ed696c2c225 (patch)
tree7a8d1735c6d4181b4819e09a430d4d50e2183e25
parent18ff7cc34f5769bd14ba2893b54647f9b639f847 (diff)
parentbf1e73305ac1b6bc9021b80fc8da99fda032c018 (diff)
downloadCMake-42bbde4193537ac9e9e955304ef63ed696c2c225.zip
CMake-42bbde4193537ac9e9e955304ef63ed696c2c225.tar.gz
CMake-42bbde4193537ac9e9e955304ef63ed696c2c225.tar.bz2
Merge topic 'cmalgorithm-modernize-cmremoveduplicates'
bf1e73305a cmAlgorithms: Refactor cmRemoveDuplicates Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !3029
-rw-r--r--Source/cmAlgorithms.h54
1 files changed, 36 insertions, 18 deletions
diff --git a/Source/cmAlgorithms.h b/Source/cmAlgorithms.h
index 2ff1ed0..0980416 100644
--- a/Source/cmAlgorithms.h
+++ b/Source/cmAlgorithms.h
@@ -233,27 +233,45 @@ typename Range::const_iterator cmRemoveMatching(Range& r, MatchRange const& m)
ContainerAlgorithms::BinarySearcher<MatchRange>(m));
}
-template <typename Range>
-typename Range::const_iterator cmRemoveDuplicates(Range& r)
+template <typename ForwardIterator>
+ForwardIterator cmRemoveDuplicates(ForwardIterator first, ForwardIterator last)
{
- typedef typename Range::value_type T;
- std::unordered_set<T> unique;
- std::vector<size_t> indices;
- size_t count = 0;
- const typename Range::const_iterator end = r.end();
- for (typename Range::const_iterator it = r.begin(); it != end;
- ++it, ++count) {
- const typename std::unordered_set<T>::iterator occur = unique.find(*it);
- if (occur == unique.end()) {
- unique.insert(*it);
- } else {
- indices.push_back(count);
+ using Value = typename std::iterator_traits<ForwardIterator>::value_type;
+ using Hash = struct
+ {
+ std::size_t operator()(ForwardIterator it) const
+ {
+ return std::hash<Value>{}(*it);
}
+ };
+
+ using Equal = struct
+ {
+ bool operator()(ForwardIterator it1, ForwardIterator it2) const
+ {
+ return *it1 == *it2;
+ }
+ };
+ std::unordered_set<ForwardIterator, Hash, Equal> uniq;
+
+ ForwardIterator result = first;
+ while (first != last) {
+ if (uniq.find(first) == uniq.end()) {
+ if (result != first) {
+ *result = std::move(*first);
+ }
+ uniq.insert(result);
+ ++result;
+ }
+ ++first;
}
- if (indices.empty()) {
- return end;
- }
- return cmRemoveIndices(r, indices);
+ return result;
+}
+
+template <typename Range>
+typename Range::const_iterator cmRemoveDuplicates(Range& r)
+{
+ return cmRemoveDuplicates(r.begin(), r.end());
}
template <typename Range>