COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/default_map.h @ 1671:3c82a0d9e0e7

Last change on this file since 1671:3c82a0d9e0e7 was 1669:66ae78d29f1e, checked in by Balazs Dezso, 19 years ago

Template assign operator for graph maps.

Some naming and coding conventions.

File size: 6.9 KB
RevLine 
[906]1/* -*- C++ -*-
[1435]2 * lemon/default_map.h - Part of LEMON, a generic C++ optimization library
[906]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]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
[921]17#ifndef LEMON_DEFAULT_MAP_H
18#define LEMON_DEFAULT_MAP_H
[822]19
20
[1307]21#include <lemon/bits/array_map.h>
22#include <lemon/bits/vector_map.h>
[822]23
[1587]24///\ingroup graphmapfactory
[822]25///\file
[946]26///\brief Graph maps that construct and destruct
[822]27///their elements dynamically.
28
[921]29namespace lemon {
[822]30
[1669]31  /// \addtogroup graphmapfactory
32  /// @{
[822]33
[1267]34  template <typename _Graph, typename _Item, typename _Value>
[946]35  struct DefaultMapSelector {
[1267]36    typedef ArrayMap<_Graph, _Item, _Value> Map;
[946]37  };
[822]38
[946]39  // bool
[1267]40  template <typename _Graph, typename _Item>
41  struct DefaultMapSelector<_Graph, _Item, bool> {
[980]42    typedef VectorMap<_Graph, _Item, bool> Map;
[946]43  };
[822]44
[946]45  // char
[1267]46  template <typename _Graph, typename _Item>
47  struct DefaultMapSelector<_Graph, _Item, char> {
[980]48    typedef VectorMap<_Graph, _Item, char> Map;
[946]49  };
[822]50
[1267]51  template <typename _Graph, typename _Item>
52  struct DefaultMapSelector<_Graph, _Item, signed char> {
[980]53    typedef VectorMap<_Graph, _Item, signed char> Map;
[946]54  };
[822]55
[1267]56  template <typename _Graph, typename _Item>
57  struct DefaultMapSelector<_Graph, _Item, unsigned char> {
[980]58    typedef VectorMap<_Graph, _Item, unsigned char> Map;
[946]59  };
[822]60
61
[946]62  // int
[1267]63  template <typename _Graph, typename _Item>
64  struct DefaultMapSelector<_Graph, _Item, signed int> {
[980]65    typedef VectorMap<_Graph, _Item, signed int> Map;
[946]66  };
[822]67
[1267]68  template <typename _Graph, typename _Item>
69  struct DefaultMapSelector<_Graph, _Item, unsigned int> {
[980]70    typedef VectorMap<_Graph, _Item, unsigned int> Map;
[946]71  };
[822]72
73
[946]74  // short
[1267]75  template <typename _Graph, typename _Item>
76  struct DefaultMapSelector<_Graph, _Item, signed short> {
[980]77    typedef VectorMap<_Graph, _Item, signed short> Map;
[946]78  };
[822]79
[1267]80  template <typename _Graph, typename _Item>
81  struct DefaultMapSelector<_Graph, _Item, unsigned short> {
[980]82    typedef VectorMap<_Graph, _Item, unsigned short> Map;
[946]83  };
84
85
86  // long
[1267]87  template <typename _Graph, typename _Item>
88  struct DefaultMapSelector<_Graph, _Item, signed long> {
[980]89    typedef VectorMap<_Graph, _Item, signed long> Map;
[946]90  };
91
[1267]92  template <typename _Graph, typename _Item>
93  struct DefaultMapSelector<_Graph, _Item, unsigned long> {
[980]94    typedef VectorMap<_Graph, _Item, unsigned long> Map;
[946]95  };
96
97  // \todo handling long long type
98
99
100  // float
[1267]101  template <typename _Graph, typename _Item>
102  struct DefaultMapSelector<_Graph, _Item, float> {
[980]103    typedef VectorMap<_Graph, _Item, float> Map;
[946]104  };
105
106
107  // double
[1267]108  template <typename _Graph, typename _Item>
109  struct DefaultMapSelector<_Graph, _Item, double> {
[980]110    typedef VectorMap<_Graph, _Item,  double> Map;
[946]111  };
112
113
114  // long double
[1267]115  template <typename _Graph, typename _Item>
116  struct DefaultMapSelector<_Graph, _Item, long double> {
[980]117    typedef VectorMap<_Graph, _Item, long double> Map;
[946]118  };
119
120
121  // pointer
[1267]122  template <typename _Graph, typename _Item, typename _Ptr>
123  struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
[980]124    typedef VectorMap<_Graph, _Item, _Ptr*> Map;
[946]125  };
126
[1669]127  /// \e
[1267]128  template <
129    typename _Graph,
130    typename _Item,
131    typename _Value>
132  class DefaultMap
133    : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
[946]134  public:
[1267]135    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
136    typedef DefaultMap<_Graph, _Item, _Value> Map;
[946]137   
138    typedef typename Parent::Graph Graph;
[987]139    typedef typename Parent::Value Value;
[946]140
[980]141    DefaultMap(const Graph& _g) : Parent(_g) {}
[987]142    DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {}
[1669]143
[946]144  };
145
146
[1669]147  /// \e
[946]148  template <typename _Base>
[1669]149  class MappableGraphExtender : public _Base {
[946]150  public:
151
[1669]152    typedef MappableGraphExtender<_Base> Graph;
[946]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>
[1267]163    class NodeMap
164      : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
[946]165    public:
[1669]166      typedef MappableGraphExtender Graph;
[1267]167      typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
[946]168
[980]169      NodeMap(const Graph& _g)
170        : Parent(_g) {}
[1022]171      NodeMap(const Graph& _g, const _Value& _v)
[980]172        : Parent(_g, _v) {}
[1669]173
174      /// \brief Template assign operator.
175      ///
176      /// The given parameter should be conform to the ReadMap
177      /// concecpt and could be indiced by the current item set of
178      /// the NodeMap. In this case the value for each item
179      /// is assigned by the value of the given ReadMap.
180      template <typename CMap>
181      NodeMap& operator=(const CMap& cmap) {
182        checkConcept<concept::ReadMap<Node, _Value>, CMap>();
183        const typename Parent::Graph* graph = Parent::getGraph();
184        Node it;
185        for (graph->first(it); it != INVALID; graph->next(it)) {
186          Parent::set(it, cmap[it]);
187        }
188        return *this;
189      }
190
[946]191    };
192
193    template <typename _Value>
[1267]194    class EdgeMap
195      : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
[946]196    public:
[1669]197      typedef MappableGraphExtender Graph;
[1267]198      typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
[946]199
[980]200      EdgeMap(const Graph& _g)
201        : Parent(_g) {}
[1022]202      EdgeMap(const Graph& _g, const _Value& _v)
[980]203        : Parent(_g, _v) {}
[1669]204
205      template <typename CMap>
206      EdgeMap& operator=(const CMap& cmap) {
207        checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
208        const typename Parent::Graph* graph = Parent::getGraph();
209        Edge it;
210        for (graph->first(it); it != INVALID; graph->next(it)) {
211          Parent::set(it, cmap[it]);
212        }
213        return *this;
214      }
[946]215    };
216   
217  };
218
[1669]219  /// \e
[1022]220  template <typename _Base>
221  class MappableUndirGraphExtender :
[1669]222    public MappableGraphExtender<_Base> {
[1022]223  public:
224
225    typedef MappableUndirGraphExtender Graph;
[1669]226    typedef MappableGraphExtender<_Base> Parent;
[1022]227
228    typedef typename Parent::UndirEdge UndirEdge;
229
230    template <typename _Value>
[1267]231    class UndirEdgeMap
232      : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
[1022]233    public:
234      typedef MappableUndirGraphExtender Graph;
[1267]235      typedef IterableMapExtender<
236        DefaultMap<Graph, UndirEdge, _Value> > Parent;
[1022]237
238      UndirEdgeMap(const Graph& _g)
239        : Parent(_g) {}
240      UndirEdgeMap(const Graph& _g, const _Value& _v)
241        : Parent(_g, _v) {}
[1669]242
243      template <typename CMap>
244      UndirEdgeMap& operator=(const CMap& cmap) {
245        checkConcept<concept::ReadMap<UndirEdge, _Value>, CMap>();
246        const typename Parent::Graph* graph = Parent::getGraph();
247        UndirEdge it;
248        for (graph->first(it); it != INVALID; graph->next(it)) {
249          Parent::set(it, cmap[it]);
250        }
251        return *this;
252      }
[1022]253    };
254
255
256  };
[822]257
[1669]258  /// @}
[822]259}
260
261#endif
Note: See TracBrowser for help on using the repository browser.