1 //===- llvm/ADT/DenseMap.h - Dense probed hash table ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DenseMap class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_DENSEMAP_H
15 #define LLVM_ADT_DENSEMAP_H
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"
29 template<typename KeyT, typename ValueT,
30 typename KeyInfoT = DenseMapInfo<KeyT>,
31 typename ValueInfoT = DenseMapInfo<ValueT>, bool IsConst = false>
32 class DenseMapIterator;
34 template<typename KeyT, typename ValueT,
35 typename KeyInfoT = DenseMapInfo<KeyT>,
36 typename ValueInfoT = DenseMapInfo<ValueT> >
38 typedef std::pair<KeyT, ValueT> BucketT;
43 unsigned NumTombstones;
45 typedef KeyT key_type;
46 typedef ValueT mapped_type;
47 typedef BucketT value_type;
49 DenseMap(const DenseMap& other) {
54 explicit DenseMap(unsigned NumInitBuckets = 64) {
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))
67 memset(Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
69 operator delete(Buckets);
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);
78 inline iterator end() {
79 return iterator(Buckets+NumBuckets, Buckets+NumBuckets);
81 inline const_iterator begin() const {
82 return const_iterator(Buckets, Buckets+NumBuckets);
84 inline const_iterator end() const {
85 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets);
88 bool empty() const { return NumEntries == 0; }
89 unsigned size() const { return NumEntries; }
91 /// Grow the densemap so that it has at least Size buckets. Does not shrink
92 void resize(size_t Size) { grow(Size); }
95 if (NumEntries == 0 && NumTombstones == 0) return;
97 // If the capacity of the array is huge, and the # elements used is small,
99 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
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)) {
114 assert(NumEntries == 0 && "Node count imbalance!");
118 /// count - Return true if the specified key is in the map.
119 bool count(const KeyT &Val) const {
121 return LookupBucketFor(Val, TheBucket);
124 iterator find(const KeyT &Val) {
126 if (LookupBucketFor(Val, TheBucket))
127 return iterator(TheBucket, Buckets+NumBuckets);
130 const_iterator find(const KeyT &Val) const {
132 if (LookupBucketFor(Val, TheBucket))
133 return const_iterator(TheBucket, Buckets+NumBuckets);
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 {
141 if (LookupBucketFor(Val, TheBucket))
142 return TheBucket->second;
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
149 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
151 if (LookupBucketFor(KV.first, TheBucket))
152 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
153 false); // Already in map.
155 // Otherwise, insert the new element.
156 TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
157 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets),
161 /// insert - Range insertion of pairs.
162 template<typename InputIt>
163 void insert(InputIt I, InputIt E) {
169 bool erase(const KeyT &Val) {
171 if (!LookupBucketFor(Val, TheBucket))
172 return false; // not in map.
174 TheBucket->second.~ValueT();
175 TheBucket->first = getTombstoneKey();
180 bool erase(iterator I) {
181 BucketT *TheBucket = &*I;
182 TheBucket->second.~ValueT();
183 TheBucket->first = getTombstoneKey();
189 value_type& FindAndConstruct(const KeyT &Key) {
191 if (LookupBucketFor(Key, TheBucket))
194 return *InsertIntoBucket(Key, ValueT(), TheBucket);
197 ValueT &operator[](const KeyT &Key) {
198 return FindAndConstruct(Key).second;
201 DenseMap& operator=(const DenseMap& other) {
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;
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; }
219 void CopyFrom(const DenseMap& other) {
220 if (NumBuckets != 0 && (!KeyInfoT::isPod() || !ValueInfoT::isPod())) {
221 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
222 for (BucketT *P = Buckets, *E = Buckets+NumBuckets; P != E; ++P) {
223 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
224 !KeyInfoT::isEqual(P->first, TombstoneKey))
230 NumEntries = other.NumEntries;
231 NumTombstones = other.NumTombstones;
235 memset(Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
237 operator delete(Buckets);
239 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) *
242 if (KeyInfoT::isPod() && ValueInfoT::isPod())
243 memcpy(Buckets, other.Buckets, other.NumBuckets * sizeof(BucketT));
245 for (size_t i = 0; i < other.NumBuckets; ++i) {
246 new (&Buckets[i].first) KeyT(other.Buckets[i].first);
247 if (!KeyInfoT::isEqual(Buckets[i].first, getEmptyKey()) &&
248 !KeyInfoT::isEqual(Buckets[i].first, getTombstoneKey()))
249 new (&Buckets[i].second) ValueT(other.Buckets[i].second);
251 NumBuckets = other.NumBuckets;
254 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
255 BucketT *TheBucket) {
256 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
257 // the buckets are empty (meaning that many are filled with tombstones),
260 // The later case is tricky. For example, if we had one empty bucket with
261 // tons of tombstones, failing lookups (e.g. for insertion) would have to
262 // probe almost the entire table until it found the empty bucket. If the
263 // table completely filled with tombstones, no lookup would ever succeed,
264 // causing infinite loops in lookup.
266 if (NumEntries*4 >= NumBuckets*3 ||
267 NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
268 this->grow(NumBuckets * 2);
269 LookupBucketFor(Key, TheBucket);
272 // If we are writing over a tombstone, remember this.
273 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
276 TheBucket->first = Key;
277 new (&TheBucket->second) ValueT(Value);
281 static unsigned getHashValue(const KeyT &Val) {
282 return KeyInfoT::getHashValue(Val);
284 static const KeyT getEmptyKey() {
285 return KeyInfoT::getEmptyKey();
287 static const KeyT getTombstoneKey() {
288 return KeyInfoT::getTombstoneKey();
291 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
292 /// FoundBucket. If the bucket contains the key and a value, this returns
293 /// true, otherwise it returns a bucket with an empty marker or tombstone and
295 bool LookupBucketFor(const KeyT &Val, BucketT *&FoundBucket) const {
296 unsigned BucketNo = getHashValue(Val);
297 unsigned ProbeAmt = 1;
298 BucketT *BucketsPtr = Buckets;
300 // FoundTombstone - Keep track of whether we find a tombstone while probing.
301 BucketT *FoundTombstone = 0;
302 const KeyT EmptyKey = getEmptyKey();
303 const KeyT TombstoneKey = getTombstoneKey();
304 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
305 !KeyInfoT::isEqual(Val, TombstoneKey) &&
306 "Empty/Tombstone value shouldn't be inserted into map!");
309 BucketT *ThisBucket = BucketsPtr + (BucketNo & (NumBuckets-1));
310 // Found Val's bucket? If so, return it.
311 if (KeyInfoT::isEqual(ThisBucket->first, Val)) {
312 FoundBucket = ThisBucket;
316 // If we found an empty bucket, the key doesn't exist in the set.
317 // Insert it and return the default value.
318 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
319 // If we've already seen a tombstone while probing, fill it in instead
320 // of the empty bucket we eventually probed to.
321 if (FoundTombstone) ThisBucket = FoundTombstone;
322 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
326 // If this is a tombstone, remember it. If Val ends up not in the map, we
327 // prefer to return it than something that would require more probing.
328 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
329 FoundTombstone = ThisBucket; // Remember the first tombstone found.
331 // Otherwise, it's a hash collision or a tombstone, continue quadratic
333 BucketNo += ProbeAmt++;
337 void init(unsigned InitBuckets) {
340 NumBuckets = InitBuckets;
341 assert(InitBuckets && (InitBuckets & (InitBuckets-1)) == 0 &&
342 "# initial buckets must be a power of two!");
343 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*InitBuckets));
344 // Initialize all the keys to EmptyKey.
345 const KeyT EmptyKey = getEmptyKey();
346 for (unsigned i = 0; i != InitBuckets; ++i)
347 new (&Buckets[i].first) KeyT(EmptyKey);
350 void grow(unsigned AtLeast) {
351 unsigned OldNumBuckets = NumBuckets;
352 BucketT *OldBuckets = Buckets;
354 // Double the number of buckets.
355 while (NumBuckets <= AtLeast)
358 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
360 // Initialize all the keys to EmptyKey.
361 const KeyT EmptyKey = getEmptyKey();
362 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
363 new (&Buckets[i].first) KeyT(EmptyKey);
365 // Insert all the old elements.
366 const KeyT TombstoneKey = getTombstoneKey();
367 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
368 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
369 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
370 // Insert the key/value into the new table.
372 bool FoundVal = LookupBucketFor(B->first, DestBucket);
373 FoundVal = FoundVal; // silence warning.
374 assert(!FoundVal && "Key already in new map?");
375 DestBucket->first = B->first;
376 new (&DestBucket->second) ValueT(B->second);
385 memset(OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
387 // Free the old table.
388 operator delete(OldBuckets);
391 void shrink_and_clear() {
392 unsigned OldNumBuckets = NumBuckets;
393 BucketT *OldBuckets = Buckets;
395 // Reduce the number of buckets.
396 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
399 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
401 // Initialize all the keys to EmptyKey.
402 const KeyT EmptyKey = getEmptyKey();
403 for (unsigned i = 0, e = NumBuckets; i != e; ++i)
404 new (&Buckets[i].first) KeyT(EmptyKey);
406 // Free the old buckets.
407 const KeyT TombstoneKey = getTombstoneKey();
408 for (BucketT *B = OldBuckets, *E = OldBuckets+OldNumBuckets; B != E; ++B) {
409 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
410 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
418 memset(OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
420 // Free the old table.
421 operator delete(OldBuckets);
427 template<typename KeyT, typename ValueT,
428 typename KeyInfoT, typename ValueInfoT, bool IsConst>
429 class DenseMapIterator {
430 typedef std::pair<KeyT, ValueT> Bucket;
431 typedef DenseMapIterator<KeyT, ValueT,
432 KeyInfoT, ValueInfoT, true> ConstIterator;
433 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, ValueInfoT, true>;
435 typedef ptrdiff_t difference_type;
436 typedef typename conditional<IsConst, const Bucket, Bucket>::type value_type;
437 typedef value_type *pointer;
438 typedef value_type &reference;
439 typedef std::forward_iterator_tag iterator_category;
443 DenseMapIterator() : Ptr(0), End(0) {}
445 DenseMapIterator(pointer Pos, pointer E) : Ptr(Pos), End(E) {
446 AdvancePastEmptyBuckets();
449 // If IsConst is true this is a converting constructor from iterator to
450 // const_iterator and the default copy constructor is used.
451 // Otherwise this is a copy constructor for iterator.
452 DenseMapIterator(const DenseMapIterator<KeyT, ValueT,
453 KeyInfoT, ValueInfoT, false>& I)
454 : Ptr(I.Ptr), End(I.End) {}
456 reference operator*() const {
459 pointer operator->() const {
463 bool operator==(const ConstIterator &RHS) const {
464 return Ptr == RHS.operator->();
466 bool operator!=(const ConstIterator &RHS) const {
467 return Ptr != RHS.operator->();
470 inline DenseMapIterator& operator++() { // Preincrement
472 AdvancePastEmptyBuckets();
475 DenseMapIterator operator++(int) { // Postincrement
476 DenseMapIterator tmp = *this; ++*this; return tmp;
480 void AdvancePastEmptyBuckets() {
481 const KeyT Empty = KeyInfoT::getEmptyKey();
482 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
485 (KeyInfoT::isEqual(Ptr->first, Empty) ||
486 KeyInfoT::isEqual(Ptr->first, Tombstone)))
491 } // end namespace llvm