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"
31 template<typename KeyT, typename ValueT,
32 typename KeyInfoT = DenseMapInfo<KeyT>,
34 class DenseMapIterator;
36 template<typename KeyT, typename ValueT,
37 typename KeyInfoT = DenseMapInfo<KeyT> >
39 typedef std::pair<KeyT, ValueT> BucketT;
44 unsigned NumTombstones;
46 typedef KeyT key_type;
47 typedef ValueT mapped_type;
48 typedef BucketT value_type;
50 DenseMap(const DenseMap &other) {
55 explicit DenseMap(unsigned NumInitBuckets = 0) {
59 template<typename InputIt>
60 DenseMap(const InputIt &I, const InputIt &E) {
61 init(NextPowerOf2(std::distance(I, E)));
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))
75 memset((void*)Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
77 operator delete(Buckets);
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);
87 inline iterator end() {
88 return iterator(Buckets+NumBuckets, Buckets+NumBuckets, true);
90 inline const_iterator begin() const {
91 return empty() ? end() : const_iterator(Buckets, Buckets+NumBuckets);
93 inline const_iterator end() const {
94 return const_iterator(Buckets+NumBuckets, Buckets+NumBuckets, true);
97 bool empty() const { return NumEntries == 0; }
98 unsigned size() const { return NumEntries; }
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)
107 if (NumEntries == 0 && NumTombstones == 0) return;
109 // If the capacity of the array is huge, and the # elements used is small,
111 if (NumEntries * 4 < NumBuckets && NumBuckets > 64) {
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)) {
126 assert(NumEntries == 0 && "Node count imbalance!");
130 /// count - Return true if the specified key is in the map.
131 bool count(const KeyT &Val) const {
133 return LookupBucketFor(Val, TheBucket);
136 iterator find(const KeyT &Val) {
138 if (LookupBucketFor(Val, TheBucket))
139 return iterator(TheBucket, Buckets+NumBuckets, true);
142 const_iterator find(const KeyT &Val) const {
144 if (LookupBucketFor(Val, TheBucket))
145 return const_iterator(TheBucket, Buckets+NumBuckets, true);
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
154 template<class LookupKeyT>
155 iterator find_as(const LookupKeyT &Val) {
157 if (LookupBucketFor(Val, TheBucket))
158 return iterator(TheBucket, Buckets+NumBuckets, true);
161 template<class LookupKeyT>
162 const_iterator find_as(const LookupKeyT &Val) const {
164 if (LookupBucketFor(Val, TheBucket))
165 return const_iterator(TheBucket, Buckets+NumBuckets, true);
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 {
173 if (LookupBucketFor(Val, TheBucket))
174 return TheBucket->second;
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
181 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
183 if (LookupBucketFor(KV.first, TheBucket))
184 return std::make_pair(iterator(TheBucket, Buckets+NumBuckets, true),
185 false); // Already in map.
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);
192 /// insert - Range insertion of pairs.
193 template<typename InputIt>
194 void insert(InputIt I, InputIt E) {
200 bool erase(const KeyT &Val) {
202 if (!LookupBucketFor(Val, TheBucket))
203 return false; // not in map.
205 TheBucket->second.~ValueT();
206 TheBucket->first = getTombstoneKey();
211 void erase(iterator I) {
212 BucketT *TheBucket = &*I;
213 TheBucket->second.~ValueT();
214 TheBucket->first = getTombstoneKey();
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);
226 value_type& FindAndConstruct(const KeyT &Key) {
228 if (LookupBucketFor(Key, TheBucket))
231 return *InsertIntoBucket(Key, ValueT(), TheBucket);
234 ValueT &operator[](const KeyT &Key) {
235 return FindAndConstruct(Key).second;
238 DenseMap& operator=(const DenseMap& other) {
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;
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; }
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))
268 NumEntries = other.NumEntries;
269 NumTombstones = other.NumTombstones;
273 memset((void*)Buckets, 0x5a, sizeof(BucketT)*NumBuckets);
275 operator delete(Buckets);
278 NumBuckets = other.NumBuckets;
280 if (NumBuckets == 0) {
285 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) * NumBuckets));
287 if (isPodLike<KeyT>::value && isPodLike<ValueT>::value)
288 memcpy(Buckets, other.Buckets, NumBuckets * sizeof(BucketT));
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);
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),
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.
310 if (NumEntries*4 >= NumBuckets*3) {
311 this->grow(NumBuckets * 2);
312 LookupBucketFor(Key, TheBucket);
314 if (NumBuckets-(NumEntries+NumTombstones) < NumBuckets/8) {
315 this->grow(NumBuckets);
316 LookupBucketFor(Key, TheBucket);
319 // If we are writing over a tombstone, remember this.
320 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
323 TheBucket->first = Key;
324 new (&TheBucket->second) ValueT(Value);
328 static unsigned getHashValue(const KeyT &Val) {
329 return KeyInfoT::getHashValue(Val);
331 template<typename LookupKeyT>
332 static unsigned getHashValue(const LookupKeyT &Val) {
333 return KeyInfoT::getHashValue(Val);
335 static const KeyT getEmptyKey() {
336 return KeyInfoT::getEmptyKey();
338 static const KeyT getTombstoneKey() {
339 return KeyInfoT::getTombstoneKey();
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
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;
352 if (NumBuckets == 0) {
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!");
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;
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;
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.
388 // Otherwise, it's a hash collision or a tombstone, continue quadratic
390 BucketNo += ProbeAmt++;
394 void init(unsigned InitBuckets) {
397 NumBuckets = InitBuckets;
399 if (InitBuckets == 0) {
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);
413 void grow(unsigned AtLeast) {
414 unsigned OldNumBuckets = NumBuckets;
415 BucketT *OldBuckets = Buckets;
420 // Double the number of buckets.
421 while (NumBuckets < AtLeast)
424 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
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);
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.
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);
452 memset((void*)OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
454 // Free the old table.
455 operator delete(OldBuckets);
458 void shrink_and_clear() {
459 unsigned OldNumBuckets = NumBuckets;
460 BucketT *OldBuckets = Buckets;
462 // Reduce the number of buckets.
463 NumBuckets = NumEntries > 32 ? 1 << (Log2_32_Ceil(NumEntries) + 1)
466 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT)*NumBuckets));
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);
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)) {
485 memset((void*)OldBuckets, 0x5a, sizeof(BucketT)*OldNumBuckets);
487 // Free the old table.
488 operator delete(OldBuckets);
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);
503 template<typename KeyT, typename ValueT,
504 typename KeyInfoT, bool IsConst>
505 class 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>;
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;
519 DenseMapIterator() : Ptr(0), End(0) {}
521 DenseMapIterator(pointer Pos, pointer E, bool NoAdvance = false)
523 if (!NoAdvance) AdvancePastEmptyBuckets();
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,
531 : Ptr(I.Ptr), End(I.End) {}
533 reference operator*() const {
536 pointer operator->() const {
540 bool operator==(const ConstIterator &RHS) const {
541 return Ptr == RHS.operator->();
543 bool operator!=(const ConstIterator &RHS) const {
544 return Ptr != RHS.operator->();
547 inline DenseMapIterator& operator++() { // Preincrement
549 AdvancePastEmptyBuckets();
552 DenseMapIterator operator++(int) { // Postincrement
553 DenseMapIterator tmp = *this; ++*this; return tmp;
557 void AdvancePastEmptyBuckets() {
558 const KeyT Empty = KeyInfoT::getEmptyKey();
559 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
562 (KeyInfoT::isEqual(Ptr->first, Empty) ||
563 KeyInfoT::isEqual(Ptr->first, Tombstone)))
568 template<typename KeyT, typename ValueT, typename KeyInfoT>
570 capacity_in_bytes(const DenseMap<KeyT, ValueT, KeyInfoT> &X) {
571 return X.getMemorySize();
574 } // end namespace llvm