1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_DEFAULT_MAP_H |
20 | 20 |
#define LEMON_BITS_DEFAULT_MAP_H |
21 | 21 |
|
22 |
#include <lemon/config.h> |
|
22 | 23 |
#include <lemon/bits/array_map.h> |
23 | 24 |
#include <lemon/bits/vector_map.h> |
24 | 25 |
//#include <lemon/bits/debug_map.h> |
25 | 26 |
|
26 | 27 |
//\ingroup graphbits |
27 | 28 |
//\file |
28 | 29 |
//\brief Graph maps that construct and destruct their elements dynamically. |
29 | 30 |
|
30 | 31 |
namespace lemon { |
31 | 32 |
|
32 | 33 |
|
33 | 34 |
//#ifndef LEMON_USE_DEBUG_MAP |
34 | 35 |
|
35 | 36 |
template <typename _Graph, typename _Item, typename _Value> |
36 | 37 |
struct DefaultMapSelector { |
37 | 38 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
38 | 39 |
}; |
39 | 40 |
|
40 | 41 |
// bool |
41 | 42 |
template <typename _Graph, typename _Item> |
42 | 43 |
struct DefaultMapSelector<_Graph, _Item, bool> { |
43 | 44 |
typedef VectorMap<_Graph, _Item, bool> Map; |
44 | 45 |
}; |
45 | 46 |
|
46 | 47 |
// char |
47 | 48 |
template <typename _Graph, typename _Item> |
48 | 49 |
struct DefaultMapSelector<_Graph, _Item, char> { |
49 | 50 |
typedef VectorMap<_Graph, _Item, char> Map; |
50 | 51 |
}; |
51 | 52 |
|
52 | 53 |
template <typename _Graph, typename _Item> |
53 | 54 |
struct DefaultMapSelector<_Graph, _Item, signed char> { |
54 | 55 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
55 | 56 |
}; |
56 | 57 |
|
57 | 58 |
template <typename _Graph, typename _Item> |
58 | 59 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
59 | 60 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
60 | 61 |
}; |
61 | 62 |
|
62 | 63 |
|
63 | 64 |
// int |
64 | 65 |
template <typename _Graph, typename _Item> |
65 | 66 |
struct DefaultMapSelector<_Graph, _Item, signed int> { |
66 | 67 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
67 | 68 |
}; |
68 | 69 |
|
69 | 70 |
template <typename _Graph, typename _Item> |
70 | 71 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
71 | 72 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
72 | 73 |
}; |
73 | 74 |
|
74 | 75 |
|
75 | 76 |
// short |
76 | 77 |
template <typename _Graph, typename _Item> |
77 | 78 |
struct DefaultMapSelector<_Graph, _Item, signed short> { |
78 | 79 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
79 | 80 |
}; |
80 | 81 |
|
81 | 82 |
template <typename _Graph, typename _Item> |
82 | 83 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
83 | 84 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
84 | 85 |
}; |
85 | 86 |
|
86 | 87 |
|
87 | 88 |
// long |
88 | 89 |
template <typename _Graph, typename _Item> |
89 | 90 |
struct DefaultMapSelector<_Graph, _Item, signed long> { |
90 | 91 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
91 | 92 |
}; |
92 | 93 |
|
93 | 94 |
template <typename _Graph, typename _Item> |
94 | 95 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
95 | 96 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
96 | 97 |
}; |
97 | 98 |
|
98 | 99 |
|
99 | 100 |
#if defined HAVE_LONG_LONG |
100 | 101 |
|
101 | 102 |
// long long |
102 | 103 |
template <typename _Graph, typename _Item> |
103 | 104 |
struct DefaultMapSelector<_Graph, _Item, signed long long> { |
104 | 105 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
105 | 106 |
}; |
106 | 107 |
|
107 | 108 |
template <typename _Graph, typename _Item> |
108 | 109 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> { |
109 | 110 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
110 | 111 |
}; |
111 | 112 |
|
112 | 113 |
#endif |
113 | 114 |
|
114 | 115 |
|
115 | 116 |
// float |
116 | 117 |
template <typename _Graph, typename _Item> |
117 | 118 |
struct DefaultMapSelector<_Graph, _Item, float> { |
118 | 119 |
typedef VectorMap<_Graph, _Item, float> Map; |
119 | 120 |
}; |
120 | 121 |
|
121 | 122 |
|
122 | 123 |
// double |
123 | 124 |
template <typename _Graph, typename _Item> |
124 | 125 |
struct DefaultMapSelector<_Graph, _Item, double> { |
125 | 126 |
typedef VectorMap<_Graph, _Item, double> Map; |
126 | 127 |
}; |
127 | 128 |
|
128 | 129 |
|
129 | 130 |
// long double |
130 | 131 |
template <typename _Graph, typename _Item> |
131 | 132 |
struct DefaultMapSelector<_Graph, _Item, long double> { |
132 | 133 |
typedef VectorMap<_Graph, _Item, long double> Map; |
133 | 134 |
}; |
134 | 135 |
|
135 | 136 |
|
136 | 137 |
// pointer |
137 | 138 |
template <typename _Graph, typename _Item, typename _Ptr> |
138 | 139 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
139 | 140 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
140 | 141 |
}; |
141 | 142 |
|
142 | 143 |
// #else |
143 | 144 |
|
144 | 145 |
// template <typename _Graph, typename _Item, typename _Value> |
145 | 146 |
// struct DefaultMapSelector { |
146 | 147 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
147 | 148 |
// }; |
148 | 149 |
|
149 | 150 |
// #endif |
150 | 151 |
|
151 | 152 |
// DefaultMap class |
152 | 153 |
template <typename _Graph, typename _Item, typename _Value> |
153 | 154 |
class DefaultMap |
154 | 155 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
155 | 156 |
public: |
156 | 157 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
157 | 158 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
158 | 159 |
|
159 | 160 |
typedef typename Parent::Graph Graph; |
160 | 161 |
typedef typename Parent::Value Value; |
161 | 162 |
|
162 | 163 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {} |
163 | 164 |
DefaultMap(const Graph& graph, const Value& value) |
164 | 165 |
: Parent(graph, value) {} |
165 | 166 |
|
166 | 167 |
DefaultMap& operator=(const DefaultMap& cmap) { |
167 | 168 |
return operator=<DefaultMap>(cmap); |
168 | 169 |
} |
169 | 170 |
|
170 | 171 |
template <typename CMap> |
171 | 172 |
DefaultMap& operator=(const CMap& cmap) { |
172 | 173 |
Parent::operator=(cmap); |
173 | 174 |
return *this; |
174 | 175 |
} |
175 | 176 |
|
176 | 177 |
}; |
177 | 178 |
|
178 | 179 |
} |
179 | 180 |
|
180 | 181 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CORE_H |
20 | 20 |
#define LEMON_CORE_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 |
#include <lemon/core.h> |
|
25 | 26 |
#include <lemon/bits/enable_if.h> |
26 | 27 |
#include <lemon/bits/traits.h> |
27 | 28 |
#include <lemon/assert.h> |
28 | 29 |
|
29 | 30 |
///\file |
30 | 31 |
///\brief LEMON core utilities. |
31 | 32 |
/// |
32 | 33 |
///This header file contains core utilities for LEMON. |
33 | 34 |
///It is automatically included by all graph types, therefore it usually |
34 | 35 |
///do not have to be included directly. |
35 | 36 |
|
36 | 37 |
namespace lemon { |
37 | 38 |
|
38 | 39 |
/// \brief Dummy type to make it easier to create invalid iterators. |
39 | 40 |
/// |
40 | 41 |
/// Dummy type to make it easier to create invalid iterators. |
41 | 42 |
/// See \ref INVALID for the usage. |
42 | 43 |
struct Invalid { |
43 | 44 |
public: |
44 | 45 |
bool operator==(Invalid) { return true; } |
45 | 46 |
bool operator!=(Invalid) { return false; } |
46 | 47 |
bool operator< (Invalid) { return false; } |
47 | 48 |
}; |
48 | 49 |
|
49 | 50 |
/// \brief Invalid iterators. |
50 | 51 |
/// |
51 | 52 |
/// \ref Invalid is a global type that converts to each iterator |
52 | 53 |
/// in such a way that the value of the target iterator will be invalid. |
53 | 54 |
#ifdef LEMON_ONLY_TEMPLATES |
54 | 55 |
const Invalid INVALID = Invalid(); |
55 | 56 |
#else |
56 | 57 |
extern const Invalid INVALID; |
57 | 58 |
#endif |
58 | 59 |
|
59 | 60 |
/// \addtogroup gutils |
60 | 61 |
/// @{ |
61 | 62 |
|
62 | 63 |
///Create convenience typedefs for the digraph types and iterators |
63 | 64 |
|
64 | 65 |
///This \c \#define creates convenient type definitions for the following |
65 | 66 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
66 | 67 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
67 | 68 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
68 | 69 |
/// |
69 | 70 |
///\note If the graph type is a dependent type, ie. the graph type depend |
70 | 71 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
71 | 72 |
///macro. |
72 | 73 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
73 | 74 |
typedef Digraph::Node Node; \ |
74 | 75 |
typedef Digraph::NodeIt NodeIt; \ |
75 | 76 |
typedef Digraph::Arc Arc; \ |
76 | 77 |
typedef Digraph::ArcIt ArcIt; \ |
77 | 78 |
typedef Digraph::InArcIt InArcIt; \ |
78 | 79 |
typedef Digraph::OutArcIt OutArcIt; \ |
79 | 80 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
80 | 81 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
81 | 82 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
82 | 83 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
83 | 84 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
84 | 85 |
typedef Digraph::ArcMap<double> DoubleArcMap |
85 | 86 |
|
86 | 87 |
///Create convenience typedefs for the digraph types and iterators |
87 | 88 |
|
88 | 89 |
///\see DIGRAPH_TYPEDEFS |
89 | 90 |
/// |
90 | 91 |
///\note Use this macro, if the graph type is a dependent type, |
91 | 92 |
///ie. the graph type depend on a template parameter. |
92 | 93 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
93 | 94 |
typedef typename Digraph::Node Node; \ |
94 | 95 |
typedef typename Digraph::NodeIt NodeIt; \ |
95 | 96 |
typedef typename Digraph::Arc Arc; \ |
96 | 97 |
typedef typename Digraph::ArcIt ArcIt; \ |
97 | 98 |
typedef typename Digraph::InArcIt InArcIt; \ |
98 | 99 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
99 | 100 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
100 | 101 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
101 | 102 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
102 | 103 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
103 | 104 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
104 | 105 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
105 | 106 |
|
106 | 107 |
///Create convenience typedefs for the graph types and iterators |
107 | 108 |
|
108 | 109 |
///This \c \#define creates the same convenient type definitions as defined |
109 | 110 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
110 | 111 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
111 | 112 |
///\c DoubleEdgeMap. |
112 | 113 |
/// |
113 | 114 |
///\note If the graph type is a dependent type, ie. the graph type depend |
114 | 115 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
115 | 116 |
///macro. |
116 | 117 |
#define GRAPH_TYPEDEFS(Graph) \ |
117 | 118 |
DIGRAPH_TYPEDEFS(Graph); \ |
118 | 119 |
typedef Graph::Edge Edge; \ |
119 | 120 |
typedef Graph::EdgeIt EdgeIt; \ |
120 | 121 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
121 | 122 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
122 | 123 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
123 | 124 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
124 | 125 |
|
125 | 126 |
///Create convenience typedefs for the graph types and iterators |
126 | 127 |
|
127 | 128 |
///\see GRAPH_TYPEDEFS |
128 | 129 |
/// |
129 | 130 |
///\note Use this macro, if the graph type is a dependent type, |
130 | 131 |
///ie. the graph type depend on a template parameter. |
131 | 132 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
132 | 133 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
133 | 134 |
typedef typename Graph::Edge Edge; \ |
134 | 135 |
typedef typename Graph::EdgeIt EdgeIt; \ |
135 | 136 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
136 | 137 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
137 | 138 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
138 | 139 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
139 | 140 |
|
140 | 141 |
/// \brief Function to count the items in a graph. |
141 | 142 |
/// |
142 | 143 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
143 | 144 |
/// The complexity of the function is linear because |
144 | 145 |
/// it iterates on all of the items. |
145 | 146 |
template <typename Graph, typename Item> |
146 | 147 |
inline int countItems(const Graph& g) { |
147 | 148 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
148 | 149 |
int num = 0; |
149 | 150 |
for (ItemIt it(g); it != INVALID; ++it) { |
150 | 151 |
++num; |
151 | 152 |
} |
152 | 153 |
return num; |
153 | 154 |
} |
154 | 155 |
|
155 | 156 |
// Node counting: |
156 | 157 |
|
157 | 158 |
namespace _core_bits { |
158 | 159 |
|
159 | 160 |
template <typename Graph, typename Enable = void> |
160 | 161 |
struct CountNodesSelector { |
161 | 162 |
static int count(const Graph &g) { |
162 | 163 |
return countItems<Graph, typename Graph::Node>(g); |
163 | 164 |
} |
164 | 165 |
}; |
165 | 166 |
|
166 | 167 |
template <typename Graph> |
167 | 168 |
struct CountNodesSelector< |
168 | 169 |
Graph, typename |
169 | 170 |
enable_if<typename Graph::NodeNumTag, void>::type> |
170 | 171 |
{ |
171 | 172 |
static int count(const Graph &g) { |
172 | 173 |
return g.nodeNum(); |
173 | 174 |
} |
174 | 175 |
}; |
175 | 176 |
} |
176 | 177 |
|
177 | 178 |
/// \brief Function to count the nodes in the graph. |
178 | 179 |
/// |
179 | 180 |
/// This function counts the nodes in the graph. |
180 | 181 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
181 | 182 |
/// graph structures it is specialized to run in <em>O</em>(1). |
182 | 183 |
/// |
183 | 184 |
/// \note If the graph contains a \c nodeNum() member function and a |
184 | 185 |
/// \c NodeNumTag tag then this function calls directly the member |
185 | 186 |
/// function to query the cardinality of the node set. |
186 | 187 |
template <typename Graph> |
187 | 188 |
inline int countNodes(const Graph& g) { |
188 | 189 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
189 | 190 |
} |
190 | 191 |
|
191 | 192 |
// Arc counting: |
192 | 193 |
|
193 | 194 |
namespace _core_bits { |
194 | 195 |
|
195 | 196 |
template <typename Graph, typename Enable = void> |
196 | 197 |
struct CountArcsSelector { |
197 | 198 |
static int count(const Graph &g) { |
198 | 199 |
return countItems<Graph, typename Graph::Arc>(g); |
199 | 200 |
} |
200 | 201 |
}; |
201 | 202 |
|
202 | 203 |
template <typename Graph> |
203 | 204 |
struct CountArcsSelector< |
204 | 205 |
Graph, |
205 | 206 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
206 | 207 |
{ |
207 | 208 |
static int count(const Graph &g) { |
208 | 209 |
return g.arcNum(); |
209 | 210 |
} |
210 | 211 |
}; |
211 | 212 |
} |
212 | 213 |
|
213 | 214 |
/// \brief Function to count the arcs in the graph. |
214 | 215 |
/// |
215 | 216 |
/// This function counts the arcs in the graph. |
216 | 217 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
0 comments (0 inline)