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