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