DenseMap.h revision 05831c073abbaacbf2fc21ef5ef160872056e075
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 "llvm/Support/MathExtras.h"
19#include <cassert>
20#include <utility>
21
22namespace llvm {
23
24template<typename T>
25struct DenseMapInfo {
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);
30  //static bool isPod()
31};
32
33// Provide DenseMapInfo for all pointers.
34template<typename T>
35struct DenseMapInfo<T*> {
36  static inline T* getEmptyKey() { return reinterpret_cast<T*>(-1); }
37  static inline T* getTombstoneKey() { return reinterpret_cast<T*>(-2); }
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  static bool isPod() { return true; }
44};
45
46template<typename KeyT, typename ValueT,
47         typename KeyInfoT = DenseMapInfo<KeyT>,
48         typename ValueInfoT = DenseMapInfo<ValueT> >
49class DenseMapIterator;
50template<typename KeyT, typename ValueT,
51         typename KeyInfoT = DenseMapInfo<KeyT>,
52         typename ValueInfoT = DenseMapInfo<ValueT> >
53class DenseMapConstIterator;
54
55template<typename KeyT, typename ValueT,
56         typename KeyInfoT = DenseMapInfo<KeyT>,
57         typename ValueInfoT = DenseMapInfo<ValueT> >
58class DenseMap {
59  typedef std::pair<KeyT, ValueT> BucketT;
60  unsigned NumBuckets;
61  BucketT *Buckets;
62
63  unsigned NumEntries;
64  unsigned NumTombstones;
65public:
66  DenseMap(const DenseMap& other) {
67    NumBuckets = 0;
68    CopyFrom(other);
69  }
70
71  explicit DenseMap(unsigned NumInitBuckets = 64) {
72    init(NumInitBuckets);
73  }
74
75  ~DenseMap() {
76    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
77    for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
78      if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
79          !KeyInfoT::isEqual(P->first, TombstoneKey))
80        P->second.~ValueT();
81      P->first.~KeyT();
82    }
83    delete[] reinterpret_cast<char*>(Buckets);
84  }
85
86  typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
87  typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
88  inline iterator begin() {
89     return iterator(Buckets, Buckets+NumBuckets);
90  }
91  inline iterator end() {
92    return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
93  }
94  inline const_iterator begin() const {
95    return const_iterator(Buckets, Buckets+NumBuckets);
96  }
97  inline const_iterator end() const {
98    return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
99  }
100
101  bool empty() const { return NumEntries == 0; }
102  unsigned size() const { return NumEntries; }
103
104  /// Grow the densemap so that it has at least Size buckets. Does not shrink
105  void resize(size_t Size) { grow(Size); }
106
107  void clear() {
108    // If the capacity of the array is huge, and the # elements used is small,
109    // shrink the array.
110    if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
111      shrink_and_clear();
112      return;
113    }
114
115    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
116    for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
117      if (!KeyInfoT::isEqual(P->first, EmptyKey)) {
118        if (!KeyInfoT::isEqual(P->first, TombstoneKey)) {
119          P->second.~ValueT();
120          --NumEntries;
121        }
122        P->first = EmptyKey;
123      }
124    }
125    assert(NumEntries == 0 && "Node count imbalance!");
126    NumTombstones = 0;
127  }
128
129  /// count - Return true if the specified key is in the map.
130  bool count(const KeyT &Val) const {
131    BucketT *TheBucket;
132    return LookupBucketFor(Val, TheBucket);
133  }
134
135  iterator find(const KeyT &Val) {
136    BucketT *TheBucket;
137    if (LookupBucketFor(Val, TheBucket))
138      return iterator(TheBucket, Buckets+NumBuckets);
139    return end();
140  }
141  const_iterator find(const KeyT &Val) const {
142    BucketT *TheBucket;
143    if (LookupBucketFor(Val, TheBucket))
144      return const_iterator(TheBucket, Buckets+NumBuckets);
145    return end();
146  }
147
148  bool insert(const std::pair<KeyT, ValueT> &KV) {
149    BucketT *TheBucket;
150    if (LookupBucketFor(KV.first, TheBucket))
151      return false; // Already in map.
152
153    // Otherwise, insert the new element.
154    InsertIntoBucket(KV.first, KV.second, TheBucket);
155    return true;
156  }
157
158  bool erase(const KeyT &Val) {
159    BucketT *TheBucket;
160    if (!LookupBucketFor(Val, TheBucket))
161      return false; // not in map.
162
163    TheBucket->second.~ValueT();
164    TheBucket->first = getTombstoneKey();
165    --NumEntries;
166    ++NumTombstones;
167    return true;
168  }
169  bool erase(iterator I) {
170    BucketT *TheBucket = &*I;
171    TheBucket->second.~ValueT();
172    TheBucket->first = getTombstoneKey();
173    --NumEntries;
174    ++NumTombstones;
175    return true;
176  }
177
178  ValueT &operator[](const KeyT &Key) {
179    BucketT *TheBucket;
180    if (LookupBucketFor(Key, TheBucket))
181      return TheBucket->second;
182
183    return InsertIntoBucket(Key, ValueT(), TheBucket)->second;
184  }
185
186  DenseMap& operator=(const DenseMap& other) {
187    CopyFrom(other);
188    return *this;
189  }
190
191private:
192  void CopyFrom(const DenseMap& other) {
193    if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
194      const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
195      for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
196        if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
197            !KeyInfoT::isEqual(P->first, TombstoneKey))
198          P->second.~ValueT();
199        P->first.~KeyT();
200      }
201    }
202
203    NumEntries = other.NumEntries;
204    NumTombstones = other.NumTombstones;
205
206    if (NumBuckets)
207      delete[] reinterpret_cast<char*>(Buckets);
208    Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT) *
209                                                  other.NumBuckets]);
210
211    if (KeyInfoT::isPod() && ValueInfoT::isPod())
212      memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
213    else
214      for (size_t i = 0; i < other.NumBuckets; ++i) {
215        new (Buckets[i].first) KeyT(other.Buckets[i].first);
216        if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
217            !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
218          new (&Buckets[i].second) ValueT(other.Buckets[i].second);
219      }
220    NumBuckets = other.NumBuckets;
221  }
222
223  BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
224                            BucketT *TheBucket) {
225    // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
226    // the buckets are empty (meaning that many are filled with tombstones),
227    // grow the table.
228    //
229    // The later case is tricky.  For example, if we had one empty bucket with
230    // tons of tombstones, failing lookups (e.g. for insertion) would have to
231    // probe almost the entire table until it found the empty bucket.  If the
232    // table completely filled with tombstones, no lookup would ever succeed,
233    // causing infinite loops in lookup.
234    if (NumEntries*4 >= NumBuckets*3 ||
235        NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
236      this->grow(NumBuckets * 2);
237      LookupBucketFor(Key, TheBucket);
238    }
239    ++NumEntries;
240
241    // If we are writing over a tombstone, remember this.
242    if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
243      --NumTombstones;
244
245    TheBucket->first = Key;
246    new (&TheBucket->second) ValueT(Value);
247    return TheBucket;
248  }
249
250  static unsigned getHashValue(const KeyT &Val) {
251    return KeyInfoT::getHashValue(Val);
252  }
253  static const KeyT getEmptyKey() {
254    return KeyInfoT::getEmptyKey();
255  }
256  static const KeyT getTombstoneKey() {
257    return KeyInfoT::getTombstoneKey();
258  }
259
260  /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
261  /// FoundBucket.  If the bucket contains the key and a value, this returns
262  /// true, otherwise it returns a bucket with an empty marker or tombstone and
263  /// returns false.
264  bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
265    unsigned BucketNo = getHashValue(Val);
266    unsigned ProbeAmt = 1;
267    BucketT *BucketsPtr = Buckets;
268
269    // FoundTombstone - Keep track of whether we find a tombstone while probing.
270    BucketT *FoundTombstone = 0;
271    const KeyT EmptyKey = getEmptyKey();
272    const KeyT TombstoneKey = getTombstoneKey();
273    assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
274           !KeyInfoT::isEqual(Val, TombstoneKey) &&
275           "Empty/Tombstone value shouldn't be inserted into map!");
276
277    while (1) {
278      BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
279      // Found Val's bucket?  If so, return it.
280      if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
281        FoundBucket = ThisBucket;
282        return true;
283      }
284
285      // If we found an empty bucket, the key doesn't exist in the set.
286      // Insert it and return the default value.
287      if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
288        // If we've already seen a tombstone while probing, fill it in instead
289        // of the empty bucket we eventually probed to.
290        if (FoundTombstone) ThisBucket = FoundTombstone;
291        FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
292        return false;
293      }
294
295      // If this is a tombstone, remember it.  If Val ends up not in the map, we
296      // prefer to return it than something that would require more probing.
297      if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
298        FoundTombstone = ThisBucket;  // Remember the first tombstone found.
299
300      // Otherwise, it's a hash collision or a tombstone, continue quadratic
301      // probing.
302      BucketNo += ProbeAmt++;
303    }
304  }
305
306  void init(unsigned InitBuckets) {
307    NumEntries = 0;
308    NumTombstones = 0;
309    NumBuckets = InitBuckets;
310    assert(InitBuckets && (InitBuckets & InitBuckets-1) == 0 &&
311           "# initial buckets must be a power of two!");
312    Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*InitBuckets]);
313    // Initialize all the keys to EmptyKey.
314    const KeyT EmptyKey = getEmptyKey();
315    for (unsigned i = 0; i != InitBuckets; ++i)
316      new (&Buckets[i].first) KeyT(EmptyKey);
317  }
318
319  void grow(unsigned AtLeast) {
320    unsigned OldNumBuckets = NumBuckets;
321    BucketT *OldBuckets = Buckets;
322
323    // Double the number of buckets.
324    while (NumBuckets <= AtLeast)
325      NumBuckets <<= 1;
326    NumTombstones = 0;
327    Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
328
329    // Initialize all the keys to EmptyKey.
330    const KeyT EmptyKey = getEmptyKey();
331    for (unsigned i = 0, e = NumBuckets; i != e; ++i)
332      new (&Buckets[i].first) KeyT(EmptyKey);
333
334    // Insert all the old elements.
335    const KeyT TombstoneKey = getTombstoneKey();
336    for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
337      if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
338          !KeyInfoT::isEqual(B->first, TombstoneKey)) {
339        // Insert the key/value into the new table.
340        BucketT *DestBucket;
341        bool FoundVal = LookupBucketFor(B->first, DestBucket);
342        FoundVal = FoundVal; // silence warning.
343        assert(!FoundVal && "Key already in new map?");
344        DestBucket->first = B->first;
345        new (&DestBucket->second) ValueT(B->second);
346
347        // Free the value.
348        B->second.~ValueT();
349      }
350      B->first.~KeyT();
351    }
352
353    // Free the old table.
354    delete[] reinterpret_cast<char*>(OldBuckets);
355  }
356
357  void shrink_and_clear() {
358    unsigned OldNumBuckets = NumBuckets;
359    BucketT *OldBuckets = Buckets;
360
361    // Reduce the number of buckets.
362    NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
363                                 : 64;
364    NumTombstones = 0;
365    Buckets = reinterpret_cast<BucketT*>(new char[sizeof(BucketT)*NumBuckets]);
366
367    // Initialize all the keys to EmptyKey.
368    const KeyT EmptyKey = getEmptyKey();
369    for (unsigned i = 0, e = NumBuckets; i != e; ++i)
370      new (&Buckets[i].first) KeyT(EmptyKey);
371
372    // Free the old buckets.
373    const KeyT TombstoneKey = getTombstoneKey();
374    for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
375      if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
376          !KeyInfoT::isEqual(B->first, TombstoneKey)) {
377        // Free the value.
378        B->second.~ValueT();
379      }
380      B->first.~KeyT();
381    }
382
383    // Free the old table.
384    delete[] reinterpret_cast<char*>(OldBuckets);
385
386    NumEntries = 0;
387  }
388};
389
390template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
391class DenseMapIterator {
392  typedef std::pair<KeyT, ValueT> BucketT;
393protected:
394  const BucketT *Ptr, *End;
395public:
396  DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
397    AdvancePastEmptyBuckets();
398  }
399
400  std::pair<KeyT, ValueT> &operator*() const {
401    return *const_cast<BucketT*>(Ptr);
402  }
403  std::pair<KeyT, ValueT> *operator->() const {
404    return const_cast<BucketT*>(Ptr);
405  }
406
407  bool operator==(const DenseMapIterator &RHS) const {
408    return Ptr == RHS.Ptr;
409  }
410  bool operator!=(const DenseMapIterator &RHS) const {
411    return Ptr != RHS.Ptr;
412  }
413
414  inline DenseMapIterator& operator++() {          // Preincrement
415    ++Ptr;
416    AdvancePastEmptyBuckets();
417    return *this;
418  }
419  DenseMapIterator operator++(int) {        // Postincrement
420    DenseMapIterator tmp = *this; ++*this; return tmp;
421  }
422
423private:
424  void AdvancePastEmptyBuckets() {
425    const KeyT Empty = KeyInfoT::getEmptyKey();
426    const KeyT Tombstone = KeyInfoT::getTombstoneKey();
427
428    while (Ptr != End &&
429           (KeyInfoT::isEqual(Ptr->first, Empty) ||
430            KeyInfoT::isEqual(Ptr->first, Tombstone)))
431      ++Ptr;
432  }
433};
434
435template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
436class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
437public:
438  DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
439                        const std::pair<KeyT, ValueT> *E)
440    : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
441  }
442  const std::pair<KeyT, ValueT> &operator*() const {
443    return *this->Ptr;
444  }
445  const std::pair<KeyT, ValueT> *operator->() const {
446    return this->Ptr;
447  }
448};
449
450} // end namespace llvm
451
452#endif
453