// -*- c++ -*- #ifndef MAP_ITERATOR_H #define MAP_ITERATOR_H #include namespace hugo { template class MapIterator; template class MapConstIterator; /** Compatible iterator with the stl maps' iterators. * It iterates on pairs of a key and a value. */ template class MapIterator { // friend class Map; friend class MapConstIterator; public: /// The key type of the iterator. typedef typename Map::KeyType KeyType; /// The iterator to iterate on the keys. typedef typename Map::KeyIt KeyIt; /// The value type of the iterator. typedef typename Map::ValueType ValueType; /// The reference type of the iterator. typedef typename Map::ReferenceType ReferenceType; /// The pointer type of the iterator. typedef typename Map::PointerType PointerType; /// The const value type of the iterator. typedef typename Map::ConstValueType ConstValueType; /// The const reference type of the iterator. typedef typename Map::ConstReferenceType ConstReferenceType; /// The pointer type of the iterator. typedef typename Map::ConstPointerType ConstPointerType; public: /** Constructor to initalize the the iterators returned * by the begin() and end(). */ MapIterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} public: /** Default constructor. */ MapIterator() {} typedef extended_pair PairReferenceType; /** Dereference operator for map. */ PairReferenceType operator*() { return PairReferenceType(it, (*map)[it]); } class PairPointerType { friend class MapIterator; private: PairReferenceType data; PairPointerType(const KeyType& key, ReferenceType val) : data(key, val) {} public: PairReferenceType* operator->() {return &data;} }; /** Arrow operator for map. */ PairPointerType operator->() { return PairPointerType(it, ((*map)[it])); } /** The pre increment operator of the map. */ MapIterator& operator++() { ++it; return *this; } /** The post increment operator of the map. */ MapIterator operator++(int) { MapIterator tmp(it); ++it; return tmp; } /** The equality operator of the map. */ bool operator==(const MapIterator& p_it) const { return p_it.it == it; } /** The not-equality operator of the map. */ bool operator!=(const MapIterator& p_it) const { return !(*this == p_it); } /** The equality operator of the map. */ bool operator==(const MapConstIterator& p_it) const { return p_it.it == it; } /** The not-equality operator of the map. */ bool operator!=(const MapConstIterator& p_it) const { return !(*this == p_it); } private: Map* map; KeyIt it; }; /** Compatible iterator with the stl maps' iterators. * It iterates on pairs of a key and a value. */ template class MapConstIterator { // friend class Map; friend class MapIterator; public: /// The key type of the iterator. typedef typename Map::KeyType KeyType; /// The iterator to iterate on the keys. typedef typename Map::KeyIt KeyIt; /// The value type of the iterator. typedef typename Map::ValueType ValueType; /// The reference type of the iterator. typedef typename Map::ReferenceType ReferenceType; /// The pointer type of the iterator. typedef typename Map::PointerType PointerType; /// The const value type of the iterator. typedef typename Map::ConstValueType ConstValueType; /// The const reference type of the iterator. typedef typename Map::ConstReferenceType ConstReferenceType; /// The pointer type of the iterator. typedef typename Map::ConstPointerType ConstPointerType; public: /** Constructor to initalize the the iterators returned * by the begin() and end(). */ MapConstIterator (const Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {} public: /** Default constructor. */ MapConstIterator() {} typedef extended_pair PairReferenceType; /** Dereference operator for map. */ PairReferenceType operator*() { return PairReferenceType(it, (*map)[it]); } class PairPointerType { friend class MapConstIterator; private: PairReferenceType data; PairPointerType(const KeyType& key, ConstReferenceType val) : data(key, val) {} public: PairReferenceType* operator->() {return &data;} }; /** Arrow operator for map. */ PairPointerType operator->() { return PairPointerType(it, ((*map)[it])); } /** The pre increment operator of the map. */ MapConstIterator& operator++() { ++it; return *this; } /** The post increment operator of the map. */ MapConstIterator operator++(int) { MapConstIterator tmp(it); ++it; return tmp; } /** The equality operator of the map. */ bool operator==(const MapIterator& p_it) const { return p_it.it == it; } /** The not-equality operator of the map. */ bool operator!=(const MapIterator& p_it) const { return !(*this == p_it); } /** The equality operator of the map. */ bool operator==(const MapConstIterator& p_it) const { return p_it.it == it; } /** The not-equality operator of the map. */ bool operator!=(const MapConstIterator& p_it) const { return !(*this == p_it); } private: const Map* map; KeyIt it; }; } #endif