lemon/bits/map_extender.h
author klao
Fri, 03 Mar 2006 21:49:39 +0000
changeset 1997 b7a70cdb5520
parent 1993 2115143eceea
child 1999 2ff283124dfc
permissions -rw-r--r--
Bugfix: an ugly artefact of the 'id' -> 'label' renaming
     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   template <typename _Map>
    33   class IterableMapExtender : public _Map {
    34   public:
    35 
    36     typedef _Map Parent;
    37     typedef IterableMapExtender Map;
    38 
    39 
    40     typedef typename Parent::Graph Graph;
    41     typedef typename Parent::Key Item;
    42 
    43     typedef typename Parent::Key Key;
    44     typedef typename Parent::Value Value;
    45 
    46     class MapIt;
    47     class ConstMapIt;
    48 
    49     friend class MapIt;
    50     friend class ConstMapIt;
    51 
    52   protected:
    53 
    54     using Parent::getGraph;
    55 
    56   public:
    57 
    58     IterableMapExtender(const Graph& graph) : Parent(graph) {}
    59 
    60     IterableMapExtender(const Graph& graph, const Value& value) 
    61       : Parent(graph, value) {}
    62 
    63 
    64     class MapIt : public ItemSetTraits<Graph, Item>::ItemIt {
    65     public:
    66       
    67       typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent;
    68 
    69       typedef typename Map::Value Value;
    70       
    71       MapIt(Map& _map) : Parent(*_map.getGraph()), map(_map) {}
    72       
    73       typename MapTraits<Map>::ConstReturnValue operator*() const {
    74 	return map[*this];
    75       }
    76 
    77       typename MapTraits<Map>::ReturnValue operator*() {
    78 	return map[*this];
    79       }
    80       
    81       void set(const Value& value) {
    82 	map.set(*this, value);
    83       }
    84       
    85     protected:
    86       Map& map;
    87       
    88     };
    89 
    90     class ConstMapIt : public ItemSetTraits<Graph, Key>::ItemIt {
    91     public:
    92 
    93       typedef typename ItemSetTraits<Graph, Key>::ItemIt Parent;
    94 
    95       typedef typename Map::Value Value;
    96 
    97       ConstMapIt(const Map& _map) : Parent(*_map.getGraph()), map(_map) {}
    98 
    99       typename MapTraits<Map>::ConstReturnValue operator*() const {
   100 	return map[*this];
   101       }
   102     protected:
   103       const Map& map;
   104     };
   105 
   106     class ItemIt : public ItemSetTraits<Graph, Key>::ItemIt {
   107     public:
   108       
   109       typedef typename ItemSetTraits<Graph, Key>::ItemIt Parent;
   110 
   111       ItemIt(Map& _map) : Parent(*_map.getGraph()) {}
   112       
   113     };
   114   };
   115 
   116 }
   117 
   118 #endif