Sfoglia il codice sorgente

cmRemoveDuplicates: Type-parameterize all uniq-operations

Stephen Kelly 11 anni fa
parent
commit
eec7091d76
1 ha cambiato i file con 22 aggiunte e 11 eliminazioni
  1. 22 11
      Source/cmAlgorithms.h

+ 22 - 11
Source/cmAlgorithms.h

@@ -176,12 +176,6 @@ private:
   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>
@@ -267,10 +261,27 @@ typename Range::const_iterator cmRemoveMatching(Range &r, MatchRange const& 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>
 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;
   unique.reserve(r.size());
   std::vector<size_t> indices;
@@ -280,11 +291,11 @@ typename Range::const_iterator cmRemoveDuplicates(Range& r)
       it != end; ++it, ++count)
     {
     const typename std::vector<T>::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
       {