DenseMap.h revision 04a3115e619740245cbe34c8c7428b4bde7868f7
1//===- llvm/ADT/DenseMap.h - Dense probed hash table ------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the DenseMap class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_DENSEMAP_H
15#define LLVM_ADT_DENSEMAP_H
16
17#include "llvm/Support/DataTypes.h"
18#include <cassert>
19#include <utility>
20
21namespace llvm {
22
23template<typename T>
24struct DenseMapKeyInfo {
25  //static inline T getEmptyKey();
26  //static inline T getTombstoneKey();
27  //static unsigned getHashValue(const T &Val);
28  //static bool isPod()
29};
30
31// Provide DenseMapKeyInfo for all pointers.
32template<typename T>
33struct DenseMapKeyInfo<T*> {
34  static inline T* getEmptyKey() { return (T*)-1; }
35  static inline T* getTombstoneKey() { return (T*)-2; }
36  static unsigned getHashValue(const T *PtrVal) {
37    return (unsigned)((uintptr_t)PtrVal >> 4) ^
38           (unsigned)((uintptr_t)PtrVal >> 9);
39  }
40  static bool isPod() { return true; }
41};
42
43template<typename KeyT, typename ValueT>
44class DenseMapIterator;
45template<typename KeyT, typename ValueT>
46class DenseMapConstIterator;
47
48template<typename KeyT, typename ValueT>
49class DenseMap {
50  typedef std::pair<KeyT, ValueT> BucketT;
51  unsigned NumBuckets;
52  BucketT *Buckets;
53
54  unsigned NumEntries;
55  unsigned NumTombstones;
56  DenseMap(const DenseMap &); // not implemented.
57public:
58  explicit DenseMap(unsigned NumInitBuckets = 64) {
59    init(NumInitBuckets);
60  }
61  ~DenseMap() {
62    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
63    for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
64      if (P->first != EmptyKey && P->first != TombstoneKey)
65        P->second.~ValueT();
66      P->first.~KeyT();
67    }
68    delete[] (char*)Buckets;
69  }
70
71  typedef DenseMapIterator<KeyT, ValueT> iterator;
72  typedef DenseMapConstIterator<KeyT, ValueT> const_iterator;
73  inline iterator begin() {
74     return DenseMapIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
75  }
76  inline iterator end() {
77    return DenseMapIterator<KeyT, ValueT>(Buckets+NumBuckets,
78                                          Buckets+NumBuckets);
79  }
80  inline const_iterator begin() const {
81    return DenseMapConstIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
82  }
83  inline const_iterator end() const {
84    return DenseMapConstIterator<KeyT, ValueT>(Buckets+NumBuckets,
85                                               Buckets+NumBuckets);
86  }
87
88  bool empty() const { return NumEntries == 0; }
89  unsigned size() const { return NumEntries; }
90
91  void clear() {
92    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
93    for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
94      if (P->first != EmptyKey && P->first != TombstoneKey) {
95        P->first = EmptyKey;
96        P->second.~ValueT();
97        --NumEntries;
98      }
99    }
100    assert(NumEntries == 0 && "Node count imbalance!");
101    NumTombstones = 0;
102  }
103
104  /// count - Return true if the specified key is in the map.
105  bool count(const KeyT &Val) const {
106    BucketT *TheBucket;
107    return LookupBucketFor(Val, TheBucket);
108  }
109
110  iterator find(const KeyT &Val) const {
111    BucketT *TheBucket;
112    if (LookupBucketFor(Val, TheBucket))
113      return iterator(TheBucket, Buckets+NumBuckets);
114    return end();
115  }
116
117  bool insert(const std::pair<KeyT, ValueT> &KV) {
118    BucketT *TheBucket;
119    if (LookupBucketFor(KV.first, TheBucket))
120      return false; // Already in map.
121
122    // Otherwise, insert the new element.
123    InsertIntoBucket(KV.first, KV.second, TheBucket);
124    return true;
125  }
126
127  bool erase(const KeyT &Val) {
128    BucketT *TheBucket;
129    if (!LookupBucketFor(Val, TheBucket))
130      return false; // not in map.
131
132    TheBucket->second.~ValueT();
133    TheBucket->first = getTombstoneKey();
134    --NumEntries;
135    ++NumTombstones;
136    return true;
137  }
138  bool erase(iterator I) {
139    BucketT *TheBucket = &*I;
140    TheBucket->second.~ValueT();
141    TheBucket->first = getTombstoneKey();
142    --NumEntries;
143    ++NumTombstones;
144    return true;
145  }
146
147  ValueT &operator[](const KeyT &Key) {
148    BucketT *TheBucket;
149    if (LookupBucketFor(Key, TheBucket))
150      return TheBucket->second;
151
152    return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
153  }
154
155private:
156  BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
157                            BucketT *TheBucket) {
158    // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
159    // the buckets are empty (meaning that many are filled with tombstones),
160    // grow the table.
161    //
162    // The later case is tricky.  For example, if we had one empty bucket with
163    // tons of tombstones, failing lookups (e.g. for insertion) would have to
164    // probe almost the entire table until it found the empty bucket.  If the
165    // table completely filled with tombstones, no lookup would ever succeed,
166    // causing infinite loops in lookup.
167    if (NumEntries*4 >= NumBuckets*3 ||
168        NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
169      this->grow();
170      LookupBucketFor(Key, TheBucket);
171    }
172    ++NumEntries;
173
174    // If we are writing over a tombstone, remember this.
175    if (TheBucket->first != getEmptyKey())
176      --NumTombstones;
177
178    TheBucket->first = Key;
179    new (&TheBucket->second) ValueT(Value);
180    return TheBucket;
181  }
182
183  static unsigned getHashValue(const KeyT &Val) {
184    return DenseMapKeyInfo<KeyT>::getHashValue(Val);
185  }
186  static const KeyT getEmptyKey() {
187    return DenseMapKeyInfo<KeyT>::getEmptyKey();
188  }
189  static const KeyT getTombstoneKey() {
190    return DenseMapKeyInfo<KeyT>::getTombstoneKey();
191  }
192
193  /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
194  /// FoundBucket.  If the bucket contains the key and a value, this returns
195  /// true, otherwise it returns a bucket with an empty marker or tombstone and
196  /// returns false.
197  bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
198    unsigned BucketNo = getHashValue(Val);
199    unsigned ProbeAmt = 1;
200    BucketT *BucketsPtr = Buckets;
201
202    // FoundTombstone - Keep track of whether we find a tombstone while probing.
203    BucketT *FoundTombstone = 0;
204    const KeyT EmptyKey = getEmptyKey();
205    const KeyT TombstoneKey = getTombstoneKey();
206    assert(Val != EmptyKey && Val != TombstoneKey &&
207           "Empty/Tombstone value shouldn't be inserted into map!");
208
209    while (1) {
210      BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
211      // Found Val's bucket?  If so, return it.
212      if (ThisBucket->first == Val) {
213        FoundBucket = ThisBucket;
214        return true;
215      }
216
217      // If we found an empty bucket, the key doesn't exist in the set.
218      // Insert it and return the default value.
219      if (ThisBucket->first == EmptyKey) {
220        // If we've already seen a tombstone while probing, fill it in instead
221        // of the empty bucket we eventually probed to.
222        if (FoundTombstone) ThisBucket = FoundTombstone;
223        FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
224        return false;
225      }
226
227      // If this is a tombstone, remember it.  If Val ends up not in the map, we
228      // prefer to return it than something that would require more probing.
229      if (ThisBucket->first == TombstoneKey && !FoundTombstone)
230        FoundTombstone = ThisBucket;  // Remember the first tombstone found.
231
232      // Otherwise, it's a hash collision or a tombstone, continue quadratic
233      // probing.
234      BucketNo += ProbeAmt++;
235    }
236  }
237
238  void init(unsigned InitBuckets) {
239    NumEntries = 0;
240    NumTombstones = 0;
241    NumBuckets = InitBuckets;
242    assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
243           "# initial buckets must be a power of two!");
244    Buckets = (BucketT*)new char[sizeof(BucketT)*InitBuckets];
245    // Initialize all the keys to EmptyKey.
246    const KeyT EmptyKey = getEmptyKey();
247    for (unsigned i = 0; i != InitBuckets; ++i)
248      new (&Buckets[i].first) KeyT(EmptyKey);
249  }
250
251  void grow() {
252    unsigned OldNumBuckets = NumBuckets;
253    BucketT *OldBuckets = Buckets;
254
255    // Double the number of buckets.
256    NumBuckets <<= 1;
257    NumTombstones = 0;
258    Buckets = (BucketT*)new char[sizeof(BucketT)*NumBuckets];
259
260    // Initialize all the keys to EmptyKey.
261    const KeyT EmptyKey = getEmptyKey();
262    for (unsigned i = 0, e = NumBuckets; i != e; ++i)
263      new (&Buckets[i].first) KeyT(EmptyKey);
264
265    // Insert all the old elements.
266    const KeyT TombstoneKey = getTombstoneKey();
267    for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
268      if (B->first != EmptyKey && B->first != TombstoneKey) {
269        // Insert the key/value into the new table.
270        BucketT *DestBucket;
271        bool FoundVal = LookupBucketFor(B->first, DestBucket);
272        FoundVal = FoundVal; // silence warning.
273        assert(!FoundVal && "Key already in new map?");
274        DestBucket->first = B->first;
275        new (&DestBucket->second) ValueT(B->second);
276
277        // Free the value.
278        B->second.~ValueT();
279      }
280      B->first.~KeyT();
281    }
282
283    // Free the old table.
284    delete[] (char*)OldBuckets;
285  }
286};
287
288template<typename KeyT, typename ValueT>
289class DenseMapIterator {
290  typedef std::pair<KeyT, ValueT> BucketT;
291protected:
292  const BucketT *Ptr, *End;
293public:
294  DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
295    AdvancePastEmptyBuckets();
296  }
297
298  std::pair<KeyT, ValueT> &operator*() const {
299    return *const_cast<BucketT*>(Ptr);
300  }
301  std::pair<KeyT, ValueT> *operator->() const {
302    return const_cast<BucketT*>(Ptr);
303  }
304
305  bool operator==(const DenseMapIterator &RHS) const {
306    return Ptr == RHS.Ptr;
307  }
308  bool operator!=(const DenseMapIterator &RHS) const {
309    return Ptr != RHS.Ptr;
310  }
311
312  inline DenseMapIterator& operator++() {          // Preincrement
313    ++Ptr;
314    AdvancePastEmptyBuckets();
315    return *this;
316  }
317  DenseMapIterator operator++(int) {        // Postincrement
318    DenseMapIterator tmp = *this; ++*this; return tmp;
319  }
320
321private:
322  void AdvancePastEmptyBuckets() {
323    const KeyT Empty = DenseMapKeyInfo<KeyT>::getEmptyKey();
324    const KeyT Tombstone = DenseMapKeyInfo<KeyT>::getTombstoneKey();
325
326    while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
327      ++Ptr;
328  }
329};
330
331template<typename KeyT, typename ValueT>
332class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT> {
333public:
334  DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
335                        const std::pair<KeyT, ValueT> *E)
336    : DenseMapIterator<KeyT, ValueT>(Pos, E) {
337  }
338  const std::pair<KeyT, ValueT> &operator*() const {
339    return *this->Ptr;
340  }
341  const std::pair<KeyT, ValueT> *operator->() const {
342    return this->Ptr;
343  }
344};
345
346} // end namespace llvm
347
348#endif
349