llvm-DenseMapInfo.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. // Taken from llvmCore-3425.0.31.
  14. #ifndef LLVM_ADT_DENSEMAPINFO_H
  15. #define LLVM_ADT_DENSEMAPINFO_H
  16. #include "objc-private.h"
  17. #include "llvm-type_traits.h"
  18. namespace objc {
  19. template<typename T>
  20. struct DenseMapInfo {
  21. //static inline T getEmptyKey();
  22. //static inline T getTombstoneKey();
  23. //static unsigned getHashValue(const T &Val);
  24. //static bool isEqual(const T &LHS, const T &RHS);
  25. };
  26. // Provide DenseMapInfo for all pointers.
  27. template<typename T>
  28. struct DenseMapInfo<T*> {
  29. static inline T* getEmptyKey() {
  30. uintptr_t Val = static_cast<uintptr_t>(-1);
  31. return reinterpret_cast<T*>(Val);
  32. }
  33. static inline T* getTombstoneKey() {
  34. uintptr_t Val = static_cast<uintptr_t>(-2);
  35. return reinterpret_cast<T*>(Val);
  36. }
  37. static unsigned getHashValue(const T *PtrVal) {
  38. return ptr_hash((uintptr_t)PtrVal);
  39. }
  40. static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
  41. };
  42. // Provide DenseMapInfo for disguised pointers.
  43. template<typename T>
  44. struct DenseMapInfo<DisguisedPtr<T>> {
  45. static inline DisguisedPtr<T> getEmptyKey() {
  46. return DisguisedPtr<T>((T*)(uintptr_t)-1);
  47. }
  48. static inline DisguisedPtr<T> getTombstoneKey() {
  49. return DisguisedPtr<T>((T*)(uintptr_t)-2);
  50. }
  51. static unsigned getHashValue(const T *PtrVal) {
  52. return ptr_hash((uintptr_t)PtrVal);
  53. }
  54. static bool isEqual(const DisguisedPtr<T> &LHS, const DisguisedPtr<T> &RHS) {
  55. return LHS == RHS;
  56. }
  57. };
  58. // Provide DenseMapInfo for cstrings.
  59. template<> struct DenseMapInfo<const char*> {
  60. static inline const char* getEmptyKey() {
  61. return reinterpret_cast<const char *>((intptr_t)-1);
  62. }
  63. static inline const char* getTombstoneKey() {
  64. return reinterpret_cast<const char *>((intptr_t)-2);
  65. }
  66. static unsigned getHashValue(const char* const &Val) {
  67. return _objc_strhash(Val);
  68. }
  69. static bool isEqual(const char* const &LHS, const char* const &RHS) {
  70. if (LHS == RHS) {
  71. return true;
  72. }
  73. if (LHS == getEmptyKey() || RHS == getEmptyKey()) {
  74. return false;
  75. }
  76. if (LHS == getTombstoneKey() || RHS == getTombstoneKey()) {
  77. return false;
  78. }
  79. return 0 == strcmp(LHS, RHS);
  80. }
  81. };
  82. // Provide DenseMapInfo for chars.
  83. template<> struct DenseMapInfo<char> {
  84. static inline char getEmptyKey() { return ~0; }
  85. static inline char getTombstoneKey() { return ~0 - 1; }
  86. static unsigned getHashValue(const char& Val) { return Val * 37U; }
  87. static bool isEqual(const char &LHS, const char &RHS) {
  88. return LHS == RHS;
  89. }
  90. };
  91. // Provide DenseMapInfo for unsigned ints.
  92. template<> struct DenseMapInfo<unsigned> {
  93. static inline unsigned getEmptyKey() { return ~0U; }
  94. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  95. static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  96. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  97. return LHS == RHS;
  98. }
  99. };
  100. // Provide DenseMapInfo for unsigned longs.
  101. template<> struct DenseMapInfo<unsigned long> {
  102. static inline unsigned long getEmptyKey() { return ~0UL; }
  103. static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
  104. static unsigned getHashValue(const unsigned long& Val) {
  105. return (unsigned)(Val * 37UL);
  106. }
  107. static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
  108. return LHS == RHS;
  109. }
  110. };
  111. // Provide DenseMapInfo for unsigned long longs.
  112. template<> struct DenseMapInfo<unsigned long long> {
  113. static inline unsigned long long getEmptyKey() { return ~0ULL; }
  114. static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
  115. static unsigned getHashValue(const unsigned long long& Val) {
  116. return (unsigned)(Val * 37ULL);
  117. }
  118. static bool isEqual(const unsigned long long& LHS,
  119. const unsigned long long& RHS) {
  120. return LHS == RHS;
  121. }
  122. };
  123. // Provide DenseMapInfo for ints.
  124. template<> struct DenseMapInfo<int> {
  125. static inline int getEmptyKey() { return 0x7fffffff; }
  126. static inline int getTombstoneKey() { return -0x7fffffff - 1; }
  127. static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
  128. static bool isEqual(const int& LHS, const int& RHS) {
  129. return LHS == RHS;
  130. }
  131. };
  132. // Provide DenseMapInfo for longs.
  133. template<> struct DenseMapInfo<long> {
  134. static inline long getEmptyKey() {
  135. return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
  136. }
  137. static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
  138. static unsigned getHashValue(const long& Val) {
  139. return (unsigned)(Val * 37UL);
  140. }
  141. static bool isEqual(const long& LHS, const long& RHS) {
  142. return LHS == RHS;
  143. }
  144. };
  145. // Provide DenseMapInfo for long longs.
  146. template<> struct DenseMapInfo<long long> {
  147. static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
  148. static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
  149. static unsigned getHashValue(const long long& Val) {
  150. return (unsigned)(Val * 37ULL);
  151. }
  152. static bool isEqual(const long long& LHS,
  153. const long long& RHS) {
  154. return LHS == RHS;
  155. }
  156. };
  157. // Provide DenseMapInfo for all pairs whose members have info.
  158. template<typename T, typename U>
  159. struct DenseMapInfo<std::pair<T, U> > {
  160. typedef std::pair<T, U> Pair;
  161. typedef DenseMapInfo<T> FirstInfo;
  162. typedef DenseMapInfo<U> SecondInfo;
  163. static inline Pair getEmptyKey() {
  164. return std::make_pair(FirstInfo::getEmptyKey(),
  165. SecondInfo::getEmptyKey());
  166. }
  167. static inline Pair getTombstoneKey() {
  168. return std::make_pair(FirstInfo::getTombstoneKey(),
  169. SecondInfo::getTombstoneKey());
  170. }
  171. static unsigned getHashValue(const Pair& PairVal) {
  172. uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
  173. | (uint64_t)SecondInfo::getHashValue(PairVal.second);
  174. key += ~(key << 32);
  175. key ^= (key >> 22);
  176. key += ~(key << 13);
  177. key ^= (key >> 8);
  178. key += (key << 3);
  179. key ^= (key >> 15);
  180. key += ~(key << 27);
  181. key ^= (key >> 31);
  182. return (unsigned)key;
  183. }
  184. static bool isEqual(const Pair &LHS, const Pair &RHS) {
  185. return FirstInfo::isEqual(LHS.first, RHS.first) &&
  186. SecondInfo::isEqual(LHS.second, RHS.second);
  187. }
  188. };
  189. template<typename T>
  190. struct DenseMapValueInfo {
  191. static inline bool isPurgeable(const T &value) {
  192. return false;
  193. }
  194. };
  195. } // end namespace objc
  196. #endif