From 899fdb1533eb7ec9ed228c872dcc86fcfb49638a Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Sun, 18 Oct 2015 14:04:56 +0000 Subject: [PATCH] Add hashing and DenseMapInfo for ArrayRef Sometimes it is more natural to use a ArrayRef than a StringRef to represent a range of bytes that is not, semantically, a string. This will be used in lld in a sec. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@250658 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/ArrayRef.h | 5 +++++ include/llvm/ADT/DenseMapInfo.h | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/llvm/ADT/ArrayRef.h b/include/llvm/ADT/ArrayRef.h index aaf32ff020e..517ba39849e 100644 --- a/include/llvm/ADT/ArrayRef.h +++ b/include/llvm/ADT/ArrayRef.h @@ -10,6 +10,7 @@ #ifndef LLVM_ADT_ARRAYREF_H #define LLVM_ADT_ARRAYREF_H +#include "llvm/ADT/Hashing.h" #include "llvm/ADT/None.h" #include "llvm/ADT/SmallVector.h" #include @@ -374,6 +375,10 @@ namespace llvm { template struct isPodLike > { static const bool value = true; }; + + template hash_code hash_value(ArrayRef S) { + return hash_combine_range(S.begin(), S.end()); + } } #endif diff --git a/include/llvm/ADT/DenseMapInfo.h b/include/llvm/ADT/DenseMapInfo.h index 07f79df0d20..a844ebcccf5 100644 --- a/include/llvm/ADT/DenseMapInfo.h +++ b/include/llvm/ADT/DenseMapInfo.h @@ -14,6 +14,7 @@ #ifndef LLVM_ADT_DENSEMAPINFO_H #define LLVM_ADT_DENSEMAPINFO_H +#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/PointerLikeTypeTraits.h" @@ -190,6 +191,31 @@ template <> struct DenseMapInfo { } }; +// Provide DenseMapInfo for ArrayRefs. +template struct DenseMapInfo> { + static inline ArrayRef getEmptyKey() { + return ArrayRef(reinterpret_cast(~static_cast(0)), + size_t(0)); + } + static inline ArrayRef getTombstoneKey() { + return ArrayRef(reinterpret_cast(~static_cast(1)), + size_t(0)); + } + static unsigned getHashValue(ArrayRef Val) { + assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!"); + assert(Val.data() != getTombstoneKey().data() && + "Cannot hash the tombstone key!"); + return (unsigned)(hash_value(Val)); + } + static bool isEqual(ArrayRef LHS, ArrayRef RHS) { + if (RHS.data() == getEmptyKey().data()) + return LHS.data() == getEmptyKey().data(); + if (RHS.data() == getTombstoneKey().data()) + return LHS.data() == getTombstoneKey().data(); + return LHS == RHS; + } +}; + } // end namespace llvm #endif -- 2.34.1