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