1 //===- llvm/ADT/DenseMapInfo.h - Type traits for DenseMap -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines DenseMapInfo traits for DenseMap.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_DENSEMAPINFO_H
15 #define LLVM_ADT_DENSEMAPINFO_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/Hashing.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/PointerLikeTypeTraits.h"
30 //static inline T getEmptyKey();
31 //static inline T getTombstoneKey();
32 //static unsigned getHashValue(const T &Val);
33 //static bool isEqual(const T &LHS, const T &RHS);
36 // Provide DenseMapInfo for all pointers.
38 struct DenseMapInfo<T*> {
39 static inline T* getEmptyKey() {
40 uintptr_t Val = static_cast<uintptr_t>(-1);
41 Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
42 return reinterpret_cast<T*>(Val);
45 static inline T* getTombstoneKey() {
46 uintptr_t Val = static_cast<uintptr_t>(-2);
47 Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
48 return reinterpret_cast<T*>(Val);
51 static unsigned getHashValue(const T *PtrVal) {
52 return (unsigned((uintptr_t)PtrVal) >> 4) ^
53 (unsigned((uintptr_t)PtrVal) >> 9);
56 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
59 // Provide DenseMapInfo for chars.
60 template<> struct DenseMapInfo<char> {
61 static inline char getEmptyKey() { return ~0; }
62 static inline char getTombstoneKey() { return ~0 - 1; }
63 static unsigned getHashValue(const char& Val) { return Val * 37U; }
65 static bool isEqual(const char &LHS, const char &RHS) {
70 // Provide DenseMapInfo for unsigned shorts.
71 template <> struct DenseMapInfo<unsigned short> {
72 static inline unsigned short getEmptyKey() { return 0xFFFF; }
73 static inline unsigned short getTombstoneKey() { return 0xFFFF - 1; }
74 static unsigned getHashValue(const unsigned short &Val) { return Val * 37U; }
76 static bool isEqual(const unsigned short &LHS, const unsigned short &RHS) {
81 // Provide DenseMapInfo for unsigned ints.
82 template<> struct DenseMapInfo<unsigned> {
83 static inline unsigned getEmptyKey() { return ~0U; }
84 static inline unsigned getTombstoneKey() { return ~0U - 1; }
85 static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
87 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
92 // Provide DenseMapInfo for unsigned longs.
93 template<> struct DenseMapInfo<unsigned long> {
94 static inline unsigned long getEmptyKey() { return ~0UL; }
95 static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
97 static unsigned getHashValue(const unsigned long& Val) {
98 return (unsigned)(Val * 37UL);
101 static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
106 // Provide DenseMapInfo for unsigned long longs.
107 template<> struct DenseMapInfo<unsigned long long> {
108 static inline unsigned long long getEmptyKey() { return ~0ULL; }
109 static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
111 static unsigned getHashValue(const unsigned long long& Val) {
112 return (unsigned)(Val * 37ULL);
115 static bool isEqual(const unsigned long long& LHS,
116 const unsigned long long& RHS) {
121 // Provide DenseMapInfo for shorts.
122 template <> struct DenseMapInfo<short> {
123 static inline short getEmptyKey() { return 0x7FFF; }
124 static inline short getTombstoneKey() { return -0x7FFF - 1; }
125 static unsigned getHashValue(const short &Val) { return Val * 37U; }
126 static bool isEqual(const short &LHS, const short &RHS) { return LHS == RHS; }
129 // Provide DenseMapInfo for ints.
130 template<> struct DenseMapInfo<int> {
131 static inline int getEmptyKey() { return 0x7fffffff; }
132 static inline int getTombstoneKey() { return -0x7fffffff - 1; }
133 static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
135 static bool isEqual(const int& LHS, const int& RHS) {
140 // Provide DenseMapInfo for longs.
141 template<> struct DenseMapInfo<long> {
142 static inline long getEmptyKey() {
143 return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
146 static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
148 static unsigned getHashValue(const long& Val) {
149 return (unsigned)(Val * 37UL);
152 static bool isEqual(const long& LHS, const long& RHS) {
157 // Provide DenseMapInfo for long longs.
158 template<> struct DenseMapInfo<long long> {
159 static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
160 static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
162 static unsigned getHashValue(const long long& Val) {
163 return (unsigned)(Val * 37ULL);
166 static bool isEqual(const long long& LHS,
167 const long long& RHS) {
172 // Provide DenseMapInfo for all pairs whose members have info.
173 template<typename T, typename U>
174 struct DenseMapInfo<std::pair<T, U>> {
175 using Pair = std::pair<T, U>;
176 using FirstInfo = DenseMapInfo<T>;
177 using SecondInfo = DenseMapInfo<U>;
179 static inline Pair getEmptyKey() {
180 return std::make_pair(FirstInfo::getEmptyKey(),
181 SecondInfo::getEmptyKey());
184 static inline Pair getTombstoneKey() {
185 return std::make_pair(FirstInfo::getTombstoneKey(),
186 SecondInfo::getTombstoneKey());
189 static unsigned getHashValue(const Pair& PairVal) {
190 uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
191 | (uint64_t)SecondInfo::getHashValue(PairVal.second);
200 return (unsigned)key;
203 static bool isEqual(const Pair &LHS, const Pair &RHS) {
204 return FirstInfo::isEqual(LHS.first, RHS.first) &&
205 SecondInfo::isEqual(LHS.second, RHS.second);
209 // Provide DenseMapInfo for StringRefs.
210 template <> struct DenseMapInfo<StringRef> {
211 static inline StringRef getEmptyKey() {
212 return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(0)),
216 static inline StringRef getTombstoneKey() {
217 return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(1)),
221 static unsigned getHashValue(StringRef Val) {
222 assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!");
223 assert(Val.data() != getTombstoneKey().data() &&
224 "Cannot hash the tombstone key!");
225 return (unsigned)(hash_value(Val));
228 static bool isEqual(StringRef LHS, StringRef RHS) {
229 if (RHS.data() == getEmptyKey().data())
230 return LHS.data() == getEmptyKey().data();
231 if (RHS.data() == getTombstoneKey().data())
232 return LHS.data() == getTombstoneKey().data();
237 // Provide DenseMapInfo for ArrayRefs.
238 template <typename T> struct DenseMapInfo<ArrayRef<T>> {
239 static inline ArrayRef<T> getEmptyKey() {
240 return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(0)),
244 static inline ArrayRef<T> getTombstoneKey() {
245 return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(1)),
249 static unsigned getHashValue(ArrayRef<T> Val) {
250 assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!");
251 assert(Val.data() != getTombstoneKey().data() &&
252 "Cannot hash the tombstone key!");
253 return (unsigned)(hash_value(Val));
256 static bool isEqual(ArrayRef<T> LHS, ArrayRef<T> RHS) {
257 if (RHS.data() == getEmptyKey().data())
258 return LHS.data() == getEmptyKey().data();
259 if (RHS.data() == getTombstoneKey().data())
260 return LHS.data() == getTombstoneKey().data();
265 } // end namespace llvm
267 #endif // LLVM_ADT_DENSEMAPINFO_H