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/Compiler.h"
18 #include "llvm/Support/AlignOf.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Support/PointerLikeTypeTraits.h"
21 #include "llvm/Support/type_traits.h"
22 #include "llvm/ADT/DenseMapInfo.h"
34 template<typename KeyT, typename ValueT,
35 typename KeyInfoT = DenseMapInfo<KeyT>,
37 class DenseMapIterator;
39 template<typename DerivedT,
40 typename KeyT, typename ValueT, typename KeyInfoT>
43 typedef std::pair<KeyT, ValueT> BucketT;
46 typedef KeyT key_type;
47 typedef ValueT mapped_type;
48 typedef BucketT value_type;
50 typedef DenseMapIterator<KeyT, ValueT, KeyInfoT> iterator;
51 typedef DenseMapIterator<KeyT, ValueT,
52 KeyInfoT, true> const_iterator;
53 inline iterator begin() {
54 // When the map is empty, avoid the overhead of AdvancePastEmptyBuckets().
55 return empty() ? end() : iterator(getBuckets(), getBucketsEnd());
57 inline iterator end() {
58 return iterator(getBucketsEnd(), getBucketsEnd(), true);
60 inline const_iterator begin() const {
61 return empty() ? end() : const_iterator(getBuckets(), getBucketsEnd());
63 inline const_iterator end() const {
64 return const_iterator(getBucketsEnd(), getBucketsEnd(), true);
67 bool empty() const { return getNumEntries() == 0; }
68 unsigned size() const { return getNumEntries(); }
70 /// Grow the densemap so that it has at least Size buckets. Does not shrink
71 void resize(size_t Size) {
72 if (Size > getNumBuckets())
77 if (getNumEntries() == 0 && getNumTombstones() == 0) return;
79 // If the capacity of the array is huge, and the # elements used is small,
81 if (getNumEntries() * 4 < getNumBuckets() && getNumBuckets() > 64) {
86 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
87 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
88 if (!KeyInfoT::isEqual(P->first, EmptyKey)) {
89 if (!KeyInfoT::isEqual(P->first, TombstoneKey)) {
91 decrementNumEntries();
96 assert(getNumEntries() == 0 && "Node count imbalance!");
100 /// count - Return true if the specified key is in the map.
101 bool count(const KeyT &Val) const {
102 const BucketT *TheBucket;
103 return LookupBucketFor(Val, TheBucket);
106 iterator find(const KeyT &Val) {
108 if (LookupBucketFor(Val, TheBucket))
109 return iterator(TheBucket, getBucketsEnd(), true);
112 const_iterator find(const KeyT &Val) const {
113 const BucketT *TheBucket;
114 if (LookupBucketFor(Val, TheBucket))
115 return const_iterator(TheBucket, getBucketsEnd(), true);
119 /// Alternate version of find() which allows a different, and possibly
120 /// less expensive, key type.
121 /// The DenseMapInfo is responsible for supplying methods
122 /// getHashValue(LookupKeyT) and isEqual(LookupKeyT, KeyT) for each key
124 template<class LookupKeyT>
125 iterator find_as(const LookupKeyT &Val) {
127 if (LookupBucketFor(Val, TheBucket))
128 return iterator(TheBucket, getBucketsEnd(), true);
131 template<class LookupKeyT>
132 const_iterator find_as(const LookupKeyT &Val) const {
133 const BucketT *TheBucket;
134 if (LookupBucketFor(Val, TheBucket))
135 return const_iterator(TheBucket, getBucketsEnd(), true);
139 /// lookup - Return the entry for the specified key, or a default
140 /// constructed value if no such entry exists.
141 ValueT lookup(const KeyT &Val) const {
142 const BucketT *TheBucket;
143 if (LookupBucketFor(Val, TheBucket))
144 return TheBucket->second;
148 // Inserts key,value pair into the map if the key isn't already in the map.
149 // If the key is already in the map, it returns false and doesn't update the
151 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
153 if (LookupBucketFor(KV.first, TheBucket))
154 return std::make_pair(iterator(TheBucket, getBucketsEnd(), true),
155 false); // Already in map.
157 // Otherwise, insert the new element.
158 TheBucket = InsertIntoBucket(KV.first, KV.second, TheBucket);
159 return std::make_pair(iterator(TheBucket, getBucketsEnd(), true), true);
162 /// insert - Range insertion of pairs.
163 template<typename InputIt>
164 void insert(InputIt I, InputIt E) {
170 bool erase(const KeyT &Val) {
172 if (!LookupBucketFor(Val, TheBucket))
173 return false; // not in map.
175 TheBucket->second.~ValueT();
176 TheBucket->first = getTombstoneKey();
177 decrementNumEntries();
178 incrementNumTombstones();
181 void erase(iterator I) {
182 BucketT *TheBucket = &*I;
183 TheBucket->second.~ValueT();
184 TheBucket->first = getTombstoneKey();
185 decrementNumEntries();
186 incrementNumTombstones();
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 #if LLVM_USE_RVALUE_REFERENCES
202 value_type& FindAndConstruct(KeyT &&Key) {
204 if (LookupBucketFor(Key, TheBucket))
207 return *InsertIntoBucket(Key, ValueT(), TheBucket);
210 ValueT &operator[](KeyT &&Key) {
211 return FindAndConstruct(Key).second;
215 /// isPointerIntoBucketsArray - Return true if the specified pointer points
216 /// somewhere into the DenseMap's array of buckets (i.e. either to a key or
217 /// value in the DenseMap).
218 bool isPointerIntoBucketsArray(const void *Ptr) const {
219 return Ptr >= getBuckets() && Ptr < getBucketsEnd();
222 /// getPointerIntoBucketsArray() - Return an opaque pointer into the buckets
223 /// array. In conjunction with the previous method, this can be used to
224 /// determine whether an insertion caused the DenseMap to reallocate.
225 const void *getPointerIntoBucketsArray() const { return getBuckets(); }
231 if (getNumBuckets() == 0) // Nothing to do.
234 const KeyT EmptyKey = getEmptyKey(), TombstoneKey = getTombstoneKey();
235 for (BucketT *P = getBuckets(), *E = getBucketsEnd(); P != E; ++P) {
236 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
237 !KeyInfoT::isEqual(P->first, TombstoneKey))
243 memset((void*)getBuckets(), 0x5a, sizeof(BucketT)*getNumBuckets());
251 assert((getNumBuckets() & (getNumBuckets()-1)) == 0 &&
252 "# initial buckets must be a power of two!");
253 const KeyT EmptyKey = getEmptyKey();
254 for (BucketT *B = getBuckets(), *E = getBucketsEnd(); B != E; ++B)
255 new (&B->first) KeyT(EmptyKey);
258 void moveFromOldBuckets(BucketT *OldBucketsBegin, BucketT *OldBucketsEnd) {
261 // Insert all the old elements.
262 const KeyT EmptyKey = getEmptyKey();
263 const KeyT TombstoneKey = getTombstoneKey();
264 for (BucketT *B = OldBucketsBegin, *E = OldBucketsEnd; B != E; ++B) {
265 if (!KeyInfoT::isEqual(B->first, EmptyKey) &&
266 !KeyInfoT::isEqual(B->first, TombstoneKey)) {
267 // Insert the key/value into the new table.
269 bool FoundVal = LookupBucketFor(B->first, DestBucket);
270 (void)FoundVal; // silence warning.
271 assert(!FoundVal && "Key already in new map?");
272 DestBucket->first = llvm_move(B->first);
273 new (&DestBucket->second) ValueT(llvm_move(B->second));
274 incrementNumEntries();
283 if (OldBucketsBegin != OldBucketsEnd)
284 memset((void*)OldBucketsBegin, 0x5a,
285 sizeof(BucketT) * (OldBucketsEnd - OldBucketsBegin));
289 template <typename OtherBaseT>
290 void copyFrom(const DenseMapBase<OtherBaseT, KeyT, ValueT, KeyInfoT>& other) {
291 assert(getNumBuckets() == other.getNumBuckets());
293 setNumEntries(other.getNumEntries());
294 setNumTombstones(other.getNumTombstones());
296 if (isPodLike<KeyT>::value && isPodLike<ValueT>::value)
297 memcpy(getBuckets(), other.getBuckets(),
298 getNumBuckets() * sizeof(BucketT));
300 for (size_t i = 0; i < getNumBuckets(); ++i) {
301 new (&getBuckets()[i].first) KeyT(other.getBuckets()[i].first);
302 if (!KeyInfoT::isEqual(getBuckets()[i].first, getEmptyKey()) &&
303 !KeyInfoT::isEqual(getBuckets()[i].first, getTombstoneKey()))
304 new (&getBuckets()[i].second) ValueT(other.getBuckets()[i].second);
308 void swap(DenseMapBase& RHS) {
309 std::swap(getNumEntries(), RHS.getNumEntries());
310 std::swap(getNumTombstones(), RHS.getNumTombstones());
313 static unsigned getHashValue(const KeyT &Val) {
314 return KeyInfoT::getHashValue(Val);
316 template<typename LookupKeyT>
317 static unsigned getHashValue(const LookupKeyT &Val) {
318 return KeyInfoT::getHashValue(Val);
320 static const KeyT getEmptyKey() {
321 return KeyInfoT::getEmptyKey();
323 static const KeyT getTombstoneKey() {
324 return KeyInfoT::getTombstoneKey();
328 unsigned getNumEntries() const {
329 return static_cast<const DerivedT *>(this)->getNumEntries();
331 void setNumEntries(unsigned Num) {
332 static_cast<DerivedT *>(this)->setNumEntries(Num);
334 void incrementNumEntries() {
335 setNumEntries(getNumEntries() + 1);
337 void decrementNumEntries() {
338 setNumEntries(getNumEntries() - 1);
340 unsigned getNumTombstones() const {
341 return static_cast<const DerivedT *>(this)->getNumTombstones();
343 void setNumTombstones(unsigned Num) {
344 static_cast<DerivedT *>(this)->setNumTombstones(Num);
346 void incrementNumTombstones() {
347 setNumTombstones(getNumTombstones() + 1);
349 void decrementNumTombstones() {
350 setNumTombstones(getNumTombstones() - 1);
352 const BucketT *getBuckets() const {
353 return static_cast<const DerivedT *>(this)->getBuckets();
355 BucketT *getBuckets() {
356 return static_cast<DerivedT *>(this)->getBuckets();
358 unsigned getNumBuckets() const {
359 return static_cast<const DerivedT *>(this)->getNumBuckets();
361 BucketT *getBucketsEnd() {
362 return getBuckets() + getNumBuckets();
364 const BucketT *getBucketsEnd() const {
365 return getBuckets() + getNumBuckets();
368 void grow(unsigned AtLeast) {
369 static_cast<DerivedT *>(this)->grow(AtLeast);
372 void shrink_and_clear() {
373 static_cast<DerivedT *>(this)->shrink_and_clear();
377 BucketT *InsertIntoBucket(const KeyT &Key, const ValueT &Value,
378 BucketT *TheBucket) {
379 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
381 TheBucket->first = Key;
382 new (&TheBucket->second) ValueT(Value);
386 #if LLVM_USE_RVALUE_REFERENCES
387 BucketT *InsertIntoBucket(const KeyT &Key, ValueT &&Value,
388 BucketT *TheBucket) {
389 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
391 TheBucket->first = Key;
392 new (&TheBucket->second) ValueT(std::move(Value));
396 BucketT *InsertIntoBucket(KeyT &&Key, ValueT &&Value, BucketT *TheBucket) {
397 TheBucket = InsertIntoBucketImpl(Key, TheBucket);
399 TheBucket->first = std::move(Key);
400 new (&TheBucket->second) ValueT(std::move(Value));
405 BucketT *InsertIntoBucketImpl(const KeyT &Key, BucketT *TheBucket) {
406 // If the load of the hash table is more than 3/4, or if fewer than 1/8 of
407 // the buckets are empty (meaning that many are filled with tombstones),
410 // The later case is tricky. For example, if we had one empty bucket with
411 // tons of tombstones, failing lookups (e.g. for insertion) would have to
412 // probe almost the entire table until it found the empty bucket. If the
413 // table completely filled with tombstones, no lookup would ever succeed,
414 // causing infinite loops in lookup.
415 unsigned NewNumEntries = getNumEntries() + 1;
416 unsigned NumBuckets = getNumBuckets();
417 if (NewNumEntries*4 >= NumBuckets*3) {
418 this->grow(NumBuckets * 2);
419 LookupBucketFor(Key, TheBucket);
420 NumBuckets = getNumBuckets();
422 if (NumBuckets-(NewNumEntries+getNumTombstones()) <= NumBuckets/8) {
423 this->grow(NumBuckets * 2);
424 LookupBucketFor(Key, TheBucket);
428 // Only update the state after we've grown our bucket space appropriately
429 // so that when growing buckets we have self-consistent entry count.
430 incrementNumEntries();
432 // If we are writing over a tombstone, remember this.
433 if (!KeyInfoT::isEqual(TheBucket->first, getEmptyKey()))
434 decrementNumTombstones();
439 /// LookupBucketFor - Lookup the appropriate bucket for Val, returning it in
440 /// FoundBucket. If the bucket contains the key and a value, this returns
441 /// true, otherwise it returns a bucket with an empty marker or tombstone and
443 template<typename LookupKeyT>
444 bool LookupBucketFor(const LookupKeyT &Val,
445 const BucketT *&FoundBucket) const {
446 const BucketT *BucketsPtr = getBuckets();
447 const unsigned NumBuckets = getNumBuckets();
449 if (NumBuckets == 0) {
454 // FoundTombstone - Keep track of whether we find a tombstone while probing.
455 const BucketT *FoundTombstone = 0;
456 const KeyT EmptyKey = getEmptyKey();
457 const KeyT TombstoneKey = getTombstoneKey();
458 assert(!KeyInfoT::isEqual(Val, EmptyKey) &&
459 !KeyInfoT::isEqual(Val, TombstoneKey) &&
460 "Empty/Tombstone value shouldn't be inserted into map!");
462 unsigned BucketNo = getHashValue(Val) & (NumBuckets-1);
463 unsigned ProbeAmt = 1;
465 const BucketT *ThisBucket = BucketsPtr + BucketNo;
466 // Found Val's bucket? If so, return it.
467 if (KeyInfoT::isEqual(Val, ThisBucket->first)) {
468 FoundBucket = ThisBucket;
472 // If we found an empty bucket, the key doesn't exist in the set.
473 // Insert it and return the default value.
474 if (KeyInfoT::isEqual(ThisBucket->first, EmptyKey)) {
475 // If we've already seen a tombstone while probing, fill it in instead
476 // of the empty bucket we eventually probed to.
477 if (FoundTombstone) ThisBucket = FoundTombstone;
478 FoundBucket = FoundTombstone ? FoundTombstone : ThisBucket;
482 // If this is a tombstone, remember it. If Val ends up not in the map, we
483 // prefer to return it than something that would require more probing.
484 if (KeyInfoT::isEqual(ThisBucket->first, TombstoneKey) && !FoundTombstone)
485 FoundTombstone = ThisBucket; // Remember the first tombstone found.
487 // Otherwise, it's a hash collision or a tombstone, continue quadratic
489 BucketNo += ProbeAmt++;
490 BucketNo &= (NumBuckets-1);
494 template <typename LookupKeyT>
495 bool LookupBucketFor(const LookupKeyT &Val, BucketT *&FoundBucket) {
496 const BucketT *ConstFoundBucket;
497 bool Result = const_cast<const DenseMapBase *>(this)
498 ->LookupBucketFor(Val, ConstFoundBucket);
499 FoundBucket = const_cast<BucketT *>(ConstFoundBucket);
504 /// Return the approximate size (in bytes) of the actual map.
505 /// This is just the raw memory used by DenseMap.
506 /// If entries are pointers to objects, the size of the referenced objects
507 /// are not included.
508 size_t getMemorySize() const {
509 return getNumBuckets() * sizeof(BucketT);
513 template<typename KeyT, typename ValueT,
514 typename KeyInfoT = DenseMapInfo<KeyT> >
516 : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT>,
517 KeyT, ValueT, KeyInfoT> {
518 // Lift some types from the dependent base class into this class for
519 // simplicity of referring to them.
520 typedef DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT> BaseT;
521 typedef typename BaseT::BucketT BucketT;
522 friend class DenseMapBase<DenseMap, KeyT, ValueT, KeyInfoT>;
526 unsigned NumTombstones;
530 explicit DenseMap(unsigned NumInitBuckets = 0) {
531 init(NumInitBuckets);
534 DenseMap(const DenseMap &other) {
539 #if LLVM_USE_RVALUE_REFERENCES
540 DenseMap(DenseMap &&other) {
546 template<typename InputIt>
547 DenseMap(const InputIt &I, const InputIt &E) {
548 init(NextPowerOf2(std::distance(I, E)));
554 operator delete(Buckets);
557 void swap(DenseMap& RHS) {
558 std::swap(Buckets, RHS.Buckets);
559 std::swap(NumEntries, RHS.NumEntries);
560 std::swap(NumTombstones, RHS.NumTombstones);
561 std::swap(NumBuckets, RHS.NumBuckets);
564 DenseMap& operator=(const DenseMap& other) {
569 #if LLVM_USE_RVALUE_REFERENCES
570 DenseMap& operator=(DenseMap &&other) {
572 operator delete(Buckets);
579 void copyFrom(const DenseMap& other) {
581 operator delete(Buckets);
582 if (allocateBuckets(other.NumBuckets)) {
583 this->BaseT::copyFrom(other);
590 void init(unsigned InitBuckets) {
591 if (allocateBuckets(InitBuckets)) {
592 this->BaseT::initEmpty();
599 void grow(unsigned AtLeast) {
600 unsigned OldNumBuckets = NumBuckets;
601 BucketT *OldBuckets = Buckets;
603 AtLeast = isPowerOf2_32(AtLeast) ? AtLeast : NextPowerOf2(AtLeast);
604 allocateBuckets(std::max<unsigned>(64, AtLeast));
607 this->BaseT::initEmpty();
611 this->moveFromOldBuckets(OldBuckets, OldBuckets+OldNumBuckets);
613 // Free the old table.
614 operator delete(OldBuckets);
617 void shrink_and_clear() {
618 unsigned OldNumEntries = NumEntries;
621 // Reduce the number of buckets.
622 unsigned NewNumBuckets = 0;
624 NewNumBuckets = std::max(64, 1 << (Log2_32_Ceil(OldNumEntries) + 1));
625 if (NewNumBuckets == NumBuckets) {
626 this->BaseT::initEmpty();
630 operator delete(Buckets);
635 unsigned getNumEntries() const {
638 void setNumEntries(unsigned Num) {
642 unsigned getNumTombstones() const {
643 return NumTombstones;
645 void setNumTombstones(unsigned Num) {
649 BucketT *getBuckets() const {
653 unsigned getNumBuckets() const {
657 bool allocateBuckets(unsigned Num) {
659 if (NumBuckets == 0) {
664 Buckets = static_cast<BucketT*>(operator new(sizeof(BucketT) * NumBuckets));
669 template<typename KeyT, typename ValueT,
670 unsigned InlineBuckets = 4,
671 typename KeyInfoT = DenseMapInfo<KeyT> >
673 : public DenseMapBase<SmallDenseMap<KeyT, ValueT, InlineBuckets, KeyInfoT>,
674 KeyT, ValueT, KeyInfoT> {
675 // Lift some types from the dependent base class into this class for
676 // simplicity of referring to them.
677 typedef DenseMapBase<SmallDenseMap, KeyT, ValueT, KeyInfoT> BaseT;
678 typedef typename BaseT::BucketT BucketT;
679 friend class DenseMapBase<SmallDenseMap, KeyT, ValueT, KeyInfoT>;
682 unsigned NumEntries : 31;
683 unsigned NumTombstones;
690 /// A "union" of an inline bucket array and the struct representing
691 /// a large bucket. This union will be discriminated by the 'Small' bit.
692 AlignedCharArrayUnion<BucketT[InlineBuckets], LargeRep> storage;
695 explicit SmallDenseMap(unsigned NumInitBuckets = 0) {
696 init(NumInitBuckets);
699 SmallDenseMap(const SmallDenseMap &other) {
704 #if LLVM_USE_RVALUE_REFERENCES
705 SmallDenseMap(SmallDenseMap &&other) {
711 template<typename InputIt>
712 SmallDenseMap(const InputIt &I, const InputIt &E) {
713 init(NextPowerOf2(std::distance(I, E)));
722 void swap(SmallDenseMap& RHS) {
723 unsigned TmpNumEntries = RHS.NumEntries;
724 RHS.NumEntries = NumEntries;
725 NumEntries = TmpNumEntries;
726 std::swap(NumTombstones, RHS.NumTombstones);
728 const KeyT EmptyKey = this->getEmptyKey();
729 const KeyT TombstoneKey = this->getTombstoneKey();
730 if (Small && RHS.Small) {
731 // If we're swapping inline bucket arrays, we have to cope with some of
732 // the tricky bits of DenseMap's storage system: the buckets are not
733 // fully initialized. Thus we swap every key, but we may have
734 // a one-directional move of the value.
735 for (unsigned i = 0, e = InlineBuckets; i != e; ++i) {
736 BucketT *LHSB = &getInlineBuckets()[i],
737 *RHSB = &RHS.getInlineBuckets()[i];
738 bool hasLHSValue = (!KeyInfoT::isEqual(LHSB->first, EmptyKey) &&
739 !KeyInfoT::isEqual(LHSB->first, TombstoneKey));
740 bool hasRHSValue = (!KeyInfoT::isEqual(RHSB->first, EmptyKey) &&
741 !KeyInfoT::isEqual(RHSB->first, TombstoneKey));
742 if (hasLHSValue && hasRHSValue) {
743 // Swap together if we can...
744 std::swap(*LHSB, *RHSB);
747 // Swap separately and handle any assymetry.
748 std::swap(LHSB->first, RHSB->first);
750 new (&RHSB->second) ValueT(llvm_move(LHSB->second));
751 LHSB->second.~ValueT();
752 } else if (hasRHSValue) {
753 new (&LHSB->second) ValueT(llvm_move(RHSB->second));
754 RHSB->second.~ValueT();
759 if (!Small && !RHS.Small) {
760 std::swap(getLargeRep()->Buckets, RHS.getLargeRep()->Buckets);
761 std::swap(getLargeRep()->NumBuckets, RHS.getLargeRep()->NumBuckets);
765 SmallDenseMap &SmallSide = Small ? *this : RHS;
766 SmallDenseMap &LargeSide = Small ? RHS : *this;
768 // First stash the large side's rep and move the small side across.
769 LargeRep TmpRep = llvm_move(*LargeSide.getLargeRep());
770 LargeSide.getLargeRep()->~LargeRep();
771 LargeSide.Small = true;
772 // This is similar to the standard move-from-old-buckets, but the bucket
773 // count hasn't actually rotated in this case. So we have to carefully
774 // move construct the keys and values into their new locations, but there
775 // is no need to re-hash things.
776 for (unsigned i = 0, e = InlineBuckets; i != e; ++i) {
777 BucketT *NewB = &LargeSide.getInlineBuckets()[i],
778 *OldB = &SmallSide.getInlineBuckets()[i];
779 new (&NewB->first) KeyT(llvm_move(OldB->first));
781 if (!KeyInfoT::isEqual(NewB->first, EmptyKey) &&
782 !KeyInfoT::isEqual(NewB->first, TombstoneKey)) {
783 new (&NewB->second) ValueT(llvm_move(OldB->second));
784 OldB->second.~ValueT();
788 // The hard part of moving the small buckets across is done, just move
789 // the TmpRep into its new home.
790 SmallSide.Small = false;
791 new (SmallSide.getLargeRep()) LargeRep(llvm_move(TmpRep));
794 SmallDenseMap& operator=(const SmallDenseMap& other) {
799 #if LLVM_USE_RVALUE_REFERENCES
800 SmallDenseMap& operator=(SmallDenseMap &&other) {
809 void copyFrom(const SmallDenseMap& other) {
813 if (other.getNumBuckets() > InlineBuckets) {
815 allocateBuckets(other.getNumBuckets());
817 this->BaseT::copyFrom(other);
820 void init(unsigned InitBuckets) {
822 if (InitBuckets > InlineBuckets) {
824 new (getLargeRep()) LargeRep(allocateBuckets(InitBuckets));
826 this->BaseT::initEmpty();
829 void grow(unsigned AtLeast) {
830 if (AtLeast >= InlineBuckets) {
831 AtLeast = isPowerOf2_32(AtLeast) ? AtLeast : NextPowerOf2(AtLeast);
832 AtLeast = std::max<unsigned>(64, AtLeast);
836 if (AtLeast < InlineBuckets)
837 return; // Nothing to do.
839 // First move the inline buckets into a temporary storage.
840 AlignedCharArrayUnion<BucketT[InlineBuckets]> TmpStorage;
841 BucketT *TmpBegin = reinterpret_cast<BucketT *>(TmpStorage.buffer);
842 BucketT *TmpEnd = TmpBegin;
844 // Loop over the buckets, moving non-empty, non-tombstones into the
845 // temporary storage. Have the loop move the TmpEnd forward as it goes.
846 const KeyT EmptyKey = this->getEmptyKey();
847 const KeyT TombstoneKey = this->getTombstoneKey();
848 for (BucketT *P = getBuckets(), *E = P + InlineBuckets; P != E; ++P) {
849 if (!KeyInfoT::isEqual(P->first, EmptyKey) &&
850 !KeyInfoT::isEqual(P->first, TombstoneKey)) {
851 assert(size_t(TmpEnd - TmpBegin) < InlineBuckets &&
852 "Too many inline buckets!");
853 new (&TmpEnd->first) KeyT(llvm_move(P->first));
854 new (&TmpEnd->second) ValueT(llvm_move(P->second));
861 // Now make this map use the large rep, and move all the entries back
864 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
865 this->moveFromOldBuckets(TmpBegin, TmpEnd);
869 LargeRep OldRep = llvm_move(*getLargeRep());
870 getLargeRep()->~LargeRep();
871 if (AtLeast <= InlineBuckets) {
874 new (getLargeRep()) LargeRep(allocateBuckets(AtLeast));
877 this->moveFromOldBuckets(OldRep.Buckets, OldRep.Buckets+OldRep.NumBuckets);
879 // Free the old table.
880 operator delete(OldRep.Buckets);
883 void shrink_and_clear() {
884 unsigned OldSize = this->size();
887 // Reduce the number of buckets.
888 unsigned NewNumBuckets = 0;
890 NewNumBuckets = 1 << (Log2_32_Ceil(OldSize) + 1);
891 if (NewNumBuckets > InlineBuckets && NewNumBuckets < 64u)
894 if ((Small && NewNumBuckets <= InlineBuckets) ||
895 (!Small && NewNumBuckets == getLargeRep()->NumBuckets)) {
896 this->BaseT::initEmpty();
905 unsigned getNumEntries() const {
908 void setNumEntries(unsigned Num) {
909 assert(Num < INT_MAX && "Cannot support more than INT_MAX entries");
913 unsigned getNumTombstones() const {
914 return NumTombstones;
916 void setNumTombstones(unsigned Num) {
920 const BucketT *getInlineBuckets() const {
922 // Note that this cast does not violate aliasing rules as we assert that
923 // the memory's dynamic type is the small, inline bucket buffer, and the
924 // 'storage.buffer' static type is 'char *'.
925 return reinterpret_cast<const BucketT *>(storage.buffer);
927 BucketT *getInlineBuckets() {
928 return const_cast<BucketT *>(
929 const_cast<const SmallDenseMap *>(this)->getInlineBuckets());
931 const LargeRep *getLargeRep() const {
933 // Note, same rule about aliasing as with getInlineBuckets.
934 return reinterpret_cast<const LargeRep *>(storage.buffer);
936 LargeRep *getLargeRep() {
937 return const_cast<LargeRep *>(
938 const_cast<const SmallDenseMap *>(this)->getLargeRep());
941 const BucketT *getBuckets() const {
942 return Small ? getInlineBuckets() : getLargeRep()->Buckets;
944 BucketT *getBuckets() {
945 return const_cast<BucketT *>(
946 const_cast<const SmallDenseMap *>(this)->getBuckets());
948 unsigned getNumBuckets() const {
949 return Small ? InlineBuckets : getLargeRep()->NumBuckets;
952 void deallocateBuckets() {
956 operator delete(getLargeRep()->Buckets);
957 getLargeRep()->~LargeRep();
960 LargeRep allocateBuckets(unsigned Num) {
961 assert(Num > InlineBuckets && "Must allocate more buckets than are inline");
963 static_cast<BucketT*>(operator new(sizeof(BucketT) * Num)), Num
969 template<typename KeyT, typename ValueT,
970 typename KeyInfoT, bool IsConst>
971 class DenseMapIterator {
972 typedef std::pair<KeyT, ValueT> Bucket;
973 typedef DenseMapIterator<KeyT, ValueT,
974 KeyInfoT, true> ConstIterator;
975 friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, true>;
977 typedef ptrdiff_t difference_type;
978 typedef typename conditional<IsConst, const Bucket, Bucket>::type value_type;
979 typedef value_type *pointer;
980 typedef value_type &reference;
981 typedef std::forward_iterator_tag iterator_category;
985 DenseMapIterator() : Ptr(0), End(0) {}
987 DenseMapIterator(pointer Pos, pointer E, bool NoAdvance = false)
989 if (!NoAdvance) AdvancePastEmptyBuckets();
992 // If IsConst is true this is a converting constructor from iterator to
993 // const_iterator and the default copy constructor is used.
994 // Otherwise this is a copy constructor for iterator.
995 DenseMapIterator(const DenseMapIterator<KeyT, ValueT,
997 : Ptr(I.Ptr), End(I.End) {}
999 reference operator*() const {
1002 pointer operator->() const {
1006 bool operator==(const ConstIterator &RHS) const {
1007 return Ptr == RHS.operator->();
1009 bool operator!=(const ConstIterator &RHS) const {
1010 return Ptr != RHS.operator->();
1013 inline DenseMapIterator& operator++() { // Preincrement
1015 AdvancePastEmptyBuckets();
1018 DenseMapIterator operator++(int) { // Postincrement
1019 DenseMapIterator tmp = *this; ++*this; return tmp;
1023 void AdvancePastEmptyBuckets() {
1024 const KeyT Empty = KeyInfoT::getEmptyKey();
1025 const KeyT Tombstone = KeyInfoT::getTombstoneKey();
1027 while (Ptr != End &&
1028 (KeyInfoT::isEqual(Ptr->first, Empty) ||
1029 KeyInfoT::isEqual(Ptr->first, Tombstone)))
1034 template<typename KeyT, typename ValueT, typename KeyInfoT>
1035 static inline size_t
1036 capacity_in_bytes(const DenseMap<KeyT, ValueT, KeyInfoT> &X) {
1037 return X.getMemorySize();
1040 } // end namespace llvm