lemon/bits/map_extender.h
author klao
Thu, 16 Mar 2006 13:52:12 +0000
changeset 2009 de287863cc98
parent 1996 5dc13b93f8b4
child 2031 080d51024ac5
permissions -rw-r--r--
rethrow fix
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     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 ///\file
    27 ///\brief Extenders for iterable maps.
    28 
    29 namespace lemon {
    30 
    31   /// \ingroup graphbits
    32   /// 
    33   /// \brief Extender for maps
    34   template <typename _Map>
    35   class MapExtender : public _Map {
    36   public:
    37 
    38     typedef _Map Parent;
    39     typedef MapExtender Map;
    40 
    41 
    42     typedef typename Parent::Graph Graph;
    43     typedef typename Parent::Key Item;
    44 
    45     typedef typename Parent::Key Key;
    46     typedef typename Parent::Value Value;
    47 
    48     class MapIt;
    49     class ConstMapIt;
    50 
    51     friend class MapIt;
    52     friend class ConstMapIt;
    53 
    54   public:
    55 
    56     MapExtender(const Graph& graph) 
    57       : Parent(graph) {}
    58 
    59     MapExtender(const Graph& graph, const Value& value) 
    60       : Parent(graph, value) {}
    61 
    62 
    63     class MapIt : public Item {
    64     public:
    65       
    66       typedef Item Parent;
    67       typedef typename Map::Value Value;
    68       
    69       MapIt() {}
    70 
    71       MapIt(Invalid i) : Parent(i) { }
    72 
    73       explicit MapIt(Map& _map) : map(_map) {
    74         map.getNotifier()->first(*this);
    75       }
    76 
    77       MapIt(const Map& _map, const Item& item) 
    78 	: Parent(item), map(_map) {}
    79 
    80       MapIt& operator++() { 
    81 	map.getNotifier()->next(*this);
    82 	return *this; 
    83       }
    84       
    85       typename MapTraits<Map>::ConstReturnValue operator*() const {
    86 	return map[*this];
    87       }
    88 
    89       typename MapTraits<Map>::ReturnValue operator*() {
    90 	return map[*this];
    91       }
    92       
    93       void set(const Value& value) {
    94 	map.set(*this, value);
    95       }
    96       
    97     protected:
    98       Map& map;
    99       
   100     };
   101 
   102     class ConstMapIt : public Item {
   103     public:
   104 
   105       typedef Item Parent;
   106 
   107       typedef typename Map::Value Value;
   108       
   109       ConstMapIt() {}
   110 
   111       ConstMapIt(Invalid i) : Parent(i) { }
   112 
   113       explicit ConstMapIt(Map& _map) : map(_map) {
   114         map.getNotifier()->first(*this);
   115       }
   116 
   117       ConstMapIt(const Map& _map, const Item& item) 
   118 	: Parent(item), map(_map) {}
   119 
   120       ConstMapIt& operator++() { 
   121 	map.getNotifier()->next(*this);
   122 	return *this; 
   123       }
   124 
   125       typename MapTraits<Map>::ConstReturnValue operator*() const {
   126 	return map[*this];
   127       }
   128 
   129     protected:
   130       const Map& map;
   131     };
   132 
   133     class ItemIt : Item {
   134     public:
   135       
   136       typedef Item Parent;
   137       
   138       ItemIt() {}
   139 
   140       ItemIt(Invalid i) : Parent(i) { }
   141 
   142       explicit ItemIt(Map& _map) : map(_map) {
   143         map->getNotifier()->first(*this);
   144       }
   145 
   146       ItemIt(const Map& _map, const Item& item) 
   147 	: Parent(item), map(_map) {}
   148 
   149       ItemIt& operator++() { 
   150 	map.getNotifier()->next(*this);
   151 	return *this; 
   152       }
   153 
   154     protected:
   155       const Map& map;
   156       
   157     };
   158   };
   159 
   160 }
   161 
   162 #endif