[compiler-rt] 4058637 - [NFC][sanitizer] Reuse forEach for operator==

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 23 15:23:37 PST 2021


Author: Vitaly Buka
Date: 2021-11-23T15:23:25-08:00
New Revision: 4058637f7ac6c0c44c90604b041dafa6b24e641b

URL: https://github.com/llvm/llvm-project/commit/4058637f7ac6c0c44c90604b041dafa6b24e641b
DIFF: https://github.com/llvm/llvm-project/commit/4058637f7ac6c0c44c90604b041dafa6b24e641b.diff

LOG: [NFC][sanitizer] Reuse forEach for operator==

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_dense_map.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_dense_map.h b/compiler-rt/lib/sanitizer_common/sanitizer_dense_map.h
index 6ddd8b1edc8a..046d77dddc9c 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_dense_map.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_dense_map.h
@@ -226,30 +226,6 @@ class DenseMapBase {
     return FindAndConstruct(__sanitizer::move(Key)).second;
   }
 
-  /// Equality comparison for DenseMap.
-  ///
-  /// Iterates over elements of LHS confirming that each (key, value) pair in
-  /// LHS is also in RHS, and that no additional pairs are in RHS. Equivalent to
-  /// N calls to RHS.find and N value comparisons. Amortized complexity is
-  /// linear, worst case is O(N^2) (if every hash collides).
-  bool operator==(const DenseMapBase &RHS) const {
-    if (size() != RHS.size())
-      return false;
-
-    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
-    for (auto *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
-      const KeyT K = P->getFirst();
-      if (!KeyInfoT::isEqual(K, EmptyKey) &&
-          !KeyInfoT::isEqual(K, TombstoneKey)) {
-        const auto *I = RHS.find(K);
-        if (!I || P->getSecond() != I->getSecond())
-          return false;
-      }
-    }
-
-    return true;
-  }
-
   /// Iterate over active entries of the container.
   ///
   /// Function can return fast to stop the process.
@@ -266,6 +242,12 @@ class DenseMapBase {
     }
   }
 
+  template <class Fn>
+  void forEach(Fn fn) const {
+    const_cast<DenseMapBase *>(this)->forEach(
+        [&](const value_type &KV) { return fn(KV); });
+  }
+
  protected:
   DenseMapBase() = default;
 
@@ -540,6 +522,35 @@ class DenseMapBase {
   }
 };
 
+/// Equality comparison for DenseMap.
+///
+/// Iterates over elements of LHS confirming that each (key, value) pair in LHS
+/// is also in RHS, and that no additional pairs are in RHS.
+/// Equivalent to N calls to RHS.find and N value comparisons. Amortized
+/// complexity is linear, worst case is O(N^2) (if every hash collides).
+template <typename DerivedT, typename KeyT, typename ValueT, typename KeyInfoT,
+          typename BucketT>
+bool operator==(
+    const DenseMapBase<DerivedT, KeyT, ValueT, KeyInfoT, BucketT> &LHS,
+    const DenseMapBase<DerivedT, KeyT, ValueT, KeyInfoT, BucketT> &RHS) {
+  if (LHS.size() != RHS.size())
+    return false;
+
+  bool R = true;
+  LHS.forEach(
+      [&](const typename DenseMapBase<DerivedT, KeyT, ValueT, KeyInfoT,
+                                      BucketT>::value_type &KV) -> bool {
+        const auto *I = RHS.find(KV.first);
+        if (!I || I->second != KV.second) {
+          R = false;
+          return false;
+        }
+        return true;
+      });
+
+  return R;
+}
+
 /// Inequality comparison for DenseMap.
 ///
 /// Equivalent to !(LHS == RHS). See operator== for performance notes.


        


More information about the llvm-commits mailing list