[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 | * |
---|
[1270] | 5 | * Copyright (C) 2003-2013 |
---|
[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 { |
---|
[664] | 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; |
---|
[627] | 49 | typedef typename Parent::Reference Reference; |
---|
| 50 | typedef typename Parent::ConstReference ConstReference; |
---|
[57] | 51 | |
---|
[765] | 52 | typedef typename Parent::ReferenceMapTag ReferenceMapTag; |
---|
| 53 | |
---|
[57] | 54 | class MapIt; |
---|
| 55 | class ConstMapIt; |
---|
| 56 | |
---|
| 57 | friend class MapIt; |
---|
| 58 | friend class ConstMapIt; |
---|
| 59 | |
---|
| 60 | public: |
---|
| 61 | |
---|
[664] | 62 | MapExtender(const GraphType& graph) |
---|
[57] | 63 | : Parent(graph) {} |
---|
| 64 | |
---|
[664] | 65 | MapExtender(const GraphType& graph, const Value& value) |
---|
[57] | 66 | : Parent(graph, value) {} |
---|
| 67 | |
---|
[263] | 68 | private: |
---|
[57] | 69 | MapExtender& operator=(const MapExtender& cmap) { |
---|
| 70 | return operator=<MapExtender>(cmap); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | template <typename CMap> |
---|
| 74 | MapExtender& operator=(const CMap& cmap) { |
---|
| 75 | Parent::operator=(cmap); |
---|
| 76 | return *this; |
---|
[209] | 77 | } |
---|
[57] | 78 | |
---|
[263] | 79 | public: |
---|
[57] | 80 | class MapIt : public Item { |
---|
[664] | 81 | typedef Item Parent; |
---|
| 82 | |
---|
[57] | 83 | public: |
---|
[209] | 84 | |
---|
[57] | 85 | typedef typename Map::Value Value; |
---|
[209] | 86 | |
---|
[865] | 87 | MapIt() : map(NULL) {} |
---|
[57] | 88 | |
---|
[865] | 89 | MapIt(Invalid i) : Parent(i), map(NULL) {} |
---|
[57] | 90 | |
---|
[865] | 91 | explicit MapIt(Map& _map) : map(&_map) { |
---|
| 92 | map->notifier()->first(*this); |
---|
[57] | 93 | } |
---|
| 94 | |
---|
[209] | 95 | MapIt(const Map& _map, const Item& item) |
---|
[865] | 96 | : Parent(item), map(&_map) {} |
---|
[57] | 97 | |
---|
[209] | 98 | MapIt& operator++() { |
---|
[865] | 99 | map->notifier()->next(*this); |
---|
[209] | 100 | return *this; |
---|
[57] | 101 | } |
---|
[209] | 102 | |
---|
[57] | 103 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[865] | 104 | return (*map)[*this]; |
---|
[57] | 105 | } |
---|
| 106 | |
---|
| 107 | typename MapTraits<Map>::ReturnValue operator*() { |
---|
[865] | 108 | return (*map)[*this]; |
---|
[57] | 109 | } |
---|
[209] | 110 | |
---|
[57] | 111 | void set(const Value& value) { |
---|
[865] | 112 | map->set(*this, value); |
---|
[57] | 113 | } |
---|
[209] | 114 | |
---|
[57] | 115 | protected: |
---|
[865] | 116 | Map* map; |
---|
[209] | 117 | |
---|
[57] | 118 | }; |
---|
| 119 | |
---|
| 120 | class ConstMapIt : public Item { |
---|
[664] | 121 | typedef Item Parent; |
---|
| 122 | |
---|
[57] | 123 | public: |
---|
| 124 | |
---|
| 125 | typedef typename Map::Value Value; |
---|
[209] | 126 | |
---|
[865] | 127 | ConstMapIt() : map(NULL) {} |
---|
[57] | 128 | |
---|
[865] | 129 | ConstMapIt(Invalid i) : Parent(i), map(NULL) {} |
---|
[57] | 130 | |
---|
[865] | 131 | explicit ConstMapIt(Map& _map) : map(&_map) { |
---|
| 132 | map->notifier()->first(*this); |
---|
[57] | 133 | } |
---|
| 134 | |
---|
[209] | 135 | ConstMapIt(const Map& _map, const Item& item) |
---|
| 136 | : Parent(item), map(_map) {} |
---|
[57] | 137 | |
---|
[209] | 138 | ConstMapIt& operator++() { |
---|
[865] | 139 | map->notifier()->next(*this); |
---|
[209] | 140 | return *this; |
---|
[57] | 141 | } |
---|
| 142 | |
---|
| 143 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 144 | return map[*this]; |
---|
[57] | 145 | } |
---|
| 146 | |
---|
| 147 | protected: |
---|
[865] | 148 | const Map* map; |
---|
[57] | 149 | }; |
---|
| 150 | |
---|
| 151 | class ItemIt : public Item { |
---|
[664] | 152 | typedef Item Parent; |
---|
| 153 | |
---|
[57] | 154 | public: |
---|
[865] | 155 | ItemIt() : map(NULL) {} |
---|
[209] | 156 | |
---|
[57] | 157 | |
---|
[865] | 158 | ItemIt(Invalid i) : Parent(i), map(NULL) {} |
---|
[57] | 159 | |
---|
[865] | 160 | explicit ItemIt(Map& _map) : map(&_map) { |
---|
| 161 | map->notifier()->first(*this); |
---|
[57] | 162 | } |
---|
| 163 | |
---|
[209] | 164 | ItemIt(const Map& _map, const Item& item) |
---|
[865] | 165 | : Parent(item), map(&_map) {} |
---|
[57] | 166 | |
---|
[209] | 167 | ItemIt& operator++() { |
---|
[865] | 168 | map->notifier()->next(*this); |
---|
[209] | 169 | return *this; |
---|
[57] | 170 | } |
---|
| 171 | |
---|
| 172 | protected: |
---|
[865] | 173 | const Map* map; |
---|
[209] | 174 | |
---|
[57] | 175 | }; |
---|
| 176 | }; |
---|
| 177 | |
---|
[314] | 178 | // \ingroup graphbits |
---|
| 179 | // |
---|
| 180 | // \brief Extender for maps which use a subset of the items. |
---|
[57] | 181 | template <typename _Graph, typename _Map> |
---|
| 182 | class SubMapExtender : public _Map { |
---|
[664] | 183 | typedef _Map Parent; |
---|
| 184 | typedef _Graph GraphType; |
---|
| 185 | |
---|
[57] | 186 | public: |
---|
| 187 | |
---|
| 188 | typedef SubMapExtender Map; |
---|
| 189 | typedef typename Parent::Key Item; |
---|
| 190 | |
---|
| 191 | typedef typename Parent::Key Key; |
---|
| 192 | typedef typename Parent::Value Value; |
---|
[627] | 193 | typedef typename Parent::Reference Reference; |
---|
| 194 | typedef typename Parent::ConstReference ConstReference; |
---|
[57] | 195 | |
---|
[765] | 196 | typedef typename Parent::ReferenceMapTag ReferenceMapTag; |
---|
| 197 | |
---|
[57] | 198 | class MapIt; |
---|
| 199 | class ConstMapIt; |
---|
| 200 | |
---|
| 201 | friend class MapIt; |
---|
| 202 | friend class ConstMapIt; |
---|
| 203 | |
---|
| 204 | public: |
---|
| 205 | |
---|
[664] | 206 | SubMapExtender(const GraphType& _graph) |
---|
[57] | 207 | : Parent(_graph), graph(_graph) {} |
---|
| 208 | |
---|
[664] | 209 | SubMapExtender(const GraphType& _graph, const Value& _value) |
---|
[57] | 210 | : Parent(_graph, _value), graph(_graph) {} |
---|
| 211 | |
---|
[263] | 212 | private: |
---|
[57] | 213 | SubMapExtender& operator=(const SubMapExtender& cmap) { |
---|
| 214 | return operator=<MapExtender>(cmap); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | template <typename CMap> |
---|
| 218 | SubMapExtender& operator=(const CMap& cmap) { |
---|
| 219 | checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
---|
| 220 | Item it; |
---|
| 221 | for (graph.first(it); it != INVALID; graph.next(it)) { |
---|
| 222 | Parent::set(it, cmap[it]); |
---|
| 223 | } |
---|
| 224 | return *this; |
---|
[209] | 225 | } |
---|
[57] | 226 | |
---|
[263] | 227 | public: |
---|
[57] | 228 | class MapIt : public Item { |
---|
[664] | 229 | typedef Item Parent; |
---|
| 230 | |
---|
[57] | 231 | public: |
---|
| 232 | typedef typename Map::Value Value; |
---|
[209] | 233 | |
---|
[865] | 234 | MapIt() : map(NULL) {} |
---|
[57] | 235 | |
---|
[865] | 236 | MapIt(Invalid i) : Parent(i), map(NULL) { } |
---|
[57] | 237 | |
---|
[865] | 238 | explicit MapIt(Map& _map) : map(&_map) { |
---|
| 239 | map->graph.first(*this); |
---|
[57] | 240 | } |
---|
| 241 | |
---|
[209] | 242 | MapIt(const Map& _map, const Item& item) |
---|
[865] | 243 | : Parent(item), map(&_map) {} |
---|
[57] | 244 | |
---|
[209] | 245 | MapIt& operator++() { |
---|
[865] | 246 | map->graph.next(*this); |
---|
[209] | 247 | return *this; |
---|
[57] | 248 | } |
---|
[209] | 249 | |
---|
[57] | 250 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[865] | 251 | return (*map)[*this]; |
---|
[57] | 252 | } |
---|
| 253 | |
---|
| 254 | typename MapTraits<Map>::ReturnValue operator*() { |
---|
[865] | 255 | return (*map)[*this]; |
---|
[57] | 256 | } |
---|
[209] | 257 | |
---|
[57] | 258 | void set(const Value& value) { |
---|
[865] | 259 | map->set(*this, value); |
---|
[57] | 260 | } |
---|
[209] | 261 | |
---|
[57] | 262 | protected: |
---|
[865] | 263 | Map* map; |
---|
[209] | 264 | |
---|
[57] | 265 | }; |
---|
| 266 | |
---|
| 267 | class ConstMapIt : public Item { |
---|
[664] | 268 | typedef Item Parent; |
---|
| 269 | |
---|
[57] | 270 | public: |
---|
| 271 | |
---|
| 272 | typedef typename Map::Value Value; |
---|
[209] | 273 | |
---|
[865] | 274 | ConstMapIt() : map(NULL) {} |
---|
[57] | 275 | |
---|
[865] | 276 | ConstMapIt(Invalid i) : Parent(i), map(NULL) { } |
---|
[57] | 277 | |
---|
[865] | 278 | explicit ConstMapIt(Map& _map) : map(&_map) { |
---|
| 279 | map->graph.first(*this); |
---|
[57] | 280 | } |
---|
| 281 | |
---|
[209] | 282 | ConstMapIt(const Map& _map, const Item& item) |
---|
[865] | 283 | : Parent(item), map(&_map) {} |
---|
[57] | 284 | |
---|
[209] | 285 | ConstMapIt& operator++() { |
---|
[865] | 286 | map->graph.next(*this); |
---|
[209] | 287 | return *this; |
---|
[57] | 288 | } |
---|
| 289 | |
---|
| 290 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[865] | 291 | return (*map)[*this]; |
---|
[57] | 292 | } |
---|
| 293 | |
---|
| 294 | protected: |
---|
[865] | 295 | const Map* map; |
---|
[57] | 296 | }; |
---|
| 297 | |
---|
| 298 | class ItemIt : public Item { |
---|
[664] | 299 | typedef Item Parent; |
---|
| 300 | |
---|
[57] | 301 | public: |
---|
[865] | 302 | ItemIt() : map(NULL) {} |
---|
[209] | 303 | |
---|
[57] | 304 | |
---|
[865] | 305 | ItemIt(Invalid i) : Parent(i), map(NULL) { } |
---|
[57] | 306 | |
---|
[865] | 307 | explicit ItemIt(Map& _map) : map(&_map) { |
---|
| 308 | map->graph.first(*this); |
---|
[57] | 309 | } |
---|
| 310 | |
---|
[209] | 311 | ItemIt(const Map& _map, const Item& item) |
---|
[865] | 312 | : Parent(item), map(&_map) {} |
---|
[57] | 313 | |
---|
[209] | 314 | ItemIt& operator++() { |
---|
[865] | 315 | map->graph.next(*this); |
---|
[209] | 316 | return *this; |
---|
[57] | 317 | } |
---|
| 318 | |
---|
| 319 | protected: |
---|
[865] | 320 | const Map* map; |
---|
[209] | 321 | |
---|
[57] | 322 | }; |
---|
[209] | 323 | |
---|
[57] | 324 | private: |
---|
| 325 | |
---|
[664] | 326 | const GraphType& graph; |
---|
[209] | 327 | |
---|
[57] | 328 | }; |
---|
| 329 | |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | #endif |
---|