lemon/bits/map_extender.h
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 20 Aug 2009 20:34:30 +0200
changeset 765 703ebf476a1d
parent 664 4137ef9aacc6
child 867 994c7df296c9
child 912 37f440367057
permissions -rw-r--r--
Also check ReferenceMapTag in concept checks (#312)
     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     typedef _Map Parent;
    40     typedef typename Parent::GraphType GraphType;
    41 
    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;
    49     typedef typename Parent::Reference Reference;
    50     typedef typename Parent::ConstReference ConstReference;
    51 
    52     typedef typename Parent::ReferenceMapTag ReferenceMapTag;
    53 
    54     class MapIt;
    55     class ConstMapIt;
    56 
    57     friend class MapIt;
    58     friend class ConstMapIt;
    59 
    60   public:
    61 
    62     MapExtender(const GraphType& graph)
    63       : Parent(graph) {}
    64 
    65     MapExtender(const GraphType& graph, const Value& value)
    66       : Parent(graph, value) {}
    67 
    68   private:
    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;
    77     }
    78 
    79   public:
    80     class MapIt : public Item {
    81       typedef Item Parent;
    82 
    83     public:
    84 
    85       typedef typename Map::Value Value;
    86 
    87       MapIt() {}
    88 
    89       MapIt(Invalid i) : Parent(i) { }
    90 
    91       explicit MapIt(Map& _map) : map(_map) {
    92         map.notifier()->first(*this);
    93       }
    94 
    95       MapIt(const Map& _map, const Item& item)
    96         : Parent(item), map(_map) {}
    97 
    98       MapIt& operator++() {
    99         map.notifier()->next(*this);
   100         return *this;
   101       }
   102 
   103       typename MapTraits<Map>::ConstReturnValue operator*() const {
   104         return map[*this];
   105       }
   106 
   107       typename MapTraits<Map>::ReturnValue operator*() {
   108         return map[*this];
   109       }
   110 
   111       void set(const Value& value) {
   112         map.set(*this, value);
   113       }
   114 
   115     protected:
   116       Map& map;
   117 
   118     };
   119 
   120     class ConstMapIt : public Item {
   121       typedef Item Parent;
   122 
   123     public:
   124 
   125       typedef typename Map::Value Value;
   126 
   127       ConstMapIt() {}
   128 
   129       ConstMapIt(Invalid i) : Parent(i) { }
   130 
   131       explicit ConstMapIt(Map& _map) : map(_map) {
   132         map.notifier()->first(*this);
   133       }
   134 
   135       ConstMapIt(const Map& _map, const Item& item)
   136         : Parent(item), map(_map) {}
   137 
   138       ConstMapIt& operator++() {
   139         map.notifier()->next(*this);
   140         return *this;
   141       }
   142 
   143       typename MapTraits<Map>::ConstReturnValue operator*() const {
   144         return map[*this];
   145       }
   146 
   147     protected:
   148       const Map& map;
   149     };
   150 
   151     class ItemIt : public Item {
   152       typedef Item Parent;
   153 
   154     public:
   155 
   156       ItemIt() {}
   157 
   158       ItemIt(Invalid i) : Parent(i) { }
   159 
   160       explicit ItemIt(Map& _map) : map(_map) {
   161         map.notifier()->first(*this);
   162       }
   163 
   164       ItemIt(const Map& _map, const Item& item)
   165         : Parent(item), map(_map) {}
   166 
   167       ItemIt& operator++() {
   168         map.notifier()->next(*this);
   169         return *this;
   170       }
   171 
   172     protected:
   173       const Map& map;
   174 
   175     };
   176   };
   177 
   178   // \ingroup graphbits
   179   //
   180   // \brief Extender for maps which use a subset of the items.
   181   template <typename _Graph, typename _Map>
   182   class SubMapExtender : public _Map {
   183     typedef _Map Parent;
   184     typedef _Graph GraphType;
   185 
   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;
   193     typedef typename Parent::Reference Reference;
   194     typedef typename Parent::ConstReference ConstReference;
   195 
   196     typedef typename Parent::ReferenceMapTag ReferenceMapTag;
   197 
   198     class MapIt;
   199     class ConstMapIt;
   200 
   201     friend class MapIt;
   202     friend class ConstMapIt;
   203 
   204   public:
   205 
   206     SubMapExtender(const GraphType& _graph)
   207       : Parent(_graph), graph(_graph) {}
   208 
   209     SubMapExtender(const GraphType& _graph, const Value& _value)
   210       : Parent(_graph, _value), graph(_graph) {}
   211 
   212   private:
   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;
   225     }
   226 
   227   public:
   228     class MapIt : public Item {
   229       typedef Item Parent;
   230 
   231     public:
   232       typedef typename Map::Value Value;
   233 
   234       MapIt() {}
   235 
   236       MapIt(Invalid i) : Parent(i) { }
   237 
   238       explicit MapIt(Map& _map) : map(_map) {
   239         map.graph.first(*this);
   240       }
   241 
   242       MapIt(const Map& _map, const Item& item)
   243         : Parent(item), map(_map) {}
   244 
   245       MapIt& operator++() {
   246         map.graph.next(*this);
   247         return *this;
   248       }
   249 
   250       typename MapTraits<Map>::ConstReturnValue operator*() const {
   251         return map[*this];
   252       }
   253 
   254       typename MapTraits<Map>::ReturnValue operator*() {
   255         return map[*this];
   256       }
   257 
   258       void set(const Value& value) {
   259         map.set(*this, value);
   260       }
   261 
   262     protected:
   263       Map& map;
   264 
   265     };
   266 
   267     class ConstMapIt : public Item {
   268       typedef Item Parent;
   269 
   270     public:
   271 
   272       typedef typename Map::Value Value;
   273 
   274       ConstMapIt() {}
   275 
   276       ConstMapIt(Invalid i) : Parent(i) { }
   277 
   278       explicit ConstMapIt(Map& _map) : map(_map) {
   279         map.graph.first(*this);
   280       }
   281 
   282       ConstMapIt(const Map& _map, const Item& item)
   283         : Parent(item), map(_map) {}
   284 
   285       ConstMapIt& operator++() {
   286         map.graph.next(*this);
   287         return *this;
   288       }
   289 
   290       typename MapTraits<Map>::ConstReturnValue operator*() const {
   291         return map[*this];
   292       }
   293 
   294     protected:
   295       const Map& map;
   296     };
   297 
   298     class ItemIt : public Item {
   299       typedef Item Parent;
   300 
   301     public:
   302 
   303       ItemIt() {}
   304 
   305       ItemIt(Invalid i) : Parent(i) { }
   306 
   307       explicit ItemIt(Map& _map) : map(_map) {
   308         map.graph.first(*this);
   309       }
   310 
   311       ItemIt(const Map& _map, const Item& item)
   312         : Parent(item), map(_map) {}
   313 
   314       ItemIt& operator++() {
   315         map.graph.next(*this);
   316         return *this;
   317       }
   318 
   319     protected:
   320       const Map& map;
   321 
   322     };
   323 
   324   private:
   325 
   326     const GraphType& graph;
   327 
   328   };
   329 
   330 }
   331 
   332 #endif