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-2008
|
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 |
#include <lemon/config.h>
|
26 |
26 |
#include <lemon/bits/enable_if.h>
|
27 |
27 |
#include <lemon/bits/traits.h>
|
28 |
28 |
#include <lemon/assert.h>
|
29 |
29 |
|
30 |
30 |
///\file
|
31 |
31 |
///\brief LEMON core utilities.
|
32 |
32 |
///
|
33 |
33 |
///This header file contains core utilities for LEMON.
|
34 |
34 |
///It is automatically included by all graph types, therefore it usually
|
35 |
35 |
///do not have to be included directly.
|
36 |
36 |
|
37 |
37 |
namespace lemon {
|
38 |
38 |
|
39 |
39 |
/// \brief Dummy type to make it easier to create invalid iterators.
|
40 |
40 |
///
|
41 |
41 |
/// Dummy type to make it easier to create invalid iterators.
|
42 |
42 |
/// See \ref INVALID for the usage.
|
43 |
43 |
struct Invalid {
|
44 |
44 |
public:
|
45 |
45 |
bool operator==(Invalid) { return true; }
|
46 |
46 |
bool operator!=(Invalid) { return false; }
|
47 |
47 |
bool operator< (Invalid) { return false; }
|
48 |
48 |
};
|
49 |
49 |
|
50 |
50 |
/// \brief Invalid iterators.
|
51 |
51 |
///
|
52 |
52 |
/// \ref Invalid is a global type that converts to each iterator
|
53 |
53 |
/// in such a way that the value of the target iterator will be invalid.
|
54 |
54 |
#ifdef LEMON_ONLY_TEMPLATES
|
55 |
55 |
const Invalid INVALID = Invalid();
|
56 |
56 |
#else
|
57 |
57 |
extern const Invalid INVALID;
|
58 |
58 |
#endif
|
59 |
59 |
|
60 |
60 |
/// \addtogroup gutils
|
61 |
61 |
/// @{
|
62 |
62 |
|
63 |
63 |
///Create convenience typedefs for the digraph types and iterators
|
64 |
64 |
|
65 |
65 |
///This \c \#define creates convenient type definitions for the following
|
66 |
66 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
|
67 |
67 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
|
68 |
68 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
|
69 |
69 |
///
|
70 |
70 |
///\note If the graph type is a dependent type, ie. the graph type depend
|
71 |
71 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
|
72 |
72 |
///macro.
|
73 |
73 |
#define DIGRAPH_TYPEDEFS(Digraph) \
|
74 |
74 |
typedef Digraph::Node Node; \
|
75 |
75 |
typedef Digraph::NodeIt NodeIt; \
|
76 |
76 |
typedef Digraph::Arc Arc; \
|
77 |
77 |
typedef Digraph::ArcIt ArcIt; \
|
78 |
78 |
typedef Digraph::InArcIt InArcIt; \
|
79 |
79 |
typedef Digraph::OutArcIt OutArcIt; \
|
80 |
80 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \
|
81 |
81 |
typedef Digraph::NodeMap<int> IntNodeMap; \
|
82 |
82 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \
|
83 |
83 |
typedef Digraph::ArcMap<bool> BoolArcMap; \
|
84 |
84 |
typedef Digraph::ArcMap<int> IntArcMap; \
|
85 |
85 |
typedef Digraph::ArcMap<double> DoubleArcMap
|
86 |
86 |
|
87 |
87 |
///Create convenience typedefs for the digraph types and iterators
|
88 |
88 |
|
89 |
89 |
///\see DIGRAPH_TYPEDEFS
|
90 |
90 |
///
|
91 |
91 |
///\note Use this macro, if the graph type is a dependent type,
|
92 |
92 |
///ie. the graph type depend on a template parameter.
|
93 |
93 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
|
94 |
94 |
typedef typename Digraph::Node Node; \
|
95 |
95 |
typedef typename Digraph::NodeIt NodeIt; \
|
96 |
96 |
typedef typename Digraph::Arc Arc; \
|
97 |
97 |
typedef typename Digraph::ArcIt ArcIt; \
|
98 |
98 |
typedef typename Digraph::InArcIt InArcIt; \
|
99 |
99 |
typedef typename Digraph::OutArcIt OutArcIt; \
|
100 |
100 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
|
101 |
101 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \
|
102 |
102 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
|
103 |
103 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
|
104 |
104 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \
|
105 |
105 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap
|
106 |
106 |
|
107 |
107 |
///Create convenience typedefs for the graph types and iterators
|
108 |
108 |
|
109 |
109 |
///This \c \#define creates the same convenient type definitions as defined
|
110 |
110 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
|
111 |
111 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
|
112 |
112 |
///\c DoubleEdgeMap.
|
113 |
113 |
///
|
114 |
114 |
///\note If the graph type is a dependent type, ie. the graph type depend
|
115 |
115 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
|
116 |
116 |
///macro.
|
117 |
117 |
#define GRAPH_TYPEDEFS(Graph) \
|
118 |
118 |
DIGRAPH_TYPEDEFS(Graph); \
|
119 |
119 |
typedef Graph::Edge Edge; \
|
120 |
120 |
typedef Graph::EdgeIt EdgeIt; \
|
121 |
121 |
typedef Graph::IncEdgeIt IncEdgeIt; \
|
122 |
122 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \
|
123 |
123 |
typedef Graph::EdgeMap<int> IntEdgeMap; \
|
124 |
124 |
typedef Graph::EdgeMap<double> DoubleEdgeMap
|
125 |
125 |
|
126 |
126 |
///Create convenience typedefs for the graph types and iterators
|
127 |
127 |
|
128 |
128 |
///\see GRAPH_TYPEDEFS
|
129 |
129 |
///
|
130 |
130 |
///\note Use this macro, if the graph type is a dependent type,
|
131 |
131 |
///ie. the graph type depend on a template parameter.
|
132 |
132 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
|
133 |
133 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
|
134 |
134 |
typedef typename Graph::Edge Edge; \
|
135 |
135 |
typedef typename Graph::EdgeIt EdgeIt; \
|
136 |
136 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \
|
137 |
137 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
|
138 |
138 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
|
139 |
139 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
|
140 |
140 |
|
141 |
141 |
/// \brief Function to count the items in a graph.
|
142 |
142 |
///
|
143 |
143 |
/// This function counts the items (nodes, arcs etc.) in a graph.
|
144 |
144 |
/// The complexity of the function is linear because
|
145 |
145 |
/// it iterates on all of the items.
|
146 |
146 |
template <typename Graph, typename Item>
|
147 |
147 |
inline int countItems(const Graph& g) {
|
148 |
148 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
149 |
149 |
int num = 0;
|
150 |
150 |
for (ItemIt it(g); it != INVALID; ++it) {
|
151 |
151 |
++num;
|
152 |
152 |
}
|
153 |
153 |
return num;
|
154 |
154 |
}
|
155 |
155 |
|
156 |
156 |
// Node counting:
|
157 |
157 |
|
158 |
158 |
namespace _core_bits {
|
159 |
159 |
|
160 |
160 |
template <typename Graph, typename Enable = void>
|
161 |
161 |
struct CountNodesSelector {
|
162 |
162 |
static int count(const Graph &g) {
|
163 |
163 |
return countItems<Graph, typename Graph::Node>(g);
|
164 |
164 |
}
|
165 |
165 |
};
|
166 |
166 |
|
167 |
167 |
template <typename Graph>
|
168 |
168 |
struct CountNodesSelector<
|
169 |
169 |
Graph, typename
|
170 |
170 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
171 |
171 |
{
|
172 |
172 |
static int count(const Graph &g) {
|
173 |
173 |
return g.nodeNum();
|
174 |
174 |
}
|
175 |
175 |
};
|
176 |
176 |
}
|
177 |
177 |
|
178 |
178 |
/// \brief Function to count the nodes in the graph.
|
179 |
179 |
///
|
180 |
180 |
/// This function counts the nodes in the graph.
|
181 |
181 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some
|
182 |
182 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
183 |
183 |
///
|
184 |
184 |
/// \note If the graph contains a \c nodeNum() member function and a
|
185 |
185 |
/// \c NodeNumTag tag then this function calls directly the member
|
186 |
186 |
/// function to query the cardinality of the node set.
|
187 |
187 |
template <typename Graph>
|
188 |
188 |
inline int countNodes(const Graph& g) {
|
189 |
189 |
return _core_bits::CountNodesSelector<Graph>::count(g);
|
190 |
190 |
}
|
191 |
191 |
|
192 |
192 |
// Arc counting:
|
193 |
193 |
|
194 |
194 |
namespace _core_bits {
|
195 |
195 |
|
196 |
196 |
template <typename Graph, typename Enable = void>
|
197 |
197 |
struct CountArcsSelector {
|
198 |
198 |
static int count(const Graph &g) {
|
199 |
199 |
return countItems<Graph, typename Graph::Arc>(g);
|
200 |
200 |
}
|
201 |
201 |
};
|
202 |
202 |
|
203 |
203 |
template <typename Graph>
|
204 |
204 |
struct CountArcsSelector<
|
205 |
205 |
Graph,
|
206 |
206 |
typename enable_if<typename Graph::ArcNumTag, void>::type>
|
207 |
207 |
{
|
208 |
208 |
static int count(const Graph &g) {
|
209 |
209 |
return g.arcNum();
|
210 |
210 |
}
|
211 |
211 |
};
|
212 |
212 |
}
|
213 |
213 |
|
214 |
214 |
/// \brief Function to count the arcs in the graph.
|
215 |
215 |
///
|
216 |
216 |
/// This function counts the arcs in the graph.
|
217 |
217 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some
|
218 |
218 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
219 |
219 |
///
|
220 |
220 |
/// \note If the graph contains a \c arcNum() member function and a
|
221 |
221 |
/// \c ArcNumTag tag then this function calls directly the member
|
222 |
222 |
/// function to query the cardinality of the arc set.
|
223 |
223 |
template <typename Graph>
|
224 |
224 |
inline int countArcs(const Graph& g) {
|
225 |
225 |
return _core_bits::CountArcsSelector<Graph>::count(g);
|
226 |
226 |
}
|
227 |
227 |
|
228 |
228 |
// Edge counting:
|
229 |
229 |
|
230 |
230 |
namespace _core_bits {
|
231 |
231 |
|
232 |
232 |
template <typename Graph, typename Enable = void>
|
233 |
233 |
struct CountEdgesSelector {
|
234 |
234 |
static int count(const Graph &g) {
|
235 |
235 |
return countItems<Graph, typename Graph::Edge>(g);
|
236 |
236 |
}
|
237 |
237 |
};
|
238 |
238 |
|
239 |
239 |
template <typename Graph>
|
240 |
240 |
struct CountEdgesSelector<
|
241 |
241 |
Graph,
|
242 |
242 |
typename enable_if<typename Graph::EdgeNumTag, void>::type>
|
243 |
243 |
{
|
244 |
244 |
static int count(const Graph &g) {
|
245 |
245 |
return g.edgeNum();
|
246 |
246 |
}
|
247 |
247 |
};
|
248 |
248 |
}
|
249 |
249 |
|
250 |
250 |
/// \brief Function to count the edges in the graph.
|
251 |
251 |
///
|
252 |
252 |
/// This function counts the edges in the graph.
|
253 |
253 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some
|
254 |
254 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
255 |
255 |
///
|
256 |
256 |
/// \note If the graph contains a \c edgeNum() member function and a
|
257 |
257 |
/// \c EdgeNumTag tag then this function calls directly the member
|
258 |
258 |
/// function to query the cardinality of the edge set.
|
259 |
259 |
template <typename Graph>
|
260 |
260 |
inline int countEdges(const Graph& g) {
|
261 |
261 |
return _core_bits::CountEdgesSelector<Graph>::count(g);
|
262 |
262 |
|
263 |
263 |
}
|
264 |
264 |
|
265 |
265 |
|
266 |
266 |
template <typename Graph, typename DegIt>
|
267 |
267 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
268 |
268 |
int num = 0;
|
269 |
269 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
270 |
270 |
++num;
|
271 |
271 |
}
|
272 |
272 |
return num;
|
273 |
273 |
}
|
274 |
274 |
|
275 |
275 |
/// \brief Function to count the number of the out-arcs from node \c n.
|
276 |
276 |
///
|
277 |
277 |
/// This function counts the number of the out-arcs from node \c n
|
278 |
278 |
/// in the graph \c g.
|
279 |
279 |
template <typename Graph>
|
280 |
280 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
|
281 |
281 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n);
|