- // The keys and values are not stored close to each other, so the iterator
- // operator->() cannot return a pointer to a std::pair like a DenseMap does.
- // Instead it returns a FakePair that contains references to Key and Value.
- // This lets code using this to look the same as if using a regular DenseMap.
- template<bool IsConst>
- struct FakePair {
- typedef typename conditional<IsConst, const ValueT, ValueT>::type VT;
- const KeyT &first;
- VT &second;
- FakePair(const KeyT &K, VT &V) : first(K), second(V) {
- }
- FakePair *operator->() {
- return this;
- }
- };
-
- template<bool IsConst>
- class IteratorTemplate {
- typedef typename MapType::const_iterator WrappedIteratorType;
- WrappedIteratorType WrappedI;
- typedef
- typename conditional<IsConst, const VectorType, VectorType>::type VT;
- VT &VecRef;
- typedef FakePair<IsConst> PairType;
- friend class IteratorTemplate<true>;
-
- public:
- IteratorTemplate(WrappedIteratorType I, VT &V) :
- WrappedI(I), VecRef(V) {
- }
-
- // If IsConst is true this is a converting constructor from iterator to
- // const_iterator and the default copy constructor is used.
- // Otherwise this is a copy constructor for iterator.
- IteratorTemplate(const IteratorTemplate<false>& I) :
- WrappedI(I.WrappedI), VecRef(I.VecRef) {
- }
-
- bool operator!=(const IteratorTemplate &RHS) const {
- return WrappedI != RHS.WrappedI;
- }
-
- IteratorTemplate &operator++() { // Preincrement
- ++WrappedI;
- return *this;
- }
-
- PairType operator->() {
- unsigned Pos = WrappedI->second;
- PairType Ret(WrappedI->first, VecRef[Pos]);
- return Ret;
- }
- };
-
- typedef IteratorTemplate<false> iterator;
- typedef IteratorTemplate<true> const_iterator;