Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.5 KiB

  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. #ifndef LLVM_ADT_DENSEMAPINFO_H
  14. #define LLVM_ADT_DENSEMAPINFO_H
  15. #include "llvm/Support/PointerLikeTypeTraits.h"
  16. #include "llvm/Support/type_traits.h"
  17. namespace llvm {
  18. template<typename T>
  19. struct DenseMapInfo {
  20. //static inline T getEmptyKey();
  21. //static inline T getTombstoneKey();
  22. //static unsigned getHashValue(const T &Val);
  23. //static bool isEqual(const T &LHS, const T &RHS);
  24. };
  25. // Provide DenseMapInfo for all pointers.
  26. template<typename T>
  27. struct DenseMapInfo<T*> {
  28. static inline T* getEmptyKey() {
  29. uintptr_t Val = static_cast<uintptr_t>(-1);
  30. Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
  31. return reinterpret_cast<T*>(Val);
  32. }
  33. static inline T* getTombstoneKey() {
  34. uintptr_t Val = static_cast<uintptr_t>(-2);
  35. Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
  36. return reinterpret_cast<T*>(Val);
  37. }
  38. static unsigned getHashValue(const T *PtrVal) {
  39. return (unsigned((uintptr_t)PtrVal) >> 4) ^
  40. (unsigned((uintptr_t)PtrVal) >> 9);
  41. }
  42. static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
  43. };
  44. // Provide DenseMapInfo for chars.
  45. template<> struct DenseMapInfo<char> {
  46. static inline char getEmptyKey() { return ~0; }
  47. static inline char getTombstoneKey() { return ~0 - 1; }
  48. static unsigned getHashValue(const char& Val) { return Val * 37U; }
  49. static bool isEqual(const char &LHS, const char &RHS) {
  50. return LHS == RHS;
  51. }
  52. };
  53. // Provide DenseMapInfo for unsigned ints.
  54. template<> struct DenseMapInfo<unsigned> {
  55. static inline unsigned getEmptyKey() { return ~0U; }
  56. static inline unsigned getTombstoneKey() { return ~0U - 1; }
  57. static unsigned getHashValue(const unsigned& Val) { return Val * 37U; }
  58. static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
  59. return LHS == RHS;
  60. }
  61. };
  62. // Provide DenseMapInfo for unsigned longs.
  63. template<> struct DenseMapInfo<unsigned long> {
  64. static inline unsigned long getEmptyKey() { return ~0UL; }
  65. static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
  66. static unsigned getHashValue(const unsigned long& Val) {
  67. return (unsigned)(Val * 37UL);
  68. }
  69. static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
  70. return LHS == RHS;
  71. }
  72. };
  73. // Provide DenseMapInfo for unsigned long longs.
  74. template<> struct DenseMapInfo<unsigned long long> {
  75. static inline unsigned long long getEmptyKey() { return ~0ULL; }
  76. static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
  77. static unsigned getHashValue(const unsigned long long& Val) {
  78. return (unsigned)(Val * 37ULL);
  79. }
  80. static bool isEqual(const unsigned long long& LHS,
  81. const unsigned long long& RHS) {
  82. return LHS == RHS;
  83. }
  84. };
  85. // Provide DenseMapInfo for ints.
  86. template<> struct DenseMapInfo<int> {
  87. static inline int getEmptyKey() { return 0x7fffffff; }
  88. static inline int getTombstoneKey() { return -0x7fffffff - 1; }
  89. static unsigned getHashValue(const int& Val) { return (unsigned)(Val * 37U); }
  90. static bool isEqual(const int& LHS, const int& RHS) {
  91. return LHS == RHS;
  92. }
  93. };
  94. // Provide DenseMapInfo for longs.
  95. template<> struct DenseMapInfo<long> {
  96. static inline long getEmptyKey() {
  97. return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
  98. }
  99. static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
  100. static unsigned getHashValue(const long& Val) {
  101. return (unsigned)(Val * 37UL);
  102. }
  103. static bool isEqual(const long& LHS, const long& RHS) {
  104. return LHS == RHS;
  105. }
  106. };
  107. // Provide DenseMapInfo for long longs.
  108. template<> struct DenseMapInfo<long long> {
  109. static inline long long getEmptyKey() { return 0x7fffffffffffffffLL; }
  110. static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL-1; }
  111. static unsigned getHashValue(const long long& Val) {
  112. return (unsigned)(Val * 37ULL);
  113. }
  114. static bool isEqual(const long long& LHS,
  115. const long long& RHS) {
  116. return LHS == RHS;
  117. }
  118. };
  119. // Provide DenseMapInfo for all pairs whose members have info.
  120. template<typename T, typename U>
  121. struct DenseMapInfo<std::pair<T, U> > {
  122. typedef std::pair<T, U> Pair;
  123. typedef DenseMapInfo<T> FirstInfo;
  124. typedef DenseMapInfo<U> SecondInfo;
  125. static inline Pair getEmptyKey() {
  126. return std::make_pair(FirstInfo::getEmptyKey(),
  127. SecondInfo::getEmptyKey());
  128. }
  129. static inline Pair getTombstoneKey() {
  130. return std::make_pair(FirstInfo::getTombstoneKey(),
  131. SecondInfo::getTombstoneKey());
  132. }
  133. static unsigned getHashValue(const Pair& PairVal) {
  134. uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
  135. | (uint64_t)SecondInfo::getHashValue(PairVal.second);
  136. key += ~(key << 32);
  137. key ^= (key >> 22);
  138. key += ~(key << 13);
  139. key ^= (key >> 8);
  140. key += (key << 3);
  141. key ^= (key >> 15);
  142. key += ~(key << 27);
  143. key ^= (key >> 31);
  144. return (unsigned)key;
  145. }
  146. static bool isEqual(const Pair &LHS, const Pair &RHS) {
  147. return FirstInfo::isEqual(LHS.first, RHS.first) &&
  148. SecondInfo::isEqual(LHS.second, RHS.second);
  149. }
  150. };
  151. } // end namespace llvm
  152. #endif