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