diff options
| author | Rafael Espindola <rafael.espindola@gmail.com> | 2015-10-18 14:04:56 +0000 |
|---|---|---|
| committer | Rafael Espindola <rafael.espindola@gmail.com> | 2015-10-18 14:04:56 +0000 |
| commit | da3b3c78e78b0df94bd38a1f6003bdf9e37c6c17 (patch) | |
| tree | 036a988536cdeca38992efa39671183b0364143d | |
| parent | 0cc6f47348ffc0b4385d2cdbd9333dff92d26084 (diff) | |
| download | bcm5719-llvm-da3b3c78e78b0df94bd38a1f6003bdf9e37c6c17.tar.gz bcm5719-llvm-da3b3c78e78b0df94bd38a1f6003bdf9e37c6c17.zip | |
Add hashing and DenseMapInfo for ArrayRef
Sometimes it is more natural to use a ArrayRef<uint8_t> than a StringRef to
represent a range of bytes that is not, semantically, a string.
This will be used in lld in a sec.
llvm-svn: 250658
| -rw-r--r-- | llvm/include/llvm/ADT/ArrayRef.h | 5 | ||||
| -rw-r--r-- | llvm/include/llvm/ADT/DenseMapInfo.h | 26 |
2 files changed, 31 insertions, 0 deletions
diff --git a/llvm/include/llvm/ADT/ArrayRef.h b/llvm/include/llvm/ADT/ArrayRef.h index aaf32ff020e..517ba39849e 100644 --- a/llvm/include/llvm/ADT/ArrayRef.h +++ b/llvm/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 <vector> @@ -374,6 +375,10 @@ namespace llvm { template <typename T> struct isPodLike<ArrayRef<T> > { static const bool value = true; }; + + template <typename T> hash_code hash_value(ArrayRef<T> S) { + return hash_combine_range(S.begin(), S.end()); + } } #endif diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h index 07f79df0d20..a844ebcccf5 100644 --- a/llvm/include/llvm/ADT/DenseMapInfo.h +++ b/llvm/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<StringRef> { } }; +// Provide DenseMapInfo for ArrayRefs. +template <typename T> struct DenseMapInfo<ArrayRef<T>> { + static inline ArrayRef<T> getEmptyKey() { + return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(0)), + size_t(0)); + } + static inline ArrayRef<T> getTombstoneKey() { + return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(1)), + size_t(0)); + } + static unsigned getHashValue(ArrayRef<T> 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<T> LHS, ArrayRef<T> 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 |

