[822] | 1 | // -*- c++ -*- |
---|
| 2 | #ifndef MAP_ITERATOR_H |
---|
| 3 | #define MAP_ITERATOR_H |
---|
| 4 | |
---|
| 5 | #include <hugo/extended_pair.h> |
---|
| 6 | |
---|
| 7 | namespace hugo { |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | template <typename Map> |
---|
| 11 | class MapIterator; |
---|
| 12 | |
---|
| 13 | template <typename Map> |
---|
| 14 | class MapConstIterator; |
---|
| 15 | |
---|
| 16 | /** Compatible iterator with the stl maps' iterators. |
---|
| 17 | * It iterates on pairs of a key and a value. |
---|
| 18 | */ |
---|
| 19 | template <typename Map> |
---|
| 20 | class MapIterator { |
---|
| 21 | // friend class Map; |
---|
| 22 | friend class MapConstIterator<Map>; |
---|
| 23 | |
---|
| 24 | public: |
---|
| 25 | |
---|
| 26 | /// The key type of the iterator. |
---|
| 27 | typedef typename Map::KeyType KeyType; |
---|
| 28 | /// The iterator to iterate on the keys. |
---|
| 29 | typedef typename Map::KeyIt KeyIt; |
---|
| 30 | |
---|
| 31 | /// The value type of the iterator. |
---|
| 32 | typedef typename Map::ValueType ValueType; |
---|
| 33 | /// The reference type of the iterator. |
---|
| 34 | typedef typename Map::ReferenceType ReferenceType; |
---|
| 35 | /// The pointer type of the iterator. |
---|
| 36 | typedef typename Map::PointerType PointerType; |
---|
| 37 | |
---|
| 38 | /// The const value type of the iterator. |
---|
| 39 | typedef typename Map::ConstValueType ConstValueType; |
---|
| 40 | /// The const reference type of the iterator. |
---|
| 41 | typedef typename Map::ConstReferenceType ConstReferenceType; |
---|
| 42 | /// The pointer type of the iterator. |
---|
| 43 | typedef typename Map::ConstPointerType ConstPointerType; |
---|
| 44 | |
---|
| 45 | public: |
---|
| 46 | |
---|
| 47 | /** Constructor to initalize the the iterators returned |
---|
| 48 | * by the begin() and end(). |
---|
| 49 | */ |
---|
| 50 | MapIterator (Map& pmap, const KeyIt& pit) |
---|
| 51 | : map(&pmap), it(pit) {} |
---|
| 52 | |
---|
| 53 | public: |
---|
| 54 | |
---|
| 55 | /** Default constructor. |
---|
| 56 | */ |
---|
| 57 | MapIterator() {} |
---|
| 58 | |
---|
| 59 | typedef extended_pair<const KeyType&, const KeyType&, |
---|
| 60 | ReferenceType, ReferenceType> PairReferenceType; |
---|
| 61 | |
---|
| 62 | /** Dereference operator for map. |
---|
| 63 | */ |
---|
| 64 | PairReferenceType operator*() { |
---|
| 65 | return PairReferenceType(it, (*map)[it]); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | class PairPointerType { |
---|
| 69 | friend class MapIterator; |
---|
| 70 | private: |
---|
| 71 | PairReferenceType data; |
---|
| 72 | PairPointerType(const KeyType& key, ReferenceType val) |
---|
| 73 | : data(key, val) {} |
---|
| 74 | public: |
---|
| 75 | PairReferenceType* operator->() {return &data;} |
---|
| 76 | }; |
---|
| 77 | |
---|
| 78 | /** Arrow operator for map. |
---|
| 79 | */ |
---|
| 80 | PairPointerType operator->() { |
---|
| 81 | return PairPointerType(it, ((*map)[it])); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | /** The pre increment operator of the map. |
---|
| 85 | */ |
---|
| 86 | MapIterator& operator++() { |
---|
| 87 | ++it; |
---|
| 88 | return *this; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** The post increment operator of the map. |
---|
| 92 | */ |
---|
| 93 | MapIterator operator++(int) { |
---|
| 94 | MapIterator tmp(it); |
---|
| 95 | ++it; |
---|
| 96 | return tmp; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** The equality operator of the map. |
---|
| 100 | */ |
---|
| 101 | bool operator==(const MapIterator& p_it) const { |
---|
| 102 | return p_it.it == it; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** The not-equality operator of the map. |
---|
| 106 | */ |
---|
| 107 | bool operator!=(const MapIterator& p_it) const { |
---|
| 108 | return !(*this == p_it); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** The equality operator of the map. |
---|
| 112 | */ |
---|
| 113 | bool operator==(const MapConstIterator<Map>& p_it) const { |
---|
| 114 | return p_it.it == it; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** The not-equality operator of the map. |
---|
| 118 | */ |
---|
| 119 | bool operator!=(const MapConstIterator<Map>& p_it) const { |
---|
| 120 | return !(*this == p_it); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | private: |
---|
| 124 | Map* map; |
---|
| 125 | KeyIt it; |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | /** Compatible iterator with the stl maps' iterators. |
---|
| 129 | * It iterates on pairs of a key and a value. |
---|
| 130 | */ |
---|
| 131 | template <typename Map> |
---|
| 132 | class MapConstIterator { |
---|
| 133 | // friend class Map; |
---|
| 134 | friend class MapIterator<Map>; |
---|
| 135 | |
---|
| 136 | public: |
---|
| 137 | |
---|
| 138 | /// The key type of the iterator. |
---|
| 139 | typedef typename Map::KeyType KeyType; |
---|
| 140 | /// The iterator to iterate on the keys. |
---|
| 141 | typedef typename Map::KeyIt KeyIt; |
---|
| 142 | |
---|
| 143 | /// The value type of the iterator. |
---|
| 144 | typedef typename Map::ValueType ValueType; |
---|
| 145 | /// The reference type of the iterator. |
---|
| 146 | typedef typename Map::ReferenceType ReferenceType; |
---|
| 147 | /// The pointer type of the iterator. |
---|
| 148 | typedef typename Map::PointerType PointerType; |
---|
| 149 | |
---|
| 150 | /// The const value type of the iterator. |
---|
| 151 | typedef typename Map::ConstValueType ConstValueType; |
---|
| 152 | /// The const reference type of the iterator. |
---|
| 153 | typedef typename Map::ConstReferenceType ConstReferenceType; |
---|
| 154 | /// The pointer type of the iterator. |
---|
| 155 | typedef typename Map::ConstPointerType ConstPointerType; |
---|
| 156 | |
---|
| 157 | public: |
---|
| 158 | |
---|
| 159 | /** Constructor to initalize the the iterators returned |
---|
| 160 | * by the begin() and end(). |
---|
| 161 | */ |
---|
| 162 | |
---|
| 163 | MapConstIterator (const Map& pmap, const KeyIt& pit) |
---|
| 164 | : map(&pmap), it(pit) {} |
---|
| 165 | |
---|
| 166 | public: |
---|
| 167 | |
---|
| 168 | /** Default constructor. |
---|
| 169 | */ |
---|
| 170 | MapConstIterator() {} |
---|
| 171 | |
---|
| 172 | typedef extended_pair<const KeyType&, const KeyType&, |
---|
| 173 | ConstReferenceType, ConstReferenceType> PairReferenceType; |
---|
| 174 | |
---|
| 175 | /** Dereference operator for map. |
---|
| 176 | */ |
---|
| 177 | PairReferenceType operator*() { |
---|
| 178 | return PairReferenceType(it, (*map)[it]); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | class PairPointerType { |
---|
| 182 | friend class MapConstIterator; |
---|
| 183 | private: |
---|
| 184 | PairReferenceType data; |
---|
| 185 | PairPointerType(const KeyType& key, ConstReferenceType val) |
---|
| 186 | : data(key, val) {} |
---|
| 187 | public: |
---|
| 188 | PairReferenceType* operator->() {return &data;} |
---|
| 189 | }; |
---|
| 190 | |
---|
| 191 | /** Arrow operator for map. |
---|
| 192 | */ |
---|
| 193 | PairPointerType operator->() { |
---|
| 194 | return PairPointerType(it, ((*map)[it])); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /** The pre increment operator of the map. |
---|
| 198 | */ |
---|
| 199 | MapConstIterator& operator++() { |
---|
| 200 | ++it; |
---|
| 201 | return *this; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** The post increment operator of the map. |
---|
| 205 | */ |
---|
| 206 | MapConstIterator operator++(int) { |
---|
| 207 | MapConstIterator<Map> tmp(it); |
---|
| 208 | ++it; |
---|
| 209 | return tmp; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** The equality operator of the map. |
---|
| 213 | */ |
---|
| 214 | bool operator==(const MapIterator<Map>& p_it) const { |
---|
| 215 | return p_it.it == it; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /** The not-equality operator of the map. |
---|
| 219 | */ |
---|
| 220 | bool operator!=(const MapIterator<Map>& p_it) const { |
---|
| 221 | return !(*this == p_it); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** The equality operator of the map. |
---|
| 225 | */ |
---|
| 226 | bool operator==(const MapConstIterator& p_it) const { |
---|
| 227 | return p_it.it == it; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /** The not-equality operator of the map. |
---|
| 231 | */ |
---|
| 232 | bool operator!=(const MapConstIterator& p_it) const { |
---|
| 233 | return !(*this == p_it); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | private: |
---|
| 237 | const Map* map; |
---|
| 238 | KeyIt it; |
---|
| 239 | }; |
---|
| 240 | |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | #endif |
---|