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