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