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