llvm.org GIT mirror llvm / 70a76a6
add find/erase, add const iterators, fix bugs in iterators. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33791 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 13 years ago
1 changed file(s) with 70 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
4040
4141 template
4242 class DenseMapIterator;
43 template
44 class DenseMapConstIterator;
4345
4446 template
4547 class DenseMap {
6466 }
6567
6668 typedef DenseMapIterator iterator;
67 typedef DenseMapIterator const_iterator;
68 inline iterator begin() const;
69 inline iterator end() const;
70
69 typedef DenseMapConstIterator const_iterator;
70 inline iterator begin() {
71 return DenseMapIterator(Buckets, Buckets+NumBuckets);
72 }
73 inline iterator end() {
74 return DenseMapIterator(Buckets+NumBuckets,
75 Buckets+NumBuckets);
76 }
77 inline const_iterator begin() const {
78 return DenseMapConstIterator(Buckets, Buckets+NumBuckets);
79 }
80 inline const_iterator end() const {
81 return DenseMapConstIterator(Buckets+NumBuckets,
82 Buckets+NumBuckets);
83 }
84
85 bool empty() const { return NumEntries == 0; }
7186 unsigned size() const { return NumEntries; }
7287
7388 void clear() {
88103 return LookupBucketFor(Val, TheBucket);
89104 }
90105
106 iterator find(const KeyT &Val) const {
107 BucketT *TheBucket;
108 if (LookupBucketFor(Val, TheBucket))
109 return iterator(TheBucket, Buckets+NumBuckets);
110 return end();
111 }
112
113 bool erase(const KeyT &Val) {
114 BucketT *TheBucket;
115 if (!LookupBucketFor(Val, TheBucket))
116 return false; // not in map.
117
118 TheBucket->second.~ValueT();
119 TheBucket->first = getTombstoneKey();
120 --NumEntries;
121 return true;
122 }
123 bool erase(iterator I) {
124 BucketT *TheBucket = &*I;
125 TheBucket->second.~ValueT();
126 TheBucket->first = getTombstoneKey();
127 --NumEntries;
128 return true;
129 }
130
91131 ValueT &operator[](const KeyT &Val) {
92132 BucketT *TheBucket;
93133 if (LookupBucketFor(Val, TheBucket))
105145 }
106146
107147 private:
108 unsigned getHashValue(const KeyT &Val) const {
148 static unsigned getHashValue(const KeyT &Val) {
109149 return DenseMapKeyInfo::getHashValue(Val);
110150 }
111 const KeyT getEmptyKey() const { return DenseMapKeyInfo::getEmptyKey();}
112 const KeyT getTombstoneKey() const {
151 static const KeyT getEmptyKey() {
152 return DenseMapKeyInfo::getEmptyKey();
153 }
154 static const KeyT getTombstoneKey() {
113155 return DenseMapKeyInfo::getTombstoneKey();
114156 }
115157
208250 template
209251 class DenseMapIterator {
210252 typedef std::pair BucketT;
253 protected:
211254 const BucketT *Ptr, *End;
212255 public:
213256 DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
214257 AdvancePastEmptyBuckets();
215258 }
216259
217 const std::pair &operator*() const {
218 return *Ptr;
219 }
220 const std::pair *operator->() const {
221 return Ptr;
260 std::pair &operator*() const {
261 return *const_cast(Ptr);
262 }
263 std::pair *operator->() const {
264 return const_cast(Ptr);
222265 }
223266
224267 bool operator==(const DenseMapIterator &RHS) const {
242285 const KeyT Empty = DenseMapKeyInfo::getEmptyKey();
243286 const KeyT Tombstone = DenseMapKeyInfo::getTombstoneKey();
244287
245 while (Ptr != End && Ptr->first != Empty && Ptr->first != Tombstone)
288 while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
246289 ++Ptr;
247290 }
248291 };
249292
250
251 template
252 inline DenseMapIterator DenseMap::begin() const {
253 return DenseMapIterator(Buckets, Buckets+NumBuckets);
254 }
255 template
256 inline DenseMapIterator DenseMap::end() const {
257 return DenseMapIterator(Buckets+NumBuckets, Buckets+NumBuckets);
258 }
293 template
294 class DenseMapConstIterator : public DenseMapIterator {
295 public:
296 DenseMapConstIterator(const std::pair *Pos,
297 const std::pair *E)
298 : DenseMapIterator(Pos, E) {
299 }
300 const std::pair &operator*() const {
301 return *this->Ptr;
302 }
303 const std::pair *operator->() const {
304 return this->Ptr;
305 }
306 };
259307
260308 } // end namespace llvm
261309