WPILibC++  unspecified
DenseMapInfo.h
1 //===- llvm/ADT/DenseMapInfo.h - Type traits for DenseMap -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines DenseMapInfo traits for DenseMap.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ADT_DENSEMAPINFO_H
15 #define LLVM_ADT_DENSEMAPINFO_H
16 
17 #include "llvm/ArrayRef.h"
18 #include "llvm/Hashing.h"
19 #include "llvm/StringRef.h"
20 #include "llvm/PointerLikeTypeTraits.h"
21 #include "llvm/type_traits.h"
22 
23 namespace llvm {
24 
25 template<typename T>
26 struct DenseMapInfo {
27  //static inline T getEmptyKey();
28  //static inline T getTombstoneKey();
29  //static unsigned getHashValue(const T &Val);
30  //static bool isEqual(const T &LHS, const T &RHS);
31 };
32 
33 template <typename T> struct CachedHash {
34  CachedHash(T Val) : Val(std::move(Val)) {
36  }
37  CachedHash(T Val, unsigned Hash) : Val(std::move(Val)), Hash(Hash) {}
38  T Val;
39  unsigned Hash;
40 };
41 
42 // Provide DenseMapInfo for all CachedHash<T>.
43 template <typename T> struct DenseMapInfo<CachedHash<T>> {
44  static CachedHash<T> getEmptyKey() {
46  return {N, 0};
47  }
48  static CachedHash<T> getTombstoneKey() {
50  return {N, 0};
51  }
52  static unsigned getHashValue(CachedHash<T> Val) {
53  assert(!isEqual(Val, getEmptyKey()) && "Cannot hash the empty key!");
54  assert(!isEqual(Val, getTombstoneKey()) &&
55  "Cannot hash the tombstone key!");
56  return Val.Hash;
57  }
58  static bool isEqual(CachedHash<T> A, CachedHash<T> B) {
59  return DenseMapInfo<T>::isEqual(A.Val, B.Val);
60  }
61 };
62 
63 // Provide DenseMapInfo for all pointers.
64 template<typename T>
65 struct DenseMapInfo<T*> {
66  static inline T* getEmptyKey() {
67  uintptr_t Val = static_cast<uintptr_t>(-1);
68  Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
69  return reinterpret_cast<T*>(Val);
70  }
71  static inline T* getTombstoneKey() {
72  uintptr_t Val = static_cast<uintptr_t>(-2);
73  Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
74  return reinterpret_cast<T*>(Val);
75  }
76  static unsigned getHashValue(const T *PtrVal) {
77  return (unsigned((uintptr_t)PtrVal) >> 4) ^
78  (unsigned((uintptr_t)PtrVal) >> 9);
79  }
80  static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
81 };
82 
83 // Provide DenseMapInfo for chars.
84 template<> struct DenseMapInfo<char> {
85  static inline char getEmptyKey() { return ~0; }
86  static inline char getTombstoneKey() { return ~0 - 1; }
87  static unsigned getHashValue(const char& Val) { return Val * 37U; }
88  static bool isEqual(const char &LHS, const char &RHS) {
89  return LHS == RHS;
90  }
91 };
92 
93 // Provide DenseMapInfo for unsigned ints.
94 template<> struct DenseMapInfo<unsigned> {
95  static inline unsigned getEmptyKey() { return ~0U; }
96  static inline unsigned getTombstoneKey() { return ~0U - 1; }
97  static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
98  static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
99  return LHS == RHS;
100  }
101 };
102 
103 // Provide DenseMapInfo for unsigned longs.
104 template<> struct DenseMapInfo<unsigned long> {
105  static inline unsigned long getEmptyKey() { return ~0UL; }
106  static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
107  static unsigned getHashValue(const unsigned long& Val) {
108  return (unsigned)(Val * 37UL);
109  }
110  static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
111  return LHS == RHS;
112  }
113 };
114 
115 // Provide DenseMapInfo for unsigned long longs.
116 template<> struct DenseMapInfo<unsigned long long> {
117  static inline unsigned long long getEmptyKey() { return ~0ULL; }
118  static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
119  static unsigned getHashValue(const unsigned long long& Val) {
120  return (unsigned)(Val * 37ULL);
121  }
122  static bool isEqual(const unsigned long long& LHS,
123  const unsigned long long& RHS) {
124  return LHS == RHS;
125  }
126 };
127 
128 // Provide DenseMapInfo for ints.
129 template<> struct DenseMapInfo<int> {
130  static inline int getEmptyKey() { return 0x7fffffff; }
131  static inline int getTombstoneKey() { return -0x7fffffff - 1; }
132  static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
133  static bool isEqual(const int& LHS, const int& RHS) {
134  return LHS == RHS;
135  }
136 };
137 
138 // Provide DenseMapInfo for longs.
139 template<> struct DenseMapInfo<long> {
140  static inline long getEmptyKey() {
141  return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
142  }
143  static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
144  static unsigned getHashValue(const long& Val) {
145  return (unsigned)(Val * 37UL);
146  }
147  static bool isEqual(const long& LHS, const long& RHS) {
148  return LHS == RHS;
149  }
150 };
151 
152 // Provide DenseMapInfo for long longs.
153 template<> struct DenseMapInfo<long long> {
154  static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
155  static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
156  static unsigned getHashValue(const long long& Val) {
157  return (unsigned)(Val * 37ULL);
158  }
159  static bool isEqual(const long long& LHS,
160  const long long& RHS) {
161  return LHS == RHS;
162  }
163 };
164 
165 // Provide DenseMapInfo for all pairs whose members have info.
166 template<typename T, typename U>
167 struct DenseMapInfo<std::pair<T, U> > {
168  typedef std::pair<T, U> Pair;
169  typedef DenseMapInfo<T> FirstInfo;
170  typedef DenseMapInfo<U> SecondInfo;
171 
172  static inline Pair getEmptyKey() {
173  return std::make_pair(FirstInfo::getEmptyKey(),
174  SecondInfo::getEmptyKey());
175  }
176  static inline Pair getTombstoneKey() {
177  return std::make_pair(FirstInfo::getTombstoneKey(),
178  SecondInfo::getTombstoneKey());
179  }
180  static unsigned getHashValue(const Pair& PairVal) {
181  uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
182  | (uint64_t)SecondInfo::getHashValue(PairVal.second);
183  key += ~(key << 32);
184  key ^= (key >> 22);
185  key += ~(key << 13);
186  key ^= (key >> 8);
187  key += (key << 3);
188  key ^= (key >> 15);
189  key += ~(key << 27);
190  key ^= (key >> 31);
191  return (unsigned)key;
192  }
193  static bool isEqual(const Pair &LHS, const Pair &RHS) {
194  return FirstInfo::isEqual(LHS.first, RHS.first) &&
195  SecondInfo::isEqual(LHS.second, RHS.second);
196  }
197 };
198 
199 // Provide DenseMapInfo for StringRefs.
200 template <> struct DenseMapInfo<StringRef> {
201  static inline StringRef getEmptyKey() {
202  return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(0)),
203  0);
204  }
205  static inline StringRef getTombstoneKey() {
206  return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(1)),
207  0);
208  }
209  static unsigned getHashValue(StringRef Val) {
210  assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!");
211  assert(Val.data() != getTombstoneKey().data() &&
212  "Cannot hash the tombstone key!");
213  return (unsigned)(hash_value(Val));
214  }
215  static bool isEqual(StringRef LHS, StringRef RHS) {
216  if (RHS.data() == getEmptyKey().data())
217  return LHS.data() == getEmptyKey().data();
218  if (RHS.data() == getTombstoneKey().data())
219  return LHS.data() == getTombstoneKey().data();
220  return LHS == RHS;
221  }
222 };
223 
224 // Provide DenseMapInfo for ArrayRefs.
225 template <typename T> struct DenseMapInfo<ArrayRef<T>> {
226  static inline ArrayRef<T> getEmptyKey() {
227  return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(0)),
228  size_t(0));
229  }
230  static inline ArrayRef<T> getTombstoneKey() {
231  return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(1)),
232  size_t(0));
233  }
234  static unsigned getHashValue(ArrayRef<T> Val) {
235  assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!");
236  assert(Val.data() != getTombstoneKey().data() &&
237  "Cannot hash the tombstone key!");
238  return (unsigned)(hash_value(Val));
239  }
240  static bool isEqual(ArrayRef<T> LHS, ArrayRef<T> RHS) {
241  if (RHS.data() == getEmptyKey().data())
242  return LHS.data() == getEmptyKey().data();
243  if (RHS.data() == getTombstoneKey().data())
244  return LHS.data() == getTombstoneKey().data();
245  return LHS == RHS;
246  }
247 };
248 
249 } // end namespace llvm
250 
251 #endif
Definition: Path.inc:27
Definition: DenseMapInfo.h:26
Definition: DenseMapInfo.h:33
Definition: json.cpp:1170
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:139
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42