DenseMap.h revision bdd376ccb25f251e115fff24b526b4e65e03a1d3
1//===- llvm/ADT/DenseMap.h - Dense probed hash table ------------*- 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 the DenseMap class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_DENSEMAP_H
15#define LLVM_ADT_DENSEMAP_H
16
17#include "llvm/Support/PointerLikeTypeTraits.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() {
37    intptr_t Val = -1;
38    Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
39    return reinterpret_cast<T*>(Val);
40  }
41  static inline T* getTombstoneKey() {
42    intptr_t Val = -2;
43    Val <<= PointerLikeTypeTraits<T*>::NumLowBitsAvailable;
44    return reinterpret_cast<T*>(Val);
45  }
46  static unsigned getHashValue(const T *PtrVal) {
47    return (unsigned((uintptr_t)PtrVal) >> 4) ^
48           (unsigned((uintptr_t)PtrVal) >> 9);
49  }
50  static bool isEqual(const T *LHS, const T *RHS) { return LHS == RHS; }
51  static bool isPod() { return true; }
52};
53
54// Provide DenseMapInfo for unsigned ints.
55template<> struct DenseMapInfo<unsigned> {
56  static inline unsigned getEmptyKey() { return ~0; }
57  static inline unsigned getTombstoneKey() { return ~0 - 1; }
58  static unsigned getHashValue(const unsigned& Val) { return Val * 37; }
59  static bool isPod() { return true; }
60  static bool isEqual(const unsigned& LHS, const unsigned& RHS) {
61  return LHS == RHS;
62  }
63};
64
65// Provide DenseMapInfo for unsigned longs.
66template<> struct DenseMapInfo<unsigned long> {
67  static inline unsigned long getEmptyKey() { return ~0L; }
68  static inline unsigned long getTombstoneKey() { return ~0L - 1L; }
69  static unsigned getHashValue(const unsigned long& Val) {
70    return (unsigned)(Val * 37L);
71  }
72  static bool isPod() { return true; }
73  static bool isEqual(const unsigned long& LHS, const unsigned long& RHS) {
74  return LHS == RHS;
75  }
76};
77
78// Provide DenseMapInfo for all pairs whose members have info.
79template<typename T, typename U>
80struct DenseMapInfo<std::pair<T, U> > {
81  typedef std::pair<T, U> Pair;
82  typedef DenseMapInfo<T> FirstInfo;
83  typedef DenseMapInfo<U> SecondInfo;
84
85  static inline Pair getEmptyKey() {
86    return std::make_pair(FirstInfo::getEmptyKey(),
87                          SecondInfo::getEmptyKey());
88  }
89  static inline Pair getTombstoneKey() {
90    return std::make_pair(FirstInfo::getTombstoneKey(),
91                            SecondInfo::getEmptyKey());
92  }
93  static unsigned getHashValue(const Pair& PairVal) {
94    uint64_t key = (uint64_t)FirstInfo::getHashValue(PairVal.first) << 32
95          | (uint64_t)SecondInfo::getHashValue(PairVal.second);
96    key += ~(key << 32);
97    key ^= (key >> 22);
98    key += ~(key << 13);
99    key ^= (key >> 8);
100    key += (key << 3);
101    key ^= (key >> 15);
102    key += ~(key << 27);
103    key ^= (key >> 31);
104    return (unsigned)key;
105  }
106  static bool isEqual(const Pair& LHS, const Pair& RHS) { return LHS == RHS; }
107  static bool isPod() { return FirstInfo::isPod() && SecondInfo::isPod(); }
108};
109
110template<typename KeyT, typename ValueT,
111         typename KeyInfoT = DenseMapInfo<KeyT>,
112         typename ValueInfoT = DenseMapInfo<ValueT> >
113class DenseMapIterator;
114template<typename KeyT, typename ValueT,
115         typename KeyInfoT = DenseMapInfo<KeyT>,
116         typename ValueInfoT = DenseMapInfo<ValueT> >
117class DenseMapConstIterator;
118
119template<typename KeyT, typename ValueT,
120         typename KeyInfoT = DenseMapInfo<KeyT>,
121         typename ValueInfoT = DenseMapInfo<ValueT> >
122class DenseMap {
123  typedef std::pair<KeyT, ValueT> BucketT;
124  unsigned NumBuckets;
125  BucketT *Buckets;
126
127  unsigned NumEntries;
128  unsigned NumTombstones;
129public:
130  typedef KeyT key_type;
131  typedef ValueT mapped_type;
132  typedef BucketT value_type;
133
134  DenseMap(const DenseMap& other) {
135    NumBuckets = 0;
136    CopyFrom(other);
137  }
138
139  explicit DenseMap(unsigned NumInitBuckets = 64) {
140    init(NumInitBuckets);
141  }
142
143  ~DenseMap() {
144    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
145    for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
146      if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
147          !KeyInfoT::isEqual(P->first, TombstoneKey))
148        P->second.~ValueT();
149      P->first.~KeyT();
150    }
151    operator delete(Buckets);
152  }
153
154  typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
155  typedef DenseMapConstIterator<KeyT, ValueT, KeyInfoT> const_iterator;
156  inline iterator begin() {
157     return iterator(Buckets, Buckets+NumBuckets);
158  }
159  inline iterator end() {
160    return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
161  }
162  inline const_iterator begin() const {
163    return const_iterator(Buckets, Buckets+NumBuckets);
164  }
165  inline const_iterator end() const {
166    return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
167  }
168
169  bool empty() const { return NumEntries == 0; }
170  unsigned size() const { return NumEntries; }
171
172  /// Grow the densemap so that it has at least Size buckets. Does not shrink
173  void resize(size_t Size) { grow(Size); }
174
175  void clear() {
176    // If the capacity of the array is huge, and the # elements used is small,
177    // shrink the array.
178    if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
179      shrink_and_clear();
180      return;
181    }
182
183    const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
184    for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
185      if (!KeyInfoT::isEqual(P->first, EmptyKey)) {
186        if (!KeyInfoT::isEqual(P->first, TombstoneKey)) {
187          P->second.~ValueT();
188          --NumEntries;
189        }
190        P->first = EmptyKey;
191      }
192    }
193    assert(NumEntries == 0 && "Node count imbalance!");
194    NumTombstones = 0;
195  }
196
197  /// count - Return true if the specified key is in the map.
198  bool count(const KeyT &Val) const {
199    BucketT *TheBucket;
200    return LookupBucketFor(Val, TheBucket);
201  }
202
203  iterator find(const KeyT &Val) {
204    BucketT *TheBucket;
205    if (LookupBucketFor(Val, TheBucket))
206      return iterator(TheBucket, Buckets+NumBuckets);
207    return end();
208  }
209  const_iterator find(const KeyT &Val) const {
210    BucketT *TheBucket;
211    if (LookupBucketFor(Val, TheBucket))
212      return const_iterator(TheBucket, Buckets+NumBuckets);
213    return end();
214  }
215
216  /// lookup - Return the entry for the specified key, or a default
217  /// constructed value if no such entry exists.
218  ValueT lookup(const KeyT &Val) const {
219    BucketT *TheBucket;
220    if (LookupBucketFor(Val, TheBucket))
221      return TheBucket->second;
222    return ValueT();
223  }
224
225  std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
226    BucketT *TheBucket;
227    if (LookupBucketFor(KV.first, TheBucket))
228      return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
229                            false); // Already in map.
230
231    // Otherwise, insert the new element.
232    TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
233    return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
234                          true);
235  }
236
237  /// insert - Range insertion of pairs.
238  template<typename InputIt>
239  void insert(InputIt I, InputIt E) {
240    for (; I != E; ++I)
241      insert(*I);
242  }
243
244
245  bool erase(const KeyT &Val) {
246    BucketT *TheBucket;
247    if (!LookupBucketFor(Val, TheBucket))
248      return false; // not in map.
249
250    TheBucket->second.~ValueT();
251    TheBucket->first = getTombstoneKey();
252    --NumEntries;
253    ++NumTombstones;
254    return true;
255  }
256  bool erase(iterator I) {
257    BucketT *TheBucket = &*I;
258    TheBucket->second.~ValueT();
259    TheBucket->first = getTombstoneKey();
260    --NumEntries;
261    ++NumTombstones;
262    return true;
263  }
264
265  value_type& FindAndConstruct(const KeyT &Key) {
266    BucketT *TheBucket;
267    if (LookupBucketFor(Key, TheBucket))
268      return *TheBucket;
269
270    return *InsertIntoBucket(Key, ValueT(), TheBucket);
271  }
272
273  ValueT &operator[](const KeyT &Key) {
274    return FindAndConstruct(Key).second;
275  }
276
277  DenseMap& operator=(const DenseMap& other) {
278    CopyFrom(other);
279    return *this;
280  }
281
282  /// isPointerIntoBucketsArray - Return true if the specified pointer points
283  /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
284  /// value in the DenseMap).
285  bool isPointerIntoBucketsArray(const void *Ptr) const {
286    return Ptr >= Buckets && Ptr < Buckets+NumBuckets;
287  }
288
289  /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
290  /// array.  In conjunction with the previous method, this can be used to
291  /// determine whether an insertion caused the DenseMap to reallocate.
292  const void *getPointerIntoBucketsArray() const { return Buckets; }
293
294private:
295  void CopyFrom(const DenseMap& other) {
296    if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
297      const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
298      for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
299        if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
300            !KeyInfoT::isEqual(P->first, TombstoneKey))
301          P->second.~ValueT();
302        P->first.~KeyT();
303      }
304    }
305
306    NumEntries = other.NumEntries;
307    NumTombstones = other.NumTombstones;
308
309    if (NumBuckets)
310      operator delete(Buckets);
311    Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) *
312                                                 other.NumBuckets));
313
314    if (KeyInfoT::isPod() && ValueInfoT::isPod())
315      memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
316    else
317      for (size_t i = 0; i < other.NumBuckets; ++i) {
318        new (&Buckets[i].first) KeyT(other.Buckets[i].first);
319        if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
320            !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
321          new (&Buckets[i].second) ValueT(other.Buckets[i].second);
322      }
323    NumBuckets = other.NumBuckets;
324  }
325
326  BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
327                            BucketT *TheBucket) {
328    // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
329    // the buckets are empty (meaning that many are filled with tombstones),
330    // grow the table.
331    //
332    // The later case is tricky.  For example, if we had one empty bucket with
333    // tons of tombstones, failing lookups (e.g. for insertion) would have to
334    // probe almost the entire table until it found the empty bucket.  If the
335    // table completely filled with tombstones, no lookup would ever succeed,
336    // causing infinite loops in lookup.
337    if (NumEntries*4 >= NumBuckets*3 ||
338        NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
339      this->grow(NumBuckets * 2);
340      LookupBucketFor(Key, TheBucket);
341    }
342    ++NumEntries;
343
344    // If we are writing over a tombstone, remember this.
345    if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
346      --NumTombstones;
347
348    TheBucket->first = Key;
349    new (&TheBucket->second) ValueT(Value);
350    return TheBucket;
351  }
352
353  static unsigned getHashValue(const KeyT &Val) {
354    return KeyInfoT::getHashValue(Val);
355  }
356  static const KeyT getEmptyKey() {
357    return KeyInfoT::getEmptyKey();
358  }
359  static const KeyT getTombstoneKey() {
360    return KeyInfoT::getTombstoneKey();
361  }
362
363  /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
364  /// FoundBucket.  If the bucket contains the key and a value, this returns
365  /// true, otherwise it returns a bucket with an empty marker or tombstone and
366  /// returns false.
367  bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
368    unsigned BucketNo = getHashValue(Val);
369    unsigned ProbeAmt = 1;
370    BucketT *BucketsPtr = Buckets;
371
372    // FoundTombstone - Keep track of whether we find a tombstone while probing.
373    BucketT *FoundTombstone = 0;
374    const KeyT EmptyKey = getEmptyKey();
375    const KeyT TombstoneKey = getTombstoneKey();
376    assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
377           !KeyInfoT::isEqual(Val, TombstoneKey) &&
378           "Empty/Tombstone value shouldn't be inserted into map!");
379
380    while (1) {
381      BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
382      // Found Val's bucket?  If so, return it.
383      if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
384        FoundBucket = ThisBucket;
385        return true;
386      }
387
388      // If we found an empty bucket, the key doesn't exist in the set.
389      // Insert it and return the default value.
390      if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
391        // If we've already seen a tombstone while probing, fill it in instead
392        // of the empty bucket we eventually probed to.
393        if (FoundTombstone) ThisBucket = FoundTombstone;
394        FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
395        return false;
396      }
397
398      // If this is a tombstone, remember it.  If Val ends up not in the map, we
399      // prefer to return it than something that would require more probing.
400      if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
401        FoundTombstone = ThisBucket;  // Remember the first tombstone found.
402
403      // Otherwise, it's a hash collision or a tombstone, continue quadratic
404      // probing.
405      BucketNo += ProbeAmt++;
406    }
407  }
408
409  void init(unsigned InitBuckets) {
410    NumEntries = 0;
411    NumTombstones = 0;
412    NumBuckets = InitBuckets;
413    assert(InitBuckets && (InitBuckets & (InitBuckets-1)) == 0 &&
414           "# initial buckets must be a power of two!");
415    Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*InitBuckets));
416    // Initialize all the keys to EmptyKey.
417    const KeyT EmptyKey = getEmptyKey();
418    for (unsigned i = 0; i != InitBuckets; ++i)
419      new (&Buckets[i].first) KeyT(EmptyKey);
420  }
421
422  void grow(unsigned AtLeast) {
423    unsigned OldNumBuckets = NumBuckets;
424    BucketT *OldBuckets = Buckets;
425
426    // Double the number of buckets.
427    while (NumBuckets <= AtLeast)
428      NumBuckets <<= 1;
429    NumTombstones = 0;
430    Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
431
432    // Initialize all the keys to EmptyKey.
433    const KeyT EmptyKey = getEmptyKey();
434    for (unsigned i = 0, e = NumBuckets; i != e; ++i)
435      new (&Buckets[i].first) KeyT(EmptyKey);
436
437    // Insert all the old elements.
438    const KeyT TombstoneKey = getTombstoneKey();
439    for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
440      if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
441          !KeyInfoT::isEqual(B->first, TombstoneKey)) {
442        // Insert the key/value into the new table.
443        BucketT *DestBucket;
444        bool FoundVal = LookupBucketFor(B->first, DestBucket);
445        FoundVal = FoundVal; // silence warning.
446        assert(!FoundVal && "Key already in new map?");
447        DestBucket->first = B->first;
448        new (&DestBucket->second) ValueT(B->second);
449
450        // Free the value.
451        B->second.~ValueT();
452      }
453      B->first.~KeyT();
454    }
455
456    // Free the old table.
457    operator delete(OldBuckets);
458  }
459
460  void shrink_and_clear() {
461    unsigned OldNumBuckets = NumBuckets;
462    BucketT *OldBuckets = Buckets;
463
464    // Reduce the number of buckets.
465    NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
466                                 : 64;
467    NumTombstones = 0;
468    Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
469
470    // Initialize all the keys to EmptyKey.
471    const KeyT EmptyKey = getEmptyKey();
472    for (unsigned i = 0, e = NumBuckets; i != e; ++i)
473      new (&Buckets[i].first) KeyT(EmptyKey);
474
475    // Free the old buckets.
476    const KeyT TombstoneKey = getTombstoneKey();
477    for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
478      if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
479          !KeyInfoT::isEqual(B->first, TombstoneKey)) {
480        // Free the value.
481        B->second.~ValueT();
482      }
483      B->first.~KeyT();
484    }
485
486    // Free the old table.
487    operator delete(OldBuckets);
488
489    NumEntries = 0;
490  }
491};
492
493template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
494class DenseMapIterator {
495  typedef std::pair<KeyT, ValueT> BucketT;
496protected:
497  const BucketT *Ptr, *End;
498public:
499  DenseMapIterator(void) : Ptr(0), End(0) {}
500
501  DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
502    AdvancePastEmptyBuckets();
503  }
504
505  std::pair<KeyT, ValueT> &operator*() const {
506    return *const_cast<BucketT*>(Ptr);
507  }
508  std::pair<KeyT, ValueT> *operator->() const {
509    return const_cast<BucketT*>(Ptr);
510  }
511
512  bool operator==(const DenseMapIterator &RHS) const {
513    return Ptr == RHS.Ptr;
514  }
515  bool operator!=(const DenseMapIterator &RHS) const {
516    return Ptr != RHS.Ptr;
517  }
518
519  inline DenseMapIterator& operator++() {          // Preincrement
520    ++Ptr;
521    AdvancePastEmptyBuckets();
522    return *this;
523  }
524  DenseMapIterator operator++(int) {        // Postincrement
525    DenseMapIterator tmp = *this; ++*this; return tmp;
526  }
527
528private:
529  void AdvancePastEmptyBuckets() {
530    const KeyT Empty = KeyInfoT::getEmptyKey();
531    const KeyT Tombstone = KeyInfoT::getTombstoneKey();
532
533    while (Ptr != End &&
534           (KeyInfoT::isEqual(Ptr->first, Empty) ||
535            KeyInfoT::isEqual(Ptr->first, Tombstone)))
536      ++Ptr;
537  }
538};
539
540template<typename KeyT, typename ValueT, typename KeyInfoT, typename ValueInfoT>
541class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT, KeyInfoT> {
542public:
543  DenseMapConstIterator(void) : DenseMapIterator<KeyT, ValueT, KeyInfoT>() {}
544  DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
545                        const std::pair<KeyT, ValueT> *E)
546    : DenseMapIterator<KeyT, ValueT, KeyInfoT>(Pos, E) {
547  }
548  const std::pair<KeyT, ValueT> &operator*() const {
549    return *this->Ptr;
550  }
551  const std::pair<KeyT, ValueT> *operator->() const {
552    return this->Ptr;
553  }
554};
555
556} // end namespace llvm
557
558#endif
559