lemon/bits/map_extender.h
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 14 Apr 2009 10:35:38 +0200
changeset 581 aa1804409f29
parent 440 88ed40ad0d4f
child 617 4137ef9aacc6
permissions -rw-r--r--
Exploit that the standard maps are reference maps (#190)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2009
     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 
    29 //\file
    30 //\brief Extenders for iterable maps.
    31 
    32 namespace lemon {
    33 
    34   // \ingroup graphbits
    35   //
    36   // \brief Extender for maps
    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     typedef typename Parent::Reference Reference;
    51     typedef typename Parent::ConstReference ConstReference;
    52 
    53     class MapIt;
    54     class ConstMapIt;
    55 
    56     friend class MapIt;
    57     friend class ConstMapIt;
    58 
    59   public:
    60 
    61     MapExtender(const Graph& graph)
    62       : Parent(graph) {}
    63 
    64     MapExtender(const Graph& graph, const Value& value)
    65       : Parent(graph, value) {}
    66 
    67   private:
    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;
    76     }
    77 
    78   public:
    79     class MapIt : public Item {
    80     public:
    81 
    82       typedef Item Parent;
    83       typedef typename Map::Value Value;
    84 
    85       MapIt() {}
    86 
    87       MapIt(Invalid i) : Parent(i) { }
    88 
    89       explicit MapIt(Map& _map) : map(_map) {
    90         map.notifier()->first(*this);
    91       }
    92 
    93       MapIt(const Map& _map, const Item& item)
    94         : Parent(item), map(_map) {}
    95 
    96       MapIt& operator++() {
    97         map.notifier()->next(*this);
    98         return *this;
    99       }
   100 
   101       typename MapTraits<Map>::ConstReturnValue operator*() const {
   102         return map[*this];
   103       }
   104 
   105       typename MapTraits<Map>::ReturnValue operator*() {
   106         return map[*this];
   107       }
   108 
   109       void set(const Value& value) {
   110         map.set(*this, value);
   111       }
   112 
   113     protected:
   114       Map& map;
   115 
   116     };
   117 
   118     class ConstMapIt : public Item {
   119     public:
   120 
   121       typedef Item Parent;
   122 
   123       typedef typename Map::Value Value;
   124 
   125       ConstMapIt() {}
   126 
   127       ConstMapIt(Invalid i) : Parent(i) { }
   128 
   129       explicit ConstMapIt(Map& _map) : map(_map) {
   130         map.notifier()->first(*this);
   131       }
   132 
   133       ConstMapIt(const Map& _map, const Item& item)
   134         : Parent(item), map(_map) {}
   135 
   136       ConstMapIt& operator++() {
   137         map.notifier()->next(*this);
   138         return *this;
   139       }
   140 
   141       typename MapTraits<Map>::ConstReturnValue operator*() const {
   142         return map[*this];
   143       }
   144 
   145     protected:
   146       const Map& map;
   147     };
   148 
   149     class ItemIt : public Item {
   150     public:
   151 
   152       typedef Item Parent;
   153 
   154       ItemIt() {}
   155 
   156       ItemIt(Invalid i) : Parent(i) { }
   157 
   158       explicit ItemIt(Map& _map) : map(_map) {
   159         map.notifier()->first(*this);
   160       }
   161 
   162       ItemIt(const Map& _map, const Item& item)
   163         : Parent(item), map(_map) {}
   164 
   165       ItemIt& operator++() {
   166         map.notifier()->next(*this);
   167         return *this;
   168       }
   169 
   170     protected:
   171       const Map& map;
   172 
   173     };
   174   };
   175 
   176   // \ingroup graphbits
   177   //
   178   // \brief Extender for maps which use a subset of the items.
   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;
   192     typedef typename Parent::Reference Reference;
   193     typedef typename Parent::ConstReference ConstReference;
   194 
   195     class MapIt;
   196     class ConstMapIt;
   197 
   198     friend class MapIt;
   199     friend class ConstMapIt;
   200 
   201   public:
   202 
   203     SubMapExtender(const Graph& _graph)
   204       : Parent(_graph), graph(_graph) {}
   205 
   206     SubMapExtender(const Graph& _graph, const Value& _value)
   207       : Parent(_graph, _value), graph(_graph) {}
   208 
   209   private:
   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;
   222     }
   223 
   224   public:
   225     class MapIt : public Item {
   226     public:
   227 
   228       typedef Item Parent;
   229       typedef typename Map::Value Value;
   230 
   231       MapIt() {}
   232 
   233       MapIt(Invalid i) : Parent(i) { }
   234 
   235       explicit MapIt(Map& _map) : map(_map) {
   236         map.graph.first(*this);
   237       }
   238 
   239       MapIt(const Map& _map, const Item& item)
   240         : Parent(item), map(_map) {}
   241 
   242       MapIt& operator++() {
   243         map.graph.next(*this);
   244         return *this;
   245       }
   246 
   247       typename MapTraits<Map>::ConstReturnValue operator*() const {
   248         return map[*this];
   249       }
   250 
   251       typename MapTraits<Map>::ReturnValue operator*() {
   252         return map[*this];
   253       }
   254 
   255       void set(const Value& value) {
   256         map.set(*this, value);
   257       }
   258 
   259     protected:
   260       Map& map;
   261 
   262     };
   263 
   264     class ConstMapIt : public Item {
   265     public:
   266 
   267       typedef Item Parent;
   268 
   269       typedef typename Map::Value Value;
   270 
   271       ConstMapIt() {}
   272 
   273       ConstMapIt(Invalid i) : Parent(i) { }
   274 
   275       explicit ConstMapIt(Map& _map) : map(_map) {
   276         map.graph.first(*this);
   277       }
   278 
   279       ConstMapIt(const Map& _map, const Item& item)
   280         : Parent(item), map(_map) {}
   281 
   282       ConstMapIt& operator++() {
   283         map.graph.next(*this);
   284         return *this;
   285       }
   286 
   287       typename MapTraits<Map>::ConstReturnValue operator*() const {
   288         return map[*this];
   289       }
   290 
   291     protected:
   292       const Map& map;
   293     };
   294 
   295     class ItemIt : public Item {
   296     public:
   297 
   298       typedef Item Parent;
   299 
   300       ItemIt() {}
   301 
   302       ItemIt(Invalid i) : Parent(i) { }
   303 
   304       explicit ItemIt(Map& _map) : map(_map) {
   305         map.graph.first(*this);
   306       }
   307 
   308       ItemIt(const Map& _map, const Item& item)
   309         : Parent(item), map(_map) {}
   310 
   311       ItemIt& operator++() {
   312         map.graph.next(*this);
   313         return *this;
   314       }
   315 
   316     protected:
   317       const Map& map;
   318 
   319     };
   320 
   321   private:
   322 
   323     const Graph& graph;
   324 
   325   };
   326 
   327 }
   328 
   329 #endif