lemon/bits/default_map.h
author deba
Thu, 08 Sep 2005 14:35:22 +0000
changeset 1676 c3e416514759
parent 1669 66ae78d29f1e
child 1703 eb90e3d6bddc
permissions -rw-r--r--
Bug fix
     1 /* -*- C++ -*-
     2  * lemon/default_map.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_DEFAULT_MAP_H
    18 #define LEMON_DEFAULT_MAP_H
    19 
    20 
    21 #include <lemon/bits/array_map.h>
    22 #include <lemon/bits/vector_map.h>
    23 
    24 ///\ingroup graphmapfactory
    25 ///\file
    26 ///\brief Graph maps that construct and destruct
    27 ///their elements dynamically.
    28 
    29 namespace lemon {
    30 
    31   /// \addtogroup graphmapfactory
    32   /// @{
    33 
    34   template <typename _Graph, typename _Item, typename _Value>
    35   struct DefaultMapSelector {
    36     typedef ArrayMap<_Graph, _Item, _Value> Map;
    37   };
    38 
    39   // bool
    40   template <typename _Graph, typename _Item>
    41   struct DefaultMapSelector<_Graph, _Item, bool> {
    42     typedef VectorMap<_Graph, _Item, bool> Map;
    43   };
    44 
    45   // char
    46   template <typename _Graph, typename _Item>
    47   struct DefaultMapSelector<_Graph, _Item, char> {
    48     typedef VectorMap<_Graph, _Item, char> Map;
    49   };
    50 
    51   template <typename _Graph, typename _Item>
    52   struct DefaultMapSelector<_Graph, _Item, signed char> {
    53     typedef VectorMap<_Graph, _Item, signed char> Map;
    54   };
    55 
    56   template <typename _Graph, typename _Item>
    57   struct DefaultMapSelector<_Graph, _Item, unsigned char> {
    58     typedef VectorMap<_Graph, _Item, unsigned char> Map;
    59   };
    60 
    61 
    62   // int
    63   template <typename _Graph, typename _Item>
    64   struct DefaultMapSelector<_Graph, _Item, signed int> {
    65     typedef VectorMap<_Graph, _Item, signed int> Map;
    66   };
    67 
    68   template <typename _Graph, typename _Item>
    69   struct DefaultMapSelector<_Graph, _Item, unsigned int> {
    70     typedef VectorMap<_Graph, _Item, unsigned int> Map;
    71   };
    72 
    73 
    74   // short
    75   template <typename _Graph, typename _Item>
    76   struct DefaultMapSelector<_Graph, _Item, signed short> {
    77     typedef VectorMap<_Graph, _Item, signed short> Map;
    78   };
    79 
    80   template <typename _Graph, typename _Item>
    81   struct DefaultMapSelector<_Graph, _Item, unsigned short> {
    82     typedef VectorMap<_Graph, _Item, unsigned short> Map;
    83   };
    84 
    85 
    86   // long
    87   template <typename _Graph, typename _Item>
    88   struct DefaultMapSelector<_Graph, _Item, signed long> {
    89     typedef VectorMap<_Graph, _Item, signed long> Map;
    90   };
    91 
    92   template <typename _Graph, typename _Item>
    93   struct DefaultMapSelector<_Graph, _Item, unsigned long> {
    94     typedef VectorMap<_Graph, _Item, unsigned long> Map;
    95   };
    96 
    97   // \todo handling long long type
    98 
    99 
   100   // float
   101   template <typename _Graph, typename _Item>
   102   struct DefaultMapSelector<_Graph, _Item, float> {
   103     typedef VectorMap<_Graph, _Item, float> Map;
   104   };
   105 
   106 
   107   // double
   108   template <typename _Graph, typename _Item>
   109   struct DefaultMapSelector<_Graph, _Item, double> {
   110     typedef VectorMap<_Graph, _Item,  double> Map;
   111   };
   112 
   113 
   114   // long double
   115   template <typename _Graph, typename _Item>
   116   struct DefaultMapSelector<_Graph, _Item, long double> {
   117     typedef VectorMap<_Graph, _Item, long double> Map;
   118   };
   119 
   120 
   121   // pointer
   122   template <typename _Graph, typename _Item, typename _Ptr>
   123   struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
   124     typedef VectorMap<_Graph, _Item, _Ptr*> Map;
   125   };
   126 
   127   /// \e
   128   template <
   129     typename _Graph, 
   130     typename _Item,
   131     typename _Value>
   132   class DefaultMap 
   133     : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
   134   public:
   135     typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
   136     typedef DefaultMap<_Graph, _Item, _Value> Map;
   137     
   138     typedef typename Parent::Graph Graph;
   139     typedef typename Parent::Value Value;
   140 
   141     DefaultMap(const Graph& _g) : Parent(_g) {}
   142     DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {}
   143 
   144   };
   145 
   146 
   147   /// \e
   148   template <typename _Base> 
   149   class MappableGraphExtender : public _Base {
   150   public:
   151 
   152     typedef MappableGraphExtender<_Base> Graph;
   153     typedef _Base Parent;
   154 
   155     typedef typename Parent::Node Node;
   156     typedef typename Parent::NodeIt NodeIt;
   157 
   158     typedef typename Parent::Edge Edge;
   159     typedef typename Parent::EdgeIt EdgeIt;
   160 
   161     
   162     template <typename _Value>
   163     class NodeMap 
   164       : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
   165     public:
   166       typedef MappableGraphExtender Graph;
   167       typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
   168 
   169       NodeMap(const Graph& _g) 
   170 	: Parent(_g) {}
   171       NodeMap(const Graph& _g, const _Value& _v) 
   172 	: Parent(_g, _v) {}
   173 
   174       NodeMap& operator=(const NodeMap& cmap) {
   175 	return operator=<NodeMap>(cmap);
   176       }
   177 
   178 
   179       /// \brief Template assign operator.
   180       ///
   181       /// The given parameter should be conform to the ReadMap
   182       /// concecpt and could be indiced by the current item set of
   183       /// the NodeMap. In this case the value for each item
   184       /// is assigned by the value of the given ReadMap. 
   185       template <typename CMap>
   186       NodeMap& operator=(const CMap& cmap) {
   187 	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
   188 	const typename Parent::Graph* graph = Parent::getGraph();
   189 	Node it;
   190 	for (graph->first(it); it != INVALID; graph->next(it)) {
   191 	  Parent::set(it, cmap[it]);
   192 	}
   193 	return *this;
   194       }
   195 
   196     };
   197 
   198     template <typename _Value>
   199     class EdgeMap 
   200       : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
   201     public:
   202       typedef MappableGraphExtender Graph;
   203       typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
   204 
   205       EdgeMap(const Graph& _g) 
   206 	: Parent(_g) {}
   207       EdgeMap(const Graph& _g, const _Value& _v) 
   208 	: Parent(_g, _v) {}
   209 
   210       EdgeMap& operator=(const EdgeMap& cmap) {
   211 	return operator=<EdgeMap>(cmap);
   212       }
   213 
   214       template <typename CMap>
   215       EdgeMap& operator=(const CMap& cmap) {
   216 	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
   217 	const typename Parent::Graph* graph = Parent::getGraph();
   218 	Edge it;
   219 	for (graph->first(it); it != INVALID; graph->next(it)) {
   220 	  Parent::set(it, cmap[it]);
   221 	}
   222 	return *this;
   223       }
   224     };
   225     
   226   };
   227 
   228   /// \e
   229   template <typename _Base> 
   230   class MappableUndirGraphExtender : 
   231     public MappableGraphExtender<_Base> {
   232   public:
   233 
   234     typedef MappableUndirGraphExtender Graph;
   235     typedef MappableGraphExtender<_Base> Parent;
   236 
   237     typedef typename Parent::UndirEdge UndirEdge;
   238 
   239     template <typename _Value>
   240     class UndirEdgeMap 
   241       : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
   242     public:
   243       typedef MappableUndirGraphExtender Graph;
   244       typedef IterableMapExtender<
   245 	DefaultMap<Graph, UndirEdge, _Value> > Parent;
   246 
   247       UndirEdgeMap(const Graph& _g) 
   248 	: Parent(_g) {}
   249       UndirEdgeMap(const Graph& _g, const _Value& _v) 
   250 	: Parent(_g, _v) {}
   251 
   252       UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
   253 	return operator=<UndirEdgeMap>(cmap);
   254       }
   255 
   256       template <typename CMap>
   257       UndirEdgeMap& operator=(const CMap& cmap) {
   258 	checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
   259 	const typename Parent::Graph* graph = Parent::getGraph();
   260 	UndirEdge it;
   261 	for (graph->first(it); it != INVALID; graph->next(it)) {
   262 	  Parent::set(it, cmap[it]);
   263 	}
   264 	return *this;
   265       }
   266     };
   267 
   268 
   269   };
   270 
   271   /// @}
   272 }
   273 
   274 #endif