]>
git.saurik.com Git - apple/objc4.git/blob - runtime/llvm-DenseMapInfo.h
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 // Taken from llvmCore-3425.0.31.
16 #ifndef LLVM_ADT_DENSEMAPINFO_H
17 #define LLVM_ADT_DENSEMAPINFO_H
19 #include "objc-private.h"
20 #include "llvm-type_traits.h"
26 //static inline T getEmptyKey();
27 //static inline T getTombstoneKey();
28 //static unsigned getHashValue(const T &Val);
29 //static bool isEqual(const T &LHS, const T &RHS);
32 // Provide DenseMapInfo for all pointers.
34 struct DenseMapInfo
<T
*> {
35 static inline T
* getEmptyKey() {
36 uintptr_t Val
= static_cast<uintptr_t>(-1);
37 return reinterpret_cast<T
*>(Val
);
39 static inline T
* getTombstoneKey() {
40 uintptr_t Val
= static_cast<uintptr_t>(-2);
41 return reinterpret_cast<T
*>(Val
);
43 static unsigned getHashValue(const T
*PtrVal
) {
44 return ptr_hash((uintptr_t)PtrVal
);
46 static bool isEqual(const T
*LHS
, const T
*RHS
) { return LHS
== RHS
; }
49 // Provide DenseMapInfo for disguised pointers.
51 struct DenseMapInfo
<DisguisedPtr
<T
>> {
52 static inline DisguisedPtr
<T
> getEmptyKey() {
53 return DisguisedPtr
<T
>((T
*)(uintptr_t)-1);
55 static inline DisguisedPtr
<T
> getTombstoneKey() {
56 return DisguisedPtr
<T
>((T
*)(uintptr_t)-2);
58 static unsigned getHashValue(const T
*PtrVal
) {
59 return ptr_hash((uintptr_t)PtrVal
);
61 static bool isEqual(const DisguisedPtr
<T
> &LHS
, const DisguisedPtr
<T
> &RHS
) {
66 // Provide DenseMapInfo for cstrings.
67 template<> struct DenseMapInfo
<const char*> {
68 static inline const char* getEmptyKey() {
69 return reinterpret_cast<const char *>((intptr_t)-1);
71 static inline const char* getTombstoneKey() {
72 return reinterpret_cast<const char *>((intptr_t)-2);
74 static unsigned getHashValue(const char* const &Val
) {
75 return _objc_strhash(Val
);
77 static bool isEqual(const char* const &LHS
, const char* const &RHS
) {
78 return 0 == strcmp(LHS
, RHS
);
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
) {
92 // Provide DenseMapInfo for unsigned ints.
93 template<> struct DenseMapInfo
<unsigned> {
94 static inline unsigned getEmptyKey() { return ~0U; }
95 static inline unsigned getTombstoneKey() { return ~0U - 1; }
96 static unsigned getHashValue(const unsigned& Val
) { return Val
* 37U; }
97 static bool isEqual(const unsigned& LHS
, const unsigned& RHS
) {
102 // Provide DenseMapInfo for unsigned longs.
103 template<> struct DenseMapInfo
<unsigned long> {
104 static inline unsigned long getEmptyKey() { return ~0UL; }
105 static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
106 static unsigned getHashValue(const unsigned long& Val
) {
107 return (unsigned)(Val
* 37UL);
109 static bool isEqual(const unsigned long& LHS
, const unsigned long& RHS
) {
114 // Provide DenseMapInfo for unsigned long longs.
115 template<> struct DenseMapInfo
<unsigned long long> {
116 static inline unsigned long long getEmptyKey() { return ~0ULL; }
117 static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
118 static unsigned getHashValue(const unsigned long long& Val
) {
119 return (unsigned)(Val
* 37ULL);
121 static bool isEqual(const unsigned long long& LHS
,
122 const unsigned long long& RHS
) {
127 // Provide DenseMapInfo for ints.
128 template<> struct DenseMapInfo
<int> {
129 static inline int getEmptyKey() { return 0x7fffffff; }
130 static inline int getTombstoneKey() { return -0x7fffffff - 1; }
131 static unsigned getHashValue(const int& Val
) { return (unsigned)(Val
* 37U); }
132 static bool isEqual(const int& LHS
, const int& RHS
) {
137 // Provide DenseMapInfo for longs.
138 template<> struct DenseMapInfo
<long> {
139 static inline long getEmptyKey() {
140 return (1UL << (sizeof(long) * 8 - 1)) - 1UL;
142 static inline long getTombstoneKey() { return getEmptyKey() - 1L; }
143 static unsigned getHashValue(const long& Val
) {
144 return (unsigned)(Val
* 37UL);
146 static bool isEqual(const long& LHS
, const long& RHS
) {
151 // Provide DenseMapInfo for long longs.
152 template<> struct DenseMapInfo
<long long> {
153 static inline long long getEmptyKey() { return 0x7fffffffffffffffLL
; }
154 static inline long long getTombstoneKey() { return -0x7fffffffffffffffLL
-1; }
155 static unsigned getHashValue(const long long& Val
) {
156 return (unsigned)(Val
* 37ULL);
158 static bool isEqual(const long long& LHS
,
159 const long long& RHS
) {
164 // Provide DenseMapInfo for all pairs whose members have info.
165 template<typename T
, typename U
>
166 struct DenseMapInfo
<std::pair
<T
, U
> > {
167 typedef std::pair
<T
, U
> Pair
;
168 typedef DenseMapInfo
<T
> FirstInfo
;
169 typedef DenseMapInfo
<U
> SecondInfo
;
171 static inline Pair
getEmptyKey() {
172 return std::make_pair(FirstInfo::getEmptyKey(),
173 SecondInfo::getEmptyKey());
175 static inline Pair
getTombstoneKey() {
176 return std::make_pair(FirstInfo::getTombstoneKey(),
177 SecondInfo::getTombstoneKey());
179 static unsigned getHashValue(const Pair
& PairVal
) {
180 uint64_t key
= (uint64_t)FirstInfo::getHashValue(PairVal
.first
) << 32
181 | (uint64_t)SecondInfo::getHashValue(PairVal
.second
);
190 return (unsigned)key
;
192 static bool isEqual(const Pair
&LHS
, const Pair
&RHS
) {
193 return FirstInfo::isEqual(LHS
.first
, RHS
.first
) &&
194 SecondInfo::isEqual(LHS
.second
, RHS
.second
);
198 } // end namespace objc