llvm-DenseMapInfo.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. return 0 == strcmp(LHS, RHS);
  71. }
  72. };
  73. // Provide DenseMapInfo for chars.
  74. template<> struct DenseMapInfo<char> {
  75. static inline char getEmptyKey() { return ~0; }
  76. static inline char getTombstoneKey() { return ~0 - 1; }
  77. static unsigned getHashValue(const char& Val) { return Val * 37U; }
  78. static bool isEqual(const char &LHS, const char &RHS) {
  79. return LHS == RHS;
  80. }
  81. };
  82. // Provide DenseMapInfo for unsigned ints.
  83. template<> struct DenseMapInfo<unsigned> {
  84. static inline unsigned getEmptyKey() { return ~0U; }
  85. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  86. static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  87. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  88. return LHS == RHS;
  89. }
  90. };
  91. // Provide DenseMapInfo for unsigned longs.
  92. template<> struct DenseMapInfo<unsigned long> {
  93. static inline unsigned long getEmptyKey() { return ~0UL; }
  94. static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
  95. static unsigned getHashValue(const unsigned long& Val) {
  96. return (unsigned)(Val * 37UL);
  97. }
  98. static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
  99. return LHS == RHS;
  100. }
  101. };
  102. // Provide DenseMapInfo for unsigned long longs.
  103. template<> struct DenseMapInfo<unsigned long long> {
  104. static inline unsigned long long getEmptyKey() { return ~0ULL; }
  105. static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
  106. static unsigned getHashValue(const unsigned long long& Val) {
  107. return (unsigned)(Val * 37ULL);
  108. }
  109. static bool isEqual(const unsigned long long& LHS,
  110. const unsigned long long& RHS) {
  111. return LHS == RHS;
  112. }
  113. };
  114. // Provide DenseMapInfo for ints.
  115. template<> struct DenseMapInfo<int> {
  116. static inline int getEmptyKey() { return 0x7fffffff; }
  117. static inline int getTombstoneKey() { return -0x7fffffff - 1; }
  118. static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
  119. static bool isEqual(const int& LHS, const int& RHS) {
  120. return LHS == RHS;
  121. }
  122. };
  123. // Provide DenseMapInfo for longs.
  124. template<> struct DenseMapInfo<long> {
  125. static inline long getEmptyKey() {
  126. return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
  127. }
  128. static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
  129. static unsigned getHashValue(const long& Val) {
  130. return (unsigned)(Val * 37UL);
  131. }
  132. static bool isEqual(const long& LHS, const long& RHS) {
  133. return LHS == RHS;
  134. }
  135. };
  136. // Provide DenseMapInfo for long longs.
  137. template<> struct DenseMapInfo<long long> {
  138. static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
  139. static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
  140. static unsigned getHashValue(const long long& Val) {
  141. return (unsigned)(Val * 37ULL);
  142. }
  143. static bool isEqual(const long long& LHS,
  144. const long long& RHS) {
  145. return LHS == RHS;
  146. }
  147. };
  148. // Provide DenseMapInfo for all pairs whose members have info.
  149. template<typename T, typename U>
  150. struct DenseMapInfo<std::pair<T, U> > {
  151. typedef std::pair<T, U> Pair;
  152. typedef DenseMapInfo<T> FirstInfo;
  153. typedef DenseMapInfo<U> SecondInfo;
  154. static inline Pair getEmptyKey() {
  155. return std::make_pair(FirstInfo::getEmptyKey(),
  156. SecondInfo::getEmptyKey());
  157. }
  158. static inline Pair getTombstoneKey() {
  159. return std::make_pair(FirstInfo::getTombstoneKey(),
  160. SecondInfo::getTombstoneKey());
  161. }
  162. static unsigned getHashValue(const Pair& PairVal) {
  163. uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
  164. | (uint64_t)SecondInfo::getHashValue(PairVal.second);
  165. key += ~(key << 32);
  166. key ^= (key >> 22);
  167. key += ~(key << 13);
  168. key ^= (key >> 8);
  169. key += (key << 3);
  170. key ^= (key >> 15);
  171. key += ~(key << 27);
  172. key ^= (key >> 31);
  173. return (unsigned)key;
  174. }
  175. static bool isEqual(const Pair &LHS, const Pair &RHS) {
  176. return FirstInfo::isEqual(LHS.first, RHS.first) &&
  177. SecondInfo::isEqual(LHS.second, RHS.second);
  178. }
  179. };
  180. } // end namespace objc
  181. #endif