diff --git a/include/llvm/ADT/DenseMap.h b/include/llvm/ADT/DenseMap.h index 50cdefd2c3f..cd76ddca24e 100644 --- a/include/llvm/ADT/DenseMap.h +++ b/include/llvm/ADT/DenseMap.h @@ -41,6 +41,8 @@ struct DenseMapKeyInfo { template class DenseMapIterator; +template +class DenseMapConstIterator; template class DenseMap { @@ -65,10 +67,23 @@ public: } typedef DenseMapIterator iterator; - typedef DenseMapIterator const_iterator; - inline iterator begin() const; - inline iterator end() const; + typedef DenseMapConstIterator const_iterator; + inline iterator begin() { + return DenseMapIterator(Buckets, Buckets+NumBuckets); + } + inline iterator end() { + return DenseMapIterator(Buckets+NumBuckets, + Buckets+NumBuckets); + } + inline const_iterator begin() const { + return DenseMapConstIterator(Buckets, Buckets+NumBuckets); + } + inline const_iterator end() const { + return DenseMapConstIterator(Buckets+NumBuckets, + Buckets+NumBuckets); + } + bool empty() const { return NumEntries == 0; } unsigned size() const { return NumEntries; } void clear() { @@ -89,6 +104,31 @@ public: return LookupBucketFor(Val, TheBucket); } + iterator find(const KeyT &Val) const { + BucketT *TheBucket; + if (LookupBucketFor(Val, TheBucket)) + return iterator(TheBucket, Buckets+NumBuckets); + return end(); + } + + bool erase(const KeyT &Val) { + BucketT *TheBucket; + if (!LookupBucketFor(Val, TheBucket)) + return false; // not in map. + + TheBucket->second.~ValueT(); + TheBucket->first = getTombstoneKey(); + --NumEntries; + return true; + } + bool erase(iterator I) { + BucketT *TheBucket = &*I; + TheBucket->second.~ValueT(); + TheBucket->first = getTombstoneKey(); + --NumEntries; + return true; + } + ValueT &operator[](const KeyT &Val) { BucketT *TheBucket; if (LookupBucketFor(Val, TheBucket)) @@ -106,11 +146,13 @@ public: } private: - unsigned getHashValue(const KeyT &Val) const { + static unsigned getHashValue(const KeyT &Val) { return DenseMapKeyInfo::getHashValue(Val); } - const KeyT getEmptyKey() const { return DenseMapKeyInfo::getEmptyKey();} - const KeyT getTombstoneKey() const { + static const KeyT getEmptyKey() { + return DenseMapKeyInfo::getEmptyKey(); + } + static const KeyT getTombstoneKey() { return DenseMapKeyInfo::getTombstoneKey(); } @@ -209,17 +251,18 @@ private: template class DenseMapIterator { typedef std::pair BucketT; +protected: const BucketT *Ptr, *End; public: DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) { AdvancePastEmptyBuckets(); } - const std::pair &operator*() const { - return *Ptr; + std::pair &operator*() const { + return *const_cast(Ptr); } - const std::pair *operator->() const { - return Ptr; + std::pair *operator->() const { + return const_cast(Ptr); } bool operator==(const DenseMapIterator &RHS) const { @@ -243,20 +286,25 @@ private: const KeyT Empty = DenseMapKeyInfo::getEmptyKey(); const KeyT Tombstone = DenseMapKeyInfo::getTombstoneKey(); - while (Ptr != End && Ptr->first != Empty && Ptr->first != Tombstone) + while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone)) ++Ptr; } }; - template -inline DenseMapIterator DenseMap::begin() const { - return DenseMapIterator(Buckets, Buckets+NumBuckets); -} -template -inline DenseMapIterator DenseMap::end() const { - return DenseMapIterator(Buckets+NumBuckets, Buckets+NumBuckets); -} +class DenseMapConstIterator : public DenseMapIterator { +public: + DenseMapConstIterator(const std::pair *Pos, + const std::pair *E) + : DenseMapIterator(Pos, E) { + } + const std::pair &operator*() const { + return *this->Ptr; + } + const std::pair *operator->() const { + return this->Ptr; + } +}; } // end namespace llvm