2  * lemon/default_map.h - Part of LEMON, a generic C++ optimization library
 
     4  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     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.
 
    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
 
    17 #ifndef LEMON_DEFAULT_MAP_H
 
    18 #define LEMON_DEFAULT_MAP_H
 
    21 #include <lemon/bits/array_map.h>
 
    22 #include <lemon/bits/vector_map.h>
 
    24 ///\ingroup graphmapfactory
 
    26 ///\brief Graph maps that construct and destruct
 
    27 ///their elements dynamically.
 
    31   /// \addtogroup graphmapfactory
 
    34   template <typename _Graph, typename _Item, typename _Value>
 
    35   struct DefaultMapSelector {
 
    36     typedef ArrayMap<_Graph, _Item, _Value> Map;
 
    40   template <typename _Graph, typename _Item>
 
    41   struct DefaultMapSelector<_Graph, _Item, bool> {
 
    42     typedef VectorMap<_Graph, _Item, bool> Map;
 
    46   template <typename _Graph, typename _Item>
 
    47   struct DefaultMapSelector<_Graph, _Item, char> {
 
    48     typedef VectorMap<_Graph, _Item, char> Map;
 
    51   template <typename _Graph, typename _Item>
 
    52   struct DefaultMapSelector<_Graph, _Item, signed char> {
 
    53     typedef VectorMap<_Graph, _Item, signed char> Map;
 
    56   template <typename _Graph, typename _Item>
 
    57   struct DefaultMapSelector<_Graph, _Item, unsigned char> {
 
    58     typedef VectorMap<_Graph, _Item, unsigned char> Map;
 
    63   template <typename _Graph, typename _Item>
 
    64   struct DefaultMapSelector<_Graph, _Item, signed int> {
 
    65     typedef VectorMap<_Graph, _Item, signed int> Map;
 
    68   template <typename _Graph, typename _Item>
 
    69   struct DefaultMapSelector<_Graph, _Item, unsigned int> {
 
    70     typedef VectorMap<_Graph, _Item, unsigned int> Map;
 
    75   template <typename _Graph, typename _Item>
 
    76   struct DefaultMapSelector<_Graph, _Item, signed short> {
 
    77     typedef VectorMap<_Graph, _Item, signed short> Map;
 
    80   template <typename _Graph, typename _Item>
 
    81   struct DefaultMapSelector<_Graph, _Item, unsigned short> {
 
    82     typedef VectorMap<_Graph, _Item, unsigned short> Map;
 
    87   template <typename _Graph, typename _Item>
 
    88   struct DefaultMapSelector<_Graph, _Item, signed long> {
 
    89     typedef VectorMap<_Graph, _Item, signed long> Map;
 
    92   template <typename _Graph, typename _Item>
 
    93   struct DefaultMapSelector<_Graph, _Item, unsigned long> {
 
    94     typedef VectorMap<_Graph, _Item, unsigned long> Map;
 
    97   // \todo handling long long type
 
   101   template <typename _Graph, typename _Item>
 
   102   struct DefaultMapSelector<_Graph, _Item, float> {
 
   103     typedef VectorMap<_Graph, _Item, float> Map;
 
   108   template <typename _Graph, typename _Item>
 
   109   struct DefaultMapSelector<_Graph, _Item, double> {
 
   110     typedef VectorMap<_Graph, _Item,  double> Map;
 
   115   template <typename _Graph, typename _Item>
 
   116   struct DefaultMapSelector<_Graph, _Item, long double> {
 
   117     typedef VectorMap<_Graph, _Item, long double> Map;
 
   122   template <typename _Graph, typename _Item, typename _Ptr>
 
   123   struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
 
   124     typedef VectorMap<_Graph, _Item, _Ptr*> Map;
 
   133     : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
 
   135     typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
 
   136     typedef DefaultMap<_Graph, _Item, _Value> Map;
 
   138     typedef typename Parent::Graph Graph;
 
   139     typedef typename Parent::Value Value;
 
   141     DefaultMap(const Graph& _g) : Parent(_g) {}
 
   142     DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {}
 
   148   template <typename _Base> 
 
   149   class MappableGraphExtender : public _Base {
 
   152     typedef MappableGraphExtender<_Base> Graph;
 
   153     typedef _Base Parent;
 
   155     typedef typename Parent::Node Node;
 
   156     typedef typename Parent::NodeIt NodeIt;
 
   158     typedef typename Parent::Edge Edge;
 
   159     typedef typename Parent::EdgeIt EdgeIt;
 
   162     template <typename _Value>
 
   164       : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
 
   166       typedef MappableGraphExtender Graph;
 
   167       typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
 
   169       NodeMap(const Graph& _g) 
 
   171       NodeMap(const Graph& _g, const _Value& _v) 
 
   174       NodeMap& operator=(const NodeMap& cmap) {
 
   175 	return operator=<NodeMap>(cmap);
 
   179       /// \brief Template assign operator.
 
   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();
 
   190 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   191 	  Parent::set(it, cmap[it]);
 
   198     template <typename _Value>
 
   200       : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
 
   202       typedef MappableGraphExtender Graph;
 
   203       typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
 
   205       EdgeMap(const Graph& _g) 
 
   207       EdgeMap(const Graph& _g, const _Value& _v) 
 
   210       EdgeMap& operator=(const EdgeMap& cmap) {
 
   211 	return operator=<EdgeMap>(cmap);
 
   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();
 
   219 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   220 	  Parent::set(it, cmap[it]);
 
   229   template <typename _Base> 
 
   230   class MappableUndirGraphExtender : 
 
   231     public MappableGraphExtender<_Base> {
 
   234     typedef MappableUndirGraphExtender Graph;
 
   235     typedef MappableGraphExtender<_Base> Parent;
 
   237     typedef typename Parent::UndirEdge UndirEdge;
 
   239     template <typename _Value>
 
   241       : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
 
   243       typedef MappableUndirGraphExtender Graph;
 
   244       typedef IterableMapExtender<
 
   245 	DefaultMap<Graph, UndirEdge, _Value> > Parent;
 
   247       UndirEdgeMap(const Graph& _g) 
 
   249       UndirEdgeMap(const Graph& _g, const _Value& _v) 
 
   252       UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
 
   253 	return operator=<UndirEdgeMap>(cmap);
 
   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();
 
   261 	for (graph->first(it); it != INVALID; graph->next(it)) {
 
   262 	  Parent::set(it, cmap[it]);