[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 | * |
---|
[463] | 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 { |
---|
| 39 | public: |
---|
| 40 | |
---|
| 41 | typedef _Map Parent; |
---|
| 42 | typedef MapExtender Map; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | typedef typename Parent::Graph Graph; |
---|
| 46 | typedef typename Parent::Key Item; |
---|
| 47 | |
---|
| 48 | typedef typename Parent::Key Key; |
---|
| 49 | typedef typename Parent::Value Value; |
---|
[627] | 50 | typedef typename Parent::Reference Reference; |
---|
| 51 | typedef typename Parent::ConstReference ConstReference; |
---|
[57] | 52 | |
---|
| 53 | class MapIt; |
---|
| 54 | class ConstMapIt; |
---|
| 55 | |
---|
| 56 | friend class MapIt; |
---|
| 57 | friend class ConstMapIt; |
---|
| 58 | |
---|
| 59 | public: |
---|
| 60 | |
---|
[209] | 61 | MapExtender(const Graph& graph) |
---|
[57] | 62 | : Parent(graph) {} |
---|
| 63 | |
---|
[209] | 64 | MapExtender(const Graph& graph, const Value& value) |
---|
[57] | 65 | : Parent(graph, value) {} |
---|
| 66 | |
---|
[263] | 67 | private: |
---|
[57] | 68 | MapExtender& operator=(const MapExtender& cmap) { |
---|
| 69 | return operator=<MapExtender>(cmap); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | template <typename CMap> |
---|
| 73 | MapExtender& operator=(const CMap& cmap) { |
---|
| 74 | Parent::operator=(cmap); |
---|
| 75 | return *this; |
---|
[209] | 76 | } |
---|
[57] | 77 | |
---|
[263] | 78 | public: |
---|
[57] | 79 | class MapIt : public Item { |
---|
| 80 | public: |
---|
[209] | 81 | |
---|
[57] | 82 | typedef Item Parent; |
---|
| 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 { |
---|
| 119 | public: |
---|
| 120 | |
---|
| 121 | typedef Item Parent; |
---|
| 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 { |
---|
| 150 | public: |
---|
[209] | 151 | |
---|
[57] | 152 | typedef Item Parent; |
---|
[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 { |
---|
| 181 | public: |
---|
| 182 | |
---|
| 183 | typedef _Map Parent; |
---|
| 184 | typedef SubMapExtender Map; |
---|
| 185 | |
---|
| 186 | typedef _Graph Graph; |
---|
| 187 | |
---|
| 188 | typedef typename Parent::Key Item; |
---|
| 189 | |
---|
| 190 | typedef typename Parent::Key Key; |
---|
| 191 | typedef typename Parent::Value Value; |
---|
[627] | 192 | typedef typename Parent::Reference Reference; |
---|
| 193 | typedef typename Parent::ConstReference ConstReference; |
---|
[57] | 194 | |
---|
| 195 | class MapIt; |
---|
| 196 | class ConstMapIt; |
---|
| 197 | |
---|
| 198 | friend class MapIt; |
---|
| 199 | friend class ConstMapIt; |
---|
| 200 | |
---|
| 201 | public: |
---|
| 202 | |
---|
[209] | 203 | SubMapExtender(const Graph& _graph) |
---|
[57] | 204 | : Parent(_graph), graph(_graph) {} |
---|
| 205 | |
---|
[209] | 206 | SubMapExtender(const Graph& _graph, const Value& _value) |
---|
[57] | 207 | : Parent(_graph, _value), graph(_graph) {} |
---|
| 208 | |
---|
[263] | 209 | private: |
---|
[57] | 210 | SubMapExtender& operator=(const SubMapExtender& cmap) { |
---|
| 211 | return operator=<MapExtender>(cmap); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | template <typename CMap> |
---|
| 215 | SubMapExtender& operator=(const CMap& cmap) { |
---|
| 216 | checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
---|
| 217 | Item it; |
---|
| 218 | for (graph.first(it); it != INVALID; graph.next(it)) { |
---|
| 219 | Parent::set(it, cmap[it]); |
---|
| 220 | } |
---|
| 221 | return *this; |
---|
[209] | 222 | } |
---|
[57] | 223 | |
---|
[263] | 224 | public: |
---|
[57] | 225 | class MapIt : public Item { |
---|
| 226 | public: |
---|
[209] | 227 | |
---|
[57] | 228 | typedef Item Parent; |
---|
| 229 | typedef typename Map::Value Value; |
---|
[209] | 230 | |
---|
[57] | 231 | MapIt() {} |
---|
| 232 | |
---|
| 233 | MapIt(Invalid i) : Parent(i) { } |
---|
| 234 | |
---|
| 235 | explicit MapIt(Map& _map) : map(_map) { |
---|
| 236 | map.graph.first(*this); |
---|
| 237 | } |
---|
| 238 | |
---|
[209] | 239 | MapIt(const Map& _map, const Item& item) |
---|
| 240 | : Parent(item), map(_map) {} |
---|
[57] | 241 | |
---|
[209] | 242 | MapIt& operator++() { |
---|
| 243 | map.graph.next(*this); |
---|
| 244 | return *this; |
---|
[57] | 245 | } |
---|
[209] | 246 | |
---|
[57] | 247 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 248 | return map[*this]; |
---|
[57] | 249 | } |
---|
| 250 | |
---|
| 251 | typename MapTraits<Map>::ReturnValue operator*() { |
---|
[209] | 252 | return map[*this]; |
---|
[57] | 253 | } |
---|
[209] | 254 | |
---|
[57] | 255 | void set(const Value& value) { |
---|
[209] | 256 | map.set(*this, value); |
---|
[57] | 257 | } |
---|
[209] | 258 | |
---|
[57] | 259 | protected: |
---|
| 260 | Map& map; |
---|
[209] | 261 | |
---|
[57] | 262 | }; |
---|
| 263 | |
---|
| 264 | class ConstMapIt : public Item { |
---|
| 265 | public: |
---|
| 266 | |
---|
| 267 | typedef Item Parent; |
---|
| 268 | |
---|
| 269 | typedef typename Map::Value Value; |
---|
[209] | 270 | |
---|
[57] | 271 | ConstMapIt() {} |
---|
| 272 | |
---|
| 273 | ConstMapIt(Invalid i) : Parent(i) { } |
---|
| 274 | |
---|
| 275 | explicit ConstMapIt(Map& _map) : map(_map) { |
---|
| 276 | map.graph.first(*this); |
---|
| 277 | } |
---|
| 278 | |
---|
[209] | 279 | ConstMapIt(const Map& _map, const Item& item) |
---|
| 280 | : Parent(item), map(_map) {} |
---|
[57] | 281 | |
---|
[209] | 282 | ConstMapIt& operator++() { |
---|
| 283 | map.graph.next(*this); |
---|
| 284 | return *this; |
---|
[57] | 285 | } |
---|
| 286 | |
---|
| 287 | typename MapTraits<Map>::ConstReturnValue operator*() const { |
---|
[209] | 288 | return map[*this]; |
---|
[57] | 289 | } |
---|
| 290 | |
---|
| 291 | protected: |
---|
| 292 | const Map& map; |
---|
| 293 | }; |
---|
| 294 | |
---|
| 295 | class ItemIt : public Item { |
---|
| 296 | public: |
---|
[209] | 297 | |
---|
[57] | 298 | typedef Item Parent; |
---|
[209] | 299 | |
---|
[57] | 300 | ItemIt() {} |
---|
| 301 | |
---|
| 302 | ItemIt(Invalid i) : Parent(i) { } |
---|
| 303 | |
---|
| 304 | explicit ItemIt(Map& _map) : map(_map) { |
---|
| 305 | map.graph.first(*this); |
---|
| 306 | } |
---|
| 307 | |
---|
[209] | 308 | ItemIt(const Map& _map, const Item& item) |
---|
| 309 | : Parent(item), map(_map) {} |
---|
[57] | 310 | |
---|
[209] | 311 | ItemIt& operator++() { |
---|
| 312 | map.graph.next(*this); |
---|
| 313 | return *this; |
---|
[57] | 314 | } |
---|
| 315 | |
---|
| 316 | protected: |
---|
| 317 | const Map& map; |
---|
[209] | 318 | |
---|
[57] | 319 | }; |
---|
[209] | 320 | |
---|
[57] | 321 | private: |
---|
| 322 | |
---|
| 323 | const Graph& graph; |
---|
[209] | 324 | |
---|
[57] | 325 | }; |
---|
| 326 | |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | #endif |
---|