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 /// \brief Template assign operator.
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();
185 for (graph->first(it); it != INVALID; graph->next(it)) {
186 Parent::set(it, cmap[it]);
193 template <typename _Value>
195 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
197 typedef MappableGraphExtender Graph;
198 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
200 EdgeMap(const Graph& _g)
202 EdgeMap(const Graph& _g, const _Value& _v)
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();
210 for (graph->first(it); it != INVALID; graph->next(it)) {
211 Parent::set(it, cmap[it]);
220 template <typename _Base>
221 class MappableUndirGraphExtender :
222 public MappableGraphExtender<_Base> {
225 typedef MappableUndirGraphExtender Graph;
226 typedef MappableGraphExtender<_Base> Parent;
228 typedef typename Parent::UndirEdge UndirEdge;
230 template <typename _Value>
232 : public IterableMapExtender<DefaultMap<Graph, UndirEdge, _Value> > {
234 typedef MappableUndirGraphExtender Graph;
235 typedef IterableMapExtender<
236 DefaultMap<Graph, UndirEdge, _Value> > Parent;
238 UndirEdgeMap(const Graph& _g)
240 UndirEdgeMap(const Graph& _g, const _Value& _v)
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();
248 for (graph->first(it); it != INVALID; graph->next(it)) {
249 Parent::set(it, cmap[it]);