| 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 |
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 |
// Disable the following warnings when compiling with MSVC:
|
|
31 |
// C4250: 'class1' : inherits 'class2::member' via dominance
|
|
32 |
// C4355: 'this' : used in base member initializer list
|
|
33 |
// C4503: 'function' : decorated name length exceeded, name was truncated
|
|
34 |
// C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning)
|
|
35 |
// C4996: 'function': was declared deprecated
|
|
36 |
#ifdef _MSC_VER
|
|
37 |
#pragma warning( disable : 4250 4355 4503 4800 4996 )
|
|
38 |
#endif
|
|
39 |
|
| 30 |
40 |
///\file
|
| 31 |
41 |
///\brief LEMON core utilities.
|
| 32 |
42 |
///
|
| 33 |
43 |
///This header file contains core utilities for LEMON.
|
| 34 |
44 |
///It is automatically included by all graph types, therefore it usually
|
| 35 |
45 |
///do not have to be included directly.
|
| 36 |
46 |
|
| 37 |
47 |
namespace lemon {
|
| 38 |
48 |
|
| 39 |
49 |
/// \brief Dummy type to make it easier to create invalid iterators.
|
| 40 |
50 |
///
|
| 41 |
51 |
/// Dummy type to make it easier to create invalid iterators.
|
| 42 |
52 |
/// See \ref INVALID for the usage.
|
| 43 |
53 |
struct Invalid {
|
| 44 |
54 |
public:
|
| 45 |
55 |
bool operator==(Invalid) { return true; }
|
| 46 |
56 |
bool operator!=(Invalid) { return false; }
|
| 47 |
57 |
bool operator< (Invalid) { return false; }
|
| 48 |
58 |
};
|
| 49 |
59 |
|
| 50 |
60 |
/// \brief Invalid iterators.
|
| 51 |
61 |
///
|
| 52 |
62 |
/// \ref Invalid is a global type that converts to each iterator
|
| 53 |
63 |
/// in such a way that the value of the target iterator will be invalid.
|
| 54 |
64 |
#ifdef LEMON_ONLY_TEMPLATES
|
| 55 |
65 |
const Invalid INVALID = Invalid();
|
| 56 |
66 |
#else
|
| 57 |
67 |
extern const Invalid INVALID;
|
| 58 |
68 |
#endif
|
| 59 |
69 |
|
| 60 |
70 |
/// \addtogroup gutils
|
| 61 |
71 |
/// @{
|
| 62 |
72 |
|
| 63 |
73 |
///Create convenience typedefs for the digraph types and iterators
|
| 64 |
74 |
|
| 65 |
75 |
///This \c \#define creates convenient type definitions for the following
|
| 66 |
76 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
|
| 67 |
77 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap,
|
| 68 |
78 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap.
|
| 69 |
79 |
///
|
| 70 |
80 |
///\note If the graph type is a dependent type, ie. the graph type depend
|
| 71 |
81 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS()
|
| 72 |
82 |
///macro.
|
| 73 |
83 |
#define DIGRAPH_TYPEDEFS(Digraph) \
|
| 74 |
84 |
typedef Digraph::Node Node; \
|
| 75 |
85 |
typedef Digraph::NodeIt NodeIt; \
|
| 76 |
86 |
typedef Digraph::Arc Arc; \
|
| 77 |
87 |
typedef Digraph::ArcIt ArcIt; \
|
| 78 |
88 |
typedef Digraph::InArcIt InArcIt; \
|
| 79 |
89 |
typedef Digraph::OutArcIt OutArcIt; \
|
| 80 |
90 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \
|
| 81 |
91 |
typedef Digraph::NodeMap<int> IntNodeMap; \
|
| 82 |
92 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \
|
| 83 |
93 |
typedef Digraph::ArcMap<bool> BoolArcMap; \
|
| 84 |
94 |
typedef Digraph::ArcMap<int> IntArcMap; \
|
| 85 |
95 |
typedef Digraph::ArcMap<double> DoubleArcMap
|
| 86 |
96 |
|
| 87 |
97 |
///Create convenience typedefs for the digraph types and iterators
|
| 88 |
98 |
|
| 89 |
99 |
///\see DIGRAPH_TYPEDEFS
|
| 90 |
100 |
///
|
| 91 |
101 |
///\note Use this macro, if the graph type is a dependent type,
|
| 92 |
102 |
///ie. the graph type depend on a template parameter.
|
| 93 |
103 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \
|
| 94 |
104 |
typedef typename Digraph::Node Node; \
|
| 95 |
105 |
typedef typename Digraph::NodeIt NodeIt; \
|
| 96 |
106 |
typedef typename Digraph::Arc Arc; \
|
| 97 |
107 |
typedef typename Digraph::ArcIt ArcIt; \
|
| 98 |
108 |
typedef typename Digraph::InArcIt InArcIt; \
|
| 99 |
109 |
typedef typename Digraph::OutArcIt OutArcIt; \
|
| 100 |
110 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \
|
| 101 |
111 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \
|
| 102 |
112 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \
|
| 103 |
113 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \
|
| 104 |
114 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \
|
| 105 |
115 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap
|
| 106 |
116 |
|
| 107 |
117 |
///Create convenience typedefs for the graph types and iterators
|
| 108 |
118 |
|
| 109 |
119 |
///This \c \#define creates the same convenient type definitions as defined
|
| 110 |
120 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
|
| 111 |
121 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
|
| 112 |
122 |
///\c DoubleEdgeMap.
|
| 113 |
123 |
///
|
| 114 |
124 |
///\note If the graph type is a dependent type, ie. the graph type depend
|
| 115 |
125 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS()
|
| 116 |
126 |
///macro.
|
| 117 |
127 |
#define GRAPH_TYPEDEFS(Graph) \
|
| 118 |
128 |
DIGRAPH_TYPEDEFS(Graph); \
|
| 119 |
129 |
typedef Graph::Edge Edge; \
|
| 120 |
130 |
typedef Graph::EdgeIt EdgeIt; \
|
| 121 |
131 |
typedef Graph::IncEdgeIt IncEdgeIt; \
|
| 122 |
132 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \
|
| 123 |
133 |
typedef Graph::EdgeMap<int> IntEdgeMap; \
|
| 124 |
134 |
typedef Graph::EdgeMap<double> DoubleEdgeMap
|
| 125 |
135 |
|
| 126 |
136 |
///Create convenience typedefs for the graph types and iterators
|
| 127 |
137 |
|
| 128 |
138 |
///\see GRAPH_TYPEDEFS
|
| 129 |
139 |
///
|
| 130 |
140 |
///\note Use this macro, if the graph type is a dependent type,
|
| 131 |
141 |
///ie. the graph type depend on a template parameter.
|
| 132 |
142 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \
|
| 133 |
143 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \
|
| 134 |
144 |
typedef typename Graph::Edge Edge; \
|
| 135 |
145 |
typedef typename Graph::EdgeIt EdgeIt; \
|
| 136 |
146 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \
|
| 137 |
147 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \
|
| 138 |
148 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \
|
| 139 |
149 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap
|
| 140 |
150 |
|
| 141 |
151 |
/// \brief Function to count the items in a graph.
|
| 142 |
152 |
///
|
| 143 |
153 |
/// This function counts the items (nodes, arcs etc.) in a graph.
|
| 144 |
154 |
/// The complexity of the function is linear because
|
| 145 |
155 |
/// it iterates on all of the items.
|
| 146 |
156 |
template <typename Graph, typename Item>
|
| 147 |
157 |
inline int countItems(const Graph& g) {
|
| 148 |
158 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
|
| 149 |
159 |
int num = 0;
|
| 150 |
160 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 151 |
161 |
++num;
|
| 152 |
162 |
}
|
| 153 |
163 |
return num;
|
| 154 |
164 |
}
|
| 155 |
165 |
|
| 156 |
166 |
// Node counting:
|
| 157 |
167 |
|
| 158 |
168 |
namespace _core_bits {
|
| 159 |
169 |
|
| 160 |
170 |
template <typename Graph, typename Enable = void>
|
| 161 |
171 |
struct CountNodesSelector {
|
| 162 |
172 |
static int count(const Graph &g) {
|
| 163 |
173 |
return countItems<Graph, typename Graph::Node>(g);
|
| 164 |
174 |
}
|
| 165 |
175 |
};
|
| 166 |
176 |
|
| 167 |
177 |
template <typename Graph>
|
| 168 |
178 |
struct CountNodesSelector<
|
| 169 |
179 |
Graph, typename
|
| 170 |
180 |
enable_if<typename Graph::NodeNumTag, void>::type>
|
| 171 |
181 |
{
|
| 172 |
182 |
static int count(const Graph &g) {
|
| 173 |
183 |
return g.nodeNum();
|
| 174 |
184 |
}
|
| 175 |
185 |
};
|
| 176 |
186 |
}
|
| 177 |
187 |
|
| 178 |
188 |
/// \brief Function to count the nodes in the graph.
|
| 179 |
189 |
///
|
| 180 |
190 |
/// This function counts the nodes in the graph.
|
| 181 |
191 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some
|
| 182 |
192 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
| 183 |
193 |
///
|
| 184 |
194 |
/// \note If the graph contains a \c nodeNum() member function and a
|
| 185 |
195 |
/// \c NodeNumTag tag then this function calls directly the member
|
| 186 |
196 |
/// function to query the cardinality of the node set.
|
| 187 |
197 |
template <typename Graph>
|
| 188 |
198 |
inline int countNodes(const Graph& g) {
|
| 189 |
199 |
return _core_bits::CountNodesSelector<Graph>::count(g);
|
| 190 |
200 |
}
|
| 191 |
201 |
|
| 192 |
202 |
// Arc counting:
|
| 193 |
203 |
|
| 194 |
204 |
namespace _core_bits {
|
| 195 |
205 |
|
| 196 |
206 |
template <typename Graph, typename Enable = void>
|
| 197 |
207 |
struct CountArcsSelector {
|
| 198 |
208 |
static int count(const Graph &g) {
|
| 199 |
209 |
return countItems<Graph, typename Graph::Arc>(g);
|
| 200 |
210 |
}
|
| 201 |
211 |
};
|
| 202 |
212 |
|
| 203 |
213 |
template <typename Graph>
|
| 204 |
214 |
struct CountArcsSelector<
|
| 205 |
215 |
Graph,
|
| 206 |
216 |
typename enable_if<typename Graph::ArcNumTag, void>::type>
|
| 207 |
217 |
{
|
| 208 |
218 |
static int count(const Graph &g) {
|
| 209 |
219 |
return g.arcNum();
|
| 210 |
220 |
}
|
| 211 |
221 |
};
|
| 212 |
222 |
}
|
| 213 |
223 |
|
| 214 |
224 |
/// \brief Function to count the arcs in the graph.
|
| 215 |
225 |
///
|
| 216 |
226 |
/// This function counts the arcs in the graph.
|
| 217 |
227 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some
|
| 218 |
228 |
/// graph structures it is specialized to run in <em>O</em>(1).
|
| 219 |
229 |
///
|
| 220 |
230 |
/// \note If the graph contains a \c arcNum() member function and a
|
| 221 |
231 |
/// \c ArcNumTag tag then this function calls directly the member
|