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