1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/hugo/map_iterator.h Wed Sep 08 12:06:45 2004 +0000
1.3 @@ -0,0 +1,243 @@
1.4 +// -*- c++ -*-
1.5 +#ifndef MAP_ITERATOR_H
1.6 +#define MAP_ITERATOR_H
1.7 +
1.8 +#include <hugo/extended_pair.h>
1.9 +
1.10 +namespace hugo {
1.11 +
1.12 +
1.13 + template <typename Map>
1.14 + class MapIterator;
1.15 +
1.16 + template <typename Map>
1.17 + class MapConstIterator;
1.18 +
1.19 + /** Compatible iterator with the stl maps' iterators.
1.20 + * It iterates on pairs of a key and a value.
1.21 + */
1.22 + template <typename Map>
1.23 + class MapIterator {
1.24 + // friend class Map;
1.25 + friend class MapConstIterator<Map>;
1.26 +
1.27 + public:
1.28 +
1.29 + /// The key type of the iterator.
1.30 + typedef typename Map::KeyType KeyType;
1.31 + /// The iterator to iterate on the keys.
1.32 + typedef typename Map::KeyIt KeyIt;
1.33 +
1.34 + /// The value type of the iterator.
1.35 + typedef typename Map::ValueType ValueType;
1.36 + /// The reference type of the iterator.
1.37 + typedef typename Map::ReferenceType ReferenceType;
1.38 + /// The pointer type of the iterator.
1.39 + typedef typename Map::PointerType PointerType;
1.40 +
1.41 + /// The const value type of the iterator.
1.42 + typedef typename Map::ConstValueType ConstValueType;
1.43 + /// The const reference type of the iterator.
1.44 + typedef typename Map::ConstReferenceType ConstReferenceType;
1.45 + /// The pointer type of the iterator.
1.46 + typedef typename Map::ConstPointerType ConstPointerType;
1.47 +
1.48 + public:
1.49 +
1.50 + /** Constructor to initalize the the iterators returned
1.51 + * by the begin() and end().
1.52 + */
1.53 + MapIterator (Map& pmap, const KeyIt& pit)
1.54 + : map(&pmap), it(pit) {}
1.55 +
1.56 + public:
1.57 +
1.58 + /** Default constructor.
1.59 + */
1.60 + MapIterator() {}
1.61 +
1.62 + typedef extended_pair<const KeyType&, const KeyType&,
1.63 + ReferenceType, ReferenceType> PairReferenceType;
1.64 +
1.65 + /** Dereference operator for map.
1.66 + */
1.67 + PairReferenceType operator*() {
1.68 + return PairReferenceType(it, (*map)[it]);
1.69 + }
1.70 +
1.71 + class PairPointerType {
1.72 + friend class MapIterator;
1.73 + private:
1.74 + PairReferenceType data;
1.75 + PairPointerType(const KeyType& key, ReferenceType val)
1.76 + : data(key, val) {}
1.77 + public:
1.78 + PairReferenceType* operator->() {return &data;}
1.79 + };
1.80 +
1.81 + /** Arrow operator for map.
1.82 + */
1.83 + PairPointerType operator->() {
1.84 + return PairPointerType(it, ((*map)[it]));
1.85 + }
1.86 +
1.87 + /** The pre increment operator of the map.
1.88 + */
1.89 + MapIterator& operator++() {
1.90 + ++it;
1.91 + return *this;
1.92 + }
1.93 +
1.94 + /** The post increment operator of the map.
1.95 + */
1.96 + MapIterator operator++(int) {
1.97 + MapIterator tmp(it);
1.98 + ++it;
1.99 + return tmp;
1.100 + }
1.101 +
1.102 + /** The equality operator of the map.
1.103 + */
1.104 + bool operator==(const MapIterator& p_it) const {
1.105 + return p_it.it == it;
1.106 + }
1.107 +
1.108 + /** The not-equality operator of the map.
1.109 + */
1.110 + bool operator!=(const MapIterator& p_it) const {
1.111 + return !(*this == p_it);
1.112 + }
1.113 +
1.114 + /** The equality operator of the map.
1.115 + */
1.116 + bool operator==(const MapConstIterator<Map>& p_it) const {
1.117 + return p_it.it == it;
1.118 + }
1.119 +
1.120 + /** The not-equality operator of the map.
1.121 + */
1.122 + bool operator!=(const MapConstIterator<Map>& p_it) const {
1.123 + return !(*this == p_it);
1.124 + }
1.125 +
1.126 + private:
1.127 + Map* map;
1.128 + KeyIt it;
1.129 + };
1.130 +
1.131 + /** Compatible iterator with the stl maps' iterators.
1.132 + * It iterates on pairs of a key and a value.
1.133 + */
1.134 + template <typename Map>
1.135 + class MapConstIterator {
1.136 + // friend class Map;
1.137 + friend class MapIterator<Map>;
1.138 +
1.139 + public:
1.140 +
1.141 + /// The key type of the iterator.
1.142 + typedef typename Map::KeyType KeyType;
1.143 + /// The iterator to iterate on the keys.
1.144 + typedef typename Map::KeyIt KeyIt;
1.145 +
1.146 + /// The value type of the iterator.
1.147 + typedef typename Map::ValueType ValueType;
1.148 + /// The reference type of the iterator.
1.149 + typedef typename Map::ReferenceType ReferenceType;
1.150 + /// The pointer type of the iterator.
1.151 + typedef typename Map::PointerType PointerType;
1.152 +
1.153 + /// The const value type of the iterator.
1.154 + typedef typename Map::ConstValueType ConstValueType;
1.155 + /// The const reference type of the iterator.
1.156 + typedef typename Map::ConstReferenceType ConstReferenceType;
1.157 + /// The pointer type of the iterator.
1.158 + typedef typename Map::ConstPointerType ConstPointerType;
1.159 +
1.160 + public:
1.161 +
1.162 + /** Constructor to initalize the the iterators returned
1.163 + * by the begin() and end().
1.164 + */
1.165 +
1.166 + MapConstIterator (const Map& pmap, const KeyIt& pit)
1.167 + : map(&pmap), it(pit) {}
1.168 +
1.169 + public:
1.170 +
1.171 + /** Default constructor.
1.172 + */
1.173 + MapConstIterator() {}
1.174 +
1.175 + typedef extended_pair<const KeyType&, const KeyType&,
1.176 + ConstReferenceType, ConstReferenceType> PairReferenceType;
1.177 +
1.178 + /** Dereference operator for map.
1.179 + */
1.180 + PairReferenceType operator*() {
1.181 + return PairReferenceType(it, (*map)[it]);
1.182 + }
1.183 +
1.184 + class PairPointerType {
1.185 + friend class MapConstIterator;
1.186 + private:
1.187 + PairReferenceType data;
1.188 + PairPointerType(const KeyType& key, ConstReferenceType val)
1.189 + : data(key, val) {}
1.190 + public:
1.191 + PairReferenceType* operator->() {return &data;}
1.192 + };
1.193 +
1.194 + /** Arrow operator for map.
1.195 + */
1.196 + PairPointerType operator->() {
1.197 + return PairPointerType(it, ((*map)[it]));
1.198 + }
1.199 +
1.200 + /** The pre increment operator of the map.
1.201 + */
1.202 + MapConstIterator& operator++() {
1.203 + ++it;
1.204 + return *this;
1.205 + }
1.206 +
1.207 + /** The post increment operator of the map.
1.208 + */
1.209 + MapConstIterator operator++(int) {
1.210 + MapConstIterator<Map> tmp(it);
1.211 + ++it;
1.212 + return tmp;
1.213 + }
1.214 +
1.215 + /** The equality operator of the map.
1.216 + */
1.217 + bool operator==(const MapIterator<Map>& p_it) const {
1.218 + return p_it.it == it;
1.219 + }
1.220 +
1.221 + /** The not-equality operator of the map.
1.222 + */
1.223 + bool operator!=(const MapIterator<Map>& p_it) const {
1.224 + return !(*this == p_it);
1.225 + }
1.226 +
1.227 + /** The equality operator of the map.
1.228 + */
1.229 + bool operator==(const MapConstIterator& p_it) const {
1.230 + return p_it.it == it;
1.231 + }
1.232 +
1.233 + /** The not-equality operator of the map.
1.234 + */
1.235 + bool operator!=(const MapConstIterator& p_it) const {
1.236 + return !(*this == p_it);
1.237 + }
1.238 +
1.239 + private:
1.240 + const Map* map;
1.241 + KeyIt it;
1.242 + };
1.243 +
1.244 +}
1.245 +
1.246 +#endif