lemon/bits/default_map.h
author alpar
Thu, 01 Sep 2005 20:35:30 +0000
changeset 1674 648aa2f33dc8
parent 1669 66ae78d29f1e
child 1703 eb90e3d6bddc
permissions -rw-r--r--
Warning added on the usage of InDegMap/OutDegMap
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@1669
    31
  /// \addtogroup graphmapfactory
deba@1669
    32
  /// @{
deba@822
    33
deba@1267
    34
  template <typename _Graph, typename _Item, typename _Value>
klao@946
    35
  struct DefaultMapSelector {
deba@1267
    36
    typedef ArrayMap<_Graph, _Item, _Value> Map;
klao@946
    37
  };
deba@822
    38
klao@946
    39
  // bool
deba@1267
    40
  template <typename _Graph, typename _Item>
deba@1267
    41
  struct DefaultMapSelector<_Graph, _Item, bool> {
deba@980
    42
    typedef VectorMap<_Graph, _Item, bool> Map;
klao@946
    43
  };
deba@822
    44
klao@946
    45
  // char
deba@1267
    46
  template <typename _Graph, typename _Item>
deba@1267
    47
  struct DefaultMapSelector<_Graph, _Item, char> {
deba@980
    48
    typedef VectorMap<_Graph, _Item, char> Map;
klao@946
    49
  };
deba@822
    50
deba@1267
    51
  template <typename _Graph, typename _Item>
deba@1267
    52
  struct DefaultMapSelector<_Graph, _Item, signed char> {
deba@980
    53
    typedef VectorMap<_Graph, _Item, signed char> Map;
klao@946
    54
  };
deba@822
    55
deba@1267
    56
  template <typename _Graph, typename _Item>
deba@1267
    57
  struct DefaultMapSelector<_Graph, _Item, unsigned char> {
deba@980
    58
    typedef VectorMap<_Graph, _Item, unsigned char> Map;
klao@946
    59
  };
deba@822
    60
deba@822
    61
klao@946
    62
  // int
deba@1267
    63
  template <typename _Graph, typename _Item>
deba@1267
    64
  struct DefaultMapSelector<_Graph, _Item, signed int> {
deba@980
    65
    typedef VectorMap<_Graph, _Item, signed int> Map;
klao@946
    66
  };
deba@822
    67
deba@1267
    68
  template <typename _Graph, typename _Item>
deba@1267
    69
  struct DefaultMapSelector<_Graph, _Item, unsigned int> {
deba@980
    70
    typedef VectorMap<_Graph, _Item, unsigned int> Map;
klao@946
    71
  };
deba@822
    72
deba@822
    73
klao@946
    74
  // short
deba@1267
    75
  template <typename _Graph, typename _Item>
deba@1267
    76
  struct DefaultMapSelector<_Graph, _Item, signed short> {
deba@980
    77
    typedef VectorMap<_Graph, _Item, signed short> Map;
klao@946
    78
  };
deba@822
    79
deba@1267
    80
  template <typename _Graph, typename _Item>
deba@1267
    81
  struct DefaultMapSelector<_Graph, _Item, unsigned short> {
deba@980
    82
    typedef VectorMap<_Graph, _Item, unsigned short> Map;
klao@946
    83
  };
klao@946
    84
klao@946
    85
klao@946
    86
  // long
deba@1267
    87
  template <typename _Graph, typename _Item>
deba@1267
    88
  struct DefaultMapSelector<_Graph, _Item, signed long> {
deba@980
    89
    typedef VectorMap<_Graph, _Item, signed long> Map;
klao@946
    90
  };
klao@946
    91
deba@1267
    92
  template <typename _Graph, typename _Item>
deba@1267
    93
  struct DefaultMapSelector<_Graph, _Item, unsigned long> {
deba@980
    94
    typedef VectorMap<_Graph, _Item, unsigned long> Map;
klao@946
    95
  };
klao@946
    96
klao@946
    97
  // \todo handling long long type
klao@946
    98
klao@946
    99
klao@946
   100
  // float
