mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-09-14 15:25:25 +00:00
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
This commit is contained in:
@@ -41,6 +41,8 @@ struct DenseMapKeyInfo<T*> {
|
|||||||
|
|
||||||
template<typename KeyT, typename ValueT>
|
template<typename KeyT, typename ValueT>
|
||||||
class DenseMapIterator;
|
class DenseMapIterator;
|
||||||
|
template<typename KeyT, typename ValueT>
|
||||||
|
class DenseMapConstIterator;
|
||||||
|
|
||||||
template<typename KeyT, typename ValueT>
|
template<typename KeyT, typename ValueT>
|
||||||
class DenseMap {
|
class DenseMap {
|
||||||
@@ -65,10 +67,23 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
typedef DenseMapIterator<KeyT, ValueT> iterator;
|
typedef DenseMapIterator<KeyT, ValueT> iterator;
|
||||||
typedef DenseMapIterator<KeyT, ValueT> const_iterator;
|
typedef DenseMapConstIterator<KeyT, ValueT> const_iterator;
|
||||||
inline iterator begin() const;
|
inline iterator begin() {
|
||||||
inline iterator end() const;
|
return DenseMapIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
|
||||||
|
}
|
||||||
|
inline iterator end() {
|
||||||
|
return DenseMapIterator<KeyT, ValueT>(Buckets+NumBuckets,
|
||||||
|
Buckets+NumBuckets);
|
||||||
|
}
|
||||||
|
inline const_iterator begin() const {
|
||||||
|
return DenseMapConstIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
|
||||||
|
}
|
||||||
|
inline const_iterator end() const {
|
||||||
|
return DenseMapConstIterator<KeyT, ValueT>(Buckets+NumBuckets,
|
||||||
|
Buckets+NumBuckets);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty() const { return NumEntries == 0; }
|
||||||
unsigned size() const { return NumEntries; }
|
unsigned size() const { return NumEntries; }
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
@@ -89,6 +104,31 @@ public:
|
|||||||
return LookupBucketFor(Val, TheBucket);
|
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) {
|
ValueT &operator[](const KeyT &Val) {
|
||||||
BucketT *TheBucket;
|
BucketT *TheBucket;
|
||||||
if (LookupBucketFor(Val, TheBucket))
|
if (LookupBucketFor(Val, TheBucket))
|
||||||
@@ -106,11 +146,13 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned getHashValue(const KeyT &Val) const {
|
static unsigned getHashValue(const KeyT &Val) {
|
||||||
return DenseMapKeyInfo<KeyT>::getHashValue(Val);
|
return DenseMapKeyInfo<KeyT>::getHashValue(Val);
|
||||||
}
|
}
|
||||||
const KeyT getEmptyKey() const { return DenseMapKeyInfo<KeyT>::getEmptyKey();}
|
static const KeyT getEmptyKey() {
|
||||||
const KeyT getTombstoneKey() const {
|
return DenseMapKeyInfo<KeyT>::getEmptyKey();
|
||||||
|
}
|
||||||
|
static const KeyT getTombstoneKey() {
|
||||||
return DenseMapKeyInfo<KeyT>::getTombstoneKey();
|
return DenseMapKeyInfo<KeyT>::getTombstoneKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,17 +251,18 @@ private:
|
|||||||
template<typename KeyT, typename ValueT>
|
template<typename KeyT, typename ValueT>
|
||||||
class DenseMapIterator {
|
class DenseMapIterator {
|
||||||
typedef std::pair<KeyT, ValueT> BucketT;
|
typedef std::pair<KeyT, ValueT> BucketT;
|
||||||
|
protected:
|
||||||
const BucketT *Ptr, *End;
|
const BucketT *Ptr, *End;
|
||||||
public:
|
public:
|
||||||
DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
|
DenseMapIterator(const BucketT *Pos, const BucketT *E) : Ptr(Pos), End(E) {
|
||||||
AdvancePastEmptyBuckets();
|
AdvancePastEmptyBuckets();
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::pair<KeyT, ValueT> &operator*() const {
|
std::pair<KeyT, ValueT> &operator*() const {
|
||||||
return *Ptr;
|
return *const_cast<BucketT*>(Ptr);
|
||||||
}
|
}
|
||||||
const std::pair<KeyT, ValueT> *operator->() const {
|
std::pair<KeyT, ValueT> *operator->() const {
|
||||||
return Ptr;
|
return const_cast<BucketT*>(Ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const DenseMapIterator &RHS) const {
|
bool operator==(const DenseMapIterator &RHS) const {
|
||||||
@@ -243,20 +286,25 @@ private:
|
|||||||
const KeyT Empty = DenseMapKeyInfo<KeyT>::getEmptyKey();
|
const KeyT Empty = DenseMapKeyInfo<KeyT>::getEmptyKey();
|
||||||
const KeyT Tombstone = DenseMapKeyInfo<KeyT>::getTombstoneKey();
|
const KeyT Tombstone = DenseMapKeyInfo<KeyT>::getTombstoneKey();
|
||||||
|
|
||||||
while (Ptr != End && Ptr->first != Empty && Ptr->first != Tombstone)
|
while (Ptr != End && (Ptr->first == Empty || Ptr->first == Tombstone))
|
||||||
++Ptr;
|
++Ptr;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<typename KeyT, typename ValueT>
|
template<typename KeyT, typename ValueT>
|
||||||
inline DenseMapIterator<KeyT, ValueT> DenseMap<KeyT, ValueT>::begin() const {
|
class DenseMapConstIterator : public DenseMapIterator<KeyT, ValueT> {
|
||||||
return DenseMapIterator<KeyT, ValueT>(Buckets, Buckets+NumBuckets);
|
public:
|
||||||
}
|
DenseMapConstIterator(const std::pair<KeyT, ValueT> *Pos,
|
||||||
template<typename KeyT, typename ValueT>
|
const std::pair<KeyT, ValueT> *E)
|
||||||
inline DenseMapIterator<KeyT, ValueT> DenseMap<KeyT, ValueT>::end() const {
|
: DenseMapIterator<KeyT, ValueT>(Pos, E) {
|
||||||
return DenseMapIterator<KeyT, ValueT>(Buckets+NumBuckets, Buckets+NumBuckets);
|
}
|
||||||
}
|
const std::pair<KeyT, ValueT> &operator*() const {
|
||||||
|
return *this->Ptr;
|
||||||
|
}
|
||||||
|
const std::pair<KeyT, ValueT> *operator->() const {
|
||||||
|
return this->Ptr;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // end namespace llvm
|
} // end namespace llvm
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user