1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 #ifndef LEMON_BITS_MAP_EXTENDER_H
20 #define LEMON_BITS_MAP_EXTENDER_H
24 #include <lemon/bits/traits.h>
26 #include <lemon/concept_check.h>
27 #include <lemon/concepts/maps.h>
30 //\brief Extenders for iterable maps.
36 // \brief Extender for maps
37 template <typename _Map>
38 class MapExtender : public _Map {
42 typedef MapExtender Map;
45 typedef typename Parent::Graph Graph;
46 typedef typename Parent::Key Item;
48 typedef typename Parent::Key Key;
49 typedef typename Parent::Value Value;
50 typedef typename Parent::Reference Reference;
51 typedef typename Parent::ConstReference ConstReference;
57 friend class ConstMapIt;
61 MapExtender(const Graph& graph)
64 MapExtender(const Graph& graph, const Value& value)
65 : Parent(graph, value) {}
68 MapExtender& operator=(const MapExtender& cmap) {
69 return operator=<MapExtender>(cmap);
72 template <typename CMap>
73 MapExtender& operator=(const CMap& cmap) {
74 Parent::operator=(cmap);
79 class MapIt : public Item {
83 typedef typename Map::Value Value;
87 MapIt(Invalid i) : Parent(i) { }
89 explicit MapIt(Map& _map) : map(_map) {
90 map.notifier()->first(*this);
93 MapIt(const Map& _map, const Item& item)
94 : Parent(item), map(_map) {}
97 map.notifier()->next(*this);
101 typename MapTraits<Map>::ConstReturnValue operator*() const {
105 typename MapTraits<Map>::ReturnValue operator*() {
109 void set(const Value& value) {
110 map.set(*this, value);
118 class ConstMapIt : public Item {
123 typedef typename Map::Value Value;
127 ConstMapIt(Invalid i) : Parent(i) { }
129 explicit ConstMapIt(Map& _map) : map(_map) {
130 map.notifier()->first(*this);
133 ConstMapIt(const Map& _map, const Item& item)
134 : Parent(item), map(_map) {}
136 ConstMapIt& operator++() {
137 map.notifier()->next(*this);
141 typename MapTraits<Map>::ConstReturnValue operator*() const {
149 class ItemIt : public Item {
156 ItemIt(Invalid i) : Parent(i) { }
158 explicit ItemIt(Map& _map) : map(_map) {
159 map.notifier()->first(*this);
162 ItemIt(const Map& _map, const Item& item)
163 : Parent(item), map(_map) {}
165 ItemIt& operator++() {
166 map.notifier()->next(*this);
176 // \ingroup graphbits
178 // \brief Extender for maps which use a subset of the items.
179 template <typename _Graph, typename _Map>
180 class SubMapExtender : public _Map {
184 typedef SubMapExtender Map;
186 typedef _Graph Graph;
188 typedef typename Parent::Key Item;
190 typedef typename Parent::Key Key;
191 typedef typename Parent::Value Value;
192 typedef typename Parent::Reference Reference;
193 typedef typename Parent::ConstReference ConstReference;
199 friend class ConstMapIt;
203 SubMapExtender(const Graph& _graph)
204 : Parent(_graph), graph(_graph) {}
206 SubMapExtender(const Graph& _graph, const Value& _value)
207 : Parent(_graph, _value), graph(_graph) {}
210 SubMapExtender& operator=(const SubMapExtender& cmap) {
211 return operator=<MapExtender>(cmap);
214 template <typename CMap>
215 SubMapExtender& operator=(const CMap& cmap) {
216 checkConcept<concepts::ReadMap<Key, Value>, CMap>();
218 for (graph.first(it); it != INVALID; graph.next(it)) {
219 Parent::set(it, cmap[it]);
225 class MapIt : public Item {
229 typedef typename Map::Value Value;
233 MapIt(Invalid i) : Parent(i) { }
235 explicit MapIt(Map& _map) : map(_map) {
236 map.graph.first(*this);
239 MapIt(const Map& _map, const Item& item)
240 : Parent(item), map(_map) {}
242 MapIt& operator++() {
243 map.graph.next(*this);
247 typename MapTraits<Map>::ConstReturnValue operator*() const {
251 typename MapTraits<Map>::ReturnValue operator*() {
255 void set(const Value& value) {
256 map.set(*this, value);
264 class ConstMapIt : public Item {
269 typedef typename Map::Value Value;
273 ConstMapIt(Invalid i) : Parent(i) { }
275 explicit ConstMapIt(Map& _map) : map(_map) {
276 map.graph.first(*this);
279 ConstMapIt(const Map& _map, const Item& item)
280 : Parent(item), map(_map) {}
282 ConstMapIt& operator++() {
283 map.graph.next(*this);
287 typename MapTraits<Map>::ConstReturnValue operator*() const {
295 class ItemIt : public Item {
302 ItemIt(Invalid i) : Parent(i) { }
304 explicit ItemIt(Map& _map) : map(_map) {
305 map.graph.first(*this);
308 ItemIt(const Map& _map, const Item& item)
309 : Parent(item), map(_map) {}
311 ItemIt& operator++() {
312 map.graph.next(*this);