deba@1267
   101
  template <typename _Graph, typename _Item>
deba@1267
   102
  struct DefaultMapSelector<_Graph, _Item, float> {
deba@980
   103
    typedef VectorMap<_Graph, _Item, float> Map;
klao@946
   104
  };
klao@946
   105
klao@946
   106
klao@946
   107
  // double
deba@1267
   108
  template <typename _Graph, typename _Item>
deba@1267
   109
  struct DefaultMapSelector<_Graph, _Item, double> {
deba@980
   110
    typedef VectorMap<_Graph, _Item,  double> Map;
klao@946
   111
  };
klao@946
   112
klao@946
   113
klao@946
   114
  // long double
deba@1267
   115
  template <typename _Graph, typename _Item>
deba@1267
   116
  struct DefaultMapSelector<_Graph, _Item, long double> {
deba@980
   117
    typedef VectorMap<_Graph, _Item, long double> Map;
klao@946
   118
  };
klao@946
   119
klao@946
   120
klao@946
   121
  // pointer
deba@1267
   122
  template <typename _Graph, typename _Item, typename _Ptr>
deba@1267
   123
  struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
deba@980
   124
    typedef VectorMap<_Graph, _Item, _Ptr*> Map;
klao@946
   125
  };
klao@946
   126
deba@1669
   127
  /// \e
deba@1267
   128
  template <
deba@1267
   129
    typename _Graph, 
deba@1267
   130
    typename _Item,
deba@1267
   131
    typename _Value>
deba@1267
   132
  class DefaultMap 
deba@1267
   133
    : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
klao@946
   134
  public:
deba@1267
   135
    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
deba@1267
   136
    typedef DefaultMap<_Graph, _Item, _Value> Map;
klao@946
   137
    
klao@946
   138
    typedef typename Parent::Graph Graph;
alpar@987
   139
    typedef typename Parent::Value Value;
klao@946
   140
deba@980
   141
    DefaultMap(const Graph& _g) : Parent(_g) {}
alpar@987
   142
    DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {}
deba@1669
   143
klao@946
   144
  };
klao@946
   145
klao@946
   146
deba@1669
   147
  /// \e
klao@946
   148
  template <typename _Base> 
deba@1669
   149
  class MappableGraphExtender : public _Base {
klao@946
   150
  public:
klao@946
   151
deba@1669
   152
    typedef MappableGraphExtender<_Base> Graph;
klao@946
   153
    typedef _Base Parent;
klao@946
   154
klao@946
   155
    typedef typename Parent::Node Node;
klao@946
   156
    typedef typename Parent::NodeIt NodeIt;
klao@946
   157
klao@946
   158
    typedef typename Parent::Edge Edge;
klao@946
   159
    typedef typename Parent::EdgeIt EdgeIt;
klao@946
   160
klao@946
   161
    
klao@946
   162
    template <typename _Value>
deba@1267
   163
    class NodeMap 
deba@1267
   164
      : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
klao@946
   165
    public:
deba@1669
   166
      typedef MappableGraphExtender Graph;
deba@1267
   167
      typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
klao@946
   168
deba@980
   169
      NodeMap(const Graph& _g) 
deba@980
   170
	: Parent(_g) {}
klao@1022
   171
      NodeMap(const Graph& _g, const _Value& _v) 
deba@980
   172
	: Parent(_g, _v) {}
deba@1669
   173
deba@1672
   174
      NodeMap& operator=(const NodeMap& cmap) {
deba@1672
   175
	return operator=<NodeMap>(cmap);
deba@1672
   176
      }
deba@1672
   177
deba@1672
   178
deba@1669
   179
      /// \brief Template assign operator.
deba@1669
   180
      ///
deba@1669
   181
      /// The given parameter should be conform to the ReadMap
deba@1669
   182
      /// concecpt and could be indiced by the current item set of
deba@1669
   183
      /// the NodeMap. In this case the value for each item
deba@1669
   184
      /// is assigned by the value of the given ReadMap. 
deba@1669
   185
      template <typename CMap>
deba@1669
   186
      NodeMap& operator=(const CMap& cmap) {
deba@1669
   187
	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
deba@1669
   188
	const typename Parent::Graph* graph = Parent::getGraph();
deba@1669
   189
	Node it;
deba@1669
   190
	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@1669
   191
	  Parent::set(it, cmap[it]);
deba@1669
   192
	}
deba@1669
   193
	return *this;
deba@1669
   194
      }
deba@1669
   195
klao@946
   196
    };
klao@946
   197
klao@946
   198
    template <typename _Value>
deba@1267
   199
    class EdgeMap 
deba@1267
   200
      : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
klao@946
   201
    public:
deba@1669
   202
      typedef MappableGraphExtender Graph;
deba@1267
   203
      typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
klao@946
   204
deba@980
   205
      EdgeMap(const Graph& _g) 
deba@980
   206
	: Parent(_g) {}
klao@1022
   207
      EdgeMap(const Graph& _g, const _Value& _v) 
deba@980
   208
	: Parent(_g, _v) {}
deba@1669
   209
deba@1672
   210
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@1672
   211
	return operator=<EdgeMap>(cmap);
deba@1672
   212
      }
deba@1672
   213
deba@1669
   214
      template <typename CMap>
deba@1669
   215
      EdgeMap& operator=(const CMap& cmap) {
deba@1669
   216
	checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
deba@1669
   217
	const typename Parent::Graph* graph = Parent::getGraph();
deba@1669
   218
	Edge it;
deba@1669
   219
	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@1669
   220
	  Parent::set(it, cmap[it]);
deba@1669
   221
	}
deba@1669
   222
	return *this;
deba@1669
   223
      }
klao@946
   224
    };
klao@946
   225
    
klao@946
   226
  };
klao@946
   227
deba@1669
   228
  /// \e
klao@1022
   229
  template <typename _Base> 
klao@1022
   230
  class MappableUndirGraphExtender : 
deba@1669
   231
    public MappableGraphExtender<_Base> {
klao@1022
   232
  public:
klao@1022
   233
klao@1022
   234
    typedef MappableUndirGraphExtender Graph;
deba@1669
   235
    typedef MappableGraphExtender<_Base> Parent;
klao@1022
   236
klao@1022
   237
    typedef typename Parent::UndirEdge UndirEdge;
klao@1022
   238
klao@1022
   239
    template <typename _Value>
deba@1267
   240
    class UndirEdgeMap 
deba@1267
   241
      : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
klao@1022
   242
    public:
klao@1022
   243
      typedef MappableUndirGraphExtender Graph;
deba@1267
   244
      typedef IterableMapExtender<
deba@1267
   245
	DefaultMap<Graph, UndirEdge, _Value> > Parent;
klao@1022
   246
klao@1022
   247
      UndirEdgeMap(const Graph& _g) 
klao@1022
   248
	: Parent(_g) {}
klao@1022
   249
      UndirEdgeMap(const Graph& _g, const _Value& _v) 
klao@1022
   250
	: Parent(_g, _v) {}
deba@1669
   251
deba@1672
   252
      UndirEdgeMap& operator=(const UndirEdgeMap& cmap) {
deba@1672
   253
	return operator=<UndirEdgeMap>(cmap);
deba@1672
   254
      }
deba@1672
   255
deba@1669
   256
      template <typename CMap>
deba@1669
   257
      UndirEdgeMap& operator=(const CMap& cmap) {
deba@1669
   258
	checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
deba@1669
   259
	const typename Parent::Graph* graph = Parent::getGraph();
deba@1669
   260
	UndirEdge it;
deba@1669
   261
	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@1669
   262
	  Parent::set(it, cmap[it]);
deba@1669
   263
	}
deba@1669
   264
	return *this;
deba@1669
   265
      }
klao@1022
   266
    };
klao@1022
   267
klao@1022
   268
klao@1022
   269
  };
deba@822
   270
deba@1669
   271
  /// @}
deba@822
   272
}
deba@822
   273
deba@822
   274
#endif