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 #ifndef LLVM_ADT_DENSEMAPINFO_H
15 #define LLVM_ADT_DENSEMAPINFO_H
17 #include "llvm/Support/PointerLikeTypeTraits.h"
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);
31 // Provide DenseMapInfo for all pointers.
33 struct DenseMapInfo<T*> {
34 static inline T* getEmptyKey() {
36 Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
37 return reinterpret_cast<T*>(Val);
39 static inline T* getTombstoneKey() {
41 Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
42 return reinterpret_cast<T*>(Val);
44 static unsigned getHashValue(const T *PtrVal) {
45 return (unsigned((uintptr_t)PtrVal) >> 4) ^
46 (unsigned((uintptr_t)PtrVal) >> 9);
48 static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
49 static bool isPod() { return true; }
52 // Provide DenseMapInfo for chars.
53 template<> struct DenseMapInfo<char> {
54 static inline char getEmptyKey() { return ~0; }
55 static inline char getTombstoneKey() { return ~0 - 1; }
56 static unsigned getHashValue(const char& Val) { return Val * 37; }
57 static bool isPod() { return true; }
58 static bool isEqual(const char &LHS, const char &RHS) {
63 // Provide DenseMapInfo for unsigned ints.
64 template<> struct DenseMapInfo<unsigned> {
65 static inline unsigned getEmptyKey() { return ~0; }
66 static inline unsigned getTombstoneKey() { return ~0U - 1; }
67 static unsigned getHashValue(const unsigned& Val) { return Val * 37; }
68 static bool isPod() { return true; }
69 static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
74 // Provide DenseMapInfo for unsigned longs.
75 template<> struct DenseMapInfo<unsigned long> {
76 static inline unsigned long getEmptyKey() { return ~0UL; }
77 static inline unsigned long getTombstoneKey() { return ~0UL - 1L; }
78 static unsigned getHashValue(const unsigned long& Val) {
81 static bool isPod() { return true; }
82 static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
87 // Provide DenseMapInfo for unsigned long longs.
88 template<> struct DenseMapInfo<unsigned long long> {
89 static inline unsigned long long getEmptyKey() { return ~0ULL; }
90 static inline unsigned long long getTombstoneKey() { return ~0ULL - 1ULL; }
91 static unsigned getHashValue(const unsigned long long& Val) {
94 static bool isPod() { return true; }
95 static bool isEqual(const unsigned long long& LHS,
96 const unsigned long long& RHS) {
101 // Provide DenseMapInfo for all pairs whose members have info.
102 template<typename T, typename U>
103 struct DenseMapInfo<std::pair<T, U> > {
104 typedef std::pair<T, U> Pair;
105 typedef DenseMapInfo<T> FirstInfo;
106 typedef DenseMapInfo<U> SecondInfo;
108 static inline Pair getEmptyKey() {
109 return std::make_pair(FirstInfo::getEmptyKey(),
110 SecondInfo::getEmptyKey());
112 static inline Pair getTombstoneKey() {
113 return std::make_pair(FirstInfo::getTombstoneKey(),
114 SecondInfo::getEmptyKey());
116 static unsigned getHashValue(const Pair& PairVal) {
117 uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
118 | (uint64_t)SecondInfo::getHashValue(PairVal.second);
127 return (unsigned)key;
129 static bool isEqual(const Pair& LHS, const Pair& RHS) { return LHS == RHS; }
130 static bool isPod() { return FirstInfo::isPod() && SecondInfo::isPod(); }
133 } // end namespace llvm