COIN-OR::LEMON - Graph Library

source: lemon/lemon/bits/map_extender.h @ 765:703ebf476a1d

Last change on this file since 765:703ebf476a1d was 765:703ebf476a1d, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Also check ReferenceMapTag? in concept checks (#312)

File size: 6.8 KB
RevLine 
[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
32namespace 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
[57]87      MapIt() {}
88
89      MapIt(Invalid i) : Parent(i) { }
90
91      explicit MapIt(Map& _map) : map(_map) {
92        map.notifier()->first(*this);
93      }
94
[209]95      MapIt(const Map& _map, const Item& item)
96        : Parent(item), map(_map) {}
[57]97
[209]98      MapIt& operator++() {
99        map.notifier()->next(*this);
100        return *this;
[57]101      }
[209]102
[57]103      typename MapTraits<Map>::ConstReturnValue operator*() const {
[209]104        return map[*this];
[57]105      }
106
107      typename MapTraits<Map>::ReturnValue operator*() {
[209]108        return map[*this];
[57]109      }
[209]110
[57]111      void set(const Value& value) {
[209]112        map.set(*this, value);
[57]113      }
[209]114
[57]115    protected:
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
[57]127      ConstMapIt() {}
128
129      ConstMapIt(Invalid i) : Parent(i) { }
130
131      explicit ConstMapIt(Map& _map) : map(_map) {
132        map.notifier()->first(*this);
133      }
134
[209]135      ConstMapIt(const Map& _map, const Item& item)
136        : Parent(item), map(_map) {}
[57]137
[209]138      ConstMapIt& operator++() {
139        map.notifier()->next(*this);
140        return *this;
[57]141      }
142
143      typename MapTraits<Map>::ConstReturnValue operator*() const {
[209]144        return map[*this];
[57]145      }
146
147    protected:
148      const Map& map;
149    };
150
151    class ItemIt : public Item {
[664]152      typedef Item Parent;
153
[57]154    public:
[209]155
[57]156      ItemIt() {}
157
158      ItemIt(Invalid i) : Parent(i) { }
159
160      explicit ItemIt(Map& _map) : map(_map) {
161        map.notifier()->first(*this);
162      }
163
[209]164      ItemIt(const Map& _map, const Item& item)
165        : Parent(item), map(_map) {}
[57]166
[209]167      ItemIt& operator++() {
168        map.notifier()->next(*this);
169        return *this;
[57]170      }
171
172    protected:
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
[57]234      MapIt() {}
235
236      MapIt(Invalid i) : Parent(i) { }
237
238      explicit MapIt(Map& _map) : map(_map) {
239        map.graph.first(*this);
240      }
241
[209]242      MapIt(const Map& _map, const Item& item)
243        : Parent(item), map(_map) {}
[57]244
[209]245      MapIt& operator++() {
246        map.graph.next(*this);
247        return *this;
[57]248      }
[209]249
[57]250      typename MapTraits<Map>::ConstReturnValue operator*() const {
[209]251        return map[*this];
[57]252      }
253
254      typename MapTraits<Map>::ReturnValue operator*() {
[209]255        return map[*this];
[57]256      }
[209]257
[57]258      void set(const Value& value) {
[209]259        map.set(*this, value);
[57]260      }
[209]261
[57]262    protected:
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
[57]274      ConstMapIt() {}
275
276      ConstMapIt(Invalid i) : Parent(i) { }
277
278      explicit ConstMapIt(Map& _map) : map(_map) {
279        map.graph.first(*this);
280      }
281
[209]282      ConstMapIt(const Map& _map, const Item& item)
283        : Parent(item), map(_map) {}
[57]284
[209]285      ConstMapIt& operator++() {
286        map.graph.next(*this);
287        return *this;
[57]288      }
289
290      typename MapTraits<Map>::ConstReturnValue operator*() const {
[209]291        return map[*this];
[57]292      }
293
294    protected:
295      const Map& map;
296    };
297
298    class ItemIt : public Item {
[664]299      typedef Item Parent;
300
[57]301    public:
[209]302
[57]303      ItemIt() {}
304
305      ItemIt(Invalid i) : Parent(i) { }
306
307      explicit ItemIt(Map& _map) : map(_map) {
308        map.graph.first(*this);
309      }
310
[209]311      ItemIt(const Map& _map, const Item& item)
312        : Parent(item), map(_map) {}
[57]313
[209]314      ItemIt& operator++() {
315        map.graph.next(*this);
316        return *this;
[57]317      }
318
319    protected:
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
Note: See TracBrowser for help on using the repository browser.