[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[57] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[57] | 4 | * |
---|
[440] | 5 | * Copyright (C) 2003-2009 |
---|
[57] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_BITS_MAP_EXTENDER_H |
---|
| 20 | #define LEMON_BITS_MAP_EXTENDER_H |
---|
| 21 | |
---|
| 22 | #include <iterator> |
---|
| 23 | |
---|
| 24 | #include <lemon/bits/traits.h> |
---|
| 25 | |
---|
| 26 | #include <lemon/concept_check.h> |
---|
| 27 | #include <lemon/concepts/maps.h> |
---|
| 28 | |
---|
[314] | 29 | //\file |
---|
| 30 | //\brief Extenders for iterable maps. |
---|
[57] | 31 | |
---|
| 32 | namespace lemon { |
---|
| 33 | |
---|
[314] | 34 | // \ingroup graphbits |
---|
| 35 | // |
---|
| 36 | // \brief Extender for maps |
---|
[57] | 37 | template <typename _Map> |
---|
| 38 | class MapExtender : public _Map { |
---|
[617] | 39 | typedef _Map Parent; |
---|
| 40 | typedef typename Parent::GraphType GraphType; |
---|
| 41 | |
---|
[57] | 42 | public: |
---|
| 43 | |
---|
| 44 | typedef MapExtender Map; |
---|
| 45 | typedef typename Parent::Key Item; |
---|
| 46 | |
---|
| 47 | typedef typename Parent::Key Key; |
---|
| 48 | typedef typename Parent::Value Value; |
---|
[580] | 49 | typedef typename Parent::Reference Reference; |
---|
| 50 | typedef typename Parent::ConstReference ConstReference; |
---|
[57] | 51 | |
---|
| 52 | class MapIt; |
---|
| 53 | class ConstMapIt; |
---|
| 54 | |
---|
| 55 | friend class MapIt; |
---|
| 56 | friend class ConstMapIt; |
---|
| 57 | |
---|
| 58 | public: |
---|
| 59 | |
---|
[617] | 60 | MapExtender(const GraphType& graph) |
---|
[57] | 61 | : Parent(graph) {} |
---|
| 62 | |
---|
[617] | 63 | MapExtender(const GraphType& graph, const Value& value) |
---|
[57] | 64 | : Parent(graph, value) {} |
---|
| 65 | |
---|
[263] | 66 | private: |
---|
[57] | 67 | MapExtender& operator=(const MapExtender& cmap) { |
---|
| 68 | return operator=<MapExtender>(cmap); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | template <typename CMap> |
---|
| 72 | MapExtender& operator=(const CMap& cmap) { |
---|
| 73 | Parent::operator=(cmap); |
---|
| 74 | return *this; |
---|
[209] | 75 | } |
---|
[57] | 76 | |
---|
[263] | 77 | public: |
---|
[57] | 78 | class MapIt : public Item { |
---|
[617] | 79 | typedef Item Parent; |
---|
| 80 | |
---|
[57] | 81 | public: |
---|
[209] | 82 | |
---|
[57] | 83 | typedef typename Map::Value Value; |
---|
[209] | 84 | |
---|
[57] | 85 | MapIt() {} |
---|
| 86 | |
---|
| 87 | MapIt(Invalid i) : Parent(i) { } |
---|
| 88 | |
---|
| 89 | explicit MapIt(Map& _map) : map(_map) { |
---|
| 90 | map.notifier()->first(*this); |
---|
| 91 | } |
---|
| 92 | |
---|
[209] | 93 | MapIt(const Map& _map, const Item& item) |
---|
| 94 | : Parent(item), map(_map) {} |
---|
[57] | 95 | |
---|
[209] | 96 | MapIt& operator++() { |
---|
| 97 | map.notifier()->next(*this); |
---|
| 98 | return *this; |
---|
[57] | 99 | } |
---|
[209] | 100 | |
---|
[57] | 101 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 102 | return map[*this]; |
---|
[57] | 103 | } |
---|
| 104 | |
---|
| 105 | typename MapTraits<Map>::ReturnValue operator*() { |
---|
[209] | 106 | return map[*this]; |
---|
[57] | 107 | } |
---|
[209] | 108 | |
---|
[57] | 109 | void set(const Value& value) { |
---|
[209] | 110 | map.set(*this, value); |
---|
[57] | 111 | } |
---|
[209] | 112 | |
---|
[57] | 113 | protected: |
---|
| 114 | Map& map; |
---|
[209] | 115 | |
---|
[57] | 116 | }; |
---|
| 117 | |
---|
| 118 | class ConstMapIt : public Item { |
---|
[617] | 119 | typedef Item Parent; |
---|
| 120 | |
---|
[57] | 121 | public: |
---|
| 122 | |
---|
| 123 | typedef typename Map::Value Value; |
---|
[209] | 124 | |
---|
[57] | 125 | ConstMapIt() {} |
---|
| 126 | |
---|
| 127 | ConstMapIt(Invalid i) : Parent(i) { } |
---|
| 128 | |
---|
| 129 | explicit ConstMapIt(Map& _map) : map(_map) { |
---|
| 130 | map.notifier()->first(*this); |
---|
| 131 | } |
---|
| 132 | |
---|
[209] | 133 | ConstMapIt(const Map& _map, const Item& item) |
---|
| 134 | : Parent(item), map(_map) {} |
---|
[57] | 135 | |
---|
[209] | 136 | ConstMapIt& operator++() { |
---|
| 137 | map.notifier()->next(*this); |
---|
| 138 | return *this; |
---|
[57] | 139 | } |
---|
| 140 | |
---|
| 141 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 142 | return map[*this]; |
---|
[57] | 143 | } |
---|
| 144 | |
---|
| 145 | protected: |
---|
| 146 | const Map& map; |
---|
| 147 | }; |
---|
| 148 | |
---|
| 149 | class ItemIt : public Item { |
---|
[617] | 150 | typedef Item Parent; |
---|
| 151 | |
---|
[57] | 152 | public: |
---|
[209] | 153 | |
---|
[57] | 154 | ItemIt() {} |
---|
| 155 | |
---|
| 156 | ItemIt(Invalid i) : Parent(i) { } |
---|
| 157 | |
---|
| 158 | explicit ItemIt(Map& _map) : map(_map) { |
---|
| 159 | map.notifier()->first(*this); |
---|
| 160 | } |
---|
| 161 | |
---|
[209] | 162 | ItemIt(const Map& _map, const Item& item) |
---|
| 163 | : Parent(item), map(_map) {} |
---|
[57] | 164 | |
---|
[209] | 165 | ItemIt& operator++() { |
---|
| 166 | map.notifier()->next(*this); |
---|
| 167 | return *this; |
---|
[57] | 168 | } |
---|
| 169 | |
---|
| 170 | protected: |
---|
| 171 | const Map& map; |
---|
[209] | 172 | |
---|
[57] | 173 | }; |
---|
| 174 | }; |
---|
| 175 | |
---|
[314] | 176 | // \ingroup graphbits |
---|
| 177 | // |
---|
| 178 | // \brief Extender for maps which use a subset of the items. |
---|
[57] | 179 | template <typename _Graph, typename _Map> |
---|
| 180 | class SubMapExtender : public _Map { |
---|
[617] | 181 | typedef _Map Parent; |
---|
| 182 | typedef _Graph GraphType; |
---|
| 183 | |
---|
[57] | 184 | public: |
---|
| 185 | |
---|
| 186 | typedef SubMapExtender Map; |
---|
| 187 | typedef typename Parent::Key Item; |
---|
| 188 | |
---|
| 189 | typedef typename Parent::Key Key; |
---|
| 190 | typedef typename Parent::Value Value; |
---|
[580] | 191 | typedef typename Parent::Reference Reference; |
---|
| 192 | typedef typename Parent::ConstReference ConstReference; |
---|
[57] | 193 | |
---|
| 194 | class MapIt; |
---|
| 195 | class ConstMapIt; |
---|
| 196 | |
---|
| 197 | friend class MapIt; |
---|
| 198 | friend class ConstMapIt; |
---|
| 199 | |
---|
| 200 | public: |
---|
| 201 | |
---|
[617] | 202 | SubMapExtender(const GraphType& _graph) |
---|
[57] | 203 | : Parent(_graph), graph(_graph) {} |
---|
| 204 | |
---|
[617] | 205 | SubMapExtender(const GraphType& _graph, const Value& _value) |
---|
[57] | 206 | : Parent(_graph, _value), graph(_graph) {} |
---|
| 207 | |
---|
[263] | 208 | private: |
---|
[57] | 209 | SubMapExtender& operator=(const SubMapExtender& cmap) { |
---|
| 210 | return operator=<MapExtender>(cmap); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | template <typename CMap> |
---|
| 214 | SubMapExtender& operator=(const CMap& cmap) { |
---|
| 215 | checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
---|
| 216 | Item it; |
---|
| 217 | for (graph.first(it); it != INVALID; graph.next(it)) { |
---|
| 218 | Parent::set(it, cmap[it]); |
---|
| 219 | } |
---|
| 220 | return *this; |
---|
[209] | 221 | } |
---|
[57] | 222 | |
---|
[263] | 223 | public: |
---|
[57] | 224 | class MapIt : public Item { |
---|
[617] | 225 | typedef Item Parent; |
---|
| 226 | |
---|
[57] | 227 | public: |
---|
| 228 | typedef typename Map::Value Value; |
---|
[209] | 229 | |
---|
[57] | 230 | MapIt() {} |
---|
| 231 | |
---|
| 232 | MapIt(Invalid i) : Parent(i) { } |
---|
| 233 | |
---|
| 234 | explicit MapIt(Map& _map) : map(_map) { |
---|
| 235 | map.graph.first(*this); |
---|
| 236 | } |
---|
| 237 | |
---|
[209] | 238 | MapIt(const Map& _map, const Item& item) |
---|
| 239 | : Parent(item), map(_map) {} |
---|
[57] | 240 | |
---|
[209] | 241 | MapIt& operator++() { |
---|
| 242 | map.graph.next(*this); |
---|
| 243 | return *this; |
---|
[57] | 244 | } |
---|
[209] | 245 | |
---|
[57] | 246 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 247 | return map[*this]; |
---|
[57] | 248 | } |
---|
| 249 | |
---|
| 250 | typename MapTraits<Map>::ReturnValue operator*() { |
---|
[209] | 251 | return map[*this]; |
---|
[57] | 252 | } |
---|
[209] | 253 | |
---|
[57] | 254 | void set(const Value& value) { |
---|
[209] | 255 | map.set(*this, value); |
---|
[57] | 256 | } |
---|
[209] | 257 | |
---|
[57] | 258 | protected: |
---|
| 259 | Map& map; |
---|
[209] | 260 | |
---|
[57] | 261 | }; |
---|
| 262 | |
---|
| 263 | class ConstMapIt : public Item { |
---|
[617] | 264 | typedef Item Parent; |
---|
| 265 | |
---|
[57] | 266 | public: |
---|
| 267 | |
---|
| 268 | typedef typename Map::Value Value; |
---|
[209] | 269 | |
---|
[57] | 270 | ConstMapIt() {} |
---|
| 271 | |
---|
| 272 | ConstMapIt(Invalid i) : Parent(i) { } |
---|
| 273 | |
---|
| 274 | explicit ConstMapIt(Map& _map) : map(_map) { |
---|
| 275 | map.graph.first(*this); |
---|
| 276 | } |
---|
| 277 | |
---|
[209] | 278 | ConstMapIt(const Map& _map, const Item& item) |
---|
| 279 | : Parent(item), map(_map) {} |
---|
[57] | 280 | |
---|
[209] | 281 | ConstMapIt& operator++() { |
---|
| 282 | map.graph.next(*this); |
---|
| 283 | return *this; |
---|
[57] | 284 | } |
---|
| 285 | |
---|
| 286 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 287 | return map[*this]; |
---|
[57] | 288 | } |
---|
| 289 | |
---|
| 290 | protected: |
---|
| 291 | const Map& map; |
---|
| 292 | }; |
---|
| 293 | |
---|
| 294 | class ItemIt : public Item { |
---|
[617] | 295 | typedef Item Parent; |
---|
| 296 | |
---|
[57] | 297 | public: |
---|
[209] | 298 | |
---|
[57] | 299 | ItemIt() {} |
---|
| 300 | |
---|
| 301 | ItemIt(Invalid i) : Parent(i) { } |
---|
| 302 | |
---|
| 303 | explicit ItemIt(Map& _map) : map(_map) { |
---|
| 304 | map.graph.first(*this); |
---|
| 305 | } |
---|
| 306 | |
---|
[209] | 307 | ItemIt(const Map& _map, const Item& item) |
---|
| 308 | : Parent(item), map(_map) {} |
---|
[57] | 309 | |
---|
[209] | 310 | ItemIt& operator++() { |
---|
| 311 | map.graph.next(*this); |
---|
| 312 | return *this; |
---|
[57] | 313 | } |
---|
| 314 | |
---|
| 315 | protected: |
---|
| 316 | const Map& map; |
---|
[209] | 317 | |
---|
[57] | 318 | }; |
---|
[209] | 319 | |
---|
[57] | 320 | private: |
---|
| 321 | |
---|
[617] | 322 | const GraphType& graph; |
---|
[209] | 323 | |
---|
[57] | 324 | }; |
---|
| 325 | |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | #endif |
---|