0
4
0
| 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 | 25 |
#include <lemon/bits/enable_if.h> |
| 26 | 26 |
#include <lemon/bits/traits.h> |
| 27 |
#include <lemon/assert.h> |
|
| 27 | 28 |
|
| 28 | 29 |
///\file |
| 29 | 30 |
///\brief LEMON core utilities. |
| 30 | 31 |
/// |
| 31 | 32 |
///This header file contains core utilities for LEMON. |
| 32 | 33 |
///It is automatically included by all graph types, therefore it usually |
| 33 | 34 |
///do not have to be included directly. |
| 34 | 35 |
|
| 35 | 36 |
namespace lemon {
|
| 36 | 37 |
|
| 37 | 38 |
/// \brief Dummy type to make it easier to create invalid iterators. |
| 38 | 39 |
/// |
| 39 | 40 |
/// Dummy type to make it easier to create invalid iterators. |
| 40 | 41 |
/// See \ref INVALID for the usage. |
| 41 | 42 |
struct Invalid {
|
| 42 | 43 |
public: |
| 43 | 44 |
bool operator==(Invalid) { return true; }
|
| 44 | 45 |
bool operator!=(Invalid) { return false; }
|
| 45 | 46 |
bool operator< (Invalid) { return false; }
|
| 46 | 47 |
}; |
| 47 | 48 |
|
| 48 | 49 |
/// \brief Invalid iterators. |
| 49 | 50 |
/// |
| 50 | 51 |
/// \ref Invalid is a global type that converts to each iterator |
| 51 | 52 |
/// in such a way that the value of the target iterator will be invalid. |
| 52 | 53 |
#ifdef LEMON_ONLY_TEMPLATES |
| 53 | 54 |
const Invalid INVALID = Invalid(); |
| 54 | 55 |
#else |
| 55 | 56 |
extern const Invalid INVALID; |
| 56 | 57 |
#endif |
| 57 | 58 |
|
| 58 | 59 |
/// \addtogroup gutils |
| 59 | 60 |
/// @{
|
| 60 | 61 |
|
| 61 | 62 |
///Create convenience typedefs for the digraph types and iterators |
| 62 | 63 |
|
| 63 | 64 |
///This \c \#define creates convenient type definitions for the following |
| 64 | 65 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
| 65 | 66 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
| 66 | 67 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
| 67 | 68 |
/// |
| 68 | 69 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 69 | 70 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
| 70 | 71 |
///macro. |
| 71 | 72 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
| 72 | 73 |
typedef Digraph::Node Node; \ |
| 73 | 74 |
typedef Digraph::NodeIt NodeIt; \ |
| 74 | 75 |
typedef Digraph::Arc Arc; \ |
| 75 | 76 |
typedef Digraph::ArcIt ArcIt; \ |
| 76 | 77 |
typedef Digraph::InArcIt InArcIt; \ |
| 77 | 78 |
typedef Digraph::OutArcIt OutArcIt; \ |
| 78 | 79 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
| 79 | 80 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
| 80 | 81 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
| 81 | 82 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
| 82 | 83 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
| 83 | 84 |
typedef Digraph::ArcMap<double> DoubleArcMap |
| 84 | 85 |
|
| 85 | 86 |
///Create convenience typedefs for the digraph types and iterators |
| 86 | 87 |
|
| 87 | 88 |
///\see DIGRAPH_TYPEDEFS |
| 88 | 89 |
/// |
| 89 | 90 |
///\note Use this macro, if the graph type is a dependent type, |
| 90 | 91 |
///ie. the graph type depend on a template parameter. |
| 91 | 92 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
| 92 | 93 |
typedef typename Digraph::Node Node; \ |
| 93 | 94 |
typedef typename Digraph::NodeIt NodeIt; \ |
| 94 | 95 |
typedef typename Digraph::Arc Arc; \ |
| 95 | 96 |
typedef typename Digraph::ArcIt ArcIt; \ |
| 96 | 97 |
typedef typename Digraph::InArcIt InArcIt; \ |
| 97 | 98 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
| 98 | 99 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
| 99 | 100 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
| 100 | 101 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
| 101 | 102 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
| 102 | 103 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
| 103 | 104 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
| 104 | 105 |
|
| 105 | 106 |
///Create convenience typedefs for the graph types and iterators |
| 106 | 107 |
|
| 107 | 108 |
///This \c \#define creates the same convenient type definitions as defined |
| 108 | 109 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
| 109 | 110 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
| 110 | 111 |
///\c DoubleEdgeMap. |
| 111 | 112 |
/// |
| 112 | 113 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 113 | 114 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
| 114 | 115 |
///macro. |
| 115 | 116 |
#define GRAPH_TYPEDEFS(Graph) \ |
| 116 | 117 |
DIGRAPH_TYPEDEFS(Graph); \ |
| 117 | 118 |
typedef Graph::Edge Edge; \ |
| 118 | 119 |
typedef Graph::EdgeIt EdgeIt; \ |
| 119 | 120 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
| 120 | 121 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
| 121 | 122 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
| 122 | 123 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
| 123 | 124 |
|
| 124 | 125 |
///Create convenience typedefs for the graph types and iterators |
| 125 | 126 |
|
| 126 | 127 |
///\see GRAPH_TYPEDEFS |
| 127 | 128 |
/// |
| 128 | 129 |
///\note Use this macro, if the graph type is a dependent type, |
| 129 | 130 |
///ie. the graph type depend on a template parameter. |
| 130 | 131 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
| 131 | 132 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
| 132 | 133 |
typedef typename Graph::Edge Edge; \ |
| 133 | 134 |
typedef typename Graph::EdgeIt EdgeIt; \ |
| 134 | 135 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
| 135 | 136 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
| 136 | 137 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
| 137 | 138 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
| 138 | 139 |
|
| 139 | 140 |
/// \brief Function to count the items in a graph. |
| 140 | 141 |
/// |
| 141 | 142 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
| 142 | 143 |
/// The complexity of the function is linear because |
| 143 | 144 |
/// it iterates on all of the items. |
| 144 | 145 |
template <typename Graph, typename Item> |
| 145 | 146 |
inline int countItems(const Graph& g) {
|
| 146 | 147 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
| 147 | 148 |
int num = 0; |
| 148 | 149 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 149 | 150 |
++num; |
| 150 | 151 |
} |
| 151 | 152 |
return num; |
| 152 | 153 |
} |
| 153 | 154 |
|
| 154 | 155 |
// Node counting: |
| 155 | 156 |
|
| 156 | 157 |
namespace _core_bits {
|
| 157 | 158 |
|
| 158 | 159 |
template <typename Graph, typename Enable = void> |
| 159 | 160 |
struct CountNodesSelector {
|
| 160 | 161 |
static int count(const Graph &g) {
|
| 161 | 162 |
return countItems<Graph, typename Graph::Node>(g); |
| 162 | 163 |
} |
| 163 | 164 |
}; |
| 164 | 165 |
|
| 165 | 166 |
template <typename Graph> |
| 166 | 167 |
struct CountNodesSelector< |
| 167 | 168 |
Graph, typename |
| 168 | 169 |
enable_if<typename Graph::NodeNumTag, void>::type> |
| 169 | 170 |
{
|
| 170 | 171 |
static int count(const Graph &g) {
|
| 171 | 172 |
return g.nodeNum(); |
| 172 | 173 |
} |
| 173 | 174 |
}; |
| 174 | 175 |
} |
| 175 | 176 |
|
| 176 | 177 |
/// \brief Function to count the nodes in the graph. |
| 177 | 178 |
/// |
| 178 | 179 |
/// This function counts the nodes in the graph. |
| 179 | 180 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
| 180 | 181 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 181 | 182 |
/// |
| 182 | 183 |
/// \note If the graph contains a \c nodeNum() member function and a |
| 183 | 184 |
/// \c NodeNumTag tag then this function calls directly the member |
| 184 | 185 |
/// function to query the cardinality of the node set. |
| 185 | 186 |
template <typename Graph> |
| 186 | 187 |
inline int countNodes(const Graph& g) {
|
| 187 | 188 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
| 188 | 189 |
} |
| 189 | 190 |
|
| 190 | 191 |
// Arc counting: |
| 191 | 192 |
|
| 192 | 193 |
namespace _core_bits {
|
| 193 | 194 |
|
| 194 | 195 |
template <typename Graph, typename Enable = void> |
| 195 | 196 |
struct CountArcsSelector {
|
| 196 | 197 |
static int count(const Graph &g) {
|
| 197 | 198 |
return countItems<Graph, typename Graph::Arc>(g); |
| 198 | 199 |
} |
| 199 | 200 |
}; |
| 200 | 201 |
|
| 201 | 202 |
template <typename Graph> |
| 202 | 203 |
struct CountArcsSelector< |
| 203 | 204 |
Graph, |
| 204 | 205 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
| 205 | 206 |
{
|
| 206 | 207 |
static int count(const Graph &g) {
|
| 207 | 208 |
return g.arcNum(); |
| 208 | 209 |
} |
| 209 | 210 |
}; |
| 210 | 211 |
} |
| 211 | 212 |
|
| 212 | 213 |
/// \brief Function to count the arcs in the graph. |
| 213 | 214 |
/// |
| 214 | 215 |
/// This function counts the arcs in the graph. |
| 215 | 216 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
| 216 | 217 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 217 | 218 |
/// |
| 218 | 219 |
/// \note If the graph contains a \c arcNum() member function and a |
| 219 | 220 |
/// \c ArcNumTag tag then this function calls directly the member |
| 220 | 221 |
/// function to query the cardinality of the arc set. |
| 221 | 222 |
template <typename Graph> |
| 222 | 223 |
inline int countArcs(const Graph& g) {
|
| 223 | 224 |
return _core_bits::CountArcsSelector<Graph>::count(g); |
| 224 | 225 |
} |
| 225 | 226 |
|
| 226 | 227 |
// Edge counting: |
| 227 | 228 |
|
| 228 | 229 |
namespace _core_bits {
|
| 229 | 230 |
|
| 230 | 231 |
template <typename Graph, typename Enable = void> |
| 231 | 232 |
struct CountEdgesSelector {
|
| 232 | 233 |
static int count(const Graph &g) {
|
| 233 | 234 |
return countItems<Graph, typename Graph::Edge>(g); |
| 234 | 235 |
} |
| 235 | 236 |
}; |
| 236 | 237 |
|
| 237 | 238 |
template <typename Graph> |
| 238 | 239 |
struct CountEdgesSelector< |
| 239 | 240 |
Graph, |
| 240 | 241 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
| 241 | 242 |
{
|
| 242 | 243 |
static int count(const Graph &g) {
|
| 243 | 244 |
return g.edgeNum(); |
| 244 | 245 |
} |
| 245 | 246 |
}; |
| 246 | 247 |
} |
| 247 | 248 |
|
| 248 | 249 |
/// \brief Function to count the edges in the graph. |
| 249 | 250 |
/// |
| 250 | 251 |
/// This function counts the edges in the graph. |
| 251 | 252 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
| 252 | 253 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 253 | 254 |
/// |
| 254 | 255 |
/// \note If the graph contains a \c edgeNum() member function and a |
| 255 | 256 |
/// \c EdgeNumTag tag then this function calls directly the member |
| 256 | 257 |
/// function to query the cardinality of the edge set. |
| 257 | 258 |
template <typename Graph> |
| 258 | 259 |
inline int countEdges(const Graph& g) {
|
| 259 | 260 |
return _core_bits::CountEdgesSelector<Graph>::count(g); |
| 260 | 261 |
|
| 261 | 262 |
} |
| 262 | 263 |
|
| 263 | 264 |
|
| 264 | 265 |
template <typename Graph, typename DegIt> |
| 265 | 266 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
|
| 266 | 267 |
int num = 0; |
| 267 | 268 |
for (DegIt it(_g, _n); it != INVALID; ++it) {
|
| 268 | 269 |
++num; |
| 269 | 270 |
} |
| 270 | 271 |
return num; |
| 271 | 272 |
} |
| 272 | 273 |
|
| 273 | 274 |
/// \brief Function to count the number of the out-arcs from node \c n. |
| 274 | 275 |
/// |
| 275 | 276 |
/// This function counts the number of the out-arcs from node \c n |
| 276 | 277 |
/// in the graph \c g. |
| 277 | 278 |
template <typename Graph> |
| 278 | 279 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) {
|
| 279 | 280 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n); |
| 280 | 281 |
} |
| 281 | 282 |
|
| 282 | 283 |
/// \brief Function to count the number of the in-arcs to node \c n. |
| 283 | 284 |
/// |
| 284 | 285 |
/// This function counts the number of the in-arcs to node \c n |
| 285 | 286 |
/// in the graph \c g. |
| 286 | 287 |
template <typename Graph> |
| 287 | 288 |
inline int countInArcs(const Graph& g, const typename Graph::Node& n) {
|
| 288 | 289 |
return countNodeDegree<Graph, typename Graph::InArcIt>(g, n); |
| 289 | 290 |
} |
| 290 | 291 |
|
| 291 | 292 |
/// \brief Function to count the number of the inc-edges to node \c n. |
| 292 | 293 |
/// |
| 293 | 294 |
/// This function counts the number of the inc-edges to node \c n |
| 294 | 295 |
/// in the undirected graph \c g. |
| 295 | 296 |
template <typename Graph> |
| 296 | 297 |
inline int countIncEdges(const Graph& g, const typename Graph::Node& n) {
|
| 297 | 298 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n); |
| 298 | 299 |
} |
| 299 | 300 |
|
| 300 | 301 |
namespace _core_bits {
|
| 301 | 302 |
|
| 302 | 303 |
template <typename Digraph, typename Item, typename RefMap> |
| 303 | 304 |
class MapCopyBase {
|
| 304 | 305 |
public: |
| 305 | 306 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
| 306 | 307 |
|
| 307 | 308 |
virtual ~MapCopyBase() {}
|
| 308 | 309 |
}; |
| 309 | 310 |
|
| 310 | 311 |
template <typename Digraph, typename Item, typename RefMap, |
| 311 | 312 |
typename FromMap, typename ToMap> |
| 312 | 313 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 313 | 314 |
public: |
| 314 | 315 |
|
| 315 | 316 |
MapCopy(const FromMap& map, ToMap& tmap) |
| 316 | 317 |
: _map(map), _tmap(tmap) {}
|
| 317 | 318 |
|
| 318 | 319 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 319 | 320 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 320 | 321 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 321 | 322 |
_tmap.set(refMap[it], _map[it]); |
| 322 | 323 |
} |
| 323 | 324 |
} |
| 324 | 325 |
|
| 325 | 326 |
private: |
| 326 | 327 |
const FromMap& _map; |
| 327 | 328 |
ToMap& _tmap; |
| 328 | 329 |
}; |
| 329 | 330 |
|
| 330 | 331 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
| 331 | 332 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 332 | 333 |
public: |
| 333 | 334 |
|
| 334 | 335 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {}
|
| 335 | 336 |
|
| 336 | 337 |
virtual void copy(const Digraph&, const RefMap& refMap) {
|
| 337 | 338 |
_it = refMap[_item]; |
| 338 | 339 |
} |
| 339 | 340 |
|
| 340 | 341 |
private: |
| 341 | 342 |
Item _item; |
| 342 | 343 |
It& _it; |
| 343 | 344 |
}; |
| 344 | 345 |
|
| 345 | 346 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
| 346 | 347 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 347 | 348 |
public: |
| 348 | 349 |
|
| 349 | 350 |
RefCopy(Ref& map) : _map(map) {}
|
| 350 | 351 |
|
| 351 | 352 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 352 | 353 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 353 | 354 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 354 | 355 |
_map.set(it, refMap[it]); |
| 355 | 356 |
} |
| 356 | 357 |
} |
| 357 | 358 |
|
| 358 | 359 |
private: |
| 359 | 360 |
Ref& _map; |
| 360 | 361 |
}; |
| 361 | 362 |
|
| 362 | 363 |
template <typename Digraph, typename Item, typename RefMap, |
| 363 | 364 |
typename CrossRef> |
| 364 | 365 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> {
|
| 365 | 366 |
public: |
| 366 | 367 |
|
| 367 | 368 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {}
|
| 368 | 369 |
|
| 369 | 370 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) {
|
| 370 | 371 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
| 371 | 372 |
for (ItemIt it(digraph); it != INVALID; ++it) {
|
| 372 | 373 |
_cmap.set(refMap[it], it); |
| 373 | 374 |
} |
| 374 | 375 |
} |
| 375 | 376 |
|
| 376 | 377 |
private: |
| 377 | 378 |
CrossRef& _cmap; |
| 378 | 379 |
}; |
| 379 | 380 |
|
| 380 | 381 |
template <typename Digraph, typename Enable = void> |
| 381 | 382 |
struct DigraphCopySelector {
|
| 382 | 383 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 383 | 384 |
static void copy(const From& from, Digraph &to, |
| 384 | 385 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 385 | 386 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 386 | 387 |
nodeRefMap[it] = to.addNode(); |
| 387 | 388 |
} |
| 388 | 389 |
for (typename From::ArcIt it(from); it != INVALID; ++it) {
|
| 389 | 390 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
| 390 | 391 |
nodeRefMap[from.target(it)]); |
| 391 | 392 |
} |
| 392 | 393 |
} |
| 393 | 394 |
}; |
| 394 | 395 |
|
| 395 | 396 |
template <typename Digraph> |
| 396 | 397 |
struct DigraphCopySelector< |
| 397 | 398 |
Digraph, |
| 398 | 399 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
| 399 | 400 |
{
|
| 400 | 401 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
| 401 | 402 |
static void copy(const From& from, Digraph &to, |
| 402 | 403 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) {
|
| 403 | 404 |
to.build(from, nodeRefMap, arcRefMap); |
| 404 | 405 |
} |
| 405 | 406 |
}; |
| 406 | 407 |
|
| 407 | 408 |
template <typename Graph, typename Enable = void> |
| 408 | 409 |
struct GraphCopySelector {
|
| 409 | 410 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
| 410 | 411 |
static void copy(const From& from, Graph &to, |
| 411 | 412 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 412 | 413 |
for (typename From::NodeIt it(from); it != INVALID; ++it) {
|
| 413 | 414 |
nodeRefMap[it] = to.addNode(); |
| 414 | 415 |
} |
| 415 | 416 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) {
|
| 416 | 417 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
| 417 | 418 |
nodeRefMap[from.v(it)]); |
| 418 | 419 |
} |
| 419 | 420 |
} |
| 420 | 421 |
}; |
| 421 | 422 |
|
| 422 | 423 |
template <typename Graph> |
| 423 | 424 |
struct GraphCopySelector< |
| 424 | 425 |
Graph, |
| 425 | 426 |
typename enable_if<typename Graph::BuildTag, void>::type> |
| 426 | 427 |
{
|
| 427 | 428 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
| 428 | 429 |
static void copy(const From& from, Graph &to, |
| 429 | 430 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) {
|
| 430 | 431 |
to.build(from, nodeRefMap, edgeRefMap); |
| 431 | 432 |
} |
| 432 | 433 |
}; |
| 433 | 434 |
|
| 434 | 435 |
} |
| 435 | 436 |
|
| 436 | 437 |
/// \brief Class to copy a digraph. |
| 437 | 438 |
/// |
| 438 | 439 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
| 439 | 440 |
/// simplest way of using it is through the \c digraphCopy() function. |
| 440 | 441 |
/// |
| 441 | 442 |
/// This class not only make a copy of a digraph, but it can create |
| 442 | 443 |
/// references and cross references between the nodes and arcs of |
| 443 | 444 |
/// the two digraphs, and it can copy maps to use with the newly created |
| 444 | 445 |
/// digraph. |
| 445 | 446 |
/// |
| 446 | 447 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
| 447 | 448 |
/// should be created, then the data belongs to the digraph should |
| 448 | 449 |
/// assigned to copy. In the end, the \c run() member should be |
| 449 | 450 |
/// called. |
| 450 | 451 |
/// |
| 451 | 452 |
/// The next code copies a digraph with several data: |
| 452 | 453 |
///\code |
| 453 | 454 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
| 454 | 455 |
/// // Create references for the nodes |
| 455 | 456 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
| 456 | 457 |
/// cg.nodeRef(nr); |
| 457 | 458 |
/// // Create cross references (inverse) for the arcs |
| 458 | 459 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
| 459 | 460 |
/// cg.arcCrossRef(acr); |
| 460 | 461 |
/// // Copy an arc map |
| 461 | 462 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
| 462 | 463 |
/// NewGraph::ArcMap<double> namap(new_graph); |
| 463 | 464 |
/// cg.arcMap(oamap, namap); |
| 464 | 465 |
/// // Copy a node |
| 465 | 466 |
/// OrigGraph::Node on; |
| 466 | 467 |
/// NewGraph::Node nn; |
| 467 | 468 |
/// cg.node(on, nn); |
| 468 | 469 |
/// // Execute copying |
| 469 | 470 |
/// cg.run(); |
| 470 | 471 |
///\endcode |
| 471 | 472 |
template <typename From, typename To> |
| 472 | 473 |
class DigraphCopy {
|
| 473 | 474 |
private: |
| 474 | 475 |
|
| 475 | 476 |
typedef typename From::Node Node; |
| 476 | 477 |
typedef typename From::NodeIt NodeIt; |
| 477 | 478 |
typedef typename From::Arc Arc; |
| 478 | 479 |
typedef typename From::ArcIt ArcIt; |
| 479 | 480 |
|
| 480 | 481 |
typedef typename To::Node TNode; |
| 481 | 482 |
typedef typename To::Arc TArc; |
| 482 | 483 |
|
| 483 | 484 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
| 484 | 485 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
| 485 | 486 |
|
| 486 | 487 |
public: |
| 487 | 488 |
|
| 488 | 489 |
/// \brief Constructor of DigraphCopy. |
| 489 | 490 |
/// |
| 490 | 491 |
/// Constructor of DigraphCopy for copying the content of the |
| 491 | 492 |
/// \c from digraph into the \c to digraph. |
| 492 | 493 |
DigraphCopy(const From& from, To& to) |
| 493 | 494 |
: _from(from), _to(to) {}
|
| 494 | 495 |
|
| 495 | 496 |
/// \brief Destructor of DigraphCopy |
| 496 | 497 |
/// |
| 497 | 498 |
/// Destructor of DigraphCopy. |
| 498 | 499 |
~DigraphCopy() {
|
| 499 | 500 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 500 | 501 |
delete _node_maps[i]; |
| 501 | 502 |
} |
| 502 | 503 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 503 | 504 |
delete _arc_maps[i]; |
| 504 | 505 |
} |
| 505 | 506 |
|
| 506 | 507 |
} |
| 507 | 508 |
|
| 508 | 509 |
/// \brief Copy the node references into the given map. |
| 509 | 510 |
/// |
| 510 | 511 |
/// This function copies the node references into the given map. |
| 511 | 512 |
/// The parameter should be a map, whose key type is the Node type of |
| 512 | 513 |
/// the source digraph, while the value type is the Node type of the |
| 513 | 514 |
/// destination digraph. |
| 514 | 515 |
template <typename NodeRef> |
| 515 | 516 |
DigraphCopy& nodeRef(NodeRef& map) {
|
| 516 | 517 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
| 517 | 518 |
NodeRefMap, NodeRef>(map)); |
| 518 | 519 |
return *this; |
| 519 | 520 |
} |
| 520 | 521 |
|
| 521 | 522 |
/// \brief Copy the node cross references into the given map. |
| 522 | 523 |
/// |
| 523 | 524 |
/// This function copies the node cross references (reverse references) |
| 524 | 525 |
/// into the given map. The parameter should be a map, whose key type |
| 525 | 526 |
/// is the Node type of the destination digraph, while the value type is |
| 526 | 527 |
/// the Node type of the source digraph. |
| 527 | 528 |
template <typename NodeCrossRef> |
| 528 | 529 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 529 | 530 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
| 530 | 531 |
NodeRefMap, NodeCrossRef>(map)); |
| 531 | 532 |
return *this; |
| 532 | 533 |
} |
| 533 | 534 |
|
| 534 | 535 |
/// \brief Make a copy of the given node map. |
| 535 | 536 |
/// |
| 536 | 537 |
/// This function makes a copy of the given node map for the newly |
| 537 | 538 |
/// created digraph. |
| 538 | 539 |
/// The key type of the new map \c tmap should be the Node type of the |
| 539 | 540 |
/// destination digraph, and the key type of the original map \c map |
| 540 | 541 |
/// should be the Node type of the source digraph. |
| 541 | 542 |
template <typename FromMap, typename ToMap> |
| 542 | 543 |
DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
|
| 543 | 544 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
| 544 | 545 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
| 545 | 546 |
return *this; |
| 546 | 547 |
} |
| 547 | 548 |
|
| 548 | 549 |
/// \brief Make a copy of the given node. |
| 549 | 550 |
/// |
| 550 | 551 |
/// This function makes a copy of the given node. |
| 551 | 552 |
DigraphCopy& node(const Node& node, TNode& tnode) {
|
| 552 | 553 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
| 553 | 554 |
NodeRefMap, TNode>(node, tnode)); |
| 554 | 555 |
return *this; |
| 555 | 556 |
} |
| 556 | 557 |
|
| 557 | 558 |
/// \brief Copy the arc references into the given map. |
| 558 | 559 |
/// |
| 559 | 560 |
/// This function copies the arc references into the given map. |
| 560 | 561 |
/// The parameter should be a map, whose key type is the Arc type of |
| 561 | 562 |
/// the source digraph, while the value type is the Arc type of the |
| 562 | 563 |
/// destination digraph. |
| 563 | 564 |
template <typename ArcRef> |
| 564 | 565 |
DigraphCopy& arcRef(ArcRef& map) {
|
| 565 | 566 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
| 566 | 567 |
ArcRefMap, ArcRef>(map)); |
| 567 | 568 |
return *this; |
| 568 | 569 |
} |
| 569 | 570 |
|
| 570 | 571 |
/// \brief Copy the arc cross references into the given map. |
| 571 | 572 |
/// |
| 572 | 573 |
/// This function copies the arc cross references (reverse references) |
| 573 | 574 |
/// into the given map. The parameter should be a map, whose key type |
| 574 | 575 |
/// is the Arc type of the destination digraph, while the value type is |
| 575 | 576 |
/// the Arc type of the source digraph. |
| 576 | 577 |
template <typename ArcCrossRef> |
| 577 | 578 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 578 | 579 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
| 579 | 580 |
ArcRefMap, ArcCrossRef>(map)); |
| 580 | 581 |
return *this; |
| 581 | 582 |
} |
| 582 | 583 |
|
| 583 | 584 |
/// \brief Make a copy of the given arc map. |
| 584 | 585 |
/// |
| 585 | 586 |
/// This function makes a copy of the given arc map for the newly |
| 586 | 587 |
/// created digraph. |
| 587 | 588 |
/// The key type of the new map \c tmap should be the Arc type of the |
| 588 | 589 |
/// destination digraph, and the key type of the original map \c map |
| 589 | 590 |
/// should be the Arc type of the source digraph. |
| 590 | 591 |
template <typename FromMap, typename ToMap> |
| 591 | 592 |
DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
|
| 592 | 593 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
| 593 | 594 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
| 594 | 595 |
return *this; |
| 595 | 596 |
} |
| 596 | 597 |
|
| 597 | 598 |
/// \brief Make a copy of the given arc. |
| 598 | 599 |
/// |
| 599 | 600 |
/// This function makes a copy of the given arc. |
| 600 | 601 |
DigraphCopy& arc(const Arc& arc, TArc& tarc) {
|
| 601 | 602 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
| 602 | 603 |
ArcRefMap, TArc>(arc, tarc)); |
| 603 | 604 |
return *this; |
| 604 | 605 |
} |
| 605 | 606 |
|
| 606 | 607 |
/// \brief Execute copying. |
| 607 | 608 |
/// |
| 608 | 609 |
/// This function executes the copying of the digraph along with the |
| 609 | 610 |
/// copying of the assigned data. |
| 610 | 611 |
void run() {
|
| 611 | 612 |
NodeRefMap nodeRefMap(_from); |
| 612 | 613 |
ArcRefMap arcRefMap(_from); |
| 613 | 614 |
_core_bits::DigraphCopySelector<To>:: |
| 614 | 615 |
copy(_from, _to, nodeRefMap, arcRefMap); |
| 615 | 616 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 616 | 617 |
_node_maps[i]->copy(_from, nodeRefMap); |
| 617 | 618 |
} |
| 618 | 619 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 619 | 620 |
_arc_maps[i]->copy(_from, arcRefMap); |
| 620 | 621 |
} |
| 621 | 622 |
} |
| 622 | 623 |
|
| 623 | 624 |
protected: |
| 624 | 625 |
|
| 625 | 626 |
const From& _from; |
| 626 | 627 |
To& _to; |
| 627 | 628 |
|
| 628 | 629 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
| 629 | 630 |
_node_maps; |
| 630 | 631 |
|
| 631 | 632 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
| 632 | 633 |
_arc_maps; |
| 633 | 634 |
|
| 634 | 635 |
}; |
| 635 | 636 |
|
| 636 | 637 |
/// \brief Copy a digraph to another digraph. |
| 637 | 638 |
/// |
| 638 | 639 |
/// This function copies a digraph to another digraph. |
| 639 | 640 |
/// The complete usage of it is detailed in the DigraphCopy class, but |
| 640 | 641 |
/// a short example shows a basic work: |
| 641 | 642 |
///\code |
| 642 | 643 |
/// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run(); |
| 643 | 644 |
///\endcode |
| 644 | 645 |
/// |
| 645 | 646 |
/// After the copy the \c nr map will contain the mapping from the |
| 646 | 647 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
| 647 | 648 |
/// \c acr will contain the mapping from the arcs of the \c to digraph |
| 648 | 649 |
/// to the arcs of the \c from digraph. |
| 649 | 650 |
/// |
| 650 | 651 |
/// \see DigraphCopy |
| 651 | 652 |
template <typename From, typename To> |
| 652 | 653 |
DigraphCopy<From, To> digraphCopy(const From& from, To& to) {
|
| 653 | 654 |
return DigraphCopy<From, To>(from, to); |
| 654 | 655 |
} |
| 655 | 656 |
|
| 656 | 657 |
/// \brief Class to copy a graph. |
| 657 | 658 |
/// |
| 658 | 659 |
/// Class to copy a graph to another graph (duplicate a graph). The |
| 659 | 660 |
/// simplest way of using it is through the \c graphCopy() function. |
| 660 | 661 |
/// |
| 661 | 662 |
/// This class not only make a copy of a graph, but it can create |
| 662 | 663 |
/// references and cross references between the nodes, edges and arcs of |
| 663 | 664 |
/// the two graphs, and it can copy maps for using with the newly created |
| 664 | 665 |
/// graph. |
| 665 | 666 |
/// |
| 666 | 667 |
/// To make a copy from a graph, first an instance of GraphCopy |
| 667 | 668 |
/// should be created, then the data belongs to the graph should |
| 668 | 669 |
/// assigned to copy. In the end, the \c run() member should be |
| 669 | 670 |
/// called. |
| 670 | 671 |
/// |
| 671 | 672 |
/// The next code copies a graph with several data: |
| 672 | 673 |
///\code |
| 673 | 674 |
/// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
| 674 | 675 |
/// // Create references for the nodes |
| 675 | 676 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
| 676 | 677 |
/// cg.nodeRef(nr); |
| 677 | 678 |
/// // Create cross references (inverse) for the edges |
| 678 | 679 |
/// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph); |
| 679 | 680 |
/// cg.edgeCrossRef(ecr); |
| 680 | 681 |
/// // Copy an edge map |
| 681 | 682 |
/// OrigGraph::EdgeMap<double> oemap(orig_graph); |
| 682 | 683 |
/// NewGraph::EdgeMap<double> nemap(new_graph); |
| 683 | 684 |
/// cg.edgeMap(oemap, nemap); |
| 684 | 685 |
/// // Copy a node |
| 685 | 686 |
/// OrigGraph::Node on; |
| 686 | 687 |
/// NewGraph::Node nn; |
| 687 | 688 |
/// cg.node(on, nn); |
| 688 | 689 |
/// // Execute copying |
| 689 | 690 |
/// cg.run(); |
| 690 | 691 |
///\endcode |
| 691 | 692 |
template <typename From, typename To> |
| 692 | 693 |
class GraphCopy {
|
| 693 | 694 |
private: |
| 694 | 695 |
|
| 695 | 696 |
typedef typename From::Node Node; |
| 696 | 697 |
typedef typename From::NodeIt NodeIt; |
| 697 | 698 |
typedef typename From::Arc Arc; |
| 698 | 699 |
typedef typename From::ArcIt ArcIt; |
| 699 | 700 |
typedef typename From::Edge Edge; |
| 700 | 701 |
typedef typename From::EdgeIt EdgeIt; |
| 701 | 702 |
|
| 702 | 703 |
typedef typename To::Node TNode; |
| 703 | 704 |
typedef typename To::Arc TArc; |
| 704 | 705 |
typedef typename To::Edge TEdge; |
| 705 | 706 |
|
| 706 | 707 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
| 707 | 708 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
| 708 | 709 |
|
| 709 | 710 |
struct ArcRefMap {
|
| 710 | 711 |
ArcRefMap(const From& from, const To& to, |
| 711 | 712 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
| 712 | 713 |
: _from(from), _to(to), |
| 713 | 714 |
_edge_ref(edge_ref), _node_ref(node_ref) {}
|
| 714 | 715 |
|
| 715 | 716 |
typedef typename From::Arc Key; |
| 716 | 717 |
typedef typename To::Arc Value; |
| 717 | 718 |
|
| 718 | 719 |
Value operator[](const Key& key) const {
|
| 719 | 720 |
bool forward = _from.u(key) != _from.v(key) ? |
| 720 | 721 |
_node_ref[_from.source(key)] == |
| 721 | 722 |
_to.source(_to.direct(_edge_ref[key], true)) : |
| 722 | 723 |
_from.direction(key); |
| 723 | 724 |
return _to.direct(_edge_ref[key], forward); |
| 724 | 725 |
} |
| 725 | 726 |
|
| 726 | 727 |
const From& _from; |
| 727 | 728 |
const To& _to; |
| 728 | 729 |
const EdgeRefMap& _edge_ref; |
| 729 | 730 |
const NodeRefMap& _node_ref; |
| 730 | 731 |
}; |
| 731 | 732 |
|
| 732 | 733 |
public: |
| 733 | 734 |
|
| 734 | 735 |
/// \brief Constructor of GraphCopy. |
| 735 | 736 |
/// |
| 736 | 737 |
/// Constructor of GraphCopy for copying the content of the |
| 737 | 738 |
/// \c from graph into the \c to graph. |
| 738 | 739 |
GraphCopy(const From& from, To& to) |
| 739 | 740 |
: _from(from), _to(to) {}
|
| 740 | 741 |
|
| 741 | 742 |
/// \brief Destructor of GraphCopy |
| 742 | 743 |
/// |
| 743 | 744 |
/// Destructor of GraphCopy. |
| 744 | 745 |
~GraphCopy() {
|
| 745 | 746 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 746 | 747 |
delete _node_maps[i]; |
| 747 | 748 |
} |
| 748 | 749 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 749 | 750 |
delete _arc_maps[i]; |
| 750 | 751 |
} |
| 751 | 752 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
| 752 | 753 |
delete _edge_maps[i]; |
| 753 | 754 |
} |
| 754 | 755 |
} |
| 755 | 756 |
|
| 756 | 757 |
/// \brief Copy the node references into the given map. |
| 757 | 758 |
/// |
| 758 | 759 |
/// This function copies the node references into the given map. |
| 759 | 760 |
/// The parameter should be a map, whose key type is the Node type of |
| 760 | 761 |
/// the source graph, while the value type is the Node type of the |
| 761 | 762 |
/// destination graph. |
| 762 | 763 |
template <typename NodeRef> |
| 763 | 764 |
GraphCopy& nodeRef(NodeRef& map) {
|
| 764 | 765 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
| 765 | 766 |
NodeRefMap, NodeRef>(map)); |
| 766 | 767 |
return *this; |
| 767 | 768 |
} |
| 768 | 769 |
|
| 769 | 770 |
/// \brief Copy the node cross references into the given map. |
| 770 | 771 |
/// |
| 771 | 772 |
/// This function copies the node cross references (reverse references) |
| 772 | 773 |
/// into the given map. The parameter should be a map, whose key type |
| 773 | 774 |
/// is the Node type of the destination graph, while the value type is |
| 774 | 775 |
/// the Node type of the source graph. |
| 775 | 776 |
template <typename NodeCrossRef> |
| 776 | 777 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) {
|
| 777 | 778 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
| 778 | 779 |
NodeRefMap, NodeCrossRef>(map)); |
| 779 | 780 |
return *this; |
| 780 | 781 |
} |
| 781 | 782 |
|
| 782 | 783 |
/// \brief Make a copy of the given node map. |
| 783 | 784 |
/// |
| 784 | 785 |
/// This function makes a copy of the given node map for the newly |
| 785 | 786 |
/// created graph. |
| 786 | 787 |
/// The key type of the new map \c tmap should be the Node type of the |
| 787 | 788 |
/// destination graph, and the key type of the original map \c map |
| 788 | 789 |
/// should be the Node type of the source graph. |
| 789 | 790 |
template <typename FromMap, typename ToMap> |
| 790 | 791 |
GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) {
|
| 791 | 792 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
| 792 | 793 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
| 793 | 794 |
return *this; |
| 794 | 795 |
} |
| 795 | 796 |
|
| 796 | 797 |
/// \brief Make a copy of the given node. |
| 797 | 798 |
/// |
| 798 | 799 |
/// This function makes a copy of the given node. |
| 799 | 800 |
GraphCopy& node(const Node& node, TNode& tnode) {
|
| 800 | 801 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
| 801 | 802 |
NodeRefMap, TNode>(node, tnode)); |
| 802 | 803 |
return *this; |
| 803 | 804 |
} |
| 804 | 805 |
|
| 805 | 806 |
/// \brief Copy the arc references into the given map. |
| 806 | 807 |
/// |
| 807 | 808 |
/// This function copies the arc references into the given map. |
| 808 | 809 |
/// The parameter should be a map, whose key type is the Arc type of |
| 809 | 810 |
/// the source graph, while the value type is the Arc type of the |
| 810 | 811 |
/// destination graph. |
| 811 | 812 |
template <typename ArcRef> |
| 812 | 813 |
GraphCopy& arcRef(ArcRef& map) {
|
| 813 | 814 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
| 814 | 815 |
ArcRefMap, ArcRef>(map)); |
| 815 | 816 |
return *this; |
| 816 | 817 |
} |
| 817 | 818 |
|
| 818 | 819 |
/// \brief Copy the arc cross references into the given map. |
| 819 | 820 |
/// |
| 820 | 821 |
/// This function copies the arc cross references (reverse references) |
| 821 | 822 |
/// into the given map. The parameter should be a map, whose key type |
| 822 | 823 |
/// is the Arc type of the destination graph, while the value type is |
| 823 | 824 |
/// the Arc type of the source graph. |
| 824 | 825 |
template <typename ArcCrossRef> |
| 825 | 826 |
GraphCopy& arcCrossRef(ArcCrossRef& map) {
|
| 826 | 827 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
| 827 | 828 |
ArcRefMap, ArcCrossRef>(map)); |
| 828 | 829 |
return *this; |
| 829 | 830 |
} |
| 830 | 831 |
|
| 831 | 832 |
/// \brief Make a copy of the given arc map. |
| 832 | 833 |
/// |
| 833 | 834 |
/// This function makes a copy of the given arc map for the newly |
| 834 | 835 |
/// created graph. |
| 835 | 836 |
/// The key type of the new map \c tmap should be the Arc type of the |
| 836 | 837 |
/// destination graph, and the key type of the original map \c map |
| 837 | 838 |
/// should be the Arc type of the source graph. |
| 838 | 839 |
template <typename FromMap, typename ToMap> |
| 839 | 840 |
GraphCopy& arcMap(const FromMap& map, ToMap& tmap) {
|
| 840 | 841 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
| 841 | 842 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
| 842 | 843 |
return *this; |
| 843 | 844 |
} |
| 844 | 845 |
|
| 845 | 846 |
/// \brief Make a copy of the given arc. |
| 846 | 847 |
/// |
| 847 | 848 |
/// This function makes a copy of the given arc. |
| 848 | 849 |
GraphCopy& arc(const Arc& arc, TArc& tarc) {
|
| 849 | 850 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
| 850 | 851 |
ArcRefMap, TArc>(arc, tarc)); |
| 851 | 852 |
return *this; |
| 852 | 853 |
} |
| 853 | 854 |
|
| 854 | 855 |
/// \brief Copy the edge references into the given map. |
| 855 | 856 |
/// |
| 856 | 857 |
/// This function copies the edge references into the given map. |
| 857 | 858 |
/// The parameter should be a map, whose key type is the Edge type of |
| 858 | 859 |
/// the source graph, while the value type is the Edge type of the |
| 859 | 860 |
/// destination graph. |
| 860 | 861 |
template <typename EdgeRef> |
| 861 | 862 |
GraphCopy& edgeRef(EdgeRef& map) {
|
| 862 | 863 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
| 863 | 864 |
EdgeRefMap, EdgeRef>(map)); |
| 864 | 865 |
return *this; |
| 865 | 866 |
} |
| 866 | 867 |
|
| 867 | 868 |
/// \brief Copy the edge cross references into the given map. |
| 868 | 869 |
/// |
| 869 | 870 |
/// This function copies the edge cross references (reverse references) |
| 870 | 871 |
/// into the given map. The parameter should be a map, whose key type |
| 871 | 872 |
/// is the Edge type of the destination graph, while the value type is |
| 872 | 873 |
/// the Edge type of the source graph. |
| 873 | 874 |
template <typename EdgeCrossRef> |
| 874 | 875 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) {
|
| 875 | 876 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
| 876 | 877 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
| 877 | 878 |
return *this; |
| 878 | 879 |
} |
| 879 | 880 |
|
| 880 | 881 |
/// \brief Make a copy of the given edge map. |
| 881 | 882 |
/// |
| 882 | 883 |
/// This function makes a copy of the given edge map for the newly |
| 883 | 884 |
/// created graph. |
| 884 | 885 |
/// The key type of the new map \c tmap should be the Edge type of the |
| 885 | 886 |
/// destination graph, and the key type of the original map \c map |
| 886 | 887 |
/// should be the Edge type of the source graph. |
| 887 | 888 |
template <typename FromMap, typename ToMap> |
| 888 | 889 |
GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) {
|
| 889 | 890 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
| 890 | 891 |
EdgeRefMap, FromMap, ToMap>(map, tmap)); |
| 891 | 892 |
return *this; |
| 892 | 893 |
} |
| 893 | 894 |
|
| 894 | 895 |
/// \brief Make a copy of the given edge. |
| 895 | 896 |
/// |
| 896 | 897 |
/// This function makes a copy of the given edge. |
| 897 | 898 |
GraphCopy& edge(const Edge& edge, TEdge& tedge) {
|
| 898 | 899 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
| 899 | 900 |
EdgeRefMap, TEdge>(edge, tedge)); |
| 900 | 901 |
return *this; |
| 901 | 902 |
} |
| 902 | 903 |
|
| 903 | 904 |
/// \brief Execute copying. |
| 904 | 905 |
/// |
| 905 | 906 |
/// This function executes the copying of the graph along with the |
| 906 | 907 |
/// copying of the assigned data. |
| 907 | 908 |
void run() {
|
| 908 | 909 |
NodeRefMap nodeRefMap(_from); |
| 909 | 910 |
EdgeRefMap edgeRefMap(_from); |
| 910 | 911 |
ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); |
| 911 | 912 |
_core_bits::GraphCopySelector<To>:: |
| 912 | 913 |
copy(_from, _to, nodeRefMap, edgeRefMap); |
| 913 | 914 |
for (int i = 0; i < int(_node_maps.size()); ++i) {
|
| 914 | 915 |
_node_maps[i]->copy(_from, nodeRefMap); |
| 915 | 916 |
} |
| 916 | 917 |
for (int i = 0; i < int(_edge_maps.size()); ++i) {
|
| 917 | 918 |
_edge_maps[i]->copy(_from, edgeRefMap); |
| 918 | 919 |
} |
| 919 | 920 |
for (int i = 0; i < int(_arc_maps.size()); ++i) {
|
| 920 | 921 |
_arc_maps[i]->copy(_from, arcRefMap); |
| 921 | 922 |
} |
| 922 | 923 |
} |
| 923 | 924 |
|
| 924 | 925 |
private: |
| 925 | 926 |
|
| 926 | 927 |
const From& _from; |
| 927 | 928 |
To& _to; |
| 928 | 929 |
|
| 929 | 930 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
| 930 | 931 |
_node_maps; |
| 931 | 932 |
|
| 932 | 933 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
| 933 | 934 |
_arc_maps; |
| 934 | 935 |
|
| 935 | 936 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
| 936 | 937 |
_edge_maps; |
| 937 | 938 |
|
| 938 | 939 |
}; |
| 939 | 940 |
|
| 940 | 941 |
/// \brief Copy a graph to another graph. |
| 941 | 942 |
/// |
| 942 | 943 |
/// This function copies a graph to another graph. |
| 943 | 944 |
/// The complete usage of it is detailed in the GraphCopy class, |
| 944 | 945 |
/// but a short example shows a basic work: |
| 945 | 946 |
///\code |
| 946 | 947 |
/// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
| 947 | 948 |
///\endcode |
| 948 | 949 |
/// |
| 949 | 950 |
/// After the copy the \c nr map will contain the mapping from the |
| 950 | 951 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
| 951 | 952 |
/// \c ecr will contain the mapping from the edges of the \c to graph |
| 952 | 953 |
/// to the edges of the \c from graph. |
| 953 | 954 |
/// |
| 954 | 955 |
/// \see GraphCopy |
| 955 | 956 |
template <typename From, typename To> |
| 956 | 957 |
GraphCopy<From, To> |
| 957 | 958 |
graphCopy(const From& from, To& to) {
|
| 958 | 959 |
return GraphCopy<From, To>(from, to); |
| 959 | 960 |
} |
| 960 | 961 |
|
| 961 | 962 |
namespace _core_bits {
|
| 962 | 963 |
|
| 963 | 964 |
template <typename Graph, typename Enable = void> |
| 964 | 965 |
struct FindArcSelector {
|
| 965 | 966 |
typedef typename Graph::Node Node; |
| 966 | 967 |
typedef typename Graph::Arc Arc; |
| 967 | 968 |
static Arc find(const Graph &g, Node u, Node v, Arc e) {
|
| 968 | 969 |
if (e == INVALID) {
|
| 969 | 970 |
g.firstOut(e, u); |
| 970 | 971 |
} else {
|
| 971 | 972 |
g.nextOut(e); |
| 972 | 973 |
} |
| 973 | 974 |
while (e != INVALID && g.target(e) != v) {
|
| 974 | 975 |
g.nextOut(e); |
| 975 | 976 |
} |
| 976 | 977 |
return e; |
| 977 | 978 |
} |
| 978 | 979 |
}; |
| 979 | 980 |
|
| 980 | 981 |
template <typename Graph> |
| 981 | 982 |
struct FindArcSelector< |
| 982 | 983 |
Graph, |
| 983 | 984 |
typename enable_if<typename Graph::FindArcTag, void>::type> |
| 984 | 985 |
{
|
| 985 | 986 |
typedef typename Graph::Node Node; |
| 986 | 987 |
typedef typename Graph::Arc Arc; |
| 987 | 988 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) {
|
| 988 | 989 |
return g.findArc(u, v, prev); |
| 989 | 990 |
} |
| 990 | 991 |
}; |
| 991 | 992 |
} |
| 992 | 993 |
|
| 993 | 994 |
/// \brief Find an arc between two nodes of a digraph. |
| 994 | 995 |
/// |
| 995 | 996 |
/// This function finds an arc from node \c u to node \c v in the |
| 996 | 997 |
/// digraph \c g. |
| 997 | 998 |
/// |
| 998 | 999 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 999 | 1000 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
| 1000 | 1001 |
/// the next arc from \c u to \c v after \c prev. |
| 1001 | 1002 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
| 1002 | 1003 |
/// |
| 1003 | 1004 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
| 1004 | 1005 |
///\code |
| 1005 | 1006 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) {
|
| 1006 | 1007 |
/// ... |
| 1007 | 1008 |
/// } |
| 1008 | 1009 |
///\endcode |
| 1009 | 1010 |
/// |
| 1010 | 1011 |
/// \note \ref ConArcIt provides iterator interface for the same |
| 1011 | 1012 |
/// functionality. |
| 1012 | 1013 |
/// |
| 1013 | 1014 |
///\sa ConArcIt |
| 1014 | 1015 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
| 1015 | 1016 |
template <typename Graph> |
| 1016 | 1017 |
inline typename Graph::Arc |
| 1017 | 1018 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 1018 | 1019 |
typename Graph::Arc prev = INVALID) {
|
| 1019 | 1020 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
| 1020 | 1021 |
} |
| 1021 | 1022 |
|
| 1022 | 1023 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
| 1023 | 1024 |
/// |
| 1024 | 1025 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is |
| 1025 | 1026 |
/// a higher level interface for the \ref findArc() function. You can |
| 1026 | 1027 |
/// use it the following way: |
| 1027 | 1028 |
///\code |
| 1028 | 1029 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) {
|
| 1029 | 1030 |
/// ... |
| 1030 | 1031 |
/// } |
| 1031 | 1032 |
///\endcode |
| 1032 | 1033 |
/// |
| 1033 | 1034 |
///\sa findArc() |
| 1034 | 1035 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
| 1035 | 1036 |
template <typename _Graph> |
| 1036 | 1037 |
class ConArcIt : public _Graph::Arc {
|
| 1037 | 1038 |
public: |
| 1038 | 1039 |
|
| 1039 | 1040 |
typedef _Graph Graph; |
| 1040 | 1041 |
typedef typename Graph::Arc Parent; |
| 1041 | 1042 |
|
| 1042 | 1043 |
typedef typename Graph::Arc Arc; |
| 1043 | 1044 |
typedef typename Graph::Node Node; |
| 1044 | 1045 |
|
| 1045 | 1046 |
/// \brief Constructor. |
| 1046 | 1047 |
/// |
| 1047 | 1048 |
/// Construct a new ConArcIt iterating on the arcs that |
| 1048 | 1049 |
/// connects nodes \c u and \c v. |
| 1049 | 1050 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 1050 | 1051 |
Parent::operator=(findArc(_graph, u, v)); |
| 1051 | 1052 |
} |
| 1052 | 1053 |
|
| 1053 | 1054 |
/// \brief Constructor. |
| 1054 | 1055 |
/// |
| 1055 | 1056 |
/// Construct a new ConArcIt that continues the iterating from arc \c a. |
| 1056 | 1057 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {}
|
| 1057 | 1058 |
|
| 1058 | 1059 |
/// \brief Increment operator. |
| 1059 | 1060 |
/// |
| 1060 | 1061 |
/// It increments the iterator and gives back the next arc. |
| 1061 | 1062 |
ConArcIt& operator++() {
|
| 1062 | 1063 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
| 1063 | 1064 |
_graph.target(*this), *this)); |
| 1064 | 1065 |
return *this; |
| 1065 | 1066 |
} |
| 1066 | 1067 |
private: |
| 1067 | 1068 |
const Graph& _graph; |
| 1068 | 1069 |
}; |
| 1069 | 1070 |
|
| 1070 | 1071 |
namespace _core_bits {
|
| 1071 | 1072 |
|
| 1072 | 1073 |
template <typename Graph, typename Enable = void> |
| 1073 | 1074 |
struct FindEdgeSelector {
|
| 1074 | 1075 |
typedef typename Graph::Node Node; |
| 1075 | 1076 |
typedef typename Graph::Edge Edge; |
| 1076 | 1077 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
| 1077 | 1078 |
bool b; |
| 1078 | 1079 |
if (u != v) {
|
| 1079 | 1080 |
if (e == INVALID) {
|
| 1080 | 1081 |
g.firstInc(e, b, u); |
| 1081 | 1082 |
} else {
|
| 1082 | 1083 |
b = g.u(e) == u; |
| 1083 | 1084 |
g.nextInc(e, b); |
| 1084 | 1085 |
} |
| 1085 | 1086 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
|
| 1086 | 1087 |
g.nextInc(e, b); |
| 1087 | 1088 |
} |
| 1088 | 1089 |
} else {
|
| 1089 | 1090 |
if (e == INVALID) {
|
| 1090 | 1091 |
g.firstInc(e, b, u); |
| 1091 | 1092 |
} else {
|
| 1092 | 1093 |
b = true; |
| 1093 | 1094 |
g.nextInc(e, b); |
| 1094 | 1095 |
} |
| 1095 | 1096 |
while (e != INVALID && (!b || g.v(e) != v)) {
|
| 1096 | 1097 |
g.nextInc(e, b); |
| 1097 | 1098 |
} |
| 1098 | 1099 |
} |
| 1099 | 1100 |
return e; |
| 1100 | 1101 |
} |
| 1101 | 1102 |
}; |
| 1102 | 1103 |
|
| 1103 | 1104 |
template <typename Graph> |
| 1104 | 1105 |
struct FindEdgeSelector< |
| 1105 | 1106 |
Graph, |
| 1106 | 1107 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
| 1107 | 1108 |
{
|
| 1108 | 1109 |
typedef typename Graph::Node Node; |
| 1109 | 1110 |
typedef typename Graph::Edge Edge; |
| 1110 | 1111 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
| 1111 | 1112 |
return g.findEdge(u, v, prev); |
| 1112 | 1113 |
} |
| 1113 | 1114 |
}; |
| 1114 | 1115 |
} |
| 1115 | 1116 |
|
| 1116 | 1117 |
/// \brief Find an edge between two nodes of a graph. |
| 1117 | 1118 |
/// |
| 1118 | 1119 |
/// This function finds an edge from node \c u to node \c v in graph \c g. |
| 1119 | 1120 |
/// If node \c u and node \c v is equal then each loop edge |
| 1120 | 1121 |
/// will be enumerated once. |
| 1121 | 1122 |
/// |
| 1122 | 1123 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 1123 | 1124 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for |
| 1124 | 1125 |
/// the next edge from \c u to \c v after \c prev. |
| 1125 | 1126 |
/// \return The found edge or \ref INVALID if there is no such an edge. |
| 1126 | 1127 |
/// |
| 1127 | 1128 |
/// Thus you can iterate through each edge between \c u and \c v |
| 1128 | 1129 |
/// as it follows. |
| 1129 | 1130 |
///\code |
| 1130 | 1131 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
|
| 1131 | 1132 |
/// ... |
| 1132 | 1133 |
/// } |
| 1133 | 1134 |
///\endcode |
| 1134 | 1135 |
/// |
| 1135 | 1136 |
/// \note \ref ConEdgeIt provides iterator interface for the same |
| 1136 | 1137 |
/// functionality. |
| 1137 | 1138 |
/// |
| 1138 | 1139 |
///\sa ConEdgeIt |
| 1139 | 1140 |
template <typename Graph> |
| 1140 | 1141 |
inline typename Graph::Edge |
| 1141 | 1142 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
| 1142 | 1143 |
typename Graph::Edge p = INVALID) {
|
| 1143 | 1144 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
| 1144 | 1145 |
} |
| 1145 | 1146 |
|
| 1146 | 1147 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes. |
| 1147 | 1148 |
/// |
| 1148 | 1149 |
/// Iterator for iterating on parallel edges connecting the same nodes. |
| 1149 | 1150 |
/// It is a higher level interface for the findEdge() function. You can |
| 1150 | 1151 |
/// use it the following way: |
| 1151 | 1152 |
///\code |
| 1152 | 1153 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) {
|
| 1153 | 1154 |
/// ... |
| 1154 | 1155 |
/// } |
| 1155 | 1156 |
///\endcode |
| 1156 | 1157 |
/// |
| 1157 | 1158 |
///\sa findEdge() |
| 1158 | 1159 |
template <typename _Graph> |
| 1159 | 1160 |
class ConEdgeIt : public _Graph::Edge {
|
| 1160 | 1161 |
public: |
| 1161 | 1162 |
|
| 1162 | 1163 |
typedef _Graph Graph; |
| 1163 | 1164 |
typedef typename Graph::Edge Parent; |
| 1164 | 1165 |
|
| 1165 | 1166 |
typedef typename Graph::Edge Edge; |
| 1166 | 1167 |
typedef typename Graph::Node Node; |
| 1167 | 1168 |
|
| 1168 | 1169 |
/// \brief Constructor. |
| 1169 | 1170 |
/// |
| 1170 | 1171 |
/// Construct a new ConEdgeIt iterating on the edges that |
| 1171 | 1172 |
/// connects nodes \c u and \c v. |
| 1172 | 1173 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) {
|
| 1173 | 1174 |
Parent::operator=(findEdge(_graph, u, v)); |
| 1174 | 1175 |
} |
| 1175 | 1176 |
|
| 1176 | 1177 |
/// \brief Constructor. |
| 1177 | 1178 |
/// |
| 1178 | 1179 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
| 1179 | 1180 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {}
|
| 1180 | 1181 |
|
| 1181 | 1182 |
/// \brief Increment operator. |
| 1182 | 1183 |
/// |
| 1183 | 1184 |
/// It increments the iterator and gives back the next edge. |
| 1184 | 1185 |
ConEdgeIt& operator++() {
|
| 1185 | 1186 |
Parent::operator=(findEdge(_graph, _graph.u(*this), |
| 1186 | 1187 |
_graph.v(*this), *this)); |
| 1187 | 1188 |
return *this; |
| 1188 | 1189 |
} |
| 1189 | 1190 |
private: |
| 1190 | 1191 |
const Graph& _graph; |
| 1191 | 1192 |
}; |
| 1192 | 1193 |
|
| 1193 | 1194 |
|
| 1194 | 1195 |
///Dynamic arc look-up between given endpoints. |
| 1195 | 1196 |
|
| 1196 | 1197 |
///Using this class, you can find an arc in a digraph from a given |
| 1197 | 1198 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
| 1198 | 1199 |
///where <em>d</em> is the out-degree of the source node. |
| 1199 | 1200 |
/// |
| 1200 | 1201 |
///It is possible to find \e all parallel arcs between two nodes with |
| 1201 | 1202 |
///the \c operator() member. |
| 1202 | 1203 |
/// |
| 1203 | 1204 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
| 1204 | 1205 |
///\ref AllArcLookUp if your digraph is not changed so frequently. |
| 1205 | 1206 |
/// |
| 1206 | 1207 |
///This class uses a self-adjusting binary search tree, the Splay tree |
| 1207 | 1208 |
///of Sleator and Tarjan to guarantee the logarithmic amortized |
| 1208 | 1209 |
///time bound for arc look-ups. This class also guarantees the |
| 1209 | 1210 |
///optimal time bound in a constant factor for any distribution of |
| 1210 | 1211 |
///queries. |
| 1211 | 1212 |
/// |
| 1212 | 1213 |
///\tparam G The type of the underlying digraph. |
| 1213 | 1214 |
/// |
| 1214 | 1215 |
///\sa ArcLookUp |
| 1215 | 1216 |
///\sa AllArcLookUp |
| 1216 | 1217 |
template<class G> |
| 1217 | 1218 |
class DynArcLookUp |
| 1218 | 1219 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
| 1219 | 1220 |
{
|
| 1220 | 1221 |
public: |
| 1221 | 1222 |
typedef typename ItemSetTraits<G, typename G::Arc> |
| 1222 | 1223 |
::ItemNotifier::ObserverBase Parent; |
| 1223 | 1224 |
|
| 1224 | 1225 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
| 1225 | 1226 |
typedef G Digraph; |
| 1226 | 1227 |
|
| 1227 | 1228 |
protected: |
| 1228 | 1229 |
|
| 1229 | 1230 |
class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type {
|
| 1230 | 1231 |
public: |
| 1231 | 1232 |
|
| 1232 | 1233 |
typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent; |
| 1233 | 1234 |
|
| 1234 | 1235 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
|
| 1235 | 1236 |
|
| 1236 | 1237 |
virtual void add(const Node& node) {
|
| 1237 | 1238 |
Parent::add(node); |
| 1238 | 1239 |
Parent::set(node, INVALID); |
| 1239 | 1240 |
} |
| 1240 | 1241 |
|
| 1241 | 1242 |
virtual void add(const std::vector<Node>& nodes) {
|
| 1242 | 1243 |
Parent::add(nodes); |
| 1243 | 1244 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 1244 | 1245 |
Parent::set(nodes[i], INVALID); |
| 1245 | 1246 |
} |
| 1246 | 1247 |
} |
| 1247 | 1248 |
|
| 1248 | 1249 |
virtual void build() {
|
| 1249 | 1250 |
Parent::build(); |
| 1250 | 1251 |
Node it; |
| 1251 | 1252 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 1252 | 1253 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1253 | 1254 |
Parent::set(it, INVALID); |
| 1254 | 1255 |
} |
| 1255 | 1256 |
} |
| 1256 | 1257 |
}; |
| 1257 | 1258 |
|
| 1258 | 1259 |
const Digraph &_g; |
| 1259 | 1260 |
AutoNodeMap _head; |
| 1260 | 1261 |
typename Digraph::template ArcMap<Arc> _parent; |
| 1261 | 1262 |
typename Digraph::template ArcMap<Arc> _left; |
| 1262 | 1263 |
typename Digraph::template ArcMap<Arc> _right; |
| 1263 | 1264 |
|
| 1264 | 1265 |
class ArcLess {
|
| 1265 | 1266 |
const Digraph &g; |
| 1266 | 1267 |
public: |
| 1267 | 1268 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 1268 | 1269 |
bool operator()(Arc a,Arc b) const |
| 1269 | 1270 |
{
|
| 1270 | 1271 |
return g.target(a)<g.target(b); |
| 1271 | 1272 |
} |
| 1272 | 1273 |
}; |
| 1273 | 1274 |
|
| 1274 | 1275 |
public: |
| 1275 | 1276 |
|
| 1276 | 1277 |
///Constructor |
| 1277 | 1278 |
|
| 1278 | 1279 |
///Constructor. |
| 1279 | 1280 |
/// |
| 1280 | 1281 |
///It builds up the search database. |
| 1281 | 1282 |
DynArcLookUp(const Digraph &g) |
| 1282 | 1283 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
| 1283 | 1284 |
{
|
| 1284 | 1285 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
| 1285 | 1286 |
refresh(); |
| 1286 | 1287 |
} |
| 1287 | 1288 |
|
| 1288 | 1289 |
protected: |
| 1289 | 1290 |
|
| 1290 | 1291 |
virtual void add(const Arc& arc) {
|
| 1291 | 1292 |
insert(arc); |
| 1292 | 1293 |
} |
| 1293 | 1294 |
|
| 1294 | 1295 |
virtual void add(const std::vector<Arc>& arcs) {
|
| 1295 | 1296 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1296 | 1297 |
insert(arcs[i]); |
| 1297 | 1298 |
} |
| 1298 | 1299 |
} |
| 1299 | 1300 |
|
| 1300 | 1301 |
virtual void erase(const Arc& arc) {
|
| 1301 | 1302 |
remove(arc); |
| 1302 | 1303 |
} |
| 1303 | 1304 |
|
| 1304 | 1305 |
virtual void erase(const std::vector<Arc>& arcs) {
|
| 1305 | 1306 |
for (int i = 0; i < int(arcs.size()); ++i) {
|
| 1306 | 1307 |
remove(arcs[i]); |
| 1307 | 1308 |
} |
| 1308 | 1309 |
} |
| 1309 | 1310 |
|
| 1310 | 1311 |
virtual void build() {
|
| 1311 | 1312 |
refresh(); |
| 1312 | 1313 |
} |
| 1313 | 1314 |
|
| 1314 | 1315 |
virtual void clear() {
|
| 1315 | 1316 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 1316 | 1317 |
_head.set(n, INVALID); |
| 1317 | 1318 |
} |
| 1318 | 1319 |
} |
| 1319 | 1320 |
|
| 1320 | 1321 |
void insert(Arc arc) {
|
| 1321 | 1322 |
Node s = _g.source(arc); |
| 1322 | 1323 |
Node t = _g.target(arc); |
| 1323 | 1324 |
_left.set(arc, INVALID); |
| 1324 | 1325 |
_right.set(arc, INVALID); |
| 1325 | 1326 |
|
| 1326 | 1327 |
Arc e = _head[s]; |
| 1327 | 1328 |
if (e == INVALID) {
|
| 1328 | 1329 |
_head.set(s, arc); |
| 1329 | 1330 |
_parent.set(arc, INVALID); |
| 1330 | 1331 |
return; |
| 1331 | 1332 |
} |
| 1332 | 1333 |
while (true) {
|
| 1333 | 1334 |
if (t < _g.target(e)) {
|
| 1334 | 1335 |
if (_left[e] == INVALID) {
|
| 1335 | 1336 |
_left.set(e, arc); |
| 1336 | 1337 |
_parent.set(arc, e); |
| 1337 | 1338 |
splay(arc); |
| 1338 | 1339 |
return; |
| 1339 | 1340 |
} else {
|
| 1340 | 1341 |
e = _left[e]; |
| 1341 | 1342 |
} |
| 1342 | 1343 |
} else {
|
| 1343 | 1344 |
if (_right[e] == INVALID) {
|
| 1344 | 1345 |
_right.set(e, arc); |
| 1345 | 1346 |
_parent.set(arc, e); |
| 1346 | 1347 |
splay(arc); |
| 1347 | 1348 |
return; |
| 1348 | 1349 |
} else {
|
| 1349 | 1350 |
e = _right[e]; |
| 1350 | 1351 |
} |
| 1351 | 1352 |
} |
| 1352 | 1353 |
} |
| 1353 | 1354 |
} |
| 1354 | 1355 |
|
| 1355 | 1356 |
void remove(Arc arc) {
|
| 1356 | 1357 |
if (_left[arc] == INVALID) {
|
| 1357 | 1358 |
if (_right[arc] != INVALID) {
|
| 1358 | 1359 |
_parent.set(_right[arc], _parent[arc]); |
| 1359 | 1360 |
} |
| 1360 | 1361 |
if (_parent[arc] != INVALID) {
|
| 1361 | 1362 |
if (_left[_parent[arc]] == arc) {
|
| 1362 | 1363 |
_left.set(_parent[arc], _right[arc]); |
| 1363 | 1364 |
} else {
|
| 1364 | 1365 |
_right.set(_parent[arc], _right[arc]); |
| 1365 | 1366 |
} |
| 1366 | 1367 |
} else {
|
| 1367 | 1368 |
_head.set(_g.source(arc), _right[arc]); |
| 1368 | 1369 |
} |
| 1369 | 1370 |
} else if (_right[arc] == INVALID) {
|
| 1370 | 1371 |
_parent.set(_left[arc], _parent[arc]); |
| 1371 | 1372 |
if (_parent[arc] != INVALID) {
|
| 1372 | 1373 |
if (_left[_parent[arc]] == arc) {
|
| 1373 | 1374 |
_left.set(_parent[arc], _left[arc]); |
| 1374 | 1375 |
} else {
|
| 1375 | 1376 |
_right.set(_parent[arc], _left[arc]); |
| 1376 | 1377 |
} |
| 1377 | 1378 |
} else {
|
| 1378 | 1379 |
_head.set(_g.source(arc), _left[arc]); |
| 1379 | 1380 |
} |
| 1380 | 1381 |
} else {
|
| 1381 | 1382 |
Arc e = _left[arc]; |
| 1382 | 1383 |
if (_right[e] != INVALID) {
|
| 1383 | 1384 |
e = _right[e]; |
| 1384 | 1385 |
while (_right[e] != INVALID) {
|
| 1385 | 1386 |
e = _right[e]; |
| 1386 | 1387 |
} |
| 1387 | 1388 |
Arc s = _parent[e]; |
| 1388 | 1389 |
_right.set(_parent[e], _left[e]); |
| 1389 | 1390 |
if (_left[e] != INVALID) {
|
| 1390 | 1391 |
_parent.set(_left[e], _parent[e]); |
| 1391 | 1392 |
} |
| 1392 | 1393 |
|
| 1393 | 1394 |
_left.set(e, _left[arc]); |
| 1394 | 1395 |
_parent.set(_left[arc], e); |
| 1395 | 1396 |
_right.set(e, _right[arc]); |
| 1396 | 1397 |
_parent.set(_right[arc], e); |
| 1397 | 1398 |
|
| 1398 | 1399 |
_parent.set(e, _parent[arc]); |
| 1399 | 1400 |
if (_parent[arc] != INVALID) {
|
| 1400 | 1401 |
if (_left[_parent[arc]] == arc) {
|
| 1401 | 1402 |
_left.set(_parent[arc], e); |
| 1402 | 1403 |
} else {
|
| 1403 | 1404 |
_right.set(_parent[arc], e); |
| 1404 | 1405 |
} |
| 1405 | 1406 |
} |
| 1406 | 1407 |
splay(s); |
| 1407 | 1408 |
} else {
|
| 1408 | 1409 |
_right.set(e, _right[arc]); |
| 1409 | 1410 |
_parent.set(_right[arc], e); |
| 1410 | 1411 |
_parent.set(e, _parent[arc]); |
| 1411 | 1412 |
|
| 1412 | 1413 |
if (_parent[arc] != INVALID) {
|
| 1413 | 1414 |
if (_left[_parent[arc]] == arc) {
|
| 1414 | 1415 |
_left.set(_parent[arc], e); |
| 1415 | 1416 |
} else {
|
| 1416 | 1417 |
_right.set(_parent[arc], e); |
| 1417 | 1418 |
} |
| 1418 | 1419 |
} else {
|
| 1419 | 1420 |
_head.set(_g.source(arc), e); |
| 1420 | 1421 |
} |
| 1421 | 1422 |
} |
| 1422 | 1423 |
} |
| 1423 | 1424 |
} |
| 1424 | 1425 |
|
| 1425 | 1426 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
| 1426 | 1427 |
{
|
| 1427 | 1428 |
int m=(a+b)/2; |
| 1428 | 1429 |
Arc me=v[m]; |
| 1429 | 1430 |
if (a < m) {
|
| 1430 | 1431 |
Arc left = refreshRec(v,a,m-1); |
| 1431 | 1432 |
_left.set(me, left); |
| 1432 | 1433 |
_parent.set(left, me); |
| 1433 | 1434 |
} else {
|
| 1434 | 1435 |
_left.set(me, INVALID); |
| 1435 | 1436 |
} |
| 1436 | 1437 |
if (m < b) {
|
| 1437 | 1438 |
Arc right = refreshRec(v,m+1,b); |
| 1438 | 1439 |
_right.set(me, right); |
| 1439 | 1440 |
_parent.set(right, me); |
| 1440 | 1441 |
} else {
|
| 1441 | 1442 |
_right.set(me, INVALID); |
| 1442 | 1443 |
} |
| 1443 | 1444 |
return me; |
| 1444 | 1445 |
} |
| 1445 | 1446 |
|
| 1446 | 1447 |
void refresh() {
|
| 1447 | 1448 |
for(NodeIt n(_g);n!=INVALID;++n) {
|
| 1448 | 1449 |
std::vector<Arc> v; |
| 1449 | 1450 |
for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a); |
| 1450 | 1451 |
if (!v.empty()) {
|
| 1451 | 1452 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
| 1452 | 1453 |
Arc head = refreshRec(v,0,v.size()-1); |
| 1453 | 1454 |
_head.set(n, head); |
| 1454 | 1455 |
_parent.set(head, INVALID); |
| 1455 | 1456 |
} |
| 1456 | 1457 |
else _head.set(n, INVALID); |
| 1457 | 1458 |
} |
| 1458 | 1459 |
} |
| 1459 | 1460 |
|
| 1460 | 1461 |
void zig(Arc v) {
|
| 1461 | 1462 |
Arc w = _parent[v]; |
| 1462 | 1463 |
_parent.set(v, _parent[w]); |
| 1463 | 1464 |
_parent.set(w, v); |
| 1464 | 1465 |
_left.set(w, _right[v]); |
| 1465 | 1466 |
_right.set(v, w); |
| 1466 | 1467 |
if (_parent[v] != INVALID) {
|
| 1467 | 1468 |
if (_right[_parent[v]] == w) {
|
| 1468 | 1469 |
_right.set(_parent[v], v); |
| 1469 | 1470 |
} else {
|
| 1470 | 1471 |
_left.set(_parent[v], v); |
| 1471 | 1472 |
} |
| 1472 | 1473 |
} |
| 1473 | 1474 |
if (_left[w] != INVALID){
|
| 1474 | 1475 |
_parent.set(_left[w], w); |
| 1475 | 1476 |
} |
| 1476 | 1477 |
} |
| 1477 | 1478 |
|
| 1478 | 1479 |
void zag(Arc v) {
|
| 1479 | 1480 |
Arc w = _parent[v]; |
| 1480 | 1481 |
_parent.set(v, _parent[w]); |
| 1481 | 1482 |
_parent.set(w, v); |
| 1482 | 1483 |
_right.set(w, _left[v]); |
| 1483 | 1484 |
_left.set(v, w); |
| 1484 | 1485 |
if (_parent[v] != INVALID){
|
| 1485 | 1486 |
if (_left[_parent[v]] == w) {
|
| 1486 | 1487 |
_left.set(_parent[v], v); |
| 1487 | 1488 |
} else {
|
| 1488 | 1489 |
_right.set(_parent[v], v); |
| 1489 | 1490 |
} |
| 1490 | 1491 |
} |
| 1491 | 1492 |
if (_right[w] != INVALID){
|
| 1492 | 1493 |
_parent.set(_right[w], w); |
| 1493 | 1494 |
} |
| 1494 | 1495 |
} |
| 1495 | 1496 |
|
| 1496 | 1497 |
void splay(Arc v) {
|
| 1497 | 1498 |
while (_parent[v] != INVALID) {
|
| 1498 | 1499 |
if (v == _left[_parent[v]]) {
|
| 1499 | 1500 |
if (_parent[_parent[v]] == INVALID) {
|
| 1500 | 1501 |
zig(v); |
| 1501 | 1502 |
} else {
|
| 1502 | 1503 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
| 1503 | 1504 |
zig(_parent[v]); |
| 1504 | 1505 |
zig(v); |
| 1505 | 1506 |
} else {
|
| 1506 | 1507 |
zig(v); |
| 1507 | 1508 |
zag(v); |
| 1508 | 1509 |
} |
| 1509 | 1510 |
} |
| 1510 | 1511 |
} else {
|
| 1511 | 1512 |
if (_parent[_parent[v]] == INVALID) {
|
| 1512 | 1513 |
zag(v); |
| 1513 | 1514 |
} else {
|
| 1514 | 1515 |
if (_parent[v] == _left[_parent[_parent[v]]]) {
|
| 1515 | 1516 |
zag(v); |
| 1516 | 1517 |
zig(v); |
| 1517 | 1518 |
} else {
|
| 1518 | 1519 |
zag(_parent[v]); |
| 1519 | 1520 |
zag(v); |
| 1520 | 1521 |
} |
| 1521 | 1522 |
} |
| 1522 | 1523 |
} |
| 1523 | 1524 |
} |
| 1524 | 1525 |
_head[_g.source(v)] = v; |
| 1525 | 1526 |
} |
| 1526 | 1527 |
|
| 1527 | 1528 |
|
| 1528 | 1529 |
public: |
| 1529 | 1530 |
|
| 1530 | 1531 |
///Find an arc between two nodes. |
| 1531 | 1532 |
|
| 1532 | 1533 |
///Find an arc between two nodes. |
| 1533 | 1534 |
///\param s The source node. |
| 1534 | 1535 |
///\param t The target node. |
| 1535 | 1536 |
///\param p The previous arc between \c s and \c t. It it is INVALID or |
| 1536 | 1537 |
///not given, the operator finds the first appropriate arc. |
| 1537 | 1538 |
///\return An arc from \c s to \c t after \c p or |
| 1538 | 1539 |
///\ref INVALID if there is no more. |
| 1539 | 1540 |
/// |
| 1540 | 1541 |
///For example, you can count the number of arcs from \c u to \c v in the |
| 1541 | 1542 |
///following way. |
| 1542 | 1543 |
///\code |
| 1543 | 1544 |
///DynArcLookUp<ListDigraph> ae(g); |
| 1544 | 1545 |
///... |
| 1545 | 1546 |
///int n = 0; |
| 1546 | 1547 |
///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++; |
| 1547 | 1548 |
///\endcode |
| 1548 | 1549 |
/// |
| 1549 | 1550 |
///Finding the arcs take at most <em>O</em>(log<em>d</em>) |
| 1550 | 1551 |
///amortized time, specifically, the time complexity of the lookups |
| 1551 | 1552 |
///is equal to the optimal search tree implementation for the |
| 1552 | 1553 |
///current query distribution in a constant factor. |
| 1553 | 1554 |
/// |
| 1554 | 1555 |
///\note This is a dynamic data structure, therefore the data |
| 1555 | 1556 |
///structure is updated after each graph alteration. Thus although |
| 1556 | 1557 |
///this data structure is theoretically faster than \ref ArcLookUp |
| 1557 | 1558 |
///and \ref AllArcLookUp, it often provides worse performance than |
| 1558 | 1559 |
///them. |
| 1559 | 1560 |
Arc operator()(Node s, Node t, Arc p = INVALID) const {
|
| 1560 | 1561 |
if (p == INVALID) {
|
| 1561 | 1562 |
Arc a = _head[s]; |
| 1562 | 1563 |
if (a == INVALID) return INVALID; |
| 1563 | 1564 |
Arc r = INVALID; |
| 1564 | 1565 |
while (true) {
|
| 1565 | 1566 |
if (_g.target(a) < t) {
|
| 1566 | 1567 |
if (_right[a] == INVALID) {
|
| 1567 | 1568 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1568 | 1569 |
return r; |
| 1569 | 1570 |
} else {
|
| 1570 | 1571 |
a = _right[a]; |
| 1571 | 1572 |
} |
| 1572 | 1573 |
} else {
|
| 1573 | 1574 |
if (_g.target(a) == t) {
|
| 1574 | 1575 |
r = a; |
| 1575 | 1576 |
} |
| 1576 | 1577 |
if (_left[a] == INVALID) {
|
| 1577 | 1578 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1578 | 1579 |
return r; |
| 1579 | 1580 |
} else {
|
| 1580 | 1581 |
a = _left[a]; |
| 1581 | 1582 |
} |
| 1582 | 1583 |
} |
| 1583 | 1584 |
} |
| 1584 | 1585 |
} else {
|
| 1585 | 1586 |
Arc a = p; |
| 1586 | 1587 |
if (_right[a] != INVALID) {
|
| 1587 | 1588 |
a = _right[a]; |
| 1588 | 1589 |
while (_left[a] != INVALID) {
|
| 1589 | 1590 |
a = _left[a]; |
| 1590 | 1591 |
} |
| 1591 | 1592 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1592 | 1593 |
} else {
|
| 1593 | 1594 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) {
|
| 1594 | 1595 |
a = _parent[a]; |
| 1595 | 1596 |
} |
| 1596 | 1597 |
if (_parent[a] == INVALID) {
|
| 1597 | 1598 |
return INVALID; |
| 1598 | 1599 |
} else {
|
| 1599 | 1600 |
a = _parent[a]; |
| 1600 | 1601 |
const_cast<DynArcLookUp&>(*this).splay(a); |
| 1601 | 1602 |
} |
| 1602 | 1603 |
} |
| 1603 | 1604 |
if (_g.target(a) == t) return a; |
| 1604 | 1605 |
else return INVALID; |
| 1605 | 1606 |
} |
| 1606 | 1607 |
} |
| 1607 | 1608 |
|
| 1608 | 1609 |
}; |
| 1609 | 1610 |
|
| 1610 | 1611 |
///Fast arc look-up between given endpoints. |
| 1611 | 1612 |
|
| 1612 | 1613 |
///Using this class, you can find an arc in a digraph from a given |
| 1613 | 1614 |
///source to a given target in time <em>O</em>(log<em>d</em>), |
| 1614 | 1615 |
///where <em>d</em> is the out-degree of the source node. |
| 1615 | 1616 |
/// |
| 1616 | 1617 |
///It is not possible to find \e all parallel arcs between two nodes. |
| 1617 | 1618 |
///Use \ref AllArcLookUp for this purpose. |
| 1618 | 1619 |
/// |
| 1619 | 1620 |
///\warning This class is static, so you should call refresh() (or at |
| 1620 | 1621 |
///least refresh(Node)) to refresh this data structure whenever the |
| 1621 | 1622 |
///digraph changes. This is a time consuming (superlinearly proportional |
| 1622 | 1623 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
| 1623 | 1624 |
/// |
| 1624 | 1625 |
///\tparam G The type of the underlying digraph. |
| 1625 | 1626 |
/// |
| 1626 | 1627 |
///\sa DynArcLookUp |
| 1627 | 1628 |
///\sa AllArcLookUp |
| 1628 | 1629 |
template<class G> |
| 1629 | 1630 |
class ArcLookUp |
| 1630 | 1631 |
{
|
| 1631 | 1632 |
public: |
| 1632 | 1633 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
| 1633 | 1634 |
typedef G Digraph; |
| 1634 | 1635 |
|
| 1635 | 1636 |
protected: |
| 1636 | 1637 |
const Digraph &_g; |
| 1637 | 1638 |
typename Digraph::template NodeMap<Arc> _head; |
| 1638 | 1639 |
typename Digraph::template ArcMap<Arc> _left; |
| 1639 | 1640 |
typename Digraph::template ArcMap<Arc> _right; |
| 1640 | 1641 |
|
| 1641 | 1642 |
class ArcLess {
|
| 1642 | 1643 |
const Digraph &g; |
| 1643 | 1644 |
public: |
| 1644 | 1645 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 1645 | 1646 |
bool operator()(Arc a,Arc b) const |
| 1646 | 1647 |
{
|
| 1647 | 1648 |
return g.target(a)<g.target(b); |
| 1648 | 1649 |
} |
| 1649 | 1650 |
}; |
| 1650 | 1651 |
|
| 1651 | 1652 |
public: |
| 1652 | 1653 |
|
| 1653 | 1654 |
///Constructor |
| 1654 | 1655 |
|
| 1655 | 1656 |
///Constructor. |
| 1656 | 1657 |
/// |
| 1657 | 1658 |
///It builds up the search database, which remains valid until the digraph |
| 1658 | 1659 |
///changes. |
| 1659 | 1660 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
|
| 1660 | 1661 |
|
| 1661 | 1662 |
private: |
| 1662 | 1663 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
| 1663 | 1664 |
{
|
| 1664 | 1665 |
int m=(a+b)/2; |
| 1665 | 1666 |
Arc me=v[m]; |
| 1666 | 1667 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
| 1667 | 1668 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
| 1668 | 1669 |
return me; |
| 1669 | 1670 |
} |
| 1670 | 1671 |
public: |
| 1671 | 1672 |
///Refresh the search data structure at a node. |
| 1672 | 1673 |
|
| 1673 | 1674 |
///Build up the search database of node \c n. |
| 1674 | 1675 |
/// |
| 1675 | 1676 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> |
| 1676 | 1677 |
///is the number of the outgoing arcs of \c n. |
| 1677 | 1678 |
void refresh(Node n) |
| 1678 | 1679 |
{
|
| 1679 | 1680 |
std::vector<Arc> v; |
| 1680 | 1681 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
| 1681 | 1682 |
if(v.size()) {
|
| 1682 | 1683 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
| 1683 | 1684 |
_head[n]=refreshRec(v,0,v.size()-1); |
| 1684 | 1685 |
} |
| 1685 | 1686 |
else _head[n]=INVALID; |
| 1686 | 1687 |
} |
| 1687 | 1688 |
///Refresh the full data structure. |
| 1688 | 1689 |
|
| 1689 | 1690 |
///Build up the full search database. In fact, it simply calls |
| 1690 | 1691 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
| 1691 | 1692 |
/// |
| 1692 | 1693 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
| 1693 | 1694 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
| 1694 | 1695 |
///out-degree of the digraph. |
| 1695 | 1696 |
void refresh() |
| 1696 | 1697 |
{
|
| 1697 | 1698 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
| 1698 | 1699 |
} |
| 1699 | 1700 |
|
| 1700 | 1701 |
///Find an arc between two nodes. |
| 1701 | 1702 |
|
| 1702 | 1703 |
///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), |
| 1703 | 1704 |
///where <em>d</em> is the number of outgoing arcs of \c s. |
| 1704 | 1705 |
///\param s The source node. |
| 1705 | 1706 |
///\param t The target node. |
| 1706 | 1707 |
///\return An arc from \c s to \c t if there exists, |
| 1707 | 1708 |
///\ref INVALID otherwise. |
| 1708 | 1709 |
/// |
| 1709 | 1710 |
///\warning If you change the digraph, refresh() must be called before using |
| 1710 | 1711 |
///this operator. If you change the outgoing arcs of |
| 1711 | 1712 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
| 1712 | 1713 |
Arc operator()(Node s, Node t) const |
| 1713 | 1714 |
{
|
| 1714 | 1715 |
Arc e; |
| 1715 | 1716 |
for(e=_head[s]; |
| 1716 | 1717 |
e!=INVALID&&_g.target(e)!=t; |
| 1717 | 1718 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
| 1718 | 1719 |
return e; |
| 1719 | 1720 |
} |
| 1720 | 1721 |
|
| 1721 | 1722 |
}; |
| 1722 | 1723 |
|
| 1723 | 1724 |
///Fast look-up of all arcs between given endpoints. |
| 1724 | 1725 |
|
| 1725 | 1726 |
///This class is the same as \ref ArcLookUp, with the addition |
| 1726 | 1727 |
///that it makes it possible to find all parallel arcs between given |
| 1727 | 1728 |
///endpoints. |
| 1728 | 1729 |
/// |
| 1729 | 1730 |
///\warning This class is static, so you should call refresh() (or at |
| 1730 | 1731 |
///least refresh(Node)) to refresh this data structure whenever the |
| 1731 | 1732 |
///digraph changes. This is a time consuming (superlinearly proportional |
| 1732 | 1733 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
| 1733 | 1734 |
/// |
| 1734 | 1735 |
///\tparam G The type of the underlying digraph. |
| 1735 | 1736 |
/// |
| 1736 | 1737 |
///\sa DynArcLookUp |
| 1737 | 1738 |
///\sa ArcLookUp |
| 1738 | 1739 |
template<class G> |
| 1739 | 1740 |
class AllArcLookUp : public ArcLookUp<G> |
| 1740 | 1741 |
{
|
| 1741 | 1742 |
using ArcLookUp<G>::_g; |
| 1742 | 1743 |
using ArcLookUp<G>::_right; |
| 1743 | 1744 |
using ArcLookUp<G>::_left; |
| 1744 | 1745 |
using ArcLookUp<G>::_head; |
| 1745 | 1746 |
|
| 1746 | 1747 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
| 1747 | 1748 |
typedef G Digraph; |
| 1748 | 1749 |
|
| 1749 | 1750 |
typename Digraph::template ArcMap<Arc> _next; |
| 1750 | 1751 |
|
| 1751 | 1752 |
Arc refreshNext(Arc head,Arc next=INVALID) |
| 1752 | 1753 |
{
|
| 1753 | 1754 |
if(head==INVALID) return next; |
| 1754 | 1755 |
else {
|
| 1755 | 1756 |
next=refreshNext(_right[head],next); |
| 1756 | 1757 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
| 1757 | 1758 |
? next : INVALID; |
| 1758 | 1759 |
return refreshNext(_left[head],head); |
| 1759 | 1760 |
} |
| 1760 | 1761 |
} |
| 1761 | 1762 |
|
| 1762 | 1763 |
void refreshNext() |
| 1763 | 1764 |
{
|
| 1764 | 1765 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
| 1765 | 1766 |
} |
| 1766 | 1767 |
|
| 1767 | 1768 |
public: |
| 1768 | 1769 |
///Constructor |
| 1769 | 1770 |
|
| 1770 | 1771 |
///Constructor. |
| 1771 | 1772 |
/// |
| 1772 | 1773 |
///It builds up the search database, which remains valid until the digraph |
| 1773 | 1774 |
///changes. |
| 1774 | 1775 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
|
| 1775 | 1776 |
|
| 1776 | 1777 |
///Refresh the data structure at a node. |
| 1777 | 1778 |
|
| 1778 | 1779 |
///Build up the search database of node \c n. |
| 1779 | 1780 |
/// |
| 1780 | 1781 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is |
| 1781 | 1782 |
///the number of the outgoing arcs of \c n. |
| 1782 | 1783 |
void refresh(Node n) |
| 1783 | 1784 |
{
|
| 1784 | 1785 |
ArcLookUp<G>::refresh(n); |
| 1785 | 1786 |
refreshNext(_head[n]); |
| 1786 | 1787 |
} |
| 1787 | 1788 |
|
| 1788 | 1789 |
///Refresh the full data structure. |
| 1789 | 1790 |
|
| 1790 | 1791 |
///Build up the full search database. In fact, it simply calls |
| 1791 | 1792 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
| 1792 | 1793 |
/// |
| 1793 | 1794 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
| 1794 | 1795 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
| 1795 | 1796 |
///out-degree of the digraph. |
| 1796 | 1797 |
void refresh() |
| 1797 | 1798 |
{
|
| 1798 | 1799 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
| 1799 | 1800 |
} |
| 1800 | 1801 |
|
| 1801 | 1802 |
///Find an arc between two nodes. |
| 1802 | 1803 |
|
| 1803 | 1804 |
///Find an arc between two nodes. |
| 1804 | 1805 |
///\param s The source node. |
| 1805 | 1806 |
///\param t The target node. |
| 1806 | 1807 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
| 1807 | 1808 |
///not given, the operator finds the first appropriate arc. |
| 1808 | 1809 |
///\return An arc from \c s to \c t after \c prev or |
| 1809 | 1810 |
///\ref INVALID if there is no more. |
| 1810 | 1811 |
/// |
| 1811 | 1812 |
///For example, you can count the number of arcs from \c u to \c v in the |
| 1812 | 1813 |
///following way. |
| 1813 | 1814 |
///\code |
| 1814 | 1815 |
///AllArcLookUp<ListDigraph> ae(g); |
| 1815 | 1816 |
///... |
| 1816 | 1817 |
///int n = 0; |
| 1817 | 1818 |
///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++; |
| 1818 | 1819 |
///\endcode |
| 1819 | 1820 |
/// |
| 1820 | 1821 |
///Finding the first arc take <em>O</em>(log<em>d</em>) time, |
| 1821 | 1822 |
///where <em>d</em> is the number of outgoing arcs of \c s. Then the |
| 1822 | 1823 |
///consecutive arcs are found in constant time. |
| 1823 | 1824 |
/// |
| 1824 | 1825 |
///\warning If you change the digraph, refresh() must be called before using |
| 1825 | 1826 |
///this operator. If you change the outgoing arcs of |
| 1826 | 1827 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
| 1827 | 1828 |
/// |
| 1828 | 1829 |
#ifdef DOXYGEN |
| 1829 | 1830 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {}
|
| 1830 | 1831 |
#else |
| 1831 | 1832 |
using ArcLookUp<G>::operator() ; |
| 1832 | 1833 |
Arc operator()(Node s, Node t, Arc prev) const |
| 1833 | 1834 |
{
|
| 1834 | 1835 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
| 1835 | 1836 |
} |
| 1836 | 1837 |
#endif |
| 1837 | 1838 |
|
| 1838 | 1839 |
}; |
| 1839 | 1840 |
|
| 1840 | 1841 |
/// @} |
| 1841 | 1842 |
|
| 1842 | 1843 |
} //namespace lemon |
| 1843 | 1844 |
|
| 1844 | 1845 |
#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-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_DFS_H |
| 20 | 20 |
#define LEMON_DFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief DFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 |
#include <lemon/assert.h> |
|
| 31 | 30 |
#include <lemon/maps.h> |
| 32 | 31 |
#include <lemon/path.h> |
| 33 | 32 |
|
| 34 | 33 |
namespace lemon {
|
| 35 | 34 |
|
| 36 | 35 |
///Default traits class of Dfs class. |
| 37 | 36 |
|
| 38 | 37 |
///Default traits class of Dfs class. |
| 39 | 38 |
///\tparam GR Digraph type. |
| 40 | 39 |
template<class GR> |
| 41 | 40 |
struct DfsDefaultTraits |
| 42 | 41 |
{
|
| 43 | 42 |
///The type of the digraph the algorithm runs on. |
| 44 | 43 |
typedef GR Digraph; |
| 45 | 44 |
|
| 46 | 45 |
///\brief The type of the map that stores the predecessor |
| 47 | 46 |
///arcs of the %DFS paths. |
| 48 | 47 |
/// |
| 49 | 48 |
///The type of the map that stores the predecessor |
| 50 | 49 |
///arcs of the %DFS paths. |
| 51 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 52 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 53 | 52 |
///Instantiates a PredMap. |
| 54 | 53 |
|
| 55 | 54 |
///This function instantiates a PredMap. |
| 56 | 55 |
///\param g is the digraph, to which we would like to define the |
| 57 | 56 |
///PredMap. |
| 58 | 57 |
static PredMap *createPredMap(const Digraph &g) |
| 59 | 58 |
{
|
| 60 | 59 |
return new PredMap(g); |
| 61 | 60 |
} |
| 62 | 61 |
|
| 63 | 62 |
///The type of the map that indicates which nodes are processed. |
| 64 | 63 |
|
| 65 | 64 |
///The type of the map that indicates which nodes are processed. |
| 66 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 67 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 68 | 67 |
///Instantiates a ProcessedMap. |
| 69 | 68 |
|
| 70 | 69 |
///This function instantiates a ProcessedMap. |
| 71 | 70 |
///\param g is the digraph, to which |
| 72 | 71 |
///we would like to define the ProcessedMap |
| 73 | 72 |
#ifdef DOXYGEN |
| 74 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 75 | 74 |
#else |
| 76 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 77 | 76 |
#endif |
| 78 | 77 |
{
|
| 79 | 78 |
return new ProcessedMap(); |
| 80 | 79 |
} |
| 81 | 80 |
|
| 82 | 81 |
///The type of the map that indicates which nodes are reached. |
| 83 | 82 |
|
| 84 | 83 |
///The type of the map that indicates which nodes are reached. |
| 85 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 86 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 87 | 86 |
///Instantiates a ReachedMap. |
| 88 | 87 |
|
| 89 | 88 |
///This function instantiates a ReachedMap. |
| 90 | 89 |
///\param g is the digraph, to which |
| 91 | 90 |
///we would like to define the ReachedMap. |
| 92 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 93 | 92 |
{
|
| 94 | 93 |
return new ReachedMap(g); |
| 95 | 94 |
} |
| 96 | 95 |
|
| 97 | 96 |
///The type of the map that stores the distances of the nodes. |
| 98 | 97 |
|
| 99 | 98 |
///The type of the map that stores the distances of the nodes. |
| 100 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 101 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 102 | 101 |
///Instantiates a DistMap. |
| 103 | 102 |
|
| 104 | 103 |
///This function instantiates a DistMap. |
| 105 | 104 |
///\param g is the digraph, to which we would like to define the |
| 106 | 105 |
///DistMap. |
| 107 | 106 |
static DistMap *createDistMap(const Digraph &g) |
| 108 | 107 |
{
|
| 109 | 108 |
return new DistMap(g); |
| 110 | 109 |
} |
| 111 | 110 |
}; |
| 112 | 111 |
|
| 113 | 112 |
///%DFS algorithm class. |
| 114 | 113 |
|
| 115 | 114 |
///\ingroup search |
| 116 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
| 117 | 116 |
/// |
| 118 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
| 119 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
| 120 | 119 |
///used easier. |
| 121 | 120 |
/// |
| 122 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
| 123 | 122 |
///The default value is \ref ListDigraph. The value of GR is not used |
| 124 | 123 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
| 125 | 124 |
///\tparam TR Traits class to set various data types used by the algorithm. |
| 126 | 125 |
///The default traits class is |
| 127 | 126 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
| 128 | 127 |
///See \ref DfsDefaultTraits for the documentation of |
| 129 | 128 |
///a Dfs traits class. |
| 130 | 129 |
#ifdef DOXYGEN |
| 131 | 130 |
template <typename GR, |
| 132 | 131 |
typename TR> |
| 133 | 132 |
#else |
| 134 | 133 |
template <typename GR=ListDigraph, |
| 135 | 134 |
typename TR=DfsDefaultTraits<GR> > |
| 136 | 135 |
#endif |
| 137 | 136 |
class Dfs {
|
| 138 | 137 |
public: |
| 139 | 138 |
|
| 140 | 139 |
///The type of the digraph the algorithm runs on. |
| 141 | 140 |
typedef typename TR::Digraph Digraph; |
| 142 | 141 |
|
| 143 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
| 144 | 143 |
///DFS paths. |
| 145 | 144 |
typedef typename TR::PredMap PredMap; |
| 146 | 145 |
///The type of the map that stores the distances of the nodes. |
| 147 | 146 |
typedef typename TR::DistMap DistMap; |
| 148 | 147 |
///The type of the map that indicates which nodes are reached. |
| 149 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
| 150 | 149 |
///The type of the map that indicates which nodes are processed. |
| 151 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 152 | 151 |
///The type of the paths. |
| 153 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 154 | 153 |
|
| 155 | 154 |
///The traits class. |
| 156 | 155 |
typedef TR Traits; |
| 157 | 156 |
|
| 158 | 157 |
private: |
| 159 | 158 |
|
| 160 | 159 |
typedef typename Digraph::Node Node; |
| 161 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
| 162 | 161 |
typedef typename Digraph::Arc Arc; |
| 163 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 164 | 163 |
|
| 165 | 164 |
//Pointer to the underlying digraph. |
| 166 | 165 |
const Digraph *G; |
| 167 | 166 |
//Pointer to the map of predecessor arcs. |
| 168 | 167 |
PredMap *_pred; |
| 169 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
| 170 | 169 |
bool local_pred; |
| 171 | 170 |
//Pointer to the map of distances. |
| 172 | 171 |
DistMap *_dist; |
| 173 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
| 174 | 173 |
bool local_dist; |
| 175 | 174 |
//Pointer to the map of reached status of the nodes. |
| 176 | 175 |
ReachedMap *_reached; |
| 177 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
| 178 | 177 |
bool local_reached; |
| 179 | 178 |
//Pointer to the map of processed status of the nodes. |
| 180 | 179 |
ProcessedMap *_processed; |
| 181 | 180 |
//Indicates if _processed is locally allocated (true) or not. |
| 182 | 181 |
bool local_processed; |
| 183 | 182 |
|
| 184 | 183 |
std::vector<typename Digraph::OutArcIt> _stack; |
| 185 | 184 |
int _stack_head; |
| 186 | 185 |
|
| 187 | 186 |
//Creates the maps if necessary. |
| 188 | 187 |
void create_maps() |
| 189 | 188 |
{
|
| 190 | 189 |
if(!_pred) {
|
| 191 | 190 |
local_pred = true; |
| 192 | 191 |
_pred = Traits::createPredMap(*G); |
| 193 | 192 |
} |
| 194 | 193 |
if(!_dist) {
|
| 195 | 194 |
local_dist = true; |
| 196 | 195 |
_dist = Traits::createDistMap(*G); |
| 197 | 196 |
} |
| 198 | 197 |
if(!_reached) {
|
| 199 | 198 |
local_reached = true; |
| 200 | 199 |
_reached = Traits::createReachedMap(*G); |
| 201 | 200 |
} |
| 202 | 201 |
if(!_processed) {
|
| 203 | 202 |
local_processed = true; |
| 204 | 203 |
_processed = Traits::createProcessedMap(*G); |
| 205 | 204 |
} |
| 206 | 205 |
} |
| 207 | 206 |
|
| 208 | 207 |
protected: |
| 209 | 208 |
|
| 210 | 209 |
Dfs() {}
|
| 211 | 210 |
|
| 212 | 211 |
public: |
| 213 | 212 |
|
| 214 | 213 |
typedef Dfs Create; |
| 215 | 214 |
|
| 216 | 215 |
///\name Named template parameters |
| 217 | 216 |
|
| 218 | 217 |
///@{
|
| 219 | 218 |
|
| 220 | 219 |
template <class T> |
| 221 | 220 |
struct SetPredMapTraits : public Traits {
|
| 222 | 221 |
typedef T PredMap; |
| 223 | 222 |
static PredMap *createPredMap(const Digraph &) |
| 224 | 223 |
{
|
| 225 | 224 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 226 | 225 |
return 0; // ignore warnings |
| 227 | 226 |
} |
| 228 | 227 |
}; |
| 229 | 228 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 230 | 229 |
///PredMap type. |
| 231 | 230 |
/// |
| 232 | 231 |
///\ref named-templ-param "Named parameter" for setting |
| 233 | 232 |
///PredMap type. |
| 234 | 233 |
template <class T> |
| 235 | 234 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
|
| 236 | 235 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
| 237 | 236 |
}; |
| 238 | 237 |
|
| 239 | 238 |
template <class T> |
| 240 | 239 |
struct SetDistMapTraits : public Traits {
|
| 241 | 240 |
typedef T DistMap; |
| 242 | 241 |
static DistMap *createDistMap(const Digraph &) |
| 243 | 242 |
{
|
| 244 | 243 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 245 | 244 |
return 0; // ignore warnings |
| 246 | 245 |
} |
| 247 | 246 |
}; |
| 248 | 247 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 249 | 248 |
///DistMap type. |
| 250 | 249 |
/// |
| 251 | 250 |
///\ref named-templ-param "Named parameter" for setting |
| 252 | 251 |
///DistMap type. |
| 253 | 252 |
template <class T> |
| 254 | 253 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
|
| 255 | 254 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
| 256 | 255 |
}; |
| 257 | 256 |
|
| 258 | 257 |
template <class T> |
| 259 | 258 |
struct SetReachedMapTraits : public Traits {
|
| 260 | 259 |
typedef T ReachedMap; |
| 261 | 260 |
static ReachedMap *createReachedMap(const Digraph &) |
| 262 | 261 |
{
|
| 263 | 262 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 264 | 263 |
return 0; // ignore warnings |
| 265 | 264 |
} |
| 266 | 265 |
}; |
| 267 | 266 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 268 | 267 |
///ReachedMap type. |
| 269 | 268 |
/// |
| 270 | 269 |
///\ref named-templ-param "Named parameter" for setting |
| 271 | 270 |
///ReachedMap type. |
| 272 | 271 |
template <class T> |
| 273 | 272 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
|
| 274 | 273 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
| 275 | 274 |
}; |
| 276 | 275 |
|
| 277 | 276 |
template <class T> |
| 278 | 277 |
struct SetProcessedMapTraits : public Traits {
|
| 279 | 278 |
typedef T ProcessedMap; |
| 280 | 279 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 281 | 280 |
{
|
| 282 | 281 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 283 | 282 |
return 0; // ignore warnings |
| 284 | 283 |
} |
| 285 | 284 |
}; |
| 286 | 285 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 287 | 286 |
///ProcessedMap type. |
| 288 | 287 |
/// |
| 289 | 288 |
///\ref named-templ-param "Named parameter" for setting |
| 290 | 289 |
///ProcessedMap type. |
| 291 | 290 |
template <class T> |
| 292 | 291 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
|
| 293 | 292 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 294 | 293 |
}; |
| 295 | 294 |
|
| 296 | 295 |
struct SetStandardProcessedMapTraits : public Traits {
|
| 297 | 296 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
| 298 | 297 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 299 | 298 |
{
|
| 300 | 299 |
return new ProcessedMap(g); |
| 301 | 300 |
} |
| 302 | 301 |
}; |
| 303 | 302 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 304 | 303 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
| 305 | 304 |
/// |
| 306 | 305 |
///\ref named-templ-param "Named parameter" for setting |
| 307 | 306 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
| 308 | 307 |
///If you don't set it explicitly, it will be automatically allocated. |
| 309 | 308 |
struct SetStandardProcessedMap : |
| 310 | 309 |
public Dfs< Digraph, SetStandardProcessedMapTraits > {
|
| 311 | 310 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
| 312 | 311 |
}; |
| 313 | 312 |
|
| 314 | 313 |
///@} |
| 315 | 314 |
|
| 316 | 315 |
public: |
| 317 | 316 |
|
| 318 | 317 |
///Constructor. |
| 319 | 318 |
|
| 320 | 319 |
///Constructor. |
| 321 | 320 |
///\param g The digraph the algorithm runs on. |
| 322 | 321 |
Dfs(const Digraph &g) : |
| 323 | 322 |
G(&g), |
| 324 | 323 |
_pred(NULL), local_pred(false), |
| 325 | 324 |
_dist(NULL), local_dist(false), |
| 326 | 325 |
_reached(NULL), local_reached(false), |
| 327 | 326 |
_processed(NULL), local_processed(false) |
| 328 | 327 |
{ }
|
| 329 | 328 |
|
| 330 | 329 |
///Destructor. |
| 331 | 330 |
~Dfs() |
| 332 | 331 |
{
|
| 333 | 332 |
if(local_pred) delete _pred; |
| 334 | 333 |
if(local_dist) delete _dist; |
| 335 | 334 |
if(local_reached) delete _reached; |
| 336 | 335 |
if(local_processed) delete _processed; |
| 337 | 336 |
} |
| 338 | 337 |
|
| 339 | 338 |
///Sets the map that stores the predecessor arcs. |
| 340 | 339 |
|
| 341 | 340 |
///Sets the map that stores the predecessor arcs. |
| 342 | 341 |
///If you don't use this function before calling \ref run(), |
| 343 | 342 |
///it will allocate one. The destructor deallocates this |
| 344 | 343 |
///automatically allocated map, of course. |
| 345 | 344 |
///\return <tt> (*this) </tt> |
| 346 | 345 |
Dfs &predMap(PredMap &m) |
| 347 | 346 |
{
|
| 348 | 347 |
if(local_pred) {
|
| 349 | 348 |
delete _pred; |
| 350 | 349 |
local_pred=false; |
| 351 | 350 |
} |
| 352 | 351 |
_pred = &m; |
| 353 | 352 |
return *this; |
| 354 | 353 |
} |
| 355 | 354 |
|
| 356 | 355 |
///Sets the map that indicates which nodes are reached. |
| 357 | 356 |
|
| 358 | 357 |
///Sets the map that indicates which nodes are reached. |
| 359 | 358 |
///If you don't use this function before calling \ref run(), |
| 360 | 359 |
///it will allocate one. The destructor deallocates this |
| 361 | 360 |
///automatically allocated map, of course. |
| 362 | 361 |
///\return <tt> (*this) </tt> |
| 363 | 362 |
Dfs &reachedMap(ReachedMap &m) |
| 364 | 363 |
{
|
| 365 | 364 |
if(local_reached) {
|
| 366 | 365 |
delete _reached; |
| 367 | 366 |
local_reached=false; |
| 368 | 367 |
} |
| 369 | 368 |
_reached = &m; |
| 370 | 369 |
return *this; |
| 371 | 370 |
} |
| 372 | 371 |
|
| 373 | 372 |
///Sets the map that indicates which nodes are processed. |
| 374 | 373 |
|
| 375 | 374 |
///Sets the map that indicates which nodes are processed. |
| 376 | 375 |
///If you don't use this function before calling \ref run(), |
| 377 | 376 |
///it will allocate one. The destructor deallocates this |
| 378 | 377 |
///automatically allocated map, of course. |
| 379 | 378 |
///\return <tt> (*this) </tt> |
| 380 | 379 |
Dfs &processedMap(ProcessedMap &m) |
| 381 | 380 |
{
|
| 382 | 381 |
if(local_processed) {
|
| 383 | 382 |
delete _processed; |
| 384 | 383 |
local_processed=false; |
| 385 | 384 |
} |
| 386 | 385 |
_processed = &m; |
| 387 | 386 |
return *this; |
| 388 | 387 |
} |
| 389 | 388 |
|
| 390 | 389 |
///Sets the map that stores the distances of the nodes. |
| 391 | 390 |
|
| 392 | 391 |
///Sets the map that stores the distances of the nodes calculated by |
| 393 | 392 |
///the algorithm. |
| 394 | 393 |
///If you don't use this function before calling \ref run(), |
| 395 | 394 |
///it will allocate one. The destructor deallocates this |
| 396 | 395 |
///automatically allocated map, of course. |
| 397 | 396 |
///\return <tt> (*this) </tt> |
| 398 | 397 |
Dfs &distMap(DistMap &m) |
| 399 | 398 |
{
|
| 400 | 399 |
if(local_dist) {
|
| 401 | 400 |
delete _dist; |
| 402 | 401 |
local_dist=false; |
| 403 | 402 |
} |
| 404 | 403 |
_dist = &m; |
| 405 | 404 |
return *this; |
| 406 | 405 |
} |
| 407 | 406 |
|
| 408 | 407 |
public: |
| 409 | 408 |
|
| 410 | 409 |
///\name Execution control |
| 411 | 410 |
///The simplest way to execute the algorithm is to use |
| 412 | 411 |
///one of the member functions called \ref lemon::Dfs::run() "run()". |
| 413 | 412 |
///\n |
| 414 | 413 |
///If you need more control on the execution, first you must call |
| 415 | 414 |
///\ref lemon::Dfs::init() "init()", then you can add a source node |
| 416 | 415 |
///with \ref lemon::Dfs::addSource() "addSource()". |
| 417 | 416 |
///Finally \ref lemon::Dfs::start() "start()" will perform the |
| 418 | 417 |
///actual path computation. |
| 419 | 418 |
|
| 420 | 419 |
///@{
|
| 421 | 420 |
|
| 422 | 421 |
///Initializes the internal data structures. |
| 423 | 422 |
|
| 424 | 423 |
///Initializes the internal data structures. |
| 425 | 424 |
/// |
| 426 | 425 |
void init() |
| 427 | 426 |
{
|
| 428 | 427 |
create_maps(); |
| 429 | 428 |
_stack.resize(countNodes(*G)); |
| 430 | 429 |
_stack_head=-1; |
| 431 | 430 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
| 432 | 431 |
_pred->set(u,INVALID); |
| 433 | 432 |
_reached->set(u,false); |
| 434 | 433 |
_processed->set(u,false); |
| 435 | 434 |
} |
| 436 | 435 |
} |
| 437 | 436 |
|
| 438 | 437 |
///Adds a new source node. |
| 439 | 438 |
|
| 440 | 439 |
///Adds a new source node to the set of nodes to be processed. |
| 441 | 440 |
/// |
| 442 | 441 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
| 443 | 442 |
///false results.) |
| 444 | 443 |
/// |
| 445 | 444 |
///\warning Distances will be wrong (or at least strange) in case of |
| 446 | 445 |
///multiple sources. |
| 447 | 446 |
void addSource(Node s) |
| 448 | 447 |
{
|
| 449 | 448 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
| 450 | 449 |
if(!(*_reached)[s]) |
| 451 | 450 |
{
|
| 452 | 451 |
_reached->set(s,true); |
| 453 | 452 |
_pred->set(s,INVALID); |
| 454 | 453 |
OutArcIt e(*G,s); |
| 455 | 454 |
if(e!=INVALID) {
|
| 456 | 455 |
_stack[++_stack_head]=e; |
| 457 | 456 |
_dist->set(s,_stack_head); |
| 458 | 457 |
} |
| 459 | 458 |
else {
|
| 460 | 459 |
_processed->set(s,true); |
| 461 | 460 |
_dist->set(s,0); |
| 462 | 461 |
} |
| 463 | 462 |
} |
| 464 | 463 |
} |
| 465 | 464 |
|
| 466 | 465 |
///Processes the next arc. |
| 467 | 466 |
|
| 468 | 467 |
///Processes the next arc. |
| 469 | 468 |
/// |
| 470 | 469 |
///\return The processed arc. |
| 471 | 470 |
/// |
| 472 | 471 |
///\pre The stack must not be empty. |
| 473 | 472 |
Arc processNextArc() |
| 474 | 473 |
{
|
| 475 | 474 |
Node m; |
| 476 | 475 |
Arc e=_stack[_stack_head]; |
| 477 | 476 |
if(!(*_reached)[m=G->target(e)]) {
|
| 478 | 477 |
_pred->set(m,e); |
| 479 | 478 |
_reached->set(m,true); |
| 480 | 479 |
++_stack_head; |
| 481 | 480 |
_stack[_stack_head] = OutArcIt(*G, m); |
| 482 | 481 |
_dist->set(m,_stack_head); |
| 483 | 482 |
} |
| 484 | 483 |
else {
|
| 485 | 484 |
m=G->source(e); |
| 486 | 485 |
++_stack[_stack_head]; |
| 487 | 486 |
} |
| 488 | 487 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
|
| 489 | 488 |
_processed->set(m,true); |
| 490 | 489 |
--_stack_head; |
| 491 | 490 |
if(_stack_head>=0) {
|
| 492 | 491 |
m=G->source(_stack[_stack_head]); |
| 493 | 492 |
++_stack[_stack_head]; |
| 494 | 493 |
} |
| 495 | 494 |
} |
| 496 | 495 |
return e; |
| 497 | 496 |
} |
| 498 | 497 |
|
| 499 | 498 |
///Next arc to be processed. |
| 500 | 499 |
|
| 501 | 500 |
///Next arc to be processed. |
| 502 | 501 |
/// |
| 503 | 502 |
///\return The next arc to be processed or \c INVALID if the stack |
| 504 | 503 |
///is empty. |
| 505 | 504 |
OutArcIt nextArc() const |
| 506 | 505 |
{
|
| 507 | 506 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
| 508 | 507 |
} |
| 509 | 508 |
|
| 510 | 509 |
///\brief Returns \c false if there are nodes |
| 511 | 510 |
///to be processed. |
| 512 | 511 |
/// |
| 513 | 512 |
///Returns \c false if there are nodes |
| 514 | 513 |
///to be processed in the queue (stack). |
| 515 | 514 |
bool emptyQueue() const { return _stack_head<0; }
|
| 516 | 515 |
|
| 517 | 516 |
///Returns the number of the nodes to be processed. |
| 518 | 517 |
|
| 519 | 518 |
///Returns the number of the nodes to be processed in the queue (stack). |
| 520 | 519 |
int queueSize() const { return _stack_head+1; }
|
| 521 | 520 |
|
| 522 | 521 |
///Executes the algorithm. |
| 523 | 522 |
|
| 524 | 523 |
///Executes the algorithm. |
| 525 | 524 |
/// |
| 526 | 525 |
///This method runs the %DFS algorithm from the root node |
| 527 | 526 |
///in order to compute the DFS path to each node. |
| 528 | 527 |
/// |
| 529 | 528 |
/// The algorithm computes |
| 530 | 529 |
///- the %DFS tree, |
| 531 | 530 |
///- the distance of each node from the root in the %DFS tree. |
| 532 | 531 |
/// |
| 533 | 532 |
///\pre init() must be called and a root node should be |
| 534 | 533 |
///added with addSource() before using this function. |
| 535 | 534 |
/// |
| 536 | 535 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
| 537 | 536 |
///\code |
| 538 | 537 |
/// while ( !d.emptyQueue() ) {
|
| 539 | 538 |
/// d.processNextArc(); |
| 540 | 539 |
/// } |
| 541 | 540 |
///\endcode |
| 542 | 541 |
void start() |
| 543 | 542 |
{
|
| 544 | 543 |
while ( !emptyQueue() ) processNextArc(); |
| 545 | 544 |
} |
| 546 | 545 |
|
| 547 | 546 |
///Executes the algorithm until the given target node is reached. |
| 548 | 547 |
|
| 549 | 548 |
///Executes the algorithm until the given target node is reached. |
| 550 | 549 |
/// |
| 551 | 550 |
///This method runs the %DFS algorithm from the root node |
| 552 | 551 |
///in order to compute the DFS path to \c t. |
| 553 | 552 |
/// |
| 554 | 553 |
///The algorithm computes |
| 555 | 554 |
///- the %DFS path to \c t, |
| 556 | 555 |
///- the distance of \c t from the root in the %DFS tree. |
| 557 | 556 |
/// |
| 558 | 557 |
///\pre init() must be called and a root node should be |
| 559 | 558 |
///added with addSource() before using this function. |
| 560 | 559 |
void start(Node t) |
| 561 | 560 |
{
|
| 562 | 561 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=t ) |
| 563 | 562 |
processNextArc(); |
| 564 | 563 |
} |
| 565 | 564 |
|
| 566 | 565 |
///Executes the algorithm until a condition is met. |
| 567 | 566 |
|
| 568 | 567 |
///Executes the algorithm until a condition is met. |
| 569 | 568 |
/// |
| 570 | 569 |
///This method runs the %DFS algorithm from the root node |
| 571 | 570 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
| 572 | 571 |
/// |
| 573 | 572 |
///\param am A \c bool (or convertible) arc map. The algorithm |
| 574 | 573 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
| 575 | 574 |
/// |
| 576 | 575 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
| 577 | 576 |
///\c INVALID if no such arc was found. |
| 578 | 577 |
/// |
| 579 | 578 |
///\pre init() must be called and a root node should be |
| 580 | 579 |
///added with addSource() before using this function. |
| 581 | 580 |
/// |
| 582 | 581 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
| 583 | 582 |
///not a node map. |
| 584 | 583 |
template<class ArcBoolMap> |
| 585 | 584 |
Arc start(const ArcBoolMap &am) |
| 586 | 585 |
{
|
| 587 | 586 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
| 588 | 587 |
processNextArc(); |
| 589 | 588 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
| 590 | 589 |
} |
| 591 | 590 |
|
| 592 | 591 |
///Runs the algorithm from the given source node. |
| 593 | 592 |
|
| 594 | 593 |
///This method runs the %DFS algorithm from node \c s |
| 595 | 594 |
///in order to compute the DFS path to each node. |
| 596 | 595 |
/// |
| 597 | 596 |
///The algorithm computes |
| 598 | 597 |
///- the %DFS tree, |
| 599 | 598 |
///- the distance of each node from the root in the %DFS tree. |
| 600 | 599 |
/// |
| 601 | 600 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
| 602 | 601 |
///\code |
| 603 | 602 |
/// d.init(); |
| 604 | 603 |
/// d.addSource(s); |
| 605 | 604 |
/// d.start(); |
| 606 | 605 |
///\endcode |
| 607 | 606 |
void run(Node s) {
|
| 608 | 607 |
init(); |
| 609 | 608 |
addSource(s); |
| 610 | 609 |
start(); |
| 611 | 610 |
} |
| 612 | 611 |
|
| 613 | 612 |
///Finds the %DFS path between \c s and \c t. |
| 614 | 613 |
|
| 615 | 614 |
///This method runs the %DFS algorithm from node \c s |
| 616 | 615 |
///in order to compute the DFS path to node \c t |
| 617 | 616 |
///(it stops searching when \c t is processed) |
| 618 | 617 |
/// |
| 619 | 618 |
///\return \c true if \c t is reachable form \c s. |
| 620 | 619 |
/// |
| 621 | 620 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
| 622 | 621 |
///just a shortcut of the following code. |
| 623 | 622 |
///\code |
| 624 | 623 |
/// d.init(); |
| 625 | 624 |
/// d.addSource(s); |
| 626 | 625 |
/// d.start(t); |
| 627 | 626 |
///\endcode |
| 628 | 627 |
bool run(Node s,Node t) {
|
| 629 | 628 |
init(); |
| 630 | 629 |
addSource(s); |
| 631 | 630 |
start(t); |
| 632 | 631 |
return reached(t); |
| 633 | 632 |
} |
| 634 | 633 |
|
| 635 | 634 |
///Runs the algorithm to visit all nodes in the digraph. |
| 636 | 635 |
|
| 637 | 636 |
///This method runs the %DFS algorithm in order to compute the |
| 638 | 637 |
///%DFS path to each node. |
| 639 | 638 |
/// |
| 640 | 639 |
///The algorithm computes |
| 641 | 640 |
///- the %DFS tree, |
| 642 | 641 |
///- the distance of each node from the root in the %DFS tree. |
| 643 | 642 |
/// |
| 644 | 643 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
| 645 | 644 |
///\code |
| 646 | 645 |
/// d.init(); |
| 647 | 646 |
/// for (NodeIt n(digraph); n != INVALID; ++n) {
|
| 648 | 647 |
/// if (!d.reached(n)) {
|
| 649 | 648 |
/// d.addSource(n); |
| 650 | 649 |
/// d.start(); |
| 651 | 650 |
/// } |
| 652 | 651 |
/// } |
| 653 | 652 |
///\endcode |
| 654 | 653 |
void run() {
|
| 655 | 654 |
init(); |
| 656 | 655 |
for (NodeIt it(*G); it != INVALID; ++it) {
|
| 657 | 656 |
if (!reached(it)) {
|
| 658 | 657 |
addSource(it); |
| 659 | 658 |
start(); |
| 660 | 659 |
} |
| 661 | 660 |
} |
| 662 | 661 |
} |
| 663 | 662 |
|
| 664 | 663 |
///@} |
| 665 | 664 |
|
| 666 | 665 |
///\name Query Functions |
| 667 | 666 |
///The result of the %DFS algorithm can be obtained using these |
| 668 | 667 |
///functions.\n |
| 669 | 668 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
| 670 | 669 |
///"start()" must be called before using them. |
| 671 | 670 |
|
| 672 | 671 |
///@{
|
| 673 | 672 |
|
| 674 | 673 |
///The DFS path to a node. |
| 675 | 674 |
|
| 676 | 675 |
///Returns the DFS path to a node. |
| 677 | 676 |
/// |
| 678 | 677 |
///\warning \c t should be reachable from the root. |
| 679 | 678 |
/// |
| 680 | 679 |
///\pre Either \ref run() or \ref start() must be called before |
| 681 | 680 |
///using this function. |
| 682 | 681 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
| 683 | 682 |
|
| 684 | 683 |
///The distance of a node from the root. |
| 685 | 684 |
|
| 686 | 685 |
///Returns the distance of a node from the root. |
| 687 | 686 |
/// |
| 688 | 687 |
///\warning If node \c v is not reachable from the root, then |
| 689 | 688 |
///the return value of this function is undefined. |
| 690 | 689 |
/// |
| 691 | 690 |
///\pre Either \ref run() or \ref start() must be called before |
| 692 | 691 |
///using this function. |
| 693 | 692 |
int dist(Node v) const { return (*_dist)[v]; }
|
| 694 | 693 |
|
| 695 | 694 |
///Returns the 'previous arc' of the %DFS tree for a node. |
| 696 | 695 |
|
| 697 | 696 |
///This function returns the 'previous arc' of the %DFS tree for the |
| 698 | 697 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
| 699 | 698 |
///root to \c v. It is \c INVALID |
| 700 | 699 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
| 701 | 700 |
/// |
| 702 | 701 |
///The %DFS tree used here is equal to the %DFS tree used in |
| 703 | 702 |
///\ref predNode(). |
| 704 | 703 |
/// |
| 705 | 704 |
///\pre Either \ref run() or \ref start() must be called before using |
| 706 | 705 |
///this function. |
| 707 | 706 |
Arc predArc(Node v) const { return (*_pred)[v];}
|
| 708 | 707 |
|
| 709 | 708 |
///Returns the 'previous node' of the %DFS tree. |
| 710 | 709 |
|
| 711 | 710 |
///This function returns the 'previous node' of the %DFS |
| 712 | 711 |
///tree for the node \c v, i.e. it returns the last but one node |
| 713 | 712 |
///from a %DFS path from the root to \c v. It is \c INVALID |
| 714 | 713 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
| 715 | 714 |
/// |
| 716 | 715 |
///The %DFS tree used here is equal to the %DFS tree used in |
| 717 | 716 |
///\ref predArc(). |
| 718 | 717 |
/// |
| 719 | 718 |
///\pre Either \ref run() or \ref start() must be called before |
| 720 | 719 |
///using this function. |
| 721 | 720 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
|
| 722 | 721 |
G->source((*_pred)[v]); } |
| 723 | 722 |
|
| 724 | 723 |
///\brief Returns a const reference to the node map that stores the |
| 725 | 724 |
///distances of the nodes. |
| 726 | 725 |
/// |
| 727 | 726 |
///Returns a const reference to the node map that stores the |
| 728 | 727 |
///distances of the nodes calculated by the algorithm. |
| 729 | 728 |
/// |
| 730 | 729 |
///\pre Either \ref run() or \ref init() |
| 731 | 730 |
///must be called before using this function. |
| 732 | 731 |
const DistMap &distMap() const { return *_dist;}
|
| 733 | 732 |
|
| 734 | 733 |
///\brief Returns a const reference to the node map that stores the |
| 735 | 734 |
///predecessor arcs. |
| 736 | 735 |
/// |
| 737 | 736 |
///Returns a const reference to the node map that stores the predecessor |
| 738 | 737 |
///arcs, which form the DFS tree. |
| 739 | 738 |
/// |
| 740 | 739 |
///\pre Either \ref run() or \ref init() |
| 741 | 740 |
///must be called before using this function. |
| 742 | 741 |
const PredMap &predMap() const { return *_pred;}
|
| 743 | 742 |
|
| 744 | 743 |
///Checks if a node is reachable from the root(s). |
| 745 | 744 |
|
| 746 | 745 |
///Returns \c true if \c v is reachable from the root(s). |
| 747 | 746 |
///\pre Either \ref run() or \ref start() |
| 748 | 747 |
///must be called before using this function. |
| 749 | 748 |
bool reached(Node v) const { return (*_reached)[v]; }
|
| 750 | 749 |
|
| 751 | 750 |
///@} |
| 752 | 751 |
}; |
| 753 | 752 |
|
| 754 | 753 |
///Default traits class of dfs() function. |
| 755 | 754 |
|
| 756 | 755 |
///Default traits class of dfs() function. |
| 757 | 756 |
///\tparam GR Digraph type. |
| 758 | 757 |
template<class GR> |
| 759 | 758 |
struct DfsWizardDefaultTraits |
| 760 | 759 |
{
|
| 761 | 760 |
///The type of the digraph the algorithm runs on. |
| 762 | 761 |
typedef GR Digraph; |
| 763 | 762 |
|
| 764 | 763 |
///\brief The type of the map that stores the predecessor |
| 765 | 764 |
///arcs of the %DFS paths. |
| 766 | 765 |
/// |
| 767 | 766 |
///The type of the map that stores the predecessor |
| 768 | 767 |
///arcs of the %DFS paths. |
| 769 | 768 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 770 | 769 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 771 | 770 |
///Instantiates a PredMap. |
| 772 | 771 |
|
| 773 | 772 |
///This function instantiates a PredMap. |
| 774 | 773 |
///\param g is the digraph, to which we would like to define the |
| 775 | 774 |
///PredMap. |
| 776 | 775 |
static PredMap *createPredMap(const Digraph &g) |
| 777 | 776 |
{
|
| 778 | 777 |
return new PredMap(g); |
| 779 | 778 |
} |
| 780 | 779 |
|
| 781 | 780 |
///The type of the map that indicates which nodes are processed. |
| 782 | 781 |
|
| 783 | 782 |
///The type of the map that indicates which nodes are processed. |
| 784 | 783 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 785 | 784 |
///By default it is a NullMap. |
| 786 | 785 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 787 | 786 |
///Instantiates a ProcessedMap. |
| 788 | 787 |
|
| 789 | 788 |
///This function instantiates a ProcessedMap. |
| 790 | 789 |
///\param g is the digraph, to which |
| 791 | 790 |
///we would like to define the ProcessedMap. |
| 792 | 791 |
#ifdef DOXYGEN |
| 793 | 792 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 794 | 793 |
#else |
| 795 | 794 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 796 | 795 |
#endif |
| 797 | 796 |
{
|
| 798 | 797 |
return new ProcessedMap(); |
| 799 | 798 |
} |
| 800 | 799 |
|
| 801 | 800 |
///The type of the map that indicates which nodes are reached. |
| 802 | 801 |
|
| 803 | 802 |
///The type of the map that indicates which nodes are reached. |
| 804 | 803 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 805 | 804 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 806 | 805 |
///Instantiates a ReachedMap. |
| 807 | 806 |
|
| 808 | 807 |
///This function instantiates a ReachedMap. |
| 809 | 808 |
///\param g is the digraph, to which |
| 810 | 809 |
///we would like to define the ReachedMap. |
| 811 | 810 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 812 | 811 |
{
|
| 813 | 812 |
return new ReachedMap(g); |
| 814 | 813 |
} |
| 815 | 814 |
|
| 816 | 815 |
///The type of the map that stores the distances of the nodes. |
| 817 | 816 |
|
| 818 | 817 |
///The type of the map that stores the distances of the nodes. |
| 819 | 818 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
| 820 | 819 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 821 | 820 |
///Instantiates a DistMap. |
| 822 | 821 |
|
| 823 | 822 |
///This function instantiates a DistMap. |
| 824 | 823 |
///\param g is the digraph, to which we would like to define |
| 825 | 824 |
///the DistMap |
| 826 | 825 |
static DistMap *createDistMap(const Digraph &g) |
| 827 | 826 |
{
|
| 828 | 827 |
return new DistMap(g); |
| 829 | 828 |
} |
| 830 | 829 |
|
| 831 | 830 |
///The type of the DFS paths. |
| 832 | 831 |
|
| 833 | 832 |
///The type of the DFS paths. |
| 834 | 833 |
///It must meet the \ref concepts::Path "Path" concept. |
| 835 | 834 |
typedef lemon::Path<Digraph> Path; |
| 836 | 835 |
}; |
| 837 | 836 |
|
| 838 | 837 |
/// Default traits class used by DfsWizard |
| 839 | 838 |
|
| 840 | 839 |
/// To make it easier to use Dfs algorithm |
| 841 | 840 |
/// we have created a wizard class. |
| 842 | 841 |
/// This \ref DfsWizard class needs default traits, |
| 843 | 842 |
/// as well as the \ref Dfs class. |
| 844 | 843 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
| 845 | 844 |
/// \ref DfsWizard class. |
| 846 | 845 |
template<class GR> |
| 847 | 846 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
| 848 | 847 |
{
|
| 849 | 848 |
|
| 850 | 849 |
typedef DfsWizardDefaultTraits<GR> Base; |
| 851 | 850 |
protected: |
| 852 | 851 |
//The type of the nodes in the digraph. |
| 853 | 852 |
typedef typename Base::Digraph::Node Node; |
| 854 | 853 |
|
| 855 | 854 |
//Pointer to the digraph the algorithm runs on. |
| 856 | 855 |
void *_g; |
| 857 | 856 |
//Pointer to the map of reached nodes. |
| 858 | 857 |
void *_reached; |
| 859 | 858 |
//Pointer to the map of processed nodes. |
| 860 | 859 |
void *_processed; |
| 861 | 860 |
//Pointer to the map of predecessors arcs. |
| 862 | 861 |
void *_pred; |
| 863 | 862 |
//Pointer to the map of distances. |
| 864 | 863 |
void *_dist; |
| 865 | 864 |
//Pointer to the DFS path to the target node. |
| 866 | 865 |
void *_path; |
| 867 | 866 |
//Pointer to the distance of the target node. |
| 868 | 867 |
int *_di; |
| 869 | 868 |
|
| 870 | 869 |
public: |
| 871 | 870 |
/// Constructor. |
| 872 | 871 |
|
| 873 | 872 |
/// This constructor does not require parameters, therefore it initiates |
| 874 | 873 |
/// all of the attributes to \c 0. |
| 875 | 874 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
| 876 | 875 |
_dist(0), _path(0), _di(0) {}
|
| 877 | 876 |
|
| 878 | 877 |
/// Constructor. |
| 879 | 878 |
|
| 880 | 879 |
/// This constructor requires one parameter, |
| 881 | 880 |
/// others are initiated to \c 0. |
| 882 | 881 |
/// \param g The digraph the algorithm runs on. |
| 883 | 882 |
DfsWizardBase(const GR &g) : |
| 884 | 883 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
| 885 | 884 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 886 | 885 |
|
| 887 | 886 |
}; |
| 888 | 887 |
|
| 889 | 888 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
| 890 | 889 |
|
| 891 | 890 |
/// This auxiliary class is created to implement the |
| 892 | 891 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
| 893 | 892 |
/// It does not have own \ref run() method, it uses the functions |
| 894 | 893 |
/// and features of the plain \ref Dfs. |
| 895 | 894 |
/// |
| 896 | 895 |
/// This class should only be used through the \ref dfs() function, |
| 897 | 896 |
/// which makes it easier to use the algorithm. |
| 898 | 897 |
template<class TR> |
| 899 | 898 |
class DfsWizard : public TR |
| 900 | 899 |
{
|
| 901 | 900 |
typedef TR Base; |
| 902 | 901 |
|
| 903 | 902 |
///The type of the digraph the algorithm runs on. |
| 904 | 903 |
typedef typename TR::Digraph Digraph; |
| 905 | 904 |
|
| 906 | 905 |
typedef typename Digraph::Node Node; |
| 907 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
| 908 | 907 |
typedef typename Digraph::Arc Arc; |
| 909 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 910 | 909 |
|
| 911 | 910 |
///\brief The type of the map that stores the predecessor |
| 912 | 911 |
///arcs of the DFS paths. |
| 913 | 912 |
typedef typename TR::PredMap PredMap; |
| 914 | 913 |
///\brief The type of the map that stores the distances of the nodes. |
| 915 | 914 |
typedef typename TR::DistMap DistMap; |
| 916 | 915 |
///\brief The type of the map that indicates which nodes are reached. |
| 917 | 916 |
typedef typename TR::ReachedMap ReachedMap; |
| 918 | 917 |
///\brief The type of the map that indicates which nodes are processed. |
| 919 | 918 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 920 | 919 |
///The type of the DFS paths |
| 921 | 920 |
typedef typename TR::Path Path; |
| 922 | 921 |
|
| 923 | 922 |
public: |
| 924 | 923 |
|
| 925 | 924 |
/// Constructor. |
| 926 | 925 |
DfsWizard() : TR() {}
|
| 927 | 926 |
|
| 928 | 927 |
/// Constructor that requires parameters. |
| 929 | 928 |
|
| 930 | 929 |
/// Constructor that requires parameters. |
| 931 | 930 |
/// These parameters will be the default values for the traits class. |
| 932 | 931 |
/// \param g The digraph the algorithm runs on. |
| 933 | 932 |
DfsWizard(const Digraph &g) : |
| 934 | 933 |
TR(g) {}
|
| 935 | 934 |
|
| 936 | 935 |
///Copy constructor |
| 937 | 936 |
DfsWizard(const TR &b) : TR(b) {}
|
| 938 | 937 |
|
| 939 | 938 |
~DfsWizard() {}
|
| 940 | 939 |
|
| 941 | 940 |
///Runs DFS algorithm from the given source node. |
| 942 | 941 |
|
| 943 | 942 |
///This method runs DFS algorithm from node \c s |
| 944 | 943 |
///in order to compute the DFS path to each node. |
| 945 | 944 |
void run(Node s) |
| 946 | 945 |
{
|
| 947 | 946 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 948 | 947 |
if (Base::_pred) |
| 949 | 948 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 950 | 949 |
if (Base::_dist) |
| 951 | 950 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 952 | 951 |
if (Base::_reached) |
| 953 | 952 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 954 | 953 |
if (Base::_processed) |
| 955 | 954 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 956 | 955 |
if (s!=INVALID) |
| 957 | 956 |
alg.run(s); |
| 958 | 957 |
else |
| 959 | 958 |
alg.run(); |
| 960 | 959 |
} |
| 961 | 960 |
|
| 962 | 961 |
///Finds the DFS path between \c s and \c t. |
| 963 | 962 |
|
| 964 | 963 |
///This method runs DFS algorithm from node \c s |
| 965 | 964 |
///in order to compute the DFS path to node \c t |
| 966 | 965 |
///(it stops searching when \c t is processed). |
| 967 | 966 |
/// |
| 968 | 967 |
///\return \c true if \c t is reachable form \c s. |
| 969 | 968 |
bool run(Node s, Node t) |
| 970 | 969 |
{
|
| 971 | 970 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 972 | 971 |
if (Base::_pred) |
| 973 | 972 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 974 | 973 |
if (Base::_dist) |
| 975 | 974 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 976 | 975 |
if (Base::_reached) |
| 977 | 976 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 978 | 977 |
if (Base::_processed) |
| 979 | 978 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 980 | 979 |
alg.run(s,t); |
| 981 | 980 |
if (Base::_path) |
| 982 | 981 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
| 983 | 982 |
if (Base::_di) |
| 984 | 983 |
*Base::_di = alg.dist(t); |
| 985 | 984 |
return alg.reached(t); |
| 986 | 985 |
} |
| 987 | 986 |
|
| 988 | 987 |
///Runs DFS algorithm to visit all nodes in the digraph. |
| 989 | 988 |
|
| 990 | 989 |
///This method runs DFS algorithm in order to compute |
| 991 | 990 |
///the DFS path to each node. |
| 992 | 991 |
void run() |
| 993 | 992 |
{
|
| 994 | 993 |
run(INVALID); |
| 995 | 994 |
} |
| 996 | 995 |
|
| 997 | 996 |
template<class T> |
| 998 | 997 |
struct SetPredMapBase : public Base {
|
| 999 | 998 |
typedef T PredMap; |
| 1000 | 999 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1001 | 1000 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1002 | 1001 |
}; |
| 1003 | 1002 |
///\brief \ref named-func-param "Named parameter" |
| 1004 | 1003 |
///for setting PredMap object. |
| 1005 | 1004 |
/// |
| 1006 | 1005 |
///\ref named-func-param "Named parameter" |
| 1007 | 1006 |
///for setting PredMap object. |
| 1008 | 1007 |
template<class T> |
| 1009 | 1008 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
| 1010 | 1009 |
{
|
| 1011 | 1010 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1012 | 1011 |
return DfsWizard<SetPredMapBase<T> >(*this); |
| 1013 | 1012 |
} |
| 1014 | 1013 |
|
| 1015 | 1014 |
template<class T> |
| 1016 | 1015 |
struct SetReachedMapBase : public Base {
|
| 1017 | 1016 |
typedef T ReachedMap; |
| 1018 | 1017 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; };
|
| 1019 | 1018 |
SetReachedMapBase(const TR &b) : TR(b) {}
|
| 1020 | 1019 |
}; |
| 1021 | 1020 |
///\brief \ref named-func-param "Named parameter" |
| 1022 | 1021 |
///for setting ReachedMap object. |
| 1023 | 1022 |
/// |
| 1024 | 1023 |
/// \ref named-func-param "Named parameter" |
| 1025 | 1024 |
///for setting ReachedMap object. |
| 1026 | 1025 |
template<class T> |
| 1027 | 1026 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
| 1028 | 1027 |
{
|
| 1029 | 1028 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1030 | 1029 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
| 1031 | 1030 |
} |
| 1032 | 1031 |
|
| 1033 | 1032 |
template<class T> |
| 1034 | 1033 |
struct SetDistMapBase : public Base {
|
| 1035 | 1034 |
typedef T DistMap; |
| 1036 | 1035 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1037 | 1036 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1038 | 1037 |
}; |
| 1039 | 1038 |
///\brief \ref named-func-param "Named parameter" |
| 1040 | 1039 |
///for setting DistMap object. |
| 1041 | 1040 |
/// |
| 1042 | 1041 |
/// \ref named-func-param "Named parameter" |
| 1043 | 1042 |
///for setting DistMap object. |
| 1044 | 1043 |
template<class T> |
| 1045 | 1044 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
| 1046 | 1045 |
{
|
| 1047 | 1046 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1048 | 1047 |
return DfsWizard<SetDistMapBase<T> >(*this); |
| 1049 | 1048 |
} |
| 1050 | 1049 |
|
| 1051 | 1050 |
template<class T> |
| 1052 | 1051 |
struct SetProcessedMapBase : public Base {
|
| 1053 | 1052 |
typedef T ProcessedMap; |
| 1054 | 1053 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
| 1055 | 1054 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
| 1056 | 1055 |
}; |
| 1057 | 1056 |
///\brief \ref named-func-param "Named parameter" |
| 1058 | 1057 |
///for setting ProcessedMap object. |
| 1059 | 1058 |
/// |
| 1060 | 1059 |
/// \ref named-func-param "Named parameter" |
| 1061 | 1060 |
///for setting ProcessedMap object. |
| 1062 | 1061 |
template<class T> |
| 1063 | 1062 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
| 1064 | 1063 |
{
|
| 1065 | 1064 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1066 | 1065 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
| 1067 | 1066 |
} |
| 1068 | 1067 |
|
| 1069 | 1068 |
template<class T> |
| 1070 | 1069 |
struct SetPathBase : public Base {
|
| 1071 | 1070 |
typedef T Path; |
| 1072 | 1071 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1073 | 1072 |
}; |
| 1074 | 1073 |
///\brief \ref named-func-param "Named parameter" |
| 1075 | 1074 |
///for getting the DFS path to the target node. |
| 1076 | 1075 |
/// |
| 1077 | 1076 |
///\ref named-func-param "Named parameter" |
| 1078 | 1077 |
///for getting the DFS path to the target node. |
| 1079 | 1078 |
template<class T> |
| 1080 | 1079 |
DfsWizard<SetPathBase<T> > path(const T &t) |
| 1081 | 1080 |
{
|
| 1082 | 1081 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1083 | 1082 |
return DfsWizard<SetPathBase<T> >(*this); |
| 1084 | 1083 |
} |
| 1085 | 1084 |
|
| 1086 | 1085 |
///\brief \ref named-func-param "Named parameter" |
| 1087 | 1086 |
///for getting the distance of the target node. |
| 1088 | 1087 |
/// |
| 1089 | 1088 |
///\ref named-func-param "Named parameter" |
| 1090 | 1089 |
///for getting the distance of the target node. |
| 1091 | 1090 |
DfsWizard dist(const int &d) |
| 1092 | 1091 |
{
|
| 1093 | 1092 |
Base::_di=const_cast<int*>(&d); |
| 1094 | 1093 |
return *this; |
| 1095 | 1094 |
} |
| 1096 | 1095 |
|
| 1097 | 1096 |
}; |
| 1098 | 1097 |
|
| 1099 | 1098 |
///Function-type interface for DFS algorithm. |
| 1100 | 1099 |
|
| 1101 | 1100 |
///\ingroup search |
| 1102 | 1101 |
///Function-type interface for DFS algorithm. |
| 1103 | 1102 |
/// |
| 1104 | 1103 |
///This function also has several \ref named-func-param "named parameters", |
| 1105 | 1104 |
///they are declared as the members of class \ref DfsWizard. |
| 1106 | 1105 |
///The following examples show how to use these parameters. |
| 1107 | 1106 |
///\code |
| 1108 | 1107 |
/// // Compute the DFS tree |
| 1109 | 1108 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
| 1110 | 1109 |
/// |
| 1111 | 1110 |
/// // Compute the DFS path from s to t |
| 1112 | 1111 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
| 1113 | 1112 |
///\endcode |
| 1114 | 1113 |
|
| 1115 | 1114 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
| 1116 | 1115 |
///to the end of the parameter list. |
| 1117 | 1116 |
///\sa DfsWizard |
| 1118 | 1117 |
///\sa Dfs |
| 1119 | 1118 |
template<class GR> |
| 1120 | 1119 |
DfsWizard<DfsWizardBase<GR> > |
| 1121 | 1120 |
dfs(const GR &digraph) |
| 1122 | 1121 |
{
|
| 1123 | 1122 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
| 1124 | 1123 |
} |
| 1125 | 1124 |
|
| 1126 | 1125 |
#ifdef DOXYGEN |
| 1127 | 1126 |
/// \brief Visitor class for DFS. |
| 1128 | 1127 |
/// |
| 1129 | 1128 |
/// This class defines the interface of the DfsVisit events, and |
| 1130 | 1129 |
/// it could be the base of a real visitor class. |
| 1131 | 1130 |
template <typename _Digraph> |
| 1132 | 1131 |
struct DfsVisitor {
|
| 1133 | 1132 |
typedef _Digraph Digraph; |
| 1134 | 1133 |
typedef typename Digraph::Arc Arc; |
| 1135 | 1134 |
typedef typename Digraph::Node Node; |
| 1136 | 1135 |
/// \brief Called for the source node of the DFS. |
| 1137 | 1136 |
/// |
| 1138 | 1137 |
/// This function is called for the source node of the DFS. |
| 1139 | 1138 |
void start(const Node& node) {}
|
| 1140 | 1139 |
/// \brief Called when the source node is leaved. |
| 1141 | 1140 |
/// |
| 1142 | 1141 |
/// This function is called when the source node is leaved. |
| 1143 | 1142 |
void stop(const Node& node) {}
|
| 1144 | 1143 |
/// \brief Called when a node is reached first time. |
| 1145 | 1144 |
/// |
| 1146 | 1145 |
/// This function is called when a node is reached first time. |
| 1147 | 1146 |
void reach(const Node& node) {}
|
| 1148 | 1147 |
/// \brief Called when an arc reaches a new node. |
| 1149 | 1148 |
/// |
| 1150 | 1149 |
/// This function is called when the DFS finds an arc whose target node |
| 1151 | 1150 |
/// is not reached yet. |
| 1152 | 1151 |
void discover(const Arc& arc) {}
|
| 1153 | 1152 |
/// \brief Called when an arc is examined but its target node is |
| 1154 | 1153 |
/// already discovered. |
| 1155 | 1154 |
/// |
| 1156 | 1155 |
/// This function is called when an arc is examined but its target node is |
| 1157 | 1156 |
/// already discovered. |
| 1158 | 1157 |
void examine(const Arc& arc) {}
|
| 1159 | 1158 |
/// \brief Called when the DFS steps back from a node. |
| 1160 | 1159 |
/// |
| 1161 | 1160 |
/// This function is called when the DFS steps back from a node. |
| 1162 | 1161 |
void leave(const Node& node) {}
|
| 1163 | 1162 |
/// \brief Called when the DFS steps back on an arc. |
| 1164 | 1163 |
/// |
| 1165 | 1164 |
/// This function is called when the DFS steps back on an arc. |
| 1166 | 1165 |
void backtrack(const Arc& arc) {}
|
| 1167 | 1166 |
}; |
| 1168 | 1167 |
#else |
| 1169 | 1168 |
template <typename _Digraph> |
| 1170 | 1169 |
struct DfsVisitor {
|
| 1171 | 1170 |
typedef _Digraph Digraph; |
| 1172 | 1171 |
typedef typename Digraph::Arc Arc; |
| 1173 | 1172 |
typedef typename Digraph::Node Node; |
| 1174 | 1173 |
void start(const Node&) {}
|
| 1175 | 1174 |
void stop(const Node&) {}
|
| 1176 | 1175 |
void reach(const Node&) {}
|
| 1177 | 1176 |
void discover(const Arc&) {}
|
| 1178 | 1177 |
void examine(const Arc&) {}
|
| 1179 | 1178 |
void leave(const Node&) {}
|
| 1180 | 1179 |
void backtrack(const Arc&) {}
|
| 1181 | 1180 |
|
| 1182 | 1181 |
template <typename _Visitor> |
| 1183 | 1182 |
struct Constraints {
|
| 1184 | 1183 |
void constraints() {
|
| 1185 | 1184 |
Arc arc; |
| 1186 | 1185 |
Node node; |
| 1187 | 1186 |
visitor.start(node); |
| 1188 | 1187 |
visitor.stop(arc); |
| 1189 | 1188 |
visitor.reach(node); |
| 1190 | 1189 |
visitor.discover(arc); |
| 1191 | 1190 |
visitor.examine(arc); |
| 1192 | 1191 |
visitor.leave(node); |
| 1193 | 1192 |
visitor.backtrack(arc); |
| 1194 | 1193 |
} |
| 1195 | 1194 |
_Visitor& visitor; |
| 1196 | 1195 |
}; |
| 1197 | 1196 |
}; |
| 1198 | 1197 |
#endif |
| 1199 | 1198 |
|
| 1200 | 1199 |
/// \brief Default traits class of DfsVisit class. |
| 1201 | 1200 |
/// |
| 1202 | 1201 |
/// Default traits class of DfsVisit class. |
| 1203 | 1202 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1204 | 1203 |
template<class _Digraph> |
| 1205 | 1204 |
struct DfsVisitDefaultTraits {
|
| 1206 | 1205 |
|
| 1207 | 1206 |
/// \brief The type of the digraph the algorithm runs on. |
| 1208 | 1207 |
typedef _Digraph Digraph; |
| 1209 | 1208 |
|
| 1210 | 1209 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1211 | 1210 |
/// |
| 1212 | 1211 |
/// The type of the map that indicates which nodes are reached. |
| 1213 | 1212 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
| 1214 | 1213 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1215 | 1214 |
|
| 1216 | 1215 |
/// \brief Instantiates a ReachedMap. |
| 1217 | 1216 |
/// |
| 1218 | 1217 |
/// This function instantiates a ReachedMap. |
| 1219 | 1218 |
/// \param digraph is the digraph, to which |
| 1220 | 1219 |
/// we would like to define the ReachedMap. |
| 1221 | 1220 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1222 | 1221 |
return new ReachedMap(digraph); |
| 1223 | 1222 |
} |
| 1224 | 1223 |
|
| 1225 | 1224 |
}; |
| 1226 | 1225 |
|
| 1227 | 1226 |
/// \ingroup search |
| 1228 | 1227 |
/// |
| 1229 | 1228 |
/// \brief %DFS algorithm class with visitor interface. |
| 1230 | 1229 |
/// |
| 1231 | 1230 |
/// This class provides an efficient implementation of the %DFS algorithm |
| 1232 | 1231 |
/// with visitor interface. |
| 1233 | 1232 |
/// |
| 1234 | 1233 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
| 1235 | 1234 |
/// class. It works with callback mechanism, the DfsVisit object calls |
| 1236 | 1235 |
/// the member functions of the \c Visitor class on every DFS event. |
| 1237 | 1236 |
/// |
| 1238 | 1237 |
/// This interface of the DFS algorithm should be used in special cases |
| 1239 | 1238 |
/// when extra actions have to be performed in connection with certain |
| 1240 | 1239 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
| 1241 | 1240 |
/// instead. |
| 1242 | 1241 |
/// |
| 1243 | 1242 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1244 | 1243 |
/// The default value is |
| 1245 | 1244 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
| 1246 | 1245 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
| 1247 | 1246 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
| 1248 | 1247 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
| 1249 | 1248 |
/// does not observe the DFS events. If you want to observe the DFS |
| 1250 | 1249 |
/// events, you should implement your own visitor class. |
| 1251 | 1250 |
/// \tparam _Traits Traits class to set various data types used by the |
| 1252 | 1251 |
/// algorithm. The default traits class is |
| 1253 | 1252 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
| 1254 | 1253 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
| 1255 | 1254 |
/// a DFS visit traits class. |
| 1256 | 1255 |
#ifdef DOXYGEN |
| 1257 | 1256 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
| 1258 | 1257 |
#else |
| 1259 | 1258 |
template <typename _Digraph = ListDigraph, |
| 1260 | 1259 |
typename _Visitor = DfsVisitor<_Digraph>, |
| 1261 | 1260 |
typename _Traits = DfsVisitDefaultTraits<_Digraph> > |
| 1262 | 1261 |
#endif |
| 1263 | 1262 |
class DfsVisit {
|
| 1264 | 1263 |
public: |
| 1265 | 1264 |
|
| 1266 | 1265 |
///The traits class. |
| 1267 | 1266 |
typedef _Traits Traits; |
| 1268 | 1267 |
|
| 1269 | 1268 |
///The type of the digraph the algorithm runs on. |
| 1270 | 1269 |
typedef typename Traits::Digraph Digraph; |
| 1271 | 1270 |
|
| 1272 | 1271 |
///The visitor type used by the algorithm. |
| 1273 | 1272 |
typedef _Visitor Visitor; |
| 1274 | 1273 |
|
| 1275 | 1274 |
///The type of the map that indicates which nodes are reached. |
| 1276 | 1275 |
typedef typename Traits::ReachedMap ReachedMap; |
| 1277 | 1276 |
|
| 1278 | 1277 |
private: |
| 1279 | 1278 |
|
| 1280 | 1279 |
typedef typename Digraph::Node Node; |
| 1281 | 1280 |
typedef typename Digraph::NodeIt NodeIt; |
| 1282 | 1281 |
typedef typename Digraph::Arc Arc; |
| 1283 | 1282 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 1284 | 1283 |
|
| 1285 | 1284 |
//Pointer to the underlying digraph. |
| 1286 | 1285 |
const Digraph *_digraph; |
| 1287 | 1286 |
//Pointer to the visitor object. |
| 1288 | 1287 |
Visitor *_visitor; |
| 1289 | 1288 |
//Pointer to the map of reached status of the nodes. |
| 1290 | 1289 |
ReachedMap *_reached; |
| 1291 | 1290 |
//Indicates if _reached is locally allocated (true) or not. |
| 1292 | 1291 |
bool local_reached; |
| 1293 | 1292 |
|
| 1294 | 1293 |
std::vector<typename Digraph::Arc> _stack; |
| 1295 | 1294 |
int _stack_head; |
| 1296 | 1295 |
|
| 1297 | 1296 |
//Creates the maps if necessary. |
| 1298 | 1297 |
void create_maps() {
|
| 1299 | 1298 |
if(!_reached) {
|
| 1300 | 1299 |
local_reached = true; |
| 1301 | 1300 |
_reached = Traits::createReachedMap(*_digraph); |
| 1302 | 1301 |
} |
| 1303 | 1302 |
} |
| 1304 | 1303 |
|
| 1305 | 1304 |
protected: |
| 1306 | 1305 |
|
| 1307 | 1306 |
DfsVisit() {}
|
| 1308 | 1307 |
|
| 1309 | 1308 |
public: |
| 1310 | 1309 |
|
| 1311 | 1310 |
typedef DfsVisit Create; |
| 1312 | 1311 |
|
| 1313 | 1312 |
/// \name Named template parameters |
| 1314 | 1313 |
|
| 1315 | 1314 |
///@{
|
| 1316 | 1315 |
template <class T> |
| 1317 | 1316 |
struct SetReachedMapTraits : public Traits {
|
| 1318 | 1317 |
typedef T ReachedMap; |
| 1319 | 1318 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1320 | 1319 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1321 | 1320 |
return 0; // ignore warnings |
| 1322 | 1321 |
} |
| 1323 | 1322 |
}; |
| 1324 | 1323 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1325 | 1324 |
/// ReachedMap type. |
| 1326 | 1325 |
/// |
| 1327 | 1326 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1328 | 1327 |
template <class T> |
| 1329 | 1328 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
| 1330 | 1329 |
SetReachedMapTraits<T> > {
|
| 1331 | 1330 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1332 | 1331 |
}; |
| 1333 | 1332 |
///@} |
| 1334 | 1333 |
|
| 1335 | 1334 |
public: |
| 1336 | 1335 |
|
| 1337 | 1336 |
/// \brief Constructor. |
| 1338 | 1337 |
/// |
| 1339 | 1338 |
/// Constructor. |
| 1340 | 1339 |
/// |
| 1341 | 1340 |
/// \param digraph The digraph the algorithm runs on. |
| 1342 | 1341 |
/// \param visitor The visitor object of the algorithm. |
| 1343 | 1342 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1344 | 1343 |
: _digraph(&digraph), _visitor(&visitor), |
| 1345 | 1344 |
_reached(0), local_reached(false) {}
|
| 1346 | 1345 |
|
| 1347 | 1346 |
/// \brief Destructor. |
| 1348 | 1347 |
~DfsVisit() {
|
| 1349 | 1348 |
if(local_reached) delete _reached; |
| 1350 | 1349 |
} |
| 1351 | 1350 |
|
| 1352 | 1351 |
/// \brief Sets the map that indicates which nodes are reached. |
| 1353 | 1352 |
/// |
| 1354 | 1353 |
/// Sets the map that indicates which nodes are reached. |
| 1355 | 1354 |
/// If you don't use this function before calling \ref run(), |
| 1356 | 1355 |
/// it will allocate one. The destructor deallocates this |
| 1357 | 1356 |
/// automatically allocated map, of course. |
| 1358 | 1357 |
/// \return <tt> (*this) </tt> |
| 1359 | 1358 |
DfsVisit &reachedMap(ReachedMap &m) {
|
| 1360 | 1359 |
if(local_reached) {
|
| 1361 | 1360 |
delete _reached; |
| 1362 | 1361 |
local_reached=false; |
| 1363 | 1362 |
} |
| 1364 | 1363 |
_reached = &m; |
| 1365 | 1364 |
return *this; |
| 1366 | 1365 |
} |
| 1367 | 1366 |
|
| 1368 | 1367 |
public: |
| 1369 | 1368 |
|
| 1370 | 1369 |
/// \name Execution control |
| 1371 | 1370 |
/// The simplest way to execute the algorithm is to use |
| 1372 | 1371 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
| 1373 | 1372 |
/// "run()". |
| 1374 | 1373 |
/// \n |
| 1375 | 1374 |
/// If you need more control on the execution, first you must call |
| 1376 | 1375 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
| 1377 | 1376 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
| 1378 | 1377 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
| 1379 | 1378 |
/// actual path computation. |
| 1380 | 1379 |
|
| 1381 | 1380 |
/// @{
|
| 1382 | 1381 |
|
| 1383 | 1382 |
/// \brief Initializes the internal data structures. |
| 1384 | 1383 |
/// |
| 1385 | 1384 |
/// Initializes the internal data structures. |
| 1386 | 1385 |
void init() {
|
| 1387 | 1386 |
create_maps(); |
| 1388 | 1387 |
_stack.resize(countNodes(*_digraph)); |
| 1389 | 1388 |
_stack_head = -1; |
| 1390 | 1389 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
|
| 1391 | 1390 |
_reached->set(u, false); |
| 1392 | 1391 |
} |
| 1393 | 1392 |
} |
| 1394 | 1393 |
|
| 1395 | 1394 |
///Adds a new source node. |
| 1396 | 1395 |
|
| 1397 | 1396 |
///Adds a new source node to the set of nodes to be processed. |
| 1398 | 1397 |
/// |
| 1399 | 1398 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
| 1400 | 1399 |
///false results.) |
| 1401 | 1400 |
/// |
| 1402 | 1401 |
///\warning Distances will be wrong (or at least strange) in case of |
| 1403 | 1402 |
///multiple sources. |
| 1404 | 1403 |
void addSource(Node s) |
| 1405 | 1404 |
{
|
| 1406 | 1405 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
| 1407 | 1406 |
if(!(*_reached)[s]) {
|
| 1408 | 1407 |
_reached->set(s,true); |
| 1409 | 1408 |
_visitor->start(s); |
| 1410 | 1409 |
_visitor->reach(s); |
| 1411 | 1410 |
Arc e; |
| 1412 | 1411 |
_digraph->firstOut(e, s); |
| 1413 | 1412 |
if (e != INVALID) {
|
| 1414 | 1413 |
_stack[++_stack_head] = e; |
| 1415 | 1414 |
} else {
|
| 1416 | 1415 |
_visitor->leave(s); |
| 1417 | 1416 |
} |
| 1418 | 1417 |
} |
| 1419 | 1418 |
} |
| 1420 | 1419 |
|
| 1421 | 1420 |
/// \brief Processes the next arc. |
| 1422 | 1421 |
/// |
| 1423 | 1422 |
/// Processes the next arc. |
| 1424 | 1423 |
/// |
| 1425 | 1424 |
/// \return The processed arc. |
| 1426 | 1425 |
/// |
| 1427 | 1426 |
/// \pre The stack must not be empty. |
| 1428 | 1427 |
Arc processNextArc() {
|
| 1429 | 1428 |
Arc e = _stack[_stack_head]; |
| 1430 | 1429 |
Node m = _digraph->target(e); |
| 1431 | 1430 |
if(!(*_reached)[m]) {
|
| 1432 | 1431 |
_visitor->discover(e); |
| 1433 | 1432 |
_visitor->reach(m); |
| 1434 | 1433 |
_reached->set(m, true); |
| 1435 | 1434 |
_digraph->firstOut(_stack[++_stack_head], m); |
| 1436 | 1435 |
} else {
|
| 1437 | 1436 |
_visitor->examine(e); |
| 1438 | 1437 |
m = _digraph->source(e); |
| 1439 | 1438 |
_digraph->nextOut(_stack[_stack_head]); |
| 1440 | 1439 |
} |
| 1441 | 1440 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
|
| 1442 | 1441 |
_visitor->leave(m); |
| 1443 | 1442 |
--_stack_head; |
| 1444 | 1443 |
if (_stack_head >= 0) {
|
| 1445 | 1444 |
_visitor->backtrack(_stack[_stack_head]); |
| 1446 | 1445 |
m = _digraph->source(_stack[_stack_head]); |
| 1447 | 1446 |
_digraph->nextOut(_stack[_stack_head]); |
| 1448 | 1447 |
} else {
|
| 1449 | 1448 |
_visitor->stop(m); |
| 1450 | 1449 |
} |
| 1451 | 1450 |
} |
| 1452 | 1451 |
return e; |
| 1453 | 1452 |
} |
| 1454 | 1453 |
|
| 1455 | 1454 |
/// \brief Next arc to be processed. |
| 1456 | 1455 |
/// |
| 1457 | 1456 |
/// Next arc to be processed. |
| 1458 | 1457 |
/// |
| 1459 | 1458 |
/// \return The next arc to be processed or INVALID if the stack is |
| 1460 | 1459 |
/// empty. |
| 1461 | 1460 |
Arc nextArc() const {
|
| 1462 | 1461 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
| 1463 | 1462 |
} |
| 1464 | 1463 |
|
| 1465 | 1464 |
/// \brief Returns \c false if there are nodes |
| 1466 | 1465 |
/// to be processed. |
| 1467 | 1466 |
/// |
| 1468 | 1467 |
/// Returns \c false if there are nodes |
| 1469 | 1468 |
/// to be processed in the queue (stack). |
| 1470 | 1469 |
bool emptyQueue() const { return _stack_head < 0; }
|
| 1471 | 1470 |
|
| 1472 | 1471 |
/// \brief Returns the number of the nodes to be processed. |
| 1473 | 1472 |
/// |
| 1474 | 1473 |
/// Returns the number of the nodes to be processed in the queue (stack). |
| 1475 | 1474 |
int queueSize() const { return _stack_head + 1; }
|
| 1476 | 1475 |
|
| 1477 | 1476 |
/// \brief Executes the algorithm. |
| 1478 | 1477 |
/// |
| 1479 | 1478 |
/// Executes the algorithm. |
| 1480 | 1479 |
/// |
| 1481 | 1480 |
/// This method runs the %DFS algorithm from the root node |
| 1482 | 1481 |
/// in order to compute the %DFS path to each node. |
| 1483 | 1482 |
/// |
| 1484 | 1483 |
/// The algorithm computes |
| 1485 | 1484 |
/// - the %DFS tree, |
| 1486 | 1485 |
/// - the distance of each node from the root in the %DFS tree. |
| 1487 | 1486 |
/// |
| 1488 | 1487 |
/// \pre init() must be called and a root node should be |
| 1489 | 1488 |
/// added with addSource() before using this function. |
| 1490 | 1489 |
/// |
| 1491 | 1490 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
| 1492 | 1491 |
/// \code |
| 1493 | 1492 |
/// while ( !d.emptyQueue() ) {
|
| 1494 | 1493 |
/// d.processNextArc(); |
| 1495 | 1494 |
/// } |
| 1496 | 1495 |
/// \endcode |
| 1497 | 1496 |
void start() {
|
| 1498 | 1497 |
while ( !emptyQueue() ) processNextArc(); |
| 1499 | 1498 |
} |
| 1500 | 1499 |
|
| 1501 | 1500 |
/// \brief Executes the algorithm until the given target node is reached. |
| 1502 | 1501 |
/// |
| 1503 | 1502 |
/// Executes the algorithm until the given target node is reached. |
| 1504 | 1503 |
/// |
| 1505 | 1504 |
/// This method runs the %DFS algorithm from the root node |
| 1506 | 1505 |
/// in order to compute the DFS path to \c t. |
| 1507 | 1506 |
/// |
| 1508 | 1507 |
/// The algorithm computes |
| 1509 | 1508 |
/// - the %DFS path to \c t, |
| 1510 | 1509 |
/// - the distance of \c t from the root in the %DFS tree. |
| 1511 | 1510 |
/// |
| 1512 | 1511 |
/// \pre init() must be called and a root node should be added |
| 1513 | 1512 |
/// with addSource() before using this function. |
| 1514 | 1513 |
void start(Node t) {
|
| 1515 | 1514 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != t ) |
| 1516 | 1515 |
processNextArc(); |
| 1517 | 1516 |
} |
| 1518 | 1517 |
|
| 1519 | 1518 |
/// \brief Executes the algorithm until a condition is met. |
| 1520 | 1519 |
/// |
| 1521 | 1520 |
/// Executes the algorithm until a condition is met. |
| 1522 | 1521 |
/// |
| 1523 | 1522 |
/// This method runs the %DFS algorithm from the root node |
| 1524 | 1523 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
| 1525 | 1524 |
/// |
| 1526 | 1525 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
| 1527 | 1526 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
| 1528 | 1527 |
/// |
| 1529 | 1528 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
| 1530 | 1529 |
/// \c INVALID if no such arc was found. |
| 1531 | 1530 |
/// |
| 1532 | 1531 |
/// \pre init() must be called and a root node should be added |
| 1533 | 1532 |
/// with addSource() before using this function. |
| 1534 | 1533 |
/// |
| 1535 | 1534 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
| 1536 | 1535 |
/// not a node map. |
| 1537 | 1536 |
template <typename AM> |
| 1538 | 1537 |
Arc start(const AM &am) {
|
| 1539 | 1538 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
| 1540 | 1539 |
processNextArc(); |
| 1541 | 1540 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
| 1542 | 1541 |
} |
| 1543 | 1542 |
|
| 1544 | 1543 |
/// \brief Runs the algorithm from the given source node. |
| 1545 | 1544 |
/// |
| 1546 | 1545 |
/// This method runs the %DFS algorithm from node \c s. |
| 1547 | 1546 |
/// in order to compute the DFS path to each node. |
| 1548 | 1547 |
/// |
| 1549 | 1548 |
/// The algorithm computes |
| 1550 | 1549 |
/// - the %DFS tree, |
| 1551 | 1550 |
/// - the distance of each node from the root in the %DFS tree. |
| 1552 | 1551 |
/// |
| 1553 | 1552 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
| 1554 | 1553 |
///\code |
| 1555 | 1554 |
/// d.init(); |
| 1556 | 1555 |
/// d.addSource(s); |
| 1557 | 1556 |
/// d.start(); |
| 1558 | 1557 |
///\endcode |
| 1559 | 1558 |
void run(Node s) {
|
| 1560 | 1559 |
init(); |
| 1561 | 1560 |
addSource(s); |
| 1562 | 1561 |
start(); |
| 1563 | 1562 |
} |
| 1564 | 1563 |
|
| 1565 | 1564 |
/// \brief Finds the %DFS path between \c s and \c t. |
| 1566 | 1565 |
|
| 1567 | 1566 |
/// This method runs the %DFS algorithm from node \c s |
| 1568 | 1567 |
/// in order to compute the DFS path to node \c t |
| 1569 | 1568 |
/// (it stops searching when \c t is processed). |
| 1570 | 1569 |
/// |
| 1571 | 1570 |
/// \return \c true if \c t is reachable form \c s. |
| 1572 | 1571 |
/// |
| 1573 | 1572 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
| 1574 | 1573 |
/// just a shortcut of the following code. |
| 1575 | 1574 |
///\code |
| 1576 | 1575 |
/// d.init(); |
| 1577 | 1576 |
/// d.addSource(s); |
| 1578 | 1577 |
/// d.start(t); |
| 1579 | 1578 |
///\endcode |
| 1580 | 1579 |
bool run(Node s,Node t) {
|
| 1581 | 1580 |
init(); |
| 1582 | 1581 |
addSource(s); |
| 1583 | 1582 |
start(t); |
| 1584 | 1583 |
return reached(t); |
| 1585 | 1584 |
} |
| 1586 | 1585 |
|
| 1587 | 1586 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
| 1588 | 1587 |
|
| 1589 | 1588 |
/// This method runs the %DFS algorithm in order to |
| 1590 | 1589 |
/// compute the %DFS path to each node. |
| 1591 | 1590 |
/// |
| 1592 | 1591 |
/// The algorithm computes |
| 1593 | 1592 |
/// - the %DFS tree, |
| 1594 | 1593 |
/// - the distance of each node from the root in the %DFS tree. |
| 1595 | 1594 |
/// |
| 1596 | 1595 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
| 1597 | 1596 |
///\code |
| 1598 | 1597 |
/// d.init(); |
| 1599 | 1598 |
/// for (NodeIt n(digraph); n != INVALID; ++n) {
|
| 1600 | 1599 |
/// if (!d.reached(n)) {
|
| 1601 | 1600 |
/// d.addSource(n); |
| 1602 | 1601 |
/// d.start(); |
| 1603 | 1602 |
/// } |
| 1604 | 1603 |
/// } |
| 1605 | 1604 |
///\endcode |
| 1606 | 1605 |
void run() {
|
| 1607 | 1606 |
init(); |
| 1608 | 1607 |
for (NodeIt it(*_digraph); it != INVALID; ++it) {
|
| 1609 | 1608 |
if (!reached(it)) {
|
| 1610 | 1609 |
addSource(it); |
| 1611 | 1610 |
start(); |
| 1612 | 1611 |
} |
| 1613 | 1612 |
} |
| 1614 | 1613 |
} |
| 1615 | 1614 |
|
| 1616 | 1615 |
///@} |
| 1617 | 1616 |
|
| 1618 | 1617 |
/// \name Query Functions |
| 1619 | 1618 |
/// The result of the %DFS algorithm can be obtained using these |
| 1620 | 1619 |
/// functions.\n |
| 1621 | 1620 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
| 1622 | 1621 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
| 1623 | 1622 |
/// using them. |
| 1624 | 1623 |
///@{
|
| 1625 | 1624 |
|
| 1626 | 1625 |
/// \brief Checks if a node is reachable from the root(s). |
| 1627 | 1626 |
/// |
| 1628 | 1627 |
/// Returns \c true if \c v is reachable from the root(s). |
| 1629 | 1628 |
/// \pre Either \ref run() or \ref start() |
| 1630 | 1629 |
/// must be called before using this function. |
| 1631 | 1630 |
bool reached(Node v) { return (*_reached)[v]; }
|
| 1632 | 1631 |
|
| 1633 | 1632 |
///@} |
| 1634 | 1633 |
|
| 1635 | 1634 |
}; |
| 1636 | 1635 |
|
| 1637 | 1636 |
} //END OF NAMESPACE LEMON |
| 1638 | 1637 |
|
| 1639 | 1638 |
#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-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 |
///\ingroup lemon_io |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" reader. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_LGF_READER_H |
| 25 | 25 |
#define LEMON_LGF_READER_H |
| 26 | 26 |
|
| 27 | 27 |
#include <iostream> |
| 28 | 28 |
#include <fstream> |
| 29 | 29 |
#include <sstream> |
| 30 | 30 |
|
| 31 | 31 |
#include <set> |
| 32 | 32 |
#include <map> |
| 33 | 33 |
|
| 34 |
#include <lemon/assert.h> |
|
| 35 | 34 |
#include <lemon/core.h> |
| 36 | 35 |
|
| 37 | 36 |
#include <lemon/lgf_writer.h> |
| 38 | 37 |
|
| 39 | 38 |
#include <lemon/concept_check.h> |
| 40 | 39 |
#include <lemon/concepts/maps.h> |
| 41 | 40 |
|
| 42 | 41 |
namespace lemon {
|
| 43 | 42 |
|
| 44 | 43 |
namespace _reader_bits {
|
| 45 | 44 |
|
| 46 | 45 |
template <typename Value> |
| 47 | 46 |
struct DefaultConverter {
|
| 48 | 47 |
Value operator()(const std::string& str) {
|
| 49 | 48 |
std::istringstream is(str); |
| 50 | 49 |
Value value; |
| 51 | 50 |
if (!(is >> value)) {
|
| 52 | 51 |
throw FormatError("Cannot read token");
|
| 53 | 52 |
} |
| 54 | 53 |
|
| 55 | 54 |
char c; |
| 56 | 55 |
if (is >> std::ws >> c) {
|
| 57 | 56 |
throw FormatError("Remaining characters in token");
|
| 58 | 57 |
} |
| 59 | 58 |
return value; |
| 60 | 59 |
} |
| 61 | 60 |
}; |
| 62 | 61 |
|
| 63 | 62 |
template <> |
| 64 | 63 |
struct DefaultConverter<std::string> {
|
| 65 | 64 |
std::string operator()(const std::string& str) {
|
| 66 | 65 |
return str; |
| 67 | 66 |
} |
| 68 | 67 |
}; |
| 69 | 68 |
|
| 70 | 69 |
template <typename _Item> |
| 71 | 70 |
class MapStorageBase {
|
| 72 | 71 |
public: |
| 73 | 72 |
typedef _Item Item; |
| 74 | 73 |
|
| 75 | 74 |
public: |
| 76 | 75 |
MapStorageBase() {}
|
| 77 | 76 |
virtual ~MapStorageBase() {}
|
| 78 | 77 |
|
| 79 | 78 |
virtual void set(const Item& item, const std::string& value) = 0; |
| 80 | 79 |
|
| 81 | 80 |
}; |
| 82 | 81 |
|
| 83 | 82 |
template <typename _Item, typename _Map, |
| 84 | 83 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 85 | 84 |
class MapStorage : public MapStorageBase<_Item> {
|
| 86 | 85 |
public: |
| 87 | 86 |
typedef _Map Map; |
| 88 | 87 |
typedef _Converter Converter; |
| 89 | 88 |
typedef _Item Item; |
| 90 | 89 |
|
| 91 | 90 |
private: |
| 92 | 91 |
Map& _map; |
| 93 | 92 |
Converter _converter; |
| 94 | 93 |
|
| 95 | 94 |
public: |
| 96 | 95 |
MapStorage(Map& map, const Converter& converter = Converter()) |
| 97 | 96 |
: _map(map), _converter(converter) {}
|
| 98 | 97 |
virtual ~MapStorage() {}
|
| 99 | 98 |
|
| 100 | 99 |
virtual void set(const Item& item ,const std::string& value) {
|
| 101 | 100 |
_map.set(item, _converter(value)); |
| 102 | 101 |
} |
| 103 | 102 |
}; |
| 104 | 103 |
|
| 105 | 104 |
template <typename _Graph, bool _dir, typename _Map, |
| 106 | 105 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 107 | 106 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
|
| 108 | 107 |
public: |
| 109 | 108 |
typedef _Map Map; |
| 110 | 109 |
typedef _Converter Converter; |
| 111 | 110 |
typedef _Graph Graph; |
| 112 | 111 |
typedef typename Graph::Edge Item; |
| 113 | 112 |
static const bool dir = _dir; |
| 114 | 113 |
|
| 115 | 114 |
private: |
| 116 | 115 |
const Graph& _graph; |
| 117 | 116 |
Map& _map; |
| 118 | 117 |
Converter _converter; |
| 119 | 118 |
|
| 120 | 119 |
public: |
| 121 | 120 |
GraphArcMapStorage(const Graph& graph, Map& map, |
| 122 | 121 |
const Converter& converter = Converter()) |
| 123 | 122 |
: _graph(graph), _map(map), _converter(converter) {}
|
| 124 | 123 |
virtual ~GraphArcMapStorage() {}
|
| 125 | 124 |
|
| 126 | 125 |
virtual void set(const Item& item ,const std::string& value) {
|
| 127 | 126 |
_map.set(_graph.direct(item, dir), _converter(value)); |
| 128 | 127 |
} |
| 129 | 128 |
}; |
| 130 | 129 |
|
| 131 | 130 |
class ValueStorageBase {
|
| 132 | 131 |
public: |
| 133 | 132 |
ValueStorageBase() {}
|
| 134 | 133 |
virtual ~ValueStorageBase() {}
|
| 135 | 134 |
|
| 136 | 135 |
virtual void set(const std::string&) = 0; |
| 137 | 136 |
}; |
| 138 | 137 |
|
| 139 | 138 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
| 140 | 139 |
class ValueStorage : public ValueStorageBase {
|
| 141 | 140 |
public: |
| 142 | 141 |
typedef _Value Value; |
| 143 | 142 |
typedef _Converter Converter; |
| 144 | 143 |
|
| 145 | 144 |
private: |
| 146 | 145 |
Value& _value; |
| 147 | 146 |
Converter _converter; |
| 148 | 147 |
|
| 149 | 148 |
public: |
| 150 | 149 |
ValueStorage(Value& value, const Converter& converter = Converter()) |
| 151 | 150 |
: _value(value), _converter(converter) {}
|
| 152 | 151 |
|
| 153 | 152 |
virtual void set(const std::string& value) {
|
| 154 | 153 |
_value = _converter(value); |
| 155 | 154 |
} |
| 156 | 155 |
}; |
| 157 | 156 |
|
| 158 | 157 |
template <typename Value> |
| 159 | 158 |
struct MapLookUpConverter {
|
| 160 | 159 |
const std::map<std::string, Value>& _map; |
| 161 | 160 |
|
| 162 | 161 |
MapLookUpConverter(const std::map<std::string, Value>& map) |
| 163 | 162 |
: _map(map) {}
|
| 164 | 163 |
|
| 165 | 164 |
Value operator()(const std::string& str) {
|
| 166 | 165 |
typename std::map<std::string, Value>::const_iterator it = |
| 167 | 166 |
_map.find(str); |
| 168 | 167 |
if (it == _map.end()) {
|
| 169 | 168 |
std::ostringstream msg; |
| 170 | 169 |
msg << "Item not found: " << str; |
| 171 | 170 |
throw FormatError(msg.str()); |
| 172 | 171 |
} |
| 173 | 172 |
return it->second; |
| 174 | 173 |
} |
| 175 | 174 |
}; |
| 176 | 175 |
|
| 177 | 176 |
template <typename Graph> |
| 178 | 177 |
struct GraphArcLookUpConverter {
|
| 179 | 178 |
const Graph& _graph; |
| 180 | 179 |
const std::map<std::string, typename Graph::Edge>& _map; |
| 181 | 180 |
|
| 182 | 181 |
GraphArcLookUpConverter(const Graph& graph, |
| 183 | 182 |
const std::map<std::string, |
| 184 | 183 |
typename Graph::Edge>& map) |
| 185 | 184 |
: _graph(graph), _map(map) {}
|
| 186 | 185 |
|
| 187 | 186 |
typename Graph::Arc operator()(const std::string& str) {
|
| 188 | 187 |
if (str.empty() || (str[0] != '+' && str[0] != '-')) {
|
| 189 | 188 |
throw FormatError("Item must start with '+' or '-'");
|
| 190 | 189 |
} |
| 191 | 190 |
typename std::map<std::string, typename Graph::Edge> |
| 192 | 191 |
::const_iterator it = _map.find(str.substr(1)); |
| 193 | 192 |
if (it == _map.end()) {
|
| 194 | 193 |
throw FormatError("Item not found");
|
| 195 | 194 |
} |
| 196 | 195 |
return _graph.direct(it->second, str[0] == '+'); |
| 197 | 196 |
} |
| 198 | 197 |
}; |
| 199 | 198 |
|
| 200 | 199 |
inline bool isWhiteSpace(char c) {
|
| 201 | 200 |
return c == ' ' || c == '\t' || c == '\v' || |
| 202 | 201 |
c == '\n' || c == '\r' || c == '\f'; |
| 203 | 202 |
} |
| 204 | 203 |
|
| 205 | 204 |
inline bool isOct(char c) {
|
| 206 | 205 |
return '0' <= c && c <='7'; |
| 207 | 206 |
} |
| 208 | 207 |
|
| 209 | 208 |
inline int valueOct(char c) {
|
| 210 | 209 |
LEMON_ASSERT(isOct(c), "The character is not octal."); |
| 211 | 210 |
return c - '0'; |
| 212 | 211 |
} |
| 213 | 212 |
|
| 214 | 213 |
inline bool isHex(char c) {
|
| 215 | 214 |
return ('0' <= c && c <= '9') ||
|
| 216 | 215 |
('a' <= c && c <= 'z') ||
|
| 217 | 216 |
('A' <= c && c <= 'Z');
|
| 218 | 217 |
} |
| 219 | 218 |
|
| 220 | 219 |
inline int valueHex(char c) {
|
| 221 | 220 |
LEMON_ASSERT(isHex(c), "The character is not hexadecimal."); |
| 222 | 221 |
if ('0' <= c && c <= '9') return c - '0';
|
| 223 | 222 |
if ('a' <= c && c <= 'z') return c - 'a' + 10;
|
| 224 | 223 |
return c - 'A' + 10; |
| 225 | 224 |
} |
| 226 | 225 |
|
| 227 | 226 |
inline bool isIdentifierFirstChar(char c) {
|
| 228 | 227 |
return ('a' <= c && c <= 'z') ||
|
| 229 | 228 |
('A' <= c && c <= 'Z') || c == '_';
|
| 230 | 229 |
} |
| 231 | 230 |
|
| 232 | 231 |
inline bool isIdentifierChar(char c) {
|
| 233 | 232 |
return isIdentifierFirstChar(c) || |
| 234 | 233 |
('0' <= c && c <= '9');
|
| 235 | 234 |
} |
| 236 | 235 |
|
| 237 | 236 |
inline char readEscape(std::istream& is) {
|
| 238 | 237 |
char c; |
| 239 | 238 |
if (!is.get(c)) |
| 240 | 239 |
throw FormatError("Escape format error");
|
| 241 | 240 |
|
| 242 | 241 |
switch (c) {
|
| 243 | 242 |
case '\\': |
| 244 | 243 |
return '\\'; |
| 245 | 244 |
case '\"': |
| 246 | 245 |
return '\"'; |
| 247 | 246 |
case '\'': |
| 248 | 247 |
return '\''; |
| 249 | 248 |
case '\?': |
| 250 | 249 |
return '\?'; |
| 251 | 250 |
case 'a': |
| 252 | 251 |
return '\a'; |
| 253 | 252 |
case 'b': |
| 254 | 253 |
return '\b'; |
| 255 | 254 |
case 'f': |
| 256 | 255 |
return '\f'; |
| 257 | 256 |
case 'n': |
| 258 | 257 |
return '\n'; |
| 259 | 258 |
case 'r': |
| 260 | 259 |
return '\r'; |
| 261 | 260 |
case 't': |
| 262 | 261 |
return '\t'; |
| 263 | 262 |
case 'v': |
| 264 | 263 |
return '\v'; |
| 265 | 264 |
case 'x': |
| 266 | 265 |
{
|
| 267 | 266 |
int code; |
| 268 | 267 |
if (!is.get(c) || !isHex(c)) |
| 269 | 268 |
throw FormatError("Escape format error");
|
| 270 | 269 |
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); |
| 271 | 270 |
else code = code * 16 + valueHex(c); |
| 272 | 271 |
return code; |
| 273 | 272 |
} |
| 274 | 273 |
default: |
| 275 | 274 |
{
|
| 276 | 275 |
int code; |
| 277 | 276 |
if (!isOct(c)) |
| 278 | 277 |
throw FormatError("Escape format error");
|
| 279 | 278 |
else if (code = valueOct(c), !is.get(c) || !isOct(c)) |
| 280 | 279 |
is.putback(c); |
| 281 | 280 |
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) |
| 282 | 281 |
is.putback(c); |
| 283 | 282 |
else code = code * 8 + valueOct(c); |
| 284 | 283 |
return code; |
| 285 | 284 |
} |
| 286 | 285 |
} |
| 287 | 286 |
} |
| 288 | 287 |
|
| 289 | 288 |
inline std::istream& readToken(std::istream& is, std::string& str) {
|
| 290 | 289 |
std::ostringstream os; |
| 291 | 290 |
|
| 292 | 291 |
char c; |
| 293 | 292 |
is >> std::ws; |
| 294 | 293 |
|
| 295 | 294 |
if (!is.get(c)) |
| 296 | 295 |
return is; |
| 297 | 296 |
|
| 298 | 297 |
if (c == '\"') {
|
| 299 | 298 |
while (is.get(c) && c != '\"') {
|
| 300 | 299 |
if (c == '\\') |
| 301 | 300 |
c = readEscape(is); |
| 302 | 301 |
os << c; |
| 303 | 302 |
} |
| 304 | 303 |
if (!is) |
| 305 | 304 |
throw FormatError("Quoted format error");
|
| 306 | 305 |
} else {
|
| 307 | 306 |
is.putback(c); |
| 308 | 307 |
while (is.get(c) && !isWhiteSpace(c)) {
|
| 309 | 308 |
if (c == '\\') |
| 310 | 309 |
c = readEscape(is); |
| 311 | 310 |
os << c; |
| 312 | 311 |
} |
| 313 | 312 |
if (!is) {
|
| 314 | 313 |
is.clear(); |
| 315 | 314 |
} else {
|
| 316 | 315 |
is.putback(c); |
| 317 | 316 |
} |
| 318 | 317 |
} |
| 319 | 318 |
str = os.str(); |
| 320 | 319 |
return is; |
| 321 | 320 |
} |
| 322 | 321 |
|
| 323 | 322 |
class Section {
|
| 324 | 323 |
public: |
| 325 | 324 |
virtual ~Section() {}
|
| 326 | 325 |
virtual void process(std::istream& is, int& line_num) = 0; |
| 327 | 326 |
}; |
| 328 | 327 |
|
| 329 | 328 |
template <typename Functor> |
| 330 | 329 |
class LineSection : public Section {
|
| 331 | 330 |
private: |
| 332 | 331 |
|
| 333 | 332 |
Functor _functor; |
| 334 | 333 |
|
| 335 | 334 |
public: |
| 336 | 335 |
|
| 337 | 336 |
LineSection(const Functor& functor) : _functor(functor) {}
|
| 338 | 337 |
virtual ~LineSection() {}
|
| 339 | 338 |
|
| 340 | 339 |
virtual void process(std::istream& is, int& line_num) {
|
| 341 | 340 |
char c; |
| 342 | 341 |
std::string line; |
| 343 | 342 |
while (is.get(c) && c != '@') {
|
| 344 | 343 |
if (c == '\n') {
|
| 345 | 344 |
++line_num; |
| 346 | 345 |
} else if (c == '#') {
|
| 347 | 346 |
getline(is, line); |
| 348 | 347 |
++line_num; |
| 349 | 348 |
} else if (!isWhiteSpace(c)) {
|
| 350 | 349 |
is.putback(c); |
| 351 | 350 |
getline(is, line); |
| 352 | 351 |
_functor(line); |
| 353 | 352 |
++line_num; |
| 354 | 353 |
} |
| 355 | 354 |
} |
| 356 | 355 |
if (is) is.putback(c); |
| 357 | 356 |
else if (is.eof()) is.clear(); |
| 358 | 357 |
} |
| 359 | 358 |
}; |
| 360 | 359 |
|
| 361 | 360 |
template <typename Functor> |
| 362 | 361 |
class StreamSection : public Section {
|
| 363 | 362 |
private: |
| 364 | 363 |
|
| 365 | 364 |
Functor _functor; |
| 366 | 365 |
|
| 367 | 366 |
public: |
| 368 | 367 |
|
| 369 | 368 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
| 370 | 369 |
virtual ~StreamSection() {}
|
| 371 | 370 |
|
| 372 | 371 |
virtual void process(std::istream& is, int& line_num) {
|
| 373 | 372 |
_functor(is, line_num); |
| 374 | 373 |
char c; |
| 375 | 374 |
std::string line; |
| 376 | 375 |
while (is.get(c) && c != '@') {
|
| 377 | 376 |
if (c == '\n') {
|
| 378 | 377 |
++line_num; |
| 379 | 378 |
} else if (!isWhiteSpace(c)) {
|
| 380 | 379 |
getline(is, line); |
| 381 | 380 |
++line_num; |
| 382 | 381 |
} |
| 383 | 382 |
} |
| 384 | 383 |
if (is) is.putback(c); |
| 385 | 384 |
else if (is.eof()) is.clear(); |
| 386 | 385 |
} |
| 387 | 386 |
}; |
| 388 | 387 |
|
| 389 | 388 |
} |
| 390 | 389 |
|
| 391 | 390 |
template <typename Digraph> |
| 392 | 391 |
class DigraphReader; |
| 393 | 392 |
|
| 394 | 393 |
/// \brief Return a \ref DigraphReader class |
| 395 | 394 |
/// |
| 396 | 395 |
/// This function just returns a \ref DigraphReader class. |
| 397 | 396 |
/// \relates DigraphReader |
| 398 | 397 |
template <typename Digraph> |
| 399 | 398 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
| 400 | 399 |
std::istream& is = std::cin) {
|
| 401 | 400 |
DigraphReader<Digraph> tmp(digraph, is); |
| 402 | 401 |
return tmp; |
| 403 | 402 |
} |
| 404 | 403 |
|
| 405 | 404 |
/// \brief Return a \ref DigraphReader class |
| 406 | 405 |
/// |
| 407 | 406 |
/// This function just returns a \ref DigraphReader class. |
| 408 | 407 |
/// \relates DigraphReader |
| 409 | 408 |
template <typename Digraph> |
| 410 | 409 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
| 411 | 410 |
const std::string& fn) {
|
| 412 | 411 |
DigraphReader<Digraph> tmp(digraph, fn); |
| 413 | 412 |
return tmp; |
| 414 | 413 |
} |
| 415 | 414 |
|
| 416 | 415 |
/// \brief Return a \ref DigraphReader class |
| 417 | 416 |
/// |
| 418 | 417 |
/// This function just returns a \ref DigraphReader class. |
| 419 | 418 |
/// \relates DigraphReader |
| 420 | 419 |
template <typename Digraph> |
| 421 | 420 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) {
|
| 422 | 421 |
DigraphReader<Digraph> tmp(digraph, fn); |
| 423 | 422 |
return tmp; |
| 424 | 423 |
} |
| 425 | 424 |
|
| 426 | 425 |
/// \ingroup lemon_io |
| 427 | 426 |
/// |
| 428 | 427 |
/// \brief \ref lgf-format "LGF" reader for directed graphs |
| 429 | 428 |
/// |
| 430 | 429 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 431 | 430 |
/// |
| 432 | 431 |
/// The reading method does a batch processing. The user creates a |
| 433 | 432 |
/// reader object, then various reading rules can be added to the |
| 434 | 433 |
/// reader, and eventually the reading is executed with the \c run() |
| 435 | 434 |
/// member function. A map reading rule can be added to the reader |
| 436 | 435 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 437 | 436 |
/// converter parameter can also be added as a standard functor |
| 438 | 437 |
/// converting from \c std::string to the value type of the map. If it |
| 439 | 438 |
/// is set, it will determine how the tokens in the file should be |
| 440 | 439 |
/// converted to the value type of the map. If the functor is not set, |
| 441 | 440 |
/// then a default conversion will be used. One map can be read into |
| 442 | 441 |
/// multiple map objects at the same time. The \c attribute(), \c |
| 443 | 442 |
/// node() and \c arc() functions are used to add attribute reading |
| 444 | 443 |
/// rules. |
| 445 | 444 |
/// |
| 446 | 445 |
///\code |
| 447 | 446 |
/// DigraphReader<Digraph>(digraph, std::cin). |
| 448 | 447 |
/// nodeMap("coordinates", coord_map).
|
| 449 | 448 |
/// arcMap("capacity", cap_map).
|
| 450 | 449 |
/// node("source", src).
|
| 451 | 450 |
/// node("target", trg).
|
| 452 | 451 |
/// attribute("caption", caption).
|
| 453 | 452 |
/// run(); |
| 454 | 453 |
///\endcode |
| 455 | 454 |
/// |
| 456 | 455 |
/// By default the reader uses the first section in the file of the |
| 457 | 456 |
/// proper type. If a section has an optional name, then it can be |
| 458 | 457 |
/// selected for reading by giving an optional name parameter to the |
| 459 | 458 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
| 460 | 459 |
/// |
| 461 | 460 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
| 462 | 461 |
/// that the nodes or arcs should not be constructed (added to the |
| 463 | 462 |
/// graph) during the reading, but instead the label map of the items |
| 464 | 463 |
/// are given as a parameter of these functions. An |
| 465 | 464 |
/// application of these functions is multipass reading, which is |
| 466 | 465 |
/// important if two \c \@arcs sections must be read from the |
| 467 | 466 |
/// file. In this case the first phase would read the node set and one |
| 468 | 467 |
/// of the arc sets, while the second phase would read the second arc |
| 469 | 468 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
| 470 | 469 |
/// The previously read label node map should be passed to the \c |
| 471 | 470 |
/// useNodes() functions. Another application of multipass reading when |
| 472 | 471 |
/// paths are given as a node map or an arc map. |
| 473 | 472 |
/// It is impossible to read this in |
| 474 | 473 |
/// a single pass, because the arcs are not constructed when the node |
| 475 | 474 |
/// maps are read. |
| 476 | 475 |
template <typename _Digraph> |
| 477 | 476 |
class DigraphReader {
|
| 478 | 477 |
public: |
| 479 | 478 |
|
| 480 | 479 |
typedef _Digraph Digraph; |
| 481 | 480 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 482 | 481 |
|
| 483 | 482 |
private: |
| 484 | 483 |
|
| 485 | 484 |
|
| 486 | 485 |
std::istream* _is; |
| 487 | 486 |
bool local_is; |
| 488 | 487 |
std::string _filename; |
| 489 | 488 |
|
| 490 | 489 |
Digraph& _digraph; |
| 491 | 490 |
|
| 492 | 491 |
std::string _nodes_caption; |
| 493 | 492 |
std::string _arcs_caption; |
| 494 | 493 |
std::string _attributes_caption; |
| 495 | 494 |
|
| 496 | 495 |
typedef std::map<std::string, Node> NodeIndex; |
| 497 | 496 |
NodeIndex _node_index; |
| 498 | 497 |
typedef std::map<std::string, Arc> ArcIndex; |
| 499 | 498 |
ArcIndex _arc_index; |
| 500 | 499 |
|
| 501 | 500 |
typedef std::vector<std::pair<std::string, |
| 502 | 501 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 503 | 502 |
NodeMaps _node_maps; |
| 504 | 503 |
|
| 505 | 504 |
typedef std::vector<std::pair<std::string, |
| 506 | 505 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
| 507 | 506 |
ArcMaps _arc_maps; |
| 508 | 507 |
|
| 509 | 508 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 510 | 509 |
Attributes; |
| 511 | 510 |
Attributes _attributes; |
| 512 | 511 |
|
| 513 | 512 |
bool _use_nodes; |
| 514 | 513 |
bool _use_arcs; |
| 515 | 514 |
|
| 516 | 515 |
bool _skip_nodes; |
| 517 | 516 |
bool _skip_arcs; |
| 518 | 517 |
|
| 519 | 518 |
int line_num; |
| 520 | 519 |
std::istringstream line; |
| 521 | 520 |
|
| 522 | 521 |
public: |
| 523 | 522 |
|
| 524 | 523 |
/// \brief Constructor |
| 525 | 524 |
/// |
| 526 | 525 |
/// Construct a directed graph reader, which reads from the given |
| 527 | 526 |
/// input stream. |
| 528 | 527 |
DigraphReader(Digraph& digraph, std::istream& is = std::cin) |
| 529 | 528 |
: _is(&is), local_is(false), _digraph(digraph), |
| 530 | 529 |
_use_nodes(false), _use_arcs(false), |
| 531 | 530 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 532 | 531 |
|
| 533 | 532 |
/// \brief Constructor |
| 534 | 533 |
/// |
| 535 | 534 |
/// Construct a directed graph reader, which reads from the given |
| 536 | 535 |
/// file. |
| 537 | 536 |
DigraphReader(Digraph& digraph, const std::string& fn) |
| 538 | 537 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
| 539 | 538 |
_filename(fn), _digraph(digraph), |
| 540 | 539 |
_use_nodes(false), _use_arcs(false), |
| 541 | 540 |
_skip_nodes(false), _skip_arcs(false) {
|
| 542 | 541 |
if (!(*_is)) {
|
| 543 | 542 |
delete _is; |
| 544 | 543 |
throw IoError("Cannot open file", fn);
|
| 545 | 544 |
} |
| 546 | 545 |
} |
| 547 | 546 |
|
| 548 | 547 |
/// \brief Constructor |
| 549 | 548 |
/// |
| 550 | 549 |
/// Construct a directed graph reader, which reads from the given |
| 551 | 550 |
/// file. |
| 552 | 551 |
DigraphReader(Digraph& digraph, const char* fn) |
| 553 | 552 |
: _is(new std::ifstream(fn)), local_is(true), |
| 554 | 553 |
_filename(fn), _digraph(digraph), |
| 555 | 554 |
_use_nodes(false), _use_arcs(false), |
| 556 | 555 |
_skip_nodes(false), _skip_arcs(false) {
|
| 557 | 556 |
if (!(*_is)) {
|
| 558 | 557 |
delete _is; |
| 559 | 558 |
throw IoError("Cannot open file", fn);
|
| 560 | 559 |
} |
| 561 | 560 |
} |
| 562 | 561 |
|
| 563 | 562 |
/// \brief Destructor |
| 564 | 563 |
~DigraphReader() {
|
| 565 | 564 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 566 | 565 |
it != _node_maps.end(); ++it) {
|
| 567 | 566 |
delete it->second; |
| 568 | 567 |
} |
| 569 | 568 |
|
| 570 | 569 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 571 | 570 |
it != _arc_maps.end(); ++it) {
|
| 572 | 571 |
delete it->second; |
| 573 | 572 |
} |
| 574 | 573 |
|
| 575 | 574 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 576 | 575 |
it != _attributes.end(); ++it) {
|
| 577 | 576 |
delete it->second; |
| 578 | 577 |
} |
| 579 | 578 |
|
| 580 | 579 |
if (local_is) {
|
| 581 | 580 |
delete _is; |
| 582 | 581 |
} |
| 583 | 582 |
|
| 584 | 583 |
} |
| 585 | 584 |
|
| 586 | 585 |
private: |
| 587 | 586 |
|
| 588 | 587 |
friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, |
| 589 | 588 |
std::istream& is); |
| 590 | 589 |
friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, |
| 591 | 590 |
const std::string& fn); |
| 592 | 591 |
friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, |
| 593 | 592 |
const char *fn); |
| 594 | 593 |
|
| 595 | 594 |
DigraphReader(DigraphReader& other) |
| 596 | 595 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
| 597 | 596 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
| 598 | 597 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
| 599 | 598 |
|
| 600 | 599 |
other._is = 0; |
| 601 | 600 |
other.local_is = false; |
| 602 | 601 |
|
| 603 | 602 |
_node_index.swap(other._node_index); |
| 604 | 603 |
_arc_index.swap(other._arc_index); |
| 605 | 604 |
|
| 606 | 605 |
_node_maps.swap(other._node_maps); |
| 607 | 606 |
_arc_maps.swap(other._arc_maps); |
| 608 | 607 |
_attributes.swap(other._attributes); |
| 609 | 608 |
|
| 610 | 609 |
_nodes_caption = other._nodes_caption; |
| 611 | 610 |
_arcs_caption = other._arcs_caption; |
| 612 | 611 |
_attributes_caption = other._attributes_caption; |
| 613 | 612 |
|
| 614 | 613 |
} |
| 615 | 614 |
|
| 616 | 615 |
DigraphReader& operator=(const DigraphReader&); |
| 617 | 616 |
|
| 618 | 617 |
public: |
| 619 | 618 |
|
| 620 | 619 |
/// \name Reading rules |
| 621 | 620 |
/// @{
|
| 622 | 621 |
|
| 623 | 622 |
/// \brief Node map reading rule |
| 624 | 623 |
/// |
| 625 | 624 |
/// Add a node map reading rule to the reader. |
| 626 | 625 |
template <typename Map> |
| 627 | 626 |
DigraphReader& nodeMap(const std::string& caption, Map& map) {
|
| 628 | 627 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 629 | 628 |
_reader_bits::MapStorageBase<Node>* storage = |
| 630 | 629 |
new _reader_bits::MapStorage<Node, Map>(map); |
| 631 | 630 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 632 | 631 |
return *this; |
| 633 | 632 |
} |
| 634 | 633 |
|
| 635 | 634 |
/// \brief Node map reading rule |
| 636 | 635 |
/// |
| 637 | 636 |
/// Add a node map reading rule with specialized converter to the |
| 638 | 637 |
/// reader. |
| 639 | 638 |
template <typename Map, typename Converter> |
| 640 | 639 |
DigraphReader& nodeMap(const std::string& caption, Map& map, |
| 641 | 640 |
const Converter& converter = Converter()) {
|
| 642 | 641 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 643 | 642 |
_reader_bits::MapStorageBase<Node>* storage = |
| 644 | 643 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 645 | 644 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 646 | 645 |
return *this; |
| 647 | 646 |
} |
| 648 | 647 |
|
| 649 | 648 |
/// \brief Arc map reading rule |
| 650 | 649 |
/// |
| 651 | 650 |
/// Add an arc map reading rule to the reader. |
| 652 | 651 |
template <typename Map> |
| 653 | 652 |
DigraphReader& arcMap(const std::string& caption, Map& map) {
|
| 654 | 653 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 655 | 654 |
_reader_bits::MapStorageBase<Arc>* storage = |
| 656 | 655 |
new _reader_bits::MapStorage<Arc, Map>(map); |
| 657 | 656 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 658 | 657 |
return *this; |
| 659 | 658 |
} |
| 660 | 659 |
|
| 661 | 660 |
/// \brief Arc map reading rule |
| 662 | 661 |
/// |
| 663 | 662 |
/// Add an arc map reading rule with specialized converter to the |
| 664 | 663 |
/// reader. |
| 665 | 664 |
template <typename Map, typename Converter> |
| 666 | 665 |
DigraphReader& arcMap(const std::string& caption, Map& map, |
| 667 | 666 |
const Converter& converter = Converter()) {
|
| 668 | 667 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 669 | 668 |
_reader_bits::MapStorageBase<Arc>* storage = |
| 670 | 669 |
new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter); |
| 671 | 670 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 672 | 671 |
return *this; |
| 673 | 672 |
} |
| 674 | 673 |
|
| 675 | 674 |
/// \brief Attribute reading rule |
| 676 | 675 |
/// |
| 677 | 676 |
/// Add an attribute reading rule to the reader. |
| 678 | 677 |
template <typename Value> |
| 679 | 678 |
DigraphReader& attribute(const std::string& caption, Value& value) {
|
| 680 | 679 |
_reader_bits::ValueStorageBase* storage = |
| 681 | 680 |
new _reader_bits::ValueStorage<Value>(value); |
| 682 | 681 |
_attributes.insert(std::make_pair(caption, storage)); |
| 683 | 682 |
return *this; |
| 684 | 683 |
} |
| 685 | 684 |
|
| 686 | 685 |
/// \brief Attribute reading rule |
| 687 | 686 |
/// |
| 688 | 687 |
/// Add an attribute reading rule with specialized converter to the |
| 689 | 688 |
/// reader. |
| 690 | 689 |
template <typename Value, typename Converter> |
| 691 | 690 |
DigraphReader& attribute(const std::string& caption, Value& value, |
| 692 | 691 |
const Converter& converter = Converter()) {
|
| 693 | 692 |
_reader_bits::ValueStorageBase* storage = |
| 694 | 693 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
| 695 | 694 |
_attributes.insert(std::make_pair(caption, storage)); |
| 696 | 695 |
return *this; |
| 697 | 696 |
} |
| 698 | 697 |
|
| 699 | 698 |
/// \brief Node reading rule |
| 700 | 699 |
/// |
| 701 | 700 |
/// Add a node reading rule to reader. |
| 702 | 701 |
DigraphReader& node(const std::string& caption, Node& node) {
|
| 703 | 702 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
| 704 | 703 |
Converter converter(_node_index); |
| 705 | 704 |
_reader_bits::ValueStorageBase* storage = |
| 706 | 705 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
| 707 | 706 |
_attributes.insert(std::make_pair(caption, storage)); |
| 708 | 707 |
return *this; |
| 709 | 708 |
} |
| 710 | 709 |
|
| 711 | 710 |
/// \brief Arc reading rule |
| 712 | 711 |
/// |
| 713 | 712 |
/// Add an arc reading rule to reader. |
| 714 | 713 |
DigraphReader& arc(const std::string& caption, Arc& arc) {
|
| 715 | 714 |
typedef _reader_bits::MapLookUpConverter<Arc> Converter; |
| 716 | 715 |
Converter converter(_arc_index); |
| 717 | 716 |
_reader_bits::ValueStorageBase* storage = |
| 718 | 717 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 719 | 718 |
_attributes.insert(std::make_pair(caption, storage)); |
| 720 | 719 |
return *this; |
| 721 | 720 |
} |
| 722 | 721 |
|
| 723 | 722 |
/// @} |
| 724 | 723 |
|
| 725 | 724 |
/// \name Select section by name |
| 726 | 725 |
/// @{
|
| 727 | 726 |
|
| 728 | 727 |
/// \brief Set \c \@nodes section to be read |
| 729 | 728 |
/// |
| 730 | 729 |
/// Set \c \@nodes section to be read |
| 731 | 730 |
DigraphReader& nodes(const std::string& caption) {
|
| 732 | 731 |
_nodes_caption = caption; |
| 733 | 732 |
return *this; |
| 734 | 733 |
} |
| 735 | 734 |
|
| 736 | 735 |
/// \brief Set \c \@arcs section to be read |
| 737 | 736 |
/// |
| 738 | 737 |
/// Set \c \@arcs section to be read |
| 739 | 738 |
DigraphReader& arcs(const std::string& caption) {
|
| 740 | 739 |
_arcs_caption = caption; |
| 741 | 740 |
return *this; |
| 742 | 741 |
} |
| 743 | 742 |
|
| 744 | 743 |
/// \brief Set \c \@attributes section to be read |
| 745 | 744 |
/// |
| 746 | 745 |
/// Set \c \@attributes section to be read |
| 747 | 746 |
DigraphReader& attributes(const std::string& caption) {
|
| 748 | 747 |
_attributes_caption = caption; |
| 749 | 748 |
return *this; |
| 750 | 749 |
} |
| 751 | 750 |
|
| 752 | 751 |
/// @} |
| 753 | 752 |
|
| 754 | 753 |
/// \name Using previously constructed node or arc set |
| 755 | 754 |
/// @{
|
| 756 | 755 |
|
| 757 | 756 |
/// \brief Use previously constructed node set |
| 758 | 757 |
/// |
| 759 | 758 |
/// Use previously constructed node set, and specify the node |
| 760 | 759 |
/// label map. |
| 761 | 760 |
template <typename Map> |
| 762 | 761 |
DigraphReader& useNodes(const Map& map) {
|
| 763 | 762 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 764 | 763 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 765 | 764 |
_use_nodes = true; |
| 766 | 765 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 767 | 766 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 768 | 767 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 769 | 768 |
} |
| 770 | 769 |
return *this; |
| 771 | 770 |
} |
| 772 | 771 |
|
| 773 | 772 |
/// \brief Use previously constructed node set |
| 774 | 773 |
/// |
| 775 | 774 |
/// Use previously constructed node set, and specify the node |
| 776 | 775 |
/// label map and a functor which converts the label map values to |
| 777 | 776 |
/// \c std::string. |
| 778 | 777 |
template <typename Map, typename Converter> |
| 779 | 778 |
DigraphReader& useNodes(const Map& map, |
| 780 | 779 |
const Converter& converter = Converter()) {
|
| 781 | 780 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 782 | 781 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 783 | 782 |
_use_nodes = true; |
| 784 | 783 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 785 | 784 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 786 | 785 |
} |
| 787 | 786 |
return *this; |
| 788 | 787 |
} |
| 789 | 788 |
|
| 790 | 789 |
/// \brief Use previously constructed arc set |
| 791 | 790 |
/// |
| 792 | 791 |
/// Use previously constructed arc set, and specify the arc |
| 793 | 792 |
/// label map. |
| 794 | 793 |
template <typename Map> |
| 795 | 794 |
DigraphReader& useArcs(const Map& map) {
|
| 796 | 795 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 797 | 796 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
| 798 | 797 |
_use_arcs = true; |
| 799 | 798 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 800 | 799 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 801 | 800 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
| 802 | 801 |
} |
| 803 | 802 |
return *this; |
| 804 | 803 |
} |
| 805 | 804 |
|
| 806 | 805 |
/// \brief Use previously constructed arc set |
| 807 | 806 |
/// |
| 808 | 807 |
/// Use previously constructed arc set, and specify the arc |
| 809 | 808 |
/// label map and a functor which converts the label map values to |
| 810 | 809 |
/// \c std::string. |
| 811 | 810 |
template <typename Map, typename Converter> |
| 812 | 811 |
DigraphReader& useArcs(const Map& map, |
| 813 | 812 |
const Converter& converter = Converter()) {
|
| 814 | 813 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 815 | 814 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
| 816 | 815 |
_use_arcs = true; |
| 817 | 816 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 818 | 817 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
| 819 | 818 |
} |
| 820 | 819 |
return *this; |
| 821 | 820 |
} |
| 822 | 821 |
|
| 823 | 822 |
/// \brief Skips the reading of node section |
| 824 | 823 |
/// |
| 825 | 824 |
/// Omit the reading of the node section. This implies that each node |
| 826 | 825 |
/// map reading rule will be abandoned, and the nodes of the graph |
| 827 | 826 |
/// will not be constructed, which usually cause that the arc set |
| 828 | 827 |
/// could not be read due to lack of node name resolving. |
| 829 | 828 |
/// Therefore \c skipArcs() function should also be used, or |
| 830 | 829 |
/// \c useNodes() should be used to specify the label of the nodes. |
| 831 | 830 |
DigraphReader& skipNodes() {
|
| 832 | 831 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
| 833 | 832 |
_skip_nodes = true; |
| 834 | 833 |
return *this; |
| 835 | 834 |
} |
| 836 | 835 |
|
| 837 | 836 |
/// \brief Skips the reading of arc section |
| 838 | 837 |
/// |
| 839 | 838 |
/// Omit the reading of the arc section. This implies that each arc |
| 840 | 839 |
/// map reading rule will be abandoned, and the arcs of the graph |
| 841 | 840 |
/// will not be constructed. |
| 842 | 841 |
DigraphReader& skipArcs() {
|
| 843 | 842 |
LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); |
| 844 | 843 |
_skip_arcs = true; |
| 845 | 844 |
return *this; |
| 846 | 845 |
} |
| 847 | 846 |
|
| 848 | 847 |
/// @} |
| 849 | 848 |
|
| 850 | 849 |
private: |
| 851 | 850 |
|
| 852 | 851 |
bool readLine() {
|
| 853 | 852 |
std::string str; |
| 854 | 853 |
while(++line_num, std::getline(*_is, str)) {
|
| 855 | 854 |
line.clear(); line.str(str); |
| 856 | 855 |
char c; |
| 857 | 856 |
if (line >> std::ws >> c && c != '#') {
|
| 858 | 857 |
line.putback(c); |
| 859 | 858 |
return true; |
| 860 | 859 |
} |
| 861 | 860 |
} |
| 862 | 861 |
return false; |
| 863 | 862 |
} |
| 864 | 863 |
|
| 865 | 864 |
bool readSuccess() {
|
| 866 | 865 |
return static_cast<bool>(*_is); |
| 867 | 866 |
} |
| 868 | 867 |
|
| 869 | 868 |
void skipSection() {
|
| 870 | 869 |
char c; |
| 871 | 870 |
while (readSuccess() && line >> c && c != '@') {
|
| 872 | 871 |
readLine(); |
| 873 | 872 |
} |
| 874 | 873 |
line.putback(c); |
| 875 | 874 |
} |
| 876 | 875 |
|
| 877 | 876 |
void readNodes() {
|
| 878 | 877 |
|
| 879 | 878 |
std::vector<int> map_index(_node_maps.size()); |
| 880 | 879 |
int map_num, label_index; |
| 881 | 880 |
|
| 882 | 881 |
char c; |
| 883 | 882 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 884 | 883 |
if (readSuccess() && line) line.putback(c); |
| 885 | 884 |
if (!_node_maps.empty()) |
| 886 | 885 |
throw FormatError("Cannot find map names");
|
| 887 | 886 |
return; |
| 888 | 887 |
} |
| 889 | 888 |
line.putback(c); |
| 890 | 889 |
|
| 891 | 890 |
{
|
| 892 | 891 |
std::map<std::string, int> maps; |
| 893 | 892 |
|
| 894 | 893 |
std::string map; |
| 895 | 894 |
int index = 0; |
| 896 | 895 |
while (_reader_bits::readToken(line, map)) {
|
| 897 | 896 |
if (maps.find(map) != maps.end()) {
|
| 898 | 897 |
std::ostringstream msg; |
| 899 | 898 |
msg << "Multiple occurence of node map: " << map; |
| 900 | 899 |
throw FormatError(msg.str()); |
| 901 | 900 |
} |
| 902 | 901 |
maps.insert(std::make_pair(map, index)); |
| 903 | 902 |
++index; |
| 904 | 903 |
} |
| 905 | 904 |
|
| 906 | 905 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
| 907 | 906 |
std::map<std::string, int>::iterator jt = |
| 908 | 907 |
maps.find(_node_maps[i].first); |
| 909 | 908 |
if (jt == maps.end()) {
|
| 910 | 909 |
std::ostringstream msg; |
| 911 | 910 |
msg << "Map not found: " << _node_maps[i].first; |
| 912 | 911 |
throw FormatError(msg.str()); |
| 913 | 912 |
} |
| 914 | 913 |
map_index[i] = jt->second; |
| 915 | 914 |
} |
| 916 | 915 |
|
| 917 | 916 |
{
|
| 918 | 917 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
| 919 | 918 |
if (jt != maps.end()) {
|
| 920 | 919 |
label_index = jt->second; |
| 921 | 920 |
} else {
|
| 922 | 921 |
label_index = -1; |
| 923 | 922 |
} |
| 924 | 923 |
} |
| 925 | 924 |
map_num = maps.size(); |
| 926 | 925 |
} |
| 927 | 926 |
|
| 928 | 927 |
while (readLine() && line >> c && c != '@') {
|
| 929 | 928 |
line.putback(c); |
| 930 | 929 |
|
| 931 | 930 |
std::vector<std::string> tokens(map_num); |
| 932 | 931 |
for (int i = 0; i < map_num; ++i) {
|
| 933 | 932 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 934 | 933 |
std::ostringstream msg; |
| 935 | 934 |
msg << "Column not found (" << i + 1 << ")";
|
| 936 | 935 |
throw FormatError(msg.str()); |
| 937 | 936 |
} |
| 938 | 937 |
} |
| 939 | 938 |
if (line >> std::ws >> c) |
| 940 | 939 |
throw FormatError("Extra character at the end of line");
|
| 941 | 940 |
|
| 942 | 941 |
Node n; |
| 943 | 942 |
if (!_use_nodes) {
|
| 944 | 943 |
n = _digraph.addNode(); |
| 945 | 944 |
if (label_index != -1) |
| 946 | 945 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
| 947 | 946 |
} else {
|
| 948 | 947 |
if (label_index == -1) |
| 949 | 948 |
throw FormatError("Label map not found");
|
| 950 | 949 |
typename std::map<std::string, Node>::iterator it = |
| 951 | 950 |
_node_index.find(tokens[label_index]); |
| 952 | 951 |
if (it == _node_index.end()) {
|
| 953 | 952 |
std::ostringstream msg; |
| 954 | 953 |
msg << "Node with label not found: " << tokens[label_index]; |
| 955 | 954 |
throw FormatError(msg.str()); |
| 956 | 955 |
} |
| 957 | 956 |
n = it->second; |
| 958 | 957 |
} |
| 959 | 958 |
|
| 960 | 959 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
| 961 | 960 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
| 962 | 961 |
} |
| 963 | 962 |
|
| 964 | 963 |
} |
| 965 | 964 |
if (readSuccess()) {
|
| 966 | 965 |
line.putback(c); |
| 967 | 966 |
} |
| 968 | 967 |
} |
| 969 | 968 |
|
| 970 | 969 |
void readArcs() {
|
| 971 | 970 |
|
| 972 | 971 |
std::vector<int> map_index(_arc_maps.size()); |
| 973 | 972 |
int map_num, label_index; |
| 974 | 973 |
|
| 975 | 974 |
char c; |
| 976 | 975 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 977 | 976 |
if (readSuccess() && line) line.putback(c); |
| 978 | 977 |
if (!_arc_maps.empty()) |
| 979 | 978 |
throw FormatError("Cannot find map names");
|
| 980 | 979 |
return; |
| 981 | 980 |
} |
| 982 | 981 |
line.putback(c); |
| 983 | 982 |
|
| 984 | 983 |
{
|
| 985 | 984 |
std::map<std::string, int> maps; |
| 986 | 985 |
|
| 987 | 986 |
std::string map; |
| 988 | 987 |
int index = 0; |
| 989 | 988 |
while (_reader_bits::readToken(line, map)) {
|
| 990 | 989 |
if (maps.find(map) != maps.end()) {
|
| 991 | 990 |
std::ostringstream msg; |
| 992 | 991 |
msg << "Multiple occurence of arc map: " << map; |
| 993 | 992 |
throw FormatError(msg.str()); |
| 994 | 993 |
} |
| 995 | 994 |
maps.insert(std::make_pair(map, index)); |
| 996 | 995 |
++index; |
| 997 | 996 |
} |
| 998 | 997 |
|
| 999 | 998 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
| 1000 | 999 |
std::map<std::string, int>::iterator jt = |
| 1001 | 1000 |
maps.find(_arc_maps[i].first); |
| 1002 | 1001 |
if (jt == maps.end()) {
|
| 1003 | 1002 |
std::ostringstream msg; |
| 1004 | 1003 |
msg << "Map not found: " << _arc_maps[i].first; |
| 1005 | 1004 |
throw FormatError(msg.str()); |
| 1006 | 1005 |
} |
| 1007 | 1006 |
map_index[i] = jt->second; |
| 1008 | 1007 |
} |
| 1009 | 1008 |
|
| 1010 | 1009 |
{
|
| 1011 | 1010 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
| 1012 | 1011 |
if (jt != maps.end()) {
|
| 1013 | 1012 |
label_index = jt->second; |
| 1014 | 1013 |
} else {
|
| 1015 | 1014 |
label_index = -1; |
| 1016 | 1015 |
} |
| 1017 | 1016 |
} |
| 1018 | 1017 |
map_num = maps.size(); |
| 1019 | 1018 |
} |
| 1020 | 1019 |
|
| 1021 | 1020 |
while (readLine() && line >> c && c != '@') {
|
| 1022 | 1021 |
line.putback(c); |
| 1023 | 1022 |
|
| 1024 | 1023 |
std::string source_token; |
| 1025 | 1024 |
std::string target_token; |
| 1026 | 1025 |
|
| 1027 | 1026 |
if (!_reader_bits::readToken(line, source_token)) |
| 1028 | 1027 |
throw FormatError("Source not found");
|
| 1029 | 1028 |
|
| 1030 | 1029 |
if (!_reader_bits::readToken(line, target_token)) |
| 1031 | 1030 |
throw FormatError("Target not found");
|
| 1032 | 1031 |
|
| 1033 | 1032 |
std::vector<std::string> tokens(map_num); |
| 1034 | 1033 |
for (int i = 0; i < map_num; ++i) {
|
| 1035 | 1034 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 1036 | 1035 |
std::ostringstream msg; |
| 1037 | 1036 |
msg << "Column not found (" << i + 1 << ")";
|
| 1038 | 1037 |
throw FormatError(msg.str()); |
| 1039 | 1038 |
} |
| 1040 | 1039 |
} |
| 1041 | 1040 |
if (line >> std::ws >> c) |
| 1042 | 1041 |
throw FormatError("Extra character at the end of line");
|
| 1043 | 1042 |
|
| 1044 | 1043 |
Arc a; |
| 1045 | 1044 |
if (!_use_arcs) {
|
| 1046 | 1045 |
|
| 1047 | 1046 |
typename NodeIndex::iterator it; |
| 1048 | 1047 |
|
| 1049 | 1048 |
it = _node_index.find(source_token); |
| 1050 | 1049 |
if (it == _node_index.end()) {
|
| 1051 | 1050 |
std::ostringstream msg; |
| 1052 | 1051 |
msg << "Item not found: " << source_token; |
| 1053 | 1052 |
throw FormatError(msg.str()); |
| 1054 | 1053 |
} |
| 1055 | 1054 |
Node source = it->second; |
| 1056 | 1055 |
|
| 1057 | 1056 |
it = _node_index.find(target_token); |
| 1058 | 1057 |
if (it == _node_index.end()) {
|
| 1059 | 1058 |
std::ostringstream msg; |
| 1060 | 1059 |
msg << "Item not found: " << target_token; |
| 1061 | 1060 |
throw FormatError(msg.str()); |
| 1062 | 1061 |
} |
| 1063 | 1062 |
Node target = it->second; |
| 1064 | 1063 |
|
| 1065 | 1064 |
a = _digraph.addArc(source, target); |
| 1066 | 1065 |
if (label_index != -1) |
| 1067 | 1066 |
_arc_index.insert(std::make_pair(tokens[label_index], a)); |
| 1068 | 1067 |
} else {
|
| 1069 | 1068 |
if (label_index == -1) |
| 1070 | 1069 |
throw FormatError("Label map not found");
|
| 1071 | 1070 |
typename std::map<std::string, Arc>::iterator it = |
| 1072 | 1071 |
_arc_index.find(tokens[label_index]); |
| 1073 | 1072 |
if (it == _arc_index.end()) {
|
| 1074 | 1073 |
std::ostringstream msg; |
| 1075 | 1074 |
msg << "Arc with label not found: " << tokens[label_index]; |
| 1076 | 1075 |
throw FormatError(msg.str()); |
| 1077 | 1076 |
} |
| 1078 | 1077 |
a = it->second; |
| 1079 | 1078 |
} |
| 1080 | 1079 |
|
| 1081 | 1080 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
| 1082 | 1081 |
_arc_maps[i].second->set(a, tokens[map_index[i]]); |
| 1083 | 1082 |
} |
| 1084 | 1083 |
|
| 1085 | 1084 |
} |
| 1086 | 1085 |
if (readSuccess()) {
|
| 1087 | 1086 |
line.putback(c); |
| 1088 | 1087 |
} |
| 1089 | 1088 |
} |
| 1090 | 1089 |
|
| 1091 | 1090 |
void readAttributes() {
|
| 1092 | 1091 |
|
| 1093 | 1092 |
std::set<std::string> read_attr; |
| 1094 | 1093 |
|
| 1095 | 1094 |
char c; |
| 1096 | 1095 |
while (readLine() && line >> c && c != '@') {
|
| 1097 | 1096 |
line.putback(c); |
| 1098 | 1097 |
|
| 1099 | 1098 |
std::string attr, token; |
| 1100 | 1099 |
if (!_reader_bits::readToken(line, attr)) |
| 1101 | 1100 |
throw FormatError("Attribute name not found");
|
| 1102 | 1101 |
if (!_reader_bits::readToken(line, token)) |
| 1103 | 1102 |
throw FormatError("Attribute value not found");
|
| 1104 | 1103 |
if (line >> c) |
| 1105 | 1104 |
throw FormatError("Extra character at the end of line");
|
| 1106 | 1105 |
|
| 1107 | 1106 |
{
|
| 1108 | 1107 |
std::set<std::string>::iterator it = read_attr.find(attr); |
| 1109 | 1108 |
if (it != read_attr.end()) {
|
| 1110 | 1109 |
std::ostringstream msg; |
| 1111 | 1110 |
msg << "Multiple occurence of attribute: " << attr; |
| 1112 | 1111 |
throw FormatError(msg.str()); |
| 1113 | 1112 |
} |
| 1114 | 1113 |
read_attr.insert(attr); |
| 1115 | 1114 |
} |
| 1116 | 1115 |
|
| 1117 | 1116 |
{
|
| 1118 | 1117 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1119 | 1118 |
while (it != _attributes.end() && it->first == attr) {
|
| 1120 | 1119 |
it->second->set(token); |
| 1121 | 1120 |
++it; |
| 1122 | 1121 |
} |
| 1123 | 1122 |
} |
| 1124 | 1123 |
|
| 1125 | 1124 |
} |
| 1126 | 1125 |
if (readSuccess()) {
|
| 1127 | 1126 |
line.putback(c); |
| 1128 | 1127 |
} |
| 1129 | 1128 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1130 | 1129 |
it != _attributes.end(); ++it) {
|
| 1131 | 1130 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1132 | 1131 |
std::ostringstream msg; |
| 1133 | 1132 |
msg << "Attribute not found: " << it->first; |
| 1134 | 1133 |
throw FormatError(msg.str()); |
| 1135 | 1134 |
} |
| 1136 | 1135 |
} |
| 1137 | 1136 |
} |
| 1138 | 1137 |
|
| 1139 | 1138 |
public: |
| 1140 | 1139 |
|
| 1141 | 1140 |
/// \name Execution of the reader |
| 1142 | 1141 |
/// @{
|
| 1143 | 1142 |
|
| 1144 | 1143 |
/// \brief Start the batch processing |
| 1145 | 1144 |
/// |
| 1146 | 1145 |
/// This function starts the batch processing |
| 1147 | 1146 |
void run() {
|
| 1148 | 1147 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1149 | 1148 |
|
| 1150 | 1149 |
bool nodes_done = _skip_nodes; |
| 1151 | 1150 |
bool arcs_done = _skip_arcs; |
| 1152 | 1151 |
bool attributes_done = false; |
| 1153 | 1152 |
|
| 1154 | 1153 |
line_num = 0; |
| 1155 | 1154 |
readLine(); |
| 1156 | 1155 |
skipSection(); |
| 1157 | 1156 |
|
| 1158 | 1157 |
while (readSuccess()) {
|
| 1159 | 1158 |
try {
|
| 1160 | 1159 |
char c; |
| 1161 | 1160 |
std::string section, caption; |
| 1162 | 1161 |
line >> c; |
| 1163 | 1162 |
_reader_bits::readToken(line, section); |
| 1164 | 1163 |
_reader_bits::readToken(line, caption); |
| 1165 | 1164 |
|
| 1166 | 1165 |
if (line >> c) |
| 1167 | 1166 |
throw FormatError("Extra character at the end of line");
|
| 1168 | 1167 |
|
| 1169 | 1168 |
if (section == "nodes" && !nodes_done) {
|
| 1170 | 1169 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 1171 | 1170 |
readNodes(); |
| 1172 | 1171 |
nodes_done = true; |
| 1173 | 1172 |
} |
| 1174 | 1173 |
} else if ((section == "arcs" || section == "edges") && |
| 1175 | 1174 |
!arcs_done) {
|
| 1176 | 1175 |
if (_arcs_caption.empty() || _arcs_caption == caption) {
|
| 1177 | 1176 |
readArcs(); |
| 1178 | 1177 |
arcs_done = true; |
| 1179 | 1178 |
} |
| 1180 | 1179 |
} else if (section == "attributes" && !attributes_done) {
|
| 1181 | 1180 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 1182 | 1181 |
readAttributes(); |
| 1183 | 1182 |
attributes_done = true; |
| 1184 | 1183 |
} |
| 1185 | 1184 |
} else {
|
| 1186 | 1185 |
readLine(); |
| 1187 | 1186 |
skipSection(); |
| 1188 | 1187 |
} |
| 1189 | 1188 |
} catch (FormatError& error) {
|
| 1190 | 1189 |
error.line(line_num); |
| 1191 | 1190 |
error.file(_filename); |
| 1192 | 1191 |
throw; |
| 1193 | 1192 |
} |
| 1194 | 1193 |
} |
| 1195 | 1194 |
|
| 1196 | 1195 |
if (!nodes_done) {
|
| 1197 | 1196 |
throw FormatError("Section @nodes not found");
|
| 1198 | 1197 |
} |
| 1199 | 1198 |
|
| 1200 | 1199 |
if (!arcs_done) {
|
| 1201 | 1200 |
throw FormatError("Section @arcs not found");
|
| 1202 | 1201 |
} |
| 1203 | 1202 |
|
| 1204 | 1203 |
if (!attributes_done && !_attributes.empty()) {
|
| 1205 | 1204 |
throw FormatError("Section @attributes not found");
|
| 1206 | 1205 |
} |
| 1207 | 1206 |
|
| 1208 | 1207 |
} |
| 1209 | 1208 |
|
| 1210 | 1209 |
/// @} |
| 1211 | 1210 |
|
| 1212 | 1211 |
}; |
| 1213 | 1212 |
|
| 1214 | 1213 |
template <typename Graph> |
| 1215 | 1214 |
class GraphReader; |
| 1216 | 1215 |
|
| 1217 | 1216 |
/// \brief Return a \ref GraphReader class |
| 1218 | 1217 |
/// |
| 1219 | 1218 |
/// This function just returns a \ref GraphReader class. |
| 1220 | 1219 |
/// \relates GraphReader |
| 1221 | 1220 |
template <typename Graph> |
| 1222 | 1221 |
GraphReader<Graph> graphReader(Graph& graph, std::istream& is = std::cin) {
|
| 1223 | 1222 |
GraphReader<Graph> tmp(graph, is); |
| 1224 | 1223 |
return tmp; |
| 1225 | 1224 |
} |
| 1226 | 1225 |
|
| 1227 | 1226 |
/// \brief Return a \ref GraphReader class |
| 1228 | 1227 |
/// |
| 1229 | 1228 |
/// This function just returns a \ref GraphReader class. |
| 1230 | 1229 |
/// \relates GraphReader |
| 1231 | 1230 |
template <typename Graph> |
| 1232 | 1231 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn) {
|
| 1233 | 1232 |
GraphReader<Graph> tmp(graph, fn); |
| 1234 | 1233 |
return tmp; |
| 1235 | 1234 |
} |
| 1236 | 1235 |
|
| 1237 | 1236 |
/// \brief Return a \ref GraphReader class |
| 1238 | 1237 |
/// |
| 1239 | 1238 |
/// This function just returns a \ref GraphReader class. |
| 1240 | 1239 |
/// \relates GraphReader |
| 1241 | 1240 |
template <typename Graph> |
| 1242 | 1241 |
GraphReader<Graph> graphReader(Graph& graph, const char* fn) {
|
| 1243 | 1242 |
GraphReader<Graph> tmp(graph, fn); |
| 1244 | 1243 |
return tmp; |
| 1245 | 1244 |
} |
| 1246 | 1245 |
|
| 1247 | 1246 |
/// \ingroup lemon_io |
| 1248 | 1247 |
/// |
| 1249 | 1248 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
| 1250 | 1249 |
/// |
| 1251 | 1250 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 1252 | 1251 |
/// |
| 1253 | 1252 |
/// It can be used almost the same way as \c DigraphReader. |
| 1254 | 1253 |
/// The only difference is that this class can handle edges and |
| 1255 | 1254 |
/// edge maps as well as arcs and arc maps. |
| 1256 | 1255 |
/// |
| 1257 | 1256 |
/// The columns in the \c \@edges (or \c \@arcs) section are the |
| 1258 | 1257 |
/// edge maps. However, if there are two maps with the same name |
| 1259 | 1258 |
/// prefixed with \c '+' and \c '-', then these can be read into an |
| 1260 | 1259 |
/// arc map. Similarly, an attribute can be read into an arc, if |
| 1261 | 1260 |
/// it's value is an edge label prefixed with \c '+' or \c '-'. |
| 1262 | 1261 |
template <typename _Graph> |
| 1263 | 1262 |
class GraphReader {
|
| 1264 | 1263 |
public: |
| 1265 | 1264 |
|
| 1266 | 1265 |
typedef _Graph Graph; |
| 1267 | 1266 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 1268 | 1267 |
|
| 1269 | 1268 |
private: |
| 1270 | 1269 |
|
| 1271 | 1270 |
std::istream* _is; |
| 1272 | 1271 |
bool local_is; |
| 1273 | 1272 |
std::string _filename; |
| 1274 | 1273 |
|
| 1275 | 1274 |
Graph& _graph; |
| 1276 | 1275 |
|
| 1277 | 1276 |
std::string _nodes_caption; |
| 1278 | 1277 |
std::string _edges_caption; |
| 1279 | 1278 |
std::string _attributes_caption; |
| 1280 | 1279 |
|
| 1281 | 1280 |
typedef std::map<std::string, Node> NodeIndex; |
| 1282 | 1281 |
NodeIndex _node_index; |
| 1283 | 1282 |
typedef std::map<std::string, Edge> EdgeIndex; |
| 1284 | 1283 |
EdgeIndex _edge_index; |
| 1285 | 1284 |
|
| 1286 | 1285 |
typedef std::vector<std::pair<std::string, |
| 1287 | 1286 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 1288 | 1287 |
NodeMaps _node_maps; |
| 1289 | 1288 |
|
| 1290 | 1289 |
typedef std::vector<std::pair<std::string, |
| 1291 | 1290 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
| 1292 | 1291 |
EdgeMaps _edge_maps; |
| 1293 | 1292 |
|
| 1294 | 1293 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 1295 | 1294 |
Attributes; |
| 1296 | 1295 |
Attributes _attributes; |
| 1297 | 1296 |
|
| 1298 | 1297 |
bool _use_nodes; |
| 1299 | 1298 |
bool _use_edges; |
| 1300 | 1299 |
|
| 1301 | 1300 |
bool _skip_nodes; |
| 1302 | 1301 |
bool _skip_edges; |
| 1303 | 1302 |
|
| 1304 | 1303 |
int line_num; |
| 1305 | 1304 |
std::istringstream line; |
| 1306 | 1305 |
|
| 1307 | 1306 |
public: |
| 1308 | 1307 |
|
| 1309 | 1308 |
/// \brief Constructor |
| 1310 | 1309 |
/// |
| 1311 | 1310 |
/// Construct an undirected graph reader, which reads from the given |
| 1312 | 1311 |
/// input stream. |
| 1313 | 1312 |
GraphReader(Graph& graph, std::istream& is = std::cin) |
| 1314 | 1313 |
: _is(&is), local_is(false), _graph(graph), |
| 1315 | 1314 |
_use_nodes(false), _use_edges(false), |
| 1316 | 1315 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1317 | 1316 |
|
| 1318 | 1317 |
/// \brief Constructor |
| 1319 | 1318 |
/// |
| 1320 | 1319 |
/// Construct an undirected graph reader, which reads from the given |
| 1321 | 1320 |
/// file. |
| 1322 | 1321 |
GraphReader(Graph& graph, const std::string& fn) |
| 1323 | 1322 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
| 1324 | 1323 |
_filename(fn), _graph(graph), |
| 1325 | 1324 |
_use_nodes(false), _use_edges(false), |
| 1326 | 1325 |
_skip_nodes(false), _skip_edges(false) {
|
| 1327 | 1326 |
if (!(*_is)) {
|
| 1328 | 1327 |
delete _is; |
| 1329 | 1328 |
throw IoError("Cannot open file", fn);
|
| 1330 | 1329 |
} |
| 1331 | 1330 |
} |
| 1332 | 1331 |
|
| 1333 | 1332 |
/// \brief Constructor |
| 1334 | 1333 |
/// |
| 1335 | 1334 |
/// Construct an undirected graph reader, which reads from the given |
| 1336 | 1335 |
/// file. |
| 1337 | 1336 |
GraphReader(Graph& graph, const char* fn) |
| 1338 | 1337 |
: _is(new std::ifstream(fn)), local_is(true), |
| 1339 | 1338 |
_filename(fn), _graph(graph), |
| 1340 | 1339 |
_use_nodes(false), _use_edges(false), |
| 1341 | 1340 |
_skip_nodes(false), _skip_edges(false) {
|
| 1342 | 1341 |
if (!(*_is)) {
|
| 1343 | 1342 |
delete _is; |
| 1344 | 1343 |
throw IoError("Cannot open file", fn);
|
| 1345 | 1344 |
} |
| 1346 | 1345 |
} |
| 1347 | 1346 |
|
| 1348 | 1347 |
/// \brief Destructor |
| 1349 | 1348 |
~GraphReader() {
|
| 1350 | 1349 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1351 | 1350 |
it != _node_maps.end(); ++it) {
|
| 1352 | 1351 |
delete it->second; |
| 1353 | 1352 |
} |
| 1354 | 1353 |
|
| 1355 | 1354 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1356 | 1355 |
it != _edge_maps.end(); ++it) {
|
| 1357 | 1356 |
delete it->second; |
| 1358 | 1357 |
} |
| 1359 | 1358 |
|
| 1360 | 1359 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1361 | 1360 |
it != _attributes.end(); ++it) {
|
| 1362 | 1361 |
delete it->second; |
| 1363 | 1362 |
} |
| 1364 | 1363 |
|
| 1365 | 1364 |
if (local_is) {
|
| 1366 | 1365 |
delete _is; |
| 1367 | 1366 |
} |
| 1368 | 1367 |
|
| 1369 | 1368 |
} |
| 1370 | 1369 |
|
| 1371 | 1370 |
private: |
| 1372 | 1371 |
friend GraphReader<Graph> graphReader<>(Graph& graph, std::istream& is); |
| 1373 | 1372 |
friend GraphReader<Graph> graphReader<>(Graph& graph, |
| 1374 | 1373 |
const std::string& fn); |
| 1375 | 1374 |
friend GraphReader<Graph> graphReader<>(Graph& graph, const char *fn); |
| 1376 | 1375 |
|
| 1377 | 1376 |
GraphReader(GraphReader& other) |
| 1378 | 1377 |
: _is(other._is), local_is(other.local_is), _graph(other._graph), |
| 1379 | 1378 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges), |
| 1380 | 1379 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
| 1381 | 1380 |
|
| 1382 | 1381 |
other._is = 0; |
| 1383 | 1382 |
other.local_is = false; |
| 1384 | 1383 |
|
| 1385 | 1384 |
_node_index.swap(other._node_index); |
| 1386 | 1385 |
_edge_index.swap(other._edge_index); |
| 1387 | 1386 |
|
| 1388 | 1387 |
_node_maps.swap(other._node_maps); |
| 1389 | 1388 |
_edge_maps.swap(other._edge_maps); |
| 1390 | 1389 |
_attributes.swap(other._attributes); |
| 1391 | 1390 |
|
| 1392 | 1391 |
_nodes_caption = other._nodes_caption; |
| 1393 | 1392 |
_edges_caption = other._edges_caption; |
| 1394 | 1393 |
_attributes_caption = other._attributes_caption; |
| 1395 | 1394 |
|
| 1396 | 1395 |
} |
| 1397 | 1396 |
|
| 1398 | 1397 |
GraphReader& operator=(const GraphReader&); |
| 1399 | 1398 |
|
| 1400 | 1399 |
public: |
| 1401 | 1400 |
|
| 1402 | 1401 |
/// \name Reading rules |
| 1403 | 1402 |
/// @{
|
| 1404 | 1403 |
|
| 1405 | 1404 |
/// \brief Node map reading rule |
| 1406 | 1405 |
/// |
| 1407 | 1406 |
/// Add a node map reading rule to the reader. |
| 1408 | 1407 |
template <typename Map> |
| 1409 | 1408 |
GraphReader& nodeMap(const std::string& caption, Map& map) {
|
| 1410 | 1409 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 1411 | 1410 |
_reader_bits::MapStorageBase<Node>* storage = |
| 1412 | 1411 |
new _reader_bits::MapStorage<Node, Map>(map); |
| 1413 | 1412 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1414 | 1413 |
return *this; |
| 1415 | 1414 |
} |
| 1416 | 1415 |
|
| 1417 | 1416 |
/// \brief Node map reading rule |
| 1418 | 1417 |
/// |
| 1419 | 1418 |
/// Add a node map reading rule with specialized converter to the |
| 1420 | 1419 |
/// reader. |
| 1421 | 1420 |
template <typename Map, typename Converter> |
| 1422 | 1421 |
GraphReader& nodeMap(const std::string& caption, Map& map, |
| 1423 | 1422 |
const Converter& converter = Converter()) {
|
| 1424 | 1423 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 1425 | 1424 |
_reader_bits::MapStorageBase<Node>* storage = |
| 1426 | 1425 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 1427 | 1426 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1428 | 1427 |
return *this; |
| 1429 | 1428 |
} |
| 1430 | 1429 |
|
| 1431 | 1430 |
/// \brief Edge map reading rule |
| 1432 | 1431 |
/// |
| 1433 | 1432 |
/// Add an edge map reading rule to the reader. |
| 1434 | 1433 |
template <typename Map> |
| 1435 | 1434 |
GraphReader& edgeMap(const std::string& caption, Map& map) {
|
| 1436 | 1435 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
| 1437 | 1436 |
_reader_bits::MapStorageBase<Edge>* storage = |
| 1438 | 1437 |
new _reader_bits::MapStorage<Edge, Map>(map); |
| 1439 | 1438 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1440 | 1439 |
return *this; |
| 1441 | 1440 |
} |
| 1442 | 1441 |
|
| 1443 | 1442 |
/// \brief Edge map reading rule |
| 1444 | 1443 |
/// |
| 1445 | 1444 |
/// Add an edge map reading rule with specialized converter to the |
| 1446 | 1445 |
/// reader. |
| 1447 | 1446 |
template <typename Map, typename Converter> |
| 1448 | 1447 |
GraphReader& edgeMap(const std::string& caption, Map& map, |
| 1449 | 1448 |
const Converter& converter = Converter()) {
|
| 1450 | 1449 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
| 1451 | 1450 |
_reader_bits::MapStorageBase<Edge>* storage = |
| 1452 | 1451 |
new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter); |
| 1453 | 1452 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1454 | 1453 |
return *this; |
| 1455 | 1454 |
} |
| 1456 | 1455 |
|
| 1457 | 1456 |
/// \brief Arc map reading rule |
| 1458 | 1457 |
/// |
| 1459 | 1458 |
/// Add an arc map reading rule to the reader. |
| 1460 | 1459 |
template <typename Map> |
| 1461 | 1460 |
GraphReader& arcMap(const std::string& caption, Map& map) {
|
| 1462 | 1461 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 1463 | 1462 |
_reader_bits::MapStorageBase<Edge>* forward_storage = |
| 1464 | 1463 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
| 1465 | 1464 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1466 | 1465 |
_reader_bits::MapStorageBase<Edge>* backward_storage = |
| 1467 | 1466 |
new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map); |
| 1468 | 1467 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1469 | 1468 |
return *this; |
| 1470 | 1469 |
} |
| 1471 | 1470 |
|
| 1472 | 1471 |
/// \brief Arc map reading rule |
| 1473 | 1472 |
/// |
| 1474 | 1473 |
/// Add an arc map reading rule with specialized converter to the |
| 1475 | 1474 |
/// reader. |
| 1476 | 1475 |
template <typename Map, typename Converter> |
| 1477 | 1476 |
GraphReader& arcMap(const std::string& caption, Map& map, |
| 1478 | 1477 |
const Converter& converter = Converter()) {
|
| 1479 | 1478 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 1480 | 1479 |
_reader_bits::MapStorageBase<Edge>* forward_storage = |
| 1481 | 1480 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter> |
| 1482 | 1481 |
(_graph, map, converter); |
| 1483 | 1482 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1484 | 1483 |
_reader_bits::MapStorageBase<Edge>* backward_storage = |
| 1485 | 1484 |
new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter> |
| 1486 | 1485 |
(_graph, map, converter); |
| 1487 | 1486 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1488 | 1487 |
return *this; |
| 1489 | 1488 |
} |
| 1490 | 1489 |
|
| 1491 | 1490 |
/// \brief Attribute reading rule |
| 1492 | 1491 |
/// |
| 1493 | 1492 |
/// Add an attribute reading rule to the reader. |
| 1494 | 1493 |
template <typename Value> |
| 1495 | 1494 |
GraphReader& attribute(const std::string& caption, Value& value) {
|
| 1496 | 1495 |
_reader_bits::ValueStorageBase* storage = |
| 1497 | 1496 |
new _reader_bits::ValueStorage<Value>(value); |
| 1498 | 1497 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1499 | 1498 |
return *this; |
| 1500 | 1499 |
} |
| 1501 | 1500 |
|
| 1502 | 1501 |
/// \brief Attribute reading rule |
| 1503 | 1502 |
/// |
| 1504 | 1503 |
/// Add an attribute reading rule with specialized converter to the |
| 1505 | 1504 |
/// reader. |
| 1506 | 1505 |
template <typename Value, typename Converter> |
| 1507 | 1506 |
GraphReader& attribute(const std::string& caption, Value& value, |
| 1508 | 1507 |
const Converter& converter = Converter()) {
|
| 1509 | 1508 |
_reader_bits::ValueStorageBase* storage = |
| 1510 | 1509 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
| 1511 | 1510 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1512 | 1511 |
return *this; |
| 1513 | 1512 |
} |
| 1514 | 1513 |
|
| 1515 | 1514 |
/// \brief Node reading rule |
| 1516 | 1515 |
/// |
| 1517 | 1516 |
/// Add a node reading rule to reader. |
| 1518 | 1517 |
GraphReader& node(const std::string& caption, Node& node) {
|
| 1519 | 1518 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
| 1520 | 1519 |
Converter converter(_node_index); |
| 1521 | 1520 |
_reader_bits::ValueStorageBase* storage = |
| 1522 | 1521 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
| 1523 | 1522 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1524 | 1523 |
return *this; |
| 1525 | 1524 |
} |
| 1526 | 1525 |
|
| 1527 | 1526 |
/// \brief Edge reading rule |
| 1528 | 1527 |
/// |
| 1529 | 1528 |
/// Add an edge reading rule to reader. |
| 1530 | 1529 |
GraphReader& edge(const std::string& caption, Edge& edge) {
|
| 1531 | 1530 |
typedef _reader_bits::MapLookUpConverter<Edge> Converter; |
| 1532 | 1531 |
Converter converter(_edge_index); |
| 1533 | 1532 |
_reader_bits::ValueStorageBase* storage = |
| 1534 | 1533 |
new _reader_bits::ValueStorage<Edge, Converter>(edge, converter); |
| 1535 | 1534 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1536 | 1535 |
return *this; |
| 1537 | 1536 |
} |
| 1538 | 1537 |
|
| 1539 | 1538 |
/// \brief Arc reading rule |
| 1540 | 1539 |
/// |
| 1541 | 1540 |
/// Add an arc reading rule to reader. |
| 1542 | 1541 |
GraphReader& arc(const std::string& caption, Arc& arc) {
|
| 1543 | 1542 |
typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter; |
| 1544 | 1543 |
Converter converter(_graph, _edge_index); |
| 1545 | 1544 |
_reader_bits::ValueStorageBase* storage = |
| 1546 | 1545 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 1547 | 1546 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1548 | 1547 |
return *this; |
| 1549 | 1548 |
} |
| 1550 | 1549 |
|
| 1551 | 1550 |
/// @} |
| 1552 | 1551 |
|
| 1553 | 1552 |
/// \name Select section by name |
| 1554 | 1553 |
/// @{
|
| 1555 | 1554 |
|
| 1556 | 1555 |
/// \brief Set \c \@nodes section to be read |
| 1557 | 1556 |
/// |
| 1558 | 1557 |
/// Set \c \@nodes section to be read. |
| 1559 | 1558 |
GraphReader& nodes(const std::string& caption) {
|
| 1560 | 1559 |
_nodes_caption = caption; |
| 1561 | 1560 |
return *this; |
| 1562 | 1561 |
} |
| 1563 | 1562 |
|
| 1564 | 1563 |
/// \brief Set \c \@edges section to be read |
| 1565 | 1564 |
/// |
| 1566 | 1565 |
/// Set \c \@edges section to be read. |
| 1567 | 1566 |
GraphReader& edges(const std::string& caption) {
|
| 1568 | 1567 |
_edges_caption = caption; |
| 1569 | 1568 |
return *this; |
| 1570 | 1569 |
} |
| 1571 | 1570 |
|
| 1572 | 1571 |
/// \brief Set \c \@attributes section to be read |
| 1573 | 1572 |
/// |
| 1574 | 1573 |
/// Set \c \@attributes section to be read. |
| 1575 | 1574 |
GraphReader& attributes(const std::string& caption) {
|
| 1576 | 1575 |
_attributes_caption = caption; |
| 1577 | 1576 |
return *this; |
| 1578 | 1577 |
} |
| 1579 | 1578 |
|
| 1580 | 1579 |
/// @} |
| 1581 | 1580 |
|
| 1582 | 1581 |
/// \name Using previously constructed node or edge set |
| 1583 | 1582 |
/// @{
|
| 1584 | 1583 |
|
| 1585 | 1584 |
/// \brief Use previously constructed node set |
| 1586 | 1585 |
/// |
| 1587 | 1586 |
/// Use previously constructed node set, and specify the node |
| 1588 | 1587 |
/// label map. |
| 1589 | 1588 |
template <typename Map> |
| 1590 | 1589 |
GraphReader& useNodes(const Map& map) {
|
| 1591 | 1590 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1592 | 1591 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 1593 | 1592 |
_use_nodes = true; |
| 1594 | 1593 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 1595 | 1594 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1596 | 1595 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 1597 | 1596 |
} |
| 1598 | 1597 |
return *this; |
| 1599 | 1598 |
} |
| 1600 | 1599 |
|
| 1601 | 1600 |
/// \brief Use previously constructed node set |
| 1602 | 1601 |
/// |
| 1603 | 1602 |
/// Use previously constructed node set, and specify the node |
| 1604 | 1603 |
/// label map and a functor which converts the label map values to |
| 1605 | 1604 |
/// \c std::string. |
| 1606 | 1605 |
template <typename Map, typename Converter> |
| 1607 | 1606 |
GraphReader& useNodes(const Map& map, |
| 1608 | 1607 |
const Converter& converter = Converter()) {
|
| 1609 | 1608 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1610 | 1609 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 1611 | 1610 |
_use_nodes = true; |
| 1612 | 1611 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1613 | 1612 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 1614 | 1613 |
} |
| 1615 | 1614 |
return *this; |
| 1616 | 1615 |
} |
| 1617 | 1616 |
|
| 1618 | 1617 |
/// \brief Use previously constructed edge set |
| 1619 | 1618 |
/// |
| 1620 | 1619 |
/// Use previously constructed edge set, and specify the edge |
| 1621 | 1620 |
/// label map. |
| 1622 | 1621 |
template <typename Map> |
| 1623 | 1622 |
GraphReader& useEdges(const Map& map) {
|
| 1624 | 1623 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1625 | 1624 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
| 1626 | 1625 |
_use_edges = true; |
| 1627 | 1626 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 1628 | 1627 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
| 1629 | 1628 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
| 1630 | 1629 |
} |
| 1631 | 1630 |
return *this; |
| 1632 | 1631 |
} |
| 1633 | 1632 |
|
| 1634 | 1633 |
/// \brief Use previously constructed edge set |
| 1635 | 1634 |
/// |
| 1636 | 1635 |
/// Use previously constructed edge set, and specify the edge |
| 1637 | 1636 |
/// label map and a functor which converts the label map values to |
| 1638 | 1637 |
/// \c std::string. |
| 1639 | 1638 |
template <typename Map, typename Converter> |
| 1640 | 1639 |
GraphReader& useEdges(const Map& map, |
| 1641 | 1640 |
const Converter& converter = Converter()) {
|
| 1642 | 1641 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1643 | 1642 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
| 1644 | 1643 |
_use_edges = true; |
| 1645 | 1644 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
| 1646 | 1645 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
| 1647 | 1646 |
} |
| 1648 | 1647 |
return *this; |
| 1649 | 1648 |
} |
| 1650 | 1649 |
|
| 1651 | 1650 |
/// \brief Skip the reading of node section |
| 1652 | 1651 |
/// |
| 1653 | 1652 |
/// Omit the reading of the node section. This implies that each node |
| 1654 | 1653 |
/// map reading rule will be abandoned, and the nodes of the graph |
| 1655 | 1654 |
/// will not be constructed, which usually cause that the edge set |
| 1656 | 1655 |
/// could not be read due to lack of node name |
| 1657 | 1656 |
/// could not be read due to lack of node name resolving. |
| 1658 | 1657 |
/// Therefore \c skipEdges() function should also be used, or |
| 1659 | 1658 |
/// \c useNodes() should be used to specify the label of the nodes. |
| 1660 | 1659 |
GraphReader& skipNodes() {
|
| 1661 | 1660 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
| 1662 | 1661 |
_skip_nodes = true; |
| 1663 | 1662 |
return *this; |
| 1664 | 1663 |
} |
| 1665 | 1664 |
|
| 1666 | 1665 |
/// \brief Skip the reading of edge section |
| 1667 | 1666 |
/// |
| 1668 | 1667 |
/// Omit the reading of the edge section. This implies that each edge |
| 1669 | 1668 |
/// map reading rule will be abandoned, and the edges of the graph |
| 1670 | 1669 |
/// will not be constructed. |
| 1671 | 1670 |
GraphReader& skipEdges() {
|
| 1672 | 1671 |
LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
| 1673 | 1672 |
_skip_edges = true; |
| 1674 | 1673 |
return *this; |
| 1675 | 1674 |
} |
| 1676 | 1675 |
|
| 1677 | 1676 |
/// @} |
| 1678 | 1677 |
|
| 1679 | 1678 |
private: |
| 1680 | 1679 |
|
| 1681 | 1680 |
bool readLine() {
|
| 1682 | 1681 |
std::string str; |
| 1683 | 1682 |
while(++line_num, std::getline(*_is, str)) {
|
| 1684 | 1683 |
line.clear(); line.str(str); |
| 1685 | 1684 |
char c; |
| 1686 | 1685 |
if (line >> std::ws >> c && c != '#') {
|
| 1687 | 1686 |
line.putback(c); |
| 1688 | 1687 |
return true; |
| 1689 | 1688 |
} |
| 1690 | 1689 |
} |
| 1691 | 1690 |
return false; |
| 1692 | 1691 |
} |
| 1693 | 1692 |
|
| 1694 | 1693 |
bool readSuccess() {
|
| 1695 | 1694 |
return static_cast<bool>(*_is); |
| 1696 | 1695 |
} |
| 1697 | 1696 |
|
| 1698 | 1697 |
void skipSection() {
|
| 1699 | 1698 |
char c; |
| 1700 | 1699 |
while (readSuccess() && line >> c && c != '@') {
|
| 1701 | 1700 |
readLine(); |
| 1702 | 1701 |
} |
| 1703 | 1702 |
line.putback(c); |
| 1704 | 1703 |
} |
| 1705 | 1704 |
|
| 1706 | 1705 |
void readNodes() {
|
| 1707 | 1706 |
|
| 1708 | 1707 |
std::vector<int> map_index(_node_maps.size()); |
| 1709 | 1708 |
int map_num, label_index; |
| 1710 | 1709 |
|
| 1711 | 1710 |
char c; |
| 1712 | 1711 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 1713 | 1712 |
if (readSuccess() && line) line.putback(c); |
| 1714 | 1713 |
if (!_node_maps.empty()) |
| 1715 | 1714 |
throw FormatError("Cannot find map names");
|
| 1716 | 1715 |
return; |
| 1717 | 1716 |
} |
| 1718 | 1717 |
line.putback(c); |
| 1719 | 1718 |
|
| 1720 | 1719 |
{
|
| 1721 | 1720 |
std::map<std::string, int> maps; |
| 1722 | 1721 |
|
| 1723 | 1722 |
std::string map; |
| 1724 | 1723 |
int index = 0; |
| 1725 | 1724 |
while (_reader_bits::readToken(line, map)) {
|
| 1726 | 1725 |
if (maps.find(map) != maps.end()) {
|
| 1727 | 1726 |
std::ostringstream msg; |
| 1728 | 1727 |
msg << "Multiple occurence of node map: " << map; |
| 1729 | 1728 |
throw FormatError(msg.str()); |
| 1730 | 1729 |
} |
| 1731 | 1730 |
maps.insert(std::make_pair(map, index)); |
| 1732 | 1731 |
++index; |
| 1733 | 1732 |
} |
| 1734 | 1733 |
|
| 1735 | 1734 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
| 1736 | 1735 |
std::map<std::string, int>::iterator jt = |
| 1737 | 1736 |
maps.find(_node_maps[i].first); |
| 1738 | 1737 |
if (jt == maps.end()) {
|
| 1739 | 1738 |
std::ostringstream msg; |
| 1740 | 1739 |
msg << "Map not found: " << _node_maps[i].first; |
| 1741 | 1740 |
throw FormatError(msg.str()); |
| 1742 | 1741 |
} |
| 1743 | 1742 |
map_index[i] = jt->second; |
| 1744 | 1743 |
} |
| 1745 | 1744 |
|
| 1746 | 1745 |
{
|
| 1747 | 1746 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
| 1748 | 1747 |
if (jt != maps.end()) {
|
| 1749 | 1748 |
label_index = jt->second; |
| 1750 | 1749 |
} else {
|
| 1751 | 1750 |
label_index = -1; |
| 1752 | 1751 |
} |
| 1753 | 1752 |
} |
| 1754 | 1753 |
map_num = maps.size(); |
| 1755 | 1754 |
} |
| 1756 | 1755 |
|
| 1757 | 1756 |
while (readLine() && line >> c && c != '@') {
|
| 1758 | 1757 |
line.putback(c); |
| 1759 | 1758 |
|
| 1760 | 1759 |
std::vector<std::string> tokens(map_num); |
| 1761 | 1760 |
for (int i = 0; i < map_num; ++i) {
|
| 1762 | 1761 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 1763 | 1762 |
std::ostringstream msg; |
| 1764 | 1763 |
msg << "Column not found (" << i + 1 << ")";
|
| 1765 | 1764 |
throw FormatError(msg.str()); |
| 1766 | 1765 |
} |
| 1767 | 1766 |
} |
| 1768 | 1767 |
if (line >> std::ws >> c) |
| 1769 | 1768 |
throw FormatError("Extra character at the end of line");
|
| 1770 | 1769 |
|
| 1771 | 1770 |
Node n; |
| 1772 | 1771 |
if (!_use_nodes) {
|
| 1773 | 1772 |
n = _graph.addNode(); |
| 1774 | 1773 |
if (label_index != -1) |
| 1775 | 1774 |
_node_index.insert(std::make_pair(tokens[label_index], n)); |
| 1776 | 1775 |
} else {
|
| 1777 | 1776 |
if (label_index == -1) |
| 1778 | 1777 |
throw FormatError("Label map not found");
|
| 1779 | 1778 |
typename std::map<std::string, Node>::iterator it = |
| 1780 | 1779 |
_node_index.find(tokens[label_index]); |
| 1781 | 1780 |
if (it == _node_index.end()) {
|
| 1782 | 1781 |
std::ostringstream msg; |
| 1783 | 1782 |
msg << "Node with label not found: " << tokens[label_index]; |
| 1784 | 1783 |
throw FormatError(msg.str()); |
| 1785 | 1784 |
} |
| 1786 | 1785 |
n = it->second; |
| 1787 | 1786 |
} |
| 1788 | 1787 |
|
| 1789 | 1788 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
| 1790 | 1789 |
_node_maps[i].second->set(n, tokens[map_index[i]]); |
| 1791 | 1790 |
} |
| 1792 | 1791 |
|
| 1793 | 1792 |
} |
| 1794 | 1793 |
if (readSuccess()) {
|
| 1795 | 1794 |
line.putback(c); |
| 1796 | 1795 |
} |
| 1797 | 1796 |
} |
| 1798 | 1797 |
|
| 1799 | 1798 |
void readEdges() {
|
| 1800 | 1799 |
|
| 1801 | 1800 |
std::vector<int> map_index(_edge_maps.size()); |
| 1802 | 1801 |
int map_num, label_index; |
| 1803 | 1802 |
|
| 1804 | 1803 |
char c; |
| 1805 | 1804 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 1806 | 1805 |
if (readSuccess() && line) line.putback(c); |
| 1807 | 1806 |
if (!_edge_maps.empty()) |
| 1808 | 1807 |
throw FormatError("Cannot find map names");
|
| 1809 | 1808 |
return; |
| 1810 | 1809 |
} |
| 1811 | 1810 |
line.putback(c); |
| 1812 | 1811 |
|
| 1813 | 1812 |
{
|
| 1814 | 1813 |
std::map<std::string, int> maps; |
| 1815 | 1814 |
|
| 1816 | 1815 |
std::string map; |
| 1817 | 1816 |
int index = 0; |
| 1818 | 1817 |
while (_reader_bits::readToken(line, map)) {
|
| 1819 | 1818 |
if (maps.find(map) != maps.end()) {
|
| 1820 | 1819 |
std::ostringstream msg; |
| 1821 | 1820 |
msg << "Multiple occurence of edge map: " << map; |
| 1822 | 1821 |
throw FormatError(msg.str()); |
| 1823 | 1822 |
} |
| 1824 | 1823 |
maps.insert(std::make_pair(map, index)); |
| 1825 | 1824 |
++index; |
| 1826 | 1825 |
} |
| 1827 | 1826 |
|
| 1828 | 1827 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
| 1829 | 1828 |
std::map<std::string, int>::iterator jt = |
| 1830 | 1829 |
maps.find(_edge_maps[i].first); |
| 1831 | 1830 |
if (jt == maps.end()) {
|
| 1832 | 1831 |
std::ostringstream msg; |
| 1833 | 1832 |
msg << "Map not found: " << _edge_maps[i].first; |
| 1834 | 1833 |
throw FormatError(msg.str()); |
| 1835 | 1834 |
} |
| 1836 | 1835 |
map_index[i] = jt->second; |
| 1837 | 1836 |
} |
| 1838 | 1837 |
|
| 1839 | 1838 |
{
|
| 1840 | 1839 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
| 1841 | 1840 |
if (jt != maps.end()) {
|
| 1842 | 1841 |
label_index = jt->second; |
| 1843 | 1842 |
} else {
|
| 1844 | 1843 |
label_index = -1; |
| 1845 | 1844 |
} |
| 1846 | 1845 |
} |
| 1847 | 1846 |
map_num = maps.size(); |
| 1848 | 1847 |
} |
| 1849 | 1848 |
|
| 1850 | 1849 |
while (readLine() && line >> c && c != '@') {
|
| 1851 | 1850 |
line.putback(c); |
| 1852 | 1851 |
|
| 1853 | 1852 |
std::string source_token; |
| 1854 | 1853 |
std::string target_token; |
| 1855 | 1854 |
|
| 1856 | 1855 |
if (!_reader_bits::readToken(line, source_token)) |
| 1857 | 1856 |
throw FormatError("Node u not found");
|
| 1858 | 1857 |
|
| 1859 | 1858 |
if (!_reader_bits::readToken(line, target_token)) |
| 1860 | 1859 |
throw FormatError("Node v not found");
|
| 1861 | 1860 |
|
| 1862 | 1861 |
std::vector<std::string> tokens(map_num); |
| 1863 | 1862 |
for (int i = 0; i < map_num; ++i) {
|
| 1864 | 1863 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 1865 | 1864 |
std::ostringstream msg; |
| 1866 | 1865 |
msg << "Column not found (" << i + 1 << ")";
|
| 1867 | 1866 |
throw FormatError(msg.str()); |
| 1868 | 1867 |
} |
| 1869 | 1868 |
} |
| 1870 | 1869 |
if (line >> std::ws >> c) |
| 1871 | 1870 |
throw FormatError("Extra character at the end of line");
|
| 1872 | 1871 |
|
| 1873 | 1872 |
Edge e; |
| 1874 | 1873 |
if (!_use_edges) {
|
| 1875 | 1874 |
|
| 1876 | 1875 |
typename NodeIndex::iterator it; |
| 1877 | 1876 |
|
| 1878 | 1877 |
it = _node_index.find(source_token); |
| 1879 | 1878 |
if (it == _node_index.end()) {
|
| 1880 | 1879 |
std::ostringstream msg; |
| 1881 | 1880 |
msg << "Item not found: " << source_token; |
| 1882 | 1881 |
throw FormatError(msg.str()); |
| 1883 | 1882 |
} |
| 1884 | 1883 |
Node source = it->second; |
| 1885 | 1884 |
|
| 1886 | 1885 |
it = _node_index.find(target_token); |
| 1887 | 1886 |
if (it == _node_index.end()) {
|
| 1888 | 1887 |
std::ostringstream msg; |
| 1889 | 1888 |
msg << "Item not found: " << target_token; |
| 1890 | 1889 |
throw FormatError(msg.str()); |
| 1891 | 1890 |
} |
| 1892 | 1891 |
Node target = it->second; |
| 1893 | 1892 |
|
| 1894 | 1893 |
e = _graph.addEdge(source, target); |
| 1895 | 1894 |
if (label_index != -1) |
| 1896 | 1895 |
_edge_index.insert(std::make_pair(tokens[label_index], e)); |
| 1897 | 1896 |
} else {
|
| 1898 | 1897 |
if (label_index == -1) |
| 1899 | 1898 |
throw FormatError("Label map not found");
|
| 1900 | 1899 |
typename std::map<std::string, Edge>::iterator it = |
| 1901 | 1900 |
_edge_index.find(tokens[label_index]); |
| 1902 | 1901 |
if (it == _edge_index.end()) {
|
| 1903 | 1902 |
std::ostringstream msg; |
| 1904 | 1903 |
msg << "Edge with label not found: " << tokens[label_index]; |
| 1905 | 1904 |
throw FormatError(msg.str()); |
| 1906 | 1905 |
} |
| 1907 | 1906 |
e = it->second; |
| 1908 | 1907 |
} |
| 1909 | 1908 |
|
| 1910 | 1909 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
| 1911 | 1910 |
_edge_maps[i].second->set(e, tokens[map_index[i]]); |
| 1912 | 1911 |
} |
| 1913 | 1912 |
|
| 1914 | 1913 |
} |
| 1915 | 1914 |
if (readSuccess()) {
|
| 1916 | 1915 |
line.putback(c); |
| 1917 | 1916 |
} |
| 1918 | 1917 |
} |
| 1919 | 1918 |
|
| 1920 | 1919 |
void readAttributes() {
|
| 1921 | 1920 |
|
| 1922 | 1921 |
std::set<std::string> read_attr; |
| 1923 | 1922 |
|
| 1924 | 1923 |
char c; |
| 1925 | 1924 |
while (readLine() && line >> c && c != '@') {
|
| 1926 | 1925 |
line.putback(c); |
| 1927 | 1926 |
|
| 1928 | 1927 |
std::string attr, token; |
| 1929 | 1928 |
if (!_reader_bits::readToken(line, attr)) |
| 1930 | 1929 |
throw FormatError("Attribute name not found");
|
| 1931 | 1930 |
if (!_reader_bits::readToken(line, token)) |
| 1932 | 1931 |
throw FormatError("Attribute value not found");
|
| 1933 | 1932 |
if (line >> c) |
| 1934 | 1933 |
throw FormatError("Extra character at the end of line");
|
| 1935 | 1934 |
|
| 1936 | 1935 |
{
|
| 1937 | 1936 |
std::set<std::string>::iterator it = read_attr.find(attr); |
| 1938 | 1937 |
if (it != read_attr.end()) {
|
| 1939 | 1938 |
std::ostringstream msg; |
| 1940 | 1939 |
msg << "Multiple occurence of attribute: " << attr; |
| 1941 | 1940 |
throw FormatError(msg.str()); |
| 1942 | 1941 |
} |
| 1943 | 1942 |
read_attr.insert(attr); |
| 1944 | 1943 |
} |
| 1945 | 1944 |
|
| 1946 | 1945 |
{
|
| 1947 | 1946 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1948 | 1947 |
while (it != _attributes.end() && it->first == attr) {
|
| 1949 | 1948 |
it->second->set(token); |
| 1950 | 1949 |
++it; |
| 1951 | 1950 |
} |
| 1952 | 1951 |
} |
| 1953 | 1952 |
|
| 1954 | 1953 |
} |
| 1955 | 1954 |
if (readSuccess()) {
|
| 1956 | 1955 |
line.putback(c); |
| 1957 | 1956 |
} |
| 1958 | 1957 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1959 | 1958 |
it != _attributes.end(); ++it) {
|
| 1960 | 1959 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1961 | 1960 |
std::ostringstream msg; |
| 1962 | 1961 |
msg << "Attribute not found: " << it->first; |
| 1963 | 1962 |
throw FormatError(msg.str()); |
| 1964 | 1963 |
} |
| 1965 | 1964 |
} |
| 1966 | 1965 |
} |
| 1967 | 1966 |
|
| 1968 | 1967 |
public: |
| 1969 | 1968 |
|
| 1970 | 1969 |
/// \name Execution of the reader |
| 1971 | 1970 |
/// @{
|
| 1972 | 1971 |
|
| 1973 | 1972 |
/// \brief Start the batch processing |
| 1974 | 1973 |
/// |
| 1975 | 1974 |
/// This function starts the batch processing |
| 1976 | 1975 |
void run() {
|
| 1977 | 1976 |
|
| 1978 | 1977 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1979 | 1978 |
|
| 1980 | 1979 |
bool nodes_done = _skip_nodes; |
| 1981 | 1980 |
bool edges_done = _skip_edges; |
| 1982 | 1981 |
bool attributes_done = false; |
| 1983 | 1982 |
|
| 1984 | 1983 |
line_num = 0; |
| 1985 | 1984 |
readLine(); |
| 1986 | 1985 |
skipSection(); |
| 1987 | 1986 |
|
| 1988 | 1987 |
while (readSuccess()) {
|
| 1989 | 1988 |
try {
|
| 1990 | 1989 |
char c; |
| 1991 | 1990 |
std::string section, caption; |
| 1992 | 1991 |
line >> c; |
| 1993 | 1992 |
_reader_bits::readToken(line, section); |
| 1994 | 1993 |
_reader_bits::readToken(line, caption); |
| 1995 | 1994 |
|
| 1996 | 1995 |
if (line >> c) |
| 1997 | 1996 |
throw FormatError("Extra character at the end of line");
|
| 1998 | 1997 |
|
| 1999 | 1998 |
if (section == "nodes" && !nodes_done) {
|
| 2000 | 1999 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 2001 | 2000 |
readNodes(); |
| 2002 | 2001 |
nodes_done = true; |
| 2003 | 2002 |
} |
| 2004 | 2003 |
} else if ((section == "edges" || section == "arcs") && |
| 2005 | 2004 |
!edges_done) {
|
| 2006 | 2005 |
if (_edges_caption.empty() || _edges_caption == caption) {
|
| 2007 | 2006 |
readEdges(); |
| 2008 | 2007 |
edges_done = true; |
| 2009 | 2008 |
} |
| 2010 | 2009 |
} else if (section == "attributes" && !attributes_done) {
|
| 2011 | 2010 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 2012 | 2011 |
readAttributes(); |
| 2013 | 2012 |
attributes_done = true; |
| 2014 | 2013 |
} |
| 2015 | 2014 |
} else {
|
| 2016 | 2015 |
readLine(); |
| 2017 | 2016 |
skipSection(); |
| 2018 | 2017 |
} |
| 2019 | 2018 |
} catch (FormatError& error) {
|
| 2020 | 2019 |
error.line(line_num); |
| 2021 | 2020 |
error.file(_filename); |
| 2022 | 2021 |
throw; |
| 2023 | 2022 |
} |
| 2024 | 2023 |
} |
| 2025 | 2024 |
|
| 2026 | 2025 |
if (!nodes_done) {
|
| 2027 | 2026 |
throw FormatError("Section @nodes not found");
|
| 2028 | 2027 |
} |
| 2029 | 2028 |
|
| 2030 | 2029 |
if (!edges_done) {
|
| 2031 | 2030 |
throw FormatError("Section @edges not found");
|
| 2032 | 2031 |
} |
| 2033 | 2032 |
|
| 2034 | 2033 |
if (!attributes_done && !_attributes.empty()) {
|
| 2035 | 2034 |
throw FormatError("Section @attributes not found");
|
| 2036 | 2035 |
} |
| 2037 | 2036 |
|
| 2038 | 2037 |
} |
| 2039 | 2038 |
|
| 2040 | 2039 |
/// @} |
| 2041 | 2040 |
|
| 2042 | 2041 |
}; |
| 2043 | 2042 |
|
| 2044 | 2043 |
class SectionReader; |
| 2045 | 2044 |
|
| 2046 | 2045 |
SectionReader sectionReader(std::istream& is); |
| 2047 | 2046 |
SectionReader sectionReader(const std::string& fn); |
| 2048 | 2047 |
SectionReader sectionReader(const char* fn); |
| 2049 | 2048 |
|
| 2050 | 2049 |
/// \ingroup lemon_io |
| 2051 | 2050 |
/// |
| 2052 | 2051 |
/// \brief Section reader class |
| 2053 | 2052 |
/// |
| 2054 | 2053 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
| 2055 | 2054 |
/// which contain any data in arbitrary format. Such sections can be |
| 2056 | 2055 |
/// read with this class. A reading rule can be added to the class |
| 2057 | 2056 |
/// with two different functions. With the \c sectionLines() function a |
| 2058 | 2057 |
/// functor can process the section line-by-line, while with the \c |
| 2059 | 2058 |
/// sectionStream() member the section can be read from an input |
| 2060 | 2059 |
/// stream. |
| 2061 | 2060 |
class SectionReader {
|
| 2062 | 2061 |
private: |
| 2063 | 2062 |
|
| 2064 | 2063 |
std::istream* _is; |
| 2065 | 2064 |
bool local_is; |
| 2066 | 2065 |
std::string _filename; |
| 2067 | 2066 |
|
| 2068 | 2067 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
| 2069 | 2068 |
Sections _sections; |
| 2070 | 2069 |
|
| 2071 | 2070 |
int line_num; |
| 2072 | 2071 |
std::istringstream line; |
| 2073 | 2072 |
|
| 2074 | 2073 |
public: |
| 2075 | 2074 |
|
| 2076 | 2075 |
/// \brief Constructor |
| 2077 | 2076 |
/// |
| 2078 | 2077 |
/// Construct a section reader, which reads from the given input |
| 2079 | 2078 |
/// stream. |
| 2080 | 2079 |
SectionReader(std::istream& is) |
| 2081 | 2080 |
: _is(&is), local_is(false) {}
|
| 2082 | 2081 |
|
| 2083 | 2082 |
/// \brief Constructor |
| 2084 | 2083 |
/// |
| 2085 | 2084 |
/// Construct a section reader, which reads from the given file. |
| 2086 | 2085 |
SectionReader(const std::string& fn) |
| 2087 | 2086 |
: _is(new std::ifstream(fn.c_str())), local_is(true), |
| 2088 | 2087 |
_filename(fn) {
|
| 2089 | 2088 |
if (!(*_is)) {
|
| 2090 | 2089 |
delete _is; |
| 2091 | 2090 |
throw IoError("Cannot open file", fn);
|
| 2092 | 2091 |
} |
| 2093 | 2092 |
} |
| 2094 | 2093 |
|
| 2095 | 2094 |
/// \brief Constructor |
| 2096 | 2095 |
/// |
| 2097 | 2096 |
/// Construct a section reader, which reads from the given file. |
| 2098 | 2097 |
SectionReader(const char* fn) |
| 2099 | 2098 |
: _is(new std::ifstream(fn)), local_is(true), |
| 2100 | 2099 |
_filename(fn) {
|
| 2101 | 2100 |
if (!(*_is)) {
|
| 2102 | 2101 |
delete _is; |
| 2103 | 2102 |
throw IoError("Cannot open file", fn);
|
| 2104 | 2103 |
} |
| 2105 | 2104 |
} |
| 2106 | 2105 |
|
| 2107 | 2106 |
/// \brief Destructor |
| 2108 | 2107 |
~SectionReader() {
|
| 2109 | 2108 |
for (Sections::iterator it = _sections.begin(); |
| 2110 | 2109 |
it != _sections.end(); ++it) {
|
| 2111 | 2110 |
delete it->second; |
| 2112 | 2111 |
} |
| 2113 | 2112 |
|
| 2114 | 2113 |
if (local_is) {
|
| 2115 | 2114 |
delete _is; |
| 2116 | 2115 |
} |
| 2117 | 2116 |
|
| 2118 | 2117 |
} |
| 2119 | 2118 |
|
| 2120 | 2119 |
private: |
| 2121 | 2120 |
|
| 2122 | 2121 |
friend SectionReader sectionReader(std::istream& is); |
| 2123 | 2122 |
friend SectionReader sectionReader(const std::string& fn); |
| 2124 | 2123 |
friend SectionReader sectionReader(const char* fn); |
| 2125 | 2124 |
|
| 2126 | 2125 |
SectionReader(SectionReader& other) |
| 2127 | 2126 |
: _is(other._is), local_is(other.local_is) {
|
| 2128 | 2127 |
|
| 2129 | 2128 |
other._is = 0; |
| 2130 | 2129 |
other.local_is = false; |
| 2131 | 2130 |
|
| 2132 | 2131 |
_sections.swap(other._sections); |
| 2133 | 2132 |
} |
| 2134 | 2133 |
|
| 2135 | 2134 |
SectionReader& operator=(const SectionReader&); |
| 2136 | 2135 |
|
| 2137 | 2136 |
public: |
| 2138 | 2137 |
|
| 2139 | 2138 |
/// \name Section readers |
| 2140 | 2139 |
/// @{
|
| 2141 | 2140 |
|
| 2142 | 2141 |
/// \brief Add a section processor with line oriented reading |
| 2143 | 2142 |
/// |
| 2144 | 2143 |
/// The first parameter is the type descriptor of the section, the |
| 2145 | 2144 |
/// second is a functor, which takes just one \c std::string |
| 2146 | 2145 |
/// parameter. At the reading process, each line of the section |
| 2147 | 2146 |
/// will be given to the functor object. However, the empty lines |
| 2148 | 2147 |
/// and the comment lines are filtered out, and the leading |
| 2149 | 2148 |
/// whitespaces are trimmed from each processed string. |
| 2150 | 2149 |
/// |
| 2151 | 2150 |
/// For example let's see a section, which contain several |
| 2152 | 2151 |
/// integers, which should be inserted into a vector. |
| 2153 | 2152 |
///\code |
| 2154 | 2153 |
/// @numbers |
| 2155 | 2154 |
/// 12 45 23 |
| 2156 | 2155 |
/// 4 |
| 2157 | 2156 |
/// 23 6 |
| 2158 | 2157 |
///\endcode |
| 2159 | 2158 |
/// |
| 2160 | 2159 |
/// The functor is implemented as a struct: |
| 2161 | 2160 |
///\code |
| 2162 | 2161 |
/// struct NumberSection {
|
| 2163 | 2162 |
/// std::vector<int>& _data; |
| 2164 | 2163 |
/// NumberSection(std::vector<int>& data) : _data(data) {}
|
| 2165 | 2164 |
/// void operator()(const std::string& line) {
|
| 2166 | 2165 |
/// std::istringstream ls(line); |
| 2167 | 2166 |
/// int value; |
| 2168 | 2167 |
/// while (ls >> value) _data.push_back(value); |
| 2169 | 2168 |
/// } |
| 2170 | 2169 |
/// }; |
| 2171 | 2170 |
/// |
| 2172 | 2171 |
/// // ... |
| 2173 | 2172 |
/// |
| 2174 | 2173 |
/// reader.sectionLines("numbers", NumberSection(vec));
|
| 2175 | 2174 |
///\endcode |
| 2176 | 2175 |
template <typename Functor> |
| 2177 | 2176 |
SectionReader& sectionLines(const std::string& type, Functor functor) {
|
| 2178 | 2177 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 2179 | 2178 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
| 2180 | 2179 |
"Multiple reading of section."); |
| 2181 | 2180 |
_sections.insert(std::make_pair(type, |
| 2182 | 2181 |
new _reader_bits::LineSection<Functor>(functor))); |
| 2183 | 2182 |
return *this; |
| 2184 | 2183 |
} |
| 2185 | 2184 |
|
| 2186 | 2185 |
|
| 2187 | 2186 |
/// \brief Add a section processor with stream oriented reading |
| 2188 | 2187 |
/// |
| 2189 | 2188 |
/// The first parameter is the type of the section, the second is |
| 2190 | 2189 |
/// a functor, which takes an \c std::istream& and an \c int& |
| 2191 | 2190 |
/// parameter, the latter regard to the line number of stream. The |
| 2192 | 2191 |
/// functor can read the input while the section go on, and the |
| 2193 | 2192 |
/// line number should be modified accordingly. |
| 2194 | 2193 |
template <typename Functor> |
| 2195 | 2194 |
SectionReader& sectionStream(const std::string& type, Functor functor) {
|
| 2196 | 2195 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 2197 | 2196 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
| 2198 | 2197 |
"Multiple reading of section."); |
| 2199 | 2198 |
_sections.insert(std::make_pair(type, |
| 2200 | 2199 |
new _reader_bits::StreamSection<Functor>(functor))); |
| 2201 | 2200 |
return *this; |
| 2202 | 2201 |
} |
| 2203 | 2202 |
|
| 2204 | 2203 |
/// @} |
| 2205 | 2204 |
|
| 2206 | 2205 |
private: |
| 2207 | 2206 |
|
| 2208 | 2207 |
bool readLine() {
|
| 2209 | 2208 |
std::string str; |
| 2210 | 2209 |
while(++line_num, std::getline(*_is, str)) {
|
| 2211 | 2210 |
line.clear(); line.str(str); |
| 2212 | 2211 |
char c; |
| 2213 | 2212 |
if (line >> std::ws >> c && c != '#') {
|
| 2214 | 2213 |
line.putback(c); |
| 2215 | 2214 |
return true; |
| 2216 | 2215 |
} |
| 2217 | 2216 |
} |
| 2218 | 2217 |
return false; |
| 2219 | 2218 |
} |
| 2220 | 2219 |
|
| 2221 | 2220 |
bool readSuccess() {
|
| 2222 | 2221 |
return static_cast<bool>(*_is); |
| 2223 | 2222 |
} |
| 2224 | 2223 |
|
| 2225 | 2224 |
void skipSection() {
|
| 2226 | 2225 |
char c; |
| 2227 | 2226 |
while (readSuccess() && line >> c && c != '@') {
|
| 2228 | 2227 |
readLine(); |
| 2229 | 2228 |
} |
| 2230 | 2229 |
line.putback(c); |
| 2231 | 2230 |
} |
| 2232 | 2231 |
|
| 2233 | 2232 |
public: |
| 2234 | 2233 |
|
| 2235 | 2234 |
|
| 2236 | 2235 |
/// \name Execution of the reader |
| 2237 | 2236 |
/// @{
|
| 2238 | 2237 |
|
| 2239 | 2238 |
/// \brief Start the batch processing |
| 2240 | 2239 |
/// |
| 2241 | 2240 |
/// This function starts the batch processing. |
| 2242 | 2241 |
void run() {
|
| 2243 | 2242 |
|
| 2244 | 2243 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 2245 | 2244 |
|
| 2246 | 2245 |
std::set<std::string> extra_sections; |
| 2247 | 2246 |
|
| 2248 | 2247 |
line_num = 0; |
| 2249 | 2248 |
readLine(); |
| 2250 | 2249 |
skipSection(); |
| 2251 | 2250 |
|
| 2252 | 2251 |
while (readSuccess()) {
|
| 2253 | 2252 |
try {
|
| 2254 | 2253 |
char c; |
| 2255 | 2254 |
std::string section, caption; |
| 2256 | 2255 |
line >> c; |
| 2257 | 2256 |
_reader_bits::readToken(line, section); |
| 2258 | 2257 |
_reader_bits::readToken(line, caption); |
| 2259 | 2258 |
|
| 2260 | 2259 |
if (line >> c) |
| 2261 | 2260 |
throw FormatError("Extra character at the end of line");
|
| 2262 | 2261 |
|
| 2263 | 2262 |
if (extra_sections.find(section) != extra_sections.end()) {
|
| 2264 | 2263 |
std::ostringstream msg; |
| 2265 | 2264 |
msg << "Multiple occurence of section: " << section; |
| 2266 | 2265 |
throw FormatError(msg.str()); |
| 2267 | 2266 |
} |
| 2268 | 2267 |
Sections::iterator it = _sections.find(section); |
| 2269 | 2268 |
if (it != _sections.end()) {
|
| 2270 | 2269 |
extra_sections.insert(section); |
| 2271 | 2270 |
it->second->process(*_is, line_num); |
| 2272 | 2271 |
} |
| 2273 | 2272 |
readLine(); |
| 2274 | 2273 |
skipSection(); |
| 2275 | 2274 |
} catch (FormatError& error) {
|
| 2276 | 2275 |
error.line(line_num); |
| 2277 | 2276 |
error.file(_filename); |
| 2278 | 2277 |
throw; |
| 2279 | 2278 |
} |
| 2280 | 2279 |
} |
| 2281 | 2280 |
for (Sections::iterator it = _sections.begin(); |
| 2282 | 2281 |
it != _sections.end(); ++it) {
|
| 2283 | 2282 |
if (extra_sections.find(it->first) == extra_sections.end()) {
|
| 2284 | 2283 |
std::ostringstream os; |
| 2285 | 2284 |
os << "Cannot find section: " << it->first; |
| 2286 | 2285 |
throw FormatError(os.str()); |
| 2287 | 2286 |
} |
| 2288 | 2287 |
} |
| 2289 | 2288 |
} |
| 2290 | 2289 |
|
| 2291 | 2290 |
/// @} |
| 2292 | 2291 |
|
| 2293 | 2292 |
}; |
| 2294 | 2293 |
|
| 2295 | 2294 |
/// \brief Return a \ref SectionReader class |
| 2296 | 2295 |
/// |
| 2297 | 2296 |
/// This function just returns a \ref SectionReader class. |
| 2298 | 2297 |
/// \relates SectionReader |
| 2299 | 2298 |
inline SectionReader sectionReader(std::istream& is) {
|
| 2300 | 2299 |
SectionReader tmp(is); |
| 2301 | 2300 |
return tmp; |
| 2302 | 2301 |
} |
| 2303 | 2302 |
|
| 2304 | 2303 |
/// \brief Return a \ref SectionReader class |
| 2305 | 2304 |
/// |
| 2306 | 2305 |
/// This function just returns a \ref SectionReader class. |
| 2307 | 2306 |
/// \relates SectionReader |
| 2308 | 2307 |
inline SectionReader sectionReader(const std::string& fn) {
|
| 2309 | 2308 |
SectionReader tmp(fn); |
| 2310 | 2309 |
return tmp; |
| 2311 | 2310 |
} |
| 2312 | 2311 |
|
| 2313 | 2312 |
/// \brief Return a \ref SectionReader class |
| 2314 | 2313 |
/// |
| 2315 | 2314 |
/// This function just returns a \ref SectionReader class. |
| 2316 | 2315 |
/// \relates SectionReader |
| 2317 | 2316 |
inline SectionReader sectionReader(const char* fn) {
|
| 2318 | 2317 |
SectionReader tmp(fn); |
| 2319 | 2318 |
return tmp; |
| 2320 | 2319 |
} |
| 2321 | 2320 |
|
| 2322 | 2321 |
/// \ingroup lemon_io |
| 2323 | 2322 |
/// |
| 2324 | 2323 |
/// \brief Reader for the contents of the \ref lgf-format "LGF" file |
| 2325 | 2324 |
/// |
| 2326 | 2325 |
/// This class can be used to read the sections, the map names and |
| 2327 | 2326 |
/// the attributes from a file. Usually, the LEMON programs know |
| 2328 | 2327 |
/// that, which type of graph, which maps and which attributes |
| 2329 | 2328 |
/// should be read from a file, but in general tools (like glemon) |
| 2330 | 2329 |
/// the contents of an LGF file should be guessed somehow. This class |
| 2331 | 2330 |
/// reads the graph and stores the appropriate information for |
| 2332 | 2331 |
/// reading the graph. |
| 2333 | 2332 |
/// |
| 2334 | 2333 |
///\code |
| 2335 | 2334 |
/// LgfContents contents("graph.lgf");
|
| 2336 | 2335 |
/// contents.run(); |
| 2337 | 2336 |
/// |
| 2338 | 2337 |
/// // Does it contain any node section and arc section? |
| 2339 | 2338 |
/// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
|
| 2340 | 2339 |
/// std::cerr << "Failure, cannot find graph." << std::endl; |
| 2341 | 2340 |
/// return -1; |
| 2342 | 2341 |
/// } |
| 2343 | 2342 |
/// std::cout << "The name of the default node section: " |
| 2344 | 2343 |
/// << contents.nodeSection(0) << std::endl; |
| 2345 | 2344 |
/// std::cout << "The number of the arc maps: " |
| 2346 | 2345 |
/// << contents.arcMaps(0).size() << std::endl; |
| 2347 | 2346 |
/// std::cout << "The name of second arc map: " |
| 2348 | 2347 |
/// << contents.arcMaps(0)[1] << std::endl; |
| 2349 | 2348 |
///\endcode |
| 2350 | 2349 |
class LgfContents {
|
| 2351 | 2350 |
private: |
| 2352 | 2351 |
|
| 2353 | 2352 |
std::istream* _is; |
| 2354 | 2353 |
bool local_is; |
| 2355 | 2354 |
|
| 2356 | 2355 |
std::vector<std::string> _node_sections; |
| 2357 | 2356 |
std::vector<std::string> _edge_sections; |
| 2358 | 2357 |
std::vector<std::string> _attribute_sections; |
| 2359 | 2358 |
std::vector<std::string> _extra_sections; |
| 2360 | 2359 |
|
| 2361 | 2360 |
std::vector<bool> _arc_sections; |
| 2362 | 2361 |
|
| 2363 | 2362 |
std::vector<std::vector<std::string> > _node_maps; |
| 2364 | 2363 |
std::vector<std::vector<std::string> > _edge_maps; |
| 2365 | 2364 |
|
| 2366 | 2365 |
std::vector<std::vector<std::string> > _attributes; |
| 2367 | 2366 |
|
| 2368 | 2367 |
|
| 2369 | 2368 |
int line_num; |
| 2370 | 2369 |
std::istringstream line; |
| 2371 | 2370 |
|
| 2372 | 2371 |
public: |
| 2373 | 2372 |
|
| 2374 | 2373 |
/// \brief Constructor |
| 2375 | 2374 |
/// |
| 2376 | 2375 |
/// Construct an \e LGF contents reader, which reads from the given |
| 2377 | 2376 |
/// input stream. |
| 2378 | 2377 |
LgfContents(std::istream& is) |
| 2379 | 2378 |
: _is(&is), local_is(false) {}
|
| 2380 | 2379 |
|
| 2381 | 2380 |
/// \brief Constructor |
| 2382 | 2381 |
/// |
| 2383 | 2382 |
/// Construct an \e LGF contents reader, which reads from the given |
| 2384 | 2383 |
/// file. |
| 2385 | 2384 |
LgfContents(const std::string& fn) |
| 2386 | 2385 |
: _is(new std::ifstream(fn.c_str())), local_is(true) {
|
| 2387 | 2386 |
if (!(*_is)) {
|
| 2388 | 2387 |
delete _is; |
| 2389 | 2388 |
throw IoError("Cannot open file", fn);
|
| 2390 | 2389 |
} |
| 2391 | 2390 |
} |
| 2392 | 2391 |
|
| 2393 | 2392 |
/// \brief Constructor |
| 2394 | 2393 |
/// |
| 2395 | 2394 |
/// Construct an \e LGF contents reader, which reads from the given |
| 2396 | 2395 |
/// file. |
| 2397 | 2396 |
LgfContents(const char* fn) |
| 2398 | 2397 |
: _is(new std::ifstream(fn)), local_is(true) {
|
| 2399 | 2398 |
if (!(*_is)) {
|
| 2400 | 2399 |
delete _is; |
| 2401 | 2400 |
throw IoError("Cannot open file", fn);
|
| 2402 | 2401 |
} |
| 2403 | 2402 |
} |
| 2404 | 2403 |
|
| 2405 | 2404 |
/// \brief Destructor |
| 2406 | 2405 |
~LgfContents() {
|
| 2407 | 2406 |
if (local_is) delete _is; |
| 2408 | 2407 |
} |
| 2409 | 2408 |
|
| 2410 | 2409 |
private: |
| 2411 | 2410 |
|
| 2412 | 2411 |
LgfContents(const LgfContents&); |
| 2413 | 2412 |
LgfContents& operator=(const LgfContents&); |
| 2414 | 2413 |
|
| 2415 | 2414 |
public: |
| 2416 | 2415 |
|
| 2417 | 2416 |
|
| 2418 | 2417 |
/// \name Node sections |
| 2419 | 2418 |
/// @{
|
| 2420 | 2419 |
|
| 2421 | 2420 |
/// \brief Gives back the number of node sections in the file. |
| 2422 | 2421 |
/// |
| 2423 | 2422 |
/// Gives back the number of node sections in the file. |
| 2424 | 2423 |
int nodeSectionNum() const {
|
| 2425 | 2424 |
return _node_sections.size(); |
| 2426 | 2425 |
} |
| 2427 | 2426 |
|
| 2428 | 2427 |
/// \brief Returns the node section name at the given position. |
| 2429 | 2428 |
/// |
| 2430 | 2429 |
/// Returns the node section name at the given position. |
| 2431 | 2430 |
const std::string& nodeSection(int i) const {
|
| 2432 | 2431 |
return _node_sections[i]; |
| 2433 | 2432 |
} |
| 2434 | 2433 |
|
| 2435 | 2434 |
/// \brief Gives back the node maps for the given section. |
| 2436 | 2435 |
/// |
| 2437 | 2436 |
/// Gives back the node maps for the given section. |
| 2438 | 2437 |
const std::vector<std::string>& nodeMapNames(int i) const {
|
| 2439 | 2438 |
return _node_maps[i]; |
| 2440 | 2439 |
} |
| 2441 | 2440 |
|
| 2442 | 2441 |
/// @} |
| 2443 | 2442 |
|
| 2444 | 2443 |
/// \name Arc/Edge sections |
| 2445 | 2444 |
/// @{
|
| 2446 | 2445 |
|
| 2447 | 2446 |
/// \brief Gives back the number of arc/edge sections in the file. |
| 2448 | 2447 |
/// |
| 2449 | 2448 |
/// Gives back the number of arc/edge sections in the file. |
| 2450 | 2449 |
/// \note It is synonym of \c edgeSectionNum(). |
| 2451 | 2450 |
int arcSectionNum() const {
|
| 2452 | 2451 |
return _edge_sections.size(); |
| 2453 | 2452 |
} |
| 2454 | 2453 |
|
| 2455 | 2454 |
/// \brief Returns the arc/edge section name at the given position. |
| 2456 | 2455 |
/// |
| 2457 | 2456 |
/// Returns the arc/edge section name at the given position. |
| 2458 | 2457 |
/// \note It is synonym of \c edgeSection(). |
| 2459 | 2458 |
const std::string& arcSection(int i) const {
|
| 2460 | 2459 |
return _edge_sections[i]; |
| 2461 | 2460 |
} |
| 2462 | 2461 |
|
| 2463 | 2462 |
/// \brief Gives back the arc/edge maps for the given section. |
| 2464 | 2463 |
/// |
| 2465 | 2464 |
/// Gives back the arc/edge maps for the given section. |
| 2466 | 2465 |
/// \note It is synonym of \c edgeMapNames(). |
| 2467 | 2466 |
const std::vector<std::string>& arcMapNames(int i) const {
|
| 2468 | 2467 |
return _edge_maps[i]; |
| 2469 | 2468 |
} |
| 2470 | 2469 |
|
| 2471 | 2470 |
/// @} |
| 2472 | 2471 |
|
| 2473 | 2472 |
/// \name Synonyms |
| 2474 | 2473 |
/// @{
|
| 2475 | 2474 |
|
| 2476 | 2475 |
/// \brief Gives back the number of arc/edge sections in the file. |
| 2477 | 2476 |
/// |
| 2478 | 2477 |
/// Gives back the number of arc/edge sections in the file. |
| 2479 | 2478 |
/// \note It is synonym of \c arcSectionNum(). |
| 2480 | 2479 |
int edgeSectionNum() const {
|
| 2481 | 2480 |
return _edge_sections.size(); |
| 2482 | 2481 |
} |
| 2483 | 2482 |
|
| 2484 | 2483 |
/// \brief Returns the section name at the given position. |
| 2485 | 2484 |
/// |
| 2486 | 2485 |
/// Returns the section name at the given position. |
| 2487 | 2486 |
/// \note It is synonym of \c arcSection(). |
| 2488 | 2487 |
const std::string& edgeSection(int i) const {
|
| 2489 | 2488 |
return _edge_sections[i]; |
| 2490 | 2489 |
} |
| 2491 | 2490 |
|
| 2492 | 2491 |
/// \brief Gives back the edge maps for the given section. |
| 2493 | 2492 |
/// |
| 2494 | 2493 |
/// Gives back the edge maps for the given section. |
| 2495 | 2494 |
/// \note It is synonym of \c arcMapNames(). |
| 2496 | 2495 |
const std::vector<std::string>& edgeMapNames(int i) const {
|
| 2497 | 2496 |
return _edge_maps[i]; |
| 2498 | 2497 |
} |
| 2499 | 2498 |
|
| 2500 | 2499 |
/// @} |
| 2501 | 2500 |
|
| 2502 | 2501 |
/// \name Attribute sections |
| 2503 | 2502 |
/// @{
|
| 2504 | 2503 |
|
| 2505 | 2504 |
/// \brief Gives back the number of attribute sections in the file. |
| 2506 | 2505 |
/// |
| 2507 | 2506 |
/// Gives back the number of attribute sections in the file. |
| 2508 | 2507 |
int attributeSectionNum() const {
|
| 2509 | 2508 |
return _attribute_sections.size(); |
| 2510 | 2509 |
} |
| 2511 | 2510 |
|
| 2512 | 2511 |
/// \brief Returns the attribute section name at the given position. |
| 2513 | 2512 |
/// |
| 2514 | 2513 |
/// Returns the attribute section name at the given position. |
| 2515 | 2514 |
const std::string& attributeSectionNames(int i) const {
|
| 2516 | 2515 |
return _attribute_sections[i]; |
| 2517 | 2516 |
} |
| 2518 | 2517 |
|
| 2519 | 2518 |
/// \brief Gives back the attributes for the given section. |
| 2520 | 2519 |
/// |
| 2521 | 2520 |
/// Gives back the attributes for the given section. |
| 2522 | 2521 |
const std::vector<std::string>& attributes(int i) const {
|
| 2523 | 2522 |
return _attributes[i]; |
| 2524 | 2523 |
} |
| 2525 | 2524 |
|
| 2526 | 2525 |
/// @} |
| 2527 | 2526 |
|
| 2528 | 2527 |
/// \name Extra sections |
| 2529 | 2528 |
/// @{
|
| 2530 | 2529 |
|
| 2531 | 2530 |
/// \brief Gives back the number of extra sections in the file. |
| 2532 | 2531 |
/// |
| 2533 | 2532 |
/// Gives back the number of extra sections in the file. |
| 2534 | 2533 |
int extraSectionNum() const {
|
| 2535 | 2534 |
return _extra_sections.size(); |
| 2536 | 2535 |
} |
| 2537 | 2536 |
|
| 2538 | 2537 |
/// \brief Returns the extra section type at the given position. |
| 2539 | 2538 |
/// |
| 2540 | 2539 |
/// Returns the section type at the given position. |
| 2541 | 2540 |
const std::string& extraSection(int i) const {
|
| 2542 | 2541 |
return _extra_sections[i]; |
| 2543 | 2542 |
} |
| 2544 | 2543 |
|
| 2545 | 2544 |
/// @} |
| 2546 | 2545 |
|
| 2547 | 2546 |
private: |
| 2548 | 2547 |
|
| 2549 | 2548 |
bool readLine() {
|
| 2550 | 2549 |
std::string str; |
| 2551 | 2550 |
while(++line_num, std::getline(*_is, str)) {
|
| 2552 | 2551 |
line.clear(); line.str(str); |
| 2553 | 2552 |
char c; |
| 2554 | 2553 |
if (line >> std::ws >> c && c != '#') {
|
| 2555 | 2554 |
line.putback(c); |
| 2556 | 2555 |
return true; |
| 2557 | 2556 |
} |
| 2558 | 2557 |
} |
| 2559 | 2558 |
return false; |
| 2560 | 2559 |
} |
| 2561 | 2560 |
|
| 2562 | 2561 |
bool readSuccess() {
|
| 2563 | 2562 |
return static_cast<bool>(*_is); |
| 2564 | 2563 |
} |
| 2565 | 2564 |
|
| 2566 | 2565 |
void skipSection() {
|
| 2567 | 2566 |
char c; |
| 2568 | 2567 |
while (readSuccess() && line >> c && c != '@') {
|
| 2569 | 2568 |
readLine(); |
| 2570 | 2569 |
} |
| 2571 | 2570 |
line.putback(c); |
| 2572 | 2571 |
} |
| 2573 | 2572 |
|
| 2574 | 2573 |
void readMaps(std::vector<std::string>& maps) {
|
| 2575 | 2574 |
char c; |
| 2576 | 2575 |
if (!readLine() || !(line >> c) || c == '@') {
|
| 2577 | 2576 |
if (readSuccess() && line) line.putback(c); |
| 2578 | 2577 |
return; |
| 2579 | 2578 |
} |
| 2580 | 2579 |
line.putback(c); |
| 2581 | 2580 |
std::string map; |
| 2582 | 2581 |
while (_reader_bits::readToken(line, map)) {
|
| 2583 | 2582 |
maps.push_back(map); |
| 2584 | 2583 |
} |
| 2585 | 2584 |
} |
| 2586 | 2585 |
|
| 2587 | 2586 |
void readAttributes(std::vector<std::string>& attrs) {
|
| 2588 | 2587 |
readLine(); |
| 2589 | 2588 |
char c; |
| 2590 | 2589 |
while (readSuccess() && line >> c && c != '@') {
|
| 2591 | 2590 |
line.putback(c); |
| 2592 | 2591 |
std::string attr; |
| 2593 | 2592 |
_reader_bits::readToken(line, attr); |
| 2594 | 2593 |
attrs.push_back(attr); |
| 2595 | 2594 |
readLine(); |
| 2596 | 2595 |
} |
| 2597 | 2596 |
line.putback(c); |
| 2598 | 2597 |
} |
| 2599 | 2598 |
|
| 2600 | 2599 |
public: |
| 2601 | 2600 |
|
| 2602 | 2601 |
/// \name Execution of the contents reader |
| 2603 | 2602 |
/// @{
|
| 2604 | 2603 |
|
| 2605 | 2604 |
/// \brief Starts the reading |
| 2606 | 2605 |
/// |
| 2607 | 2606 |
/// This function starts the reading. |
| 2608 | 2607 |
void run() {
|
| 2609 | 2608 |
|
| 2610 | 2609 |
readLine(); |
| 2611 | 2610 |
skipSection(); |
| 2612 | 2611 |
|
| 2613 | 2612 |
while (readSuccess()) {
|
| 2614 | 2613 |
|
| 2615 | 2614 |
char c; |
| 2616 | 2615 |
line >> c; |
| 2617 | 2616 |
|
| 2618 | 2617 |
std::string section, caption; |
| 2619 | 2618 |
_reader_bits::readToken(line, section); |
| 2620 | 2619 |
_reader_bits::readToken(line, caption); |
| 2621 | 2620 |
|
| 2622 | 2621 |
if (section == "nodes") {
|
| 2623 | 2622 |
_node_sections.push_back(caption); |
| 2624 | 2623 |
_node_maps.push_back(std::vector<std::string>()); |
| 2625 | 2624 |
readMaps(_node_maps.back()); |
| 2626 | 2625 |
readLine(); skipSection(); |
| 2627 | 2626 |
} else if (section == "arcs" || section == "edges") {
|
| 2628 | 2627 |
_edge_sections.push_back(caption); |
| 2629 | 2628 |
_arc_sections.push_back(section == "arcs"); |
| 2630 | 2629 |
_edge_maps.push_back(std::vector<std::string>()); |
| 2631 | 2630 |
readMaps(_edge_maps.back()); |
| 2632 | 2631 |
readLine(); skipSection(); |
| 2633 | 2632 |
} else if (section == "attributes") {
|
| 2634 | 2633 |
_attribute_sections.push_back(caption); |
| 2635 | 2634 |
_attributes.push_back(std::vector<std::string>()); |
| 2636 | 2635 |
readAttributes(_attributes.back()); |
| 2637 | 2636 |
} else {
|
| 2638 | 2637 |
_extra_sections.push_back(section); |
| 2639 | 2638 |
readLine(); skipSection(); |
| 2640 | 2639 |
} |
| 2641 | 2640 |
} |
| 2642 | 2641 |
} |
| 2643 | 2642 |
|
| 2644 | 2643 |
/// @} |
| 2645 | 2644 |
|
| 2646 | 2645 |
}; |
| 2647 | 2646 |
} |
| 2648 | 2647 |
|
| 2649 | 2648 |
#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-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 |
///\ingroup lemon_io |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" writer. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_LGF_WRITER_H |
| 25 | 25 |
#define LEMON_LGF_WRITER_H |
| 26 | 26 |
|
| 27 | 27 |
#include <iostream> |
| 28 | 28 |
#include <fstream> |
| 29 | 29 |
#include <sstream> |
| 30 | 30 |
|
| 31 | 31 |
#include <algorithm> |
| 32 | 32 |
|
| 33 | 33 |
#include <vector> |
| 34 | 34 |
#include <functional> |
| 35 | 35 |
|
| 36 |
#include <lemon/assert.h> |
|
| 37 | 36 |
#include <lemon/core.h> |
| 38 | 37 |
#include <lemon/maps.h> |
| 39 | 38 |
|
| 40 | 39 |
#include <lemon/concept_check.h> |
| 41 | 40 |
#include <lemon/concepts/maps.h> |
| 42 | 41 |
|
| 43 | 42 |
namespace lemon {
|
| 44 | 43 |
|
| 45 | 44 |
namespace _writer_bits {
|
| 46 | 45 |
|
| 47 | 46 |
template <typename Value> |
| 48 | 47 |
struct DefaultConverter {
|
| 49 | 48 |
std::string operator()(const Value& value) {
|
| 50 | 49 |
std::ostringstream os; |
| 51 | 50 |
os << value; |
| 52 | 51 |
return os.str(); |
| 53 | 52 |
} |
| 54 | 53 |
}; |
| 55 | 54 |
|
| 56 | 55 |
template <typename T> |
| 57 | 56 |
bool operator<(const T&, const T&) {
|
| 58 | 57 |
throw FormatError("Label map is not comparable");
|
| 59 | 58 |
} |
| 60 | 59 |
|
| 61 | 60 |
template <typename _Map> |
| 62 | 61 |
class MapLess {
|
| 63 | 62 |
public: |
| 64 | 63 |
typedef _Map Map; |
| 65 | 64 |
typedef typename Map::Key Item; |
| 66 | 65 |
|
| 67 | 66 |
private: |
| 68 | 67 |
const Map& _map; |
| 69 | 68 |
|
| 70 | 69 |
public: |
| 71 | 70 |
MapLess(const Map& map) : _map(map) {}
|
| 72 | 71 |
|
| 73 | 72 |
bool operator()(const Item& left, const Item& right) {
|
| 74 | 73 |
return _map[left] < _map[right]; |
| 75 | 74 |
} |
| 76 | 75 |
}; |
| 77 | 76 |
|
| 78 | 77 |
template <typename _Graph, bool _dir, typename _Map> |
| 79 | 78 |
class GraphArcMapLess {
|
| 80 | 79 |
public: |
| 81 | 80 |
typedef _Map Map; |
| 82 | 81 |
typedef _Graph Graph; |
| 83 | 82 |
typedef typename Graph::Edge Item; |
| 84 | 83 |
|
| 85 | 84 |
private: |
| 86 | 85 |
const Graph& _graph; |
| 87 | 86 |
const Map& _map; |
| 88 | 87 |
|
| 89 | 88 |
public: |
| 90 | 89 |
GraphArcMapLess(const Graph& graph, const Map& map) |
| 91 | 90 |
: _graph(graph), _map(map) {}
|
| 92 | 91 |
|
| 93 | 92 |
bool operator()(const Item& left, const Item& right) {
|
| 94 | 93 |
return _map[_graph.direct(left, _dir)] < |
| 95 | 94 |
_map[_graph.direct(right, _dir)]; |
| 96 | 95 |
} |
| 97 | 96 |
}; |
| 98 | 97 |
|
| 99 | 98 |
template <typename _Item> |
| 100 | 99 |
class MapStorageBase {
|
| 101 | 100 |
public: |
| 102 | 101 |
typedef _Item Item; |
| 103 | 102 |
|
| 104 | 103 |
public: |
| 105 | 104 |
MapStorageBase() {}
|
| 106 | 105 |
virtual ~MapStorageBase() {}
|
| 107 | 106 |
|
| 108 | 107 |
virtual std::string get(const Item& item) = 0; |
| 109 | 108 |
virtual void sort(std::vector<Item>&) = 0; |
| 110 | 109 |
}; |
| 111 | 110 |
|
| 112 | 111 |
template <typename _Item, typename _Map, |
| 113 | 112 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 114 | 113 |
class MapStorage : public MapStorageBase<_Item> {
|
| 115 | 114 |
public: |
| 116 | 115 |
typedef _Map Map; |
| 117 | 116 |
typedef _Converter Converter; |
| 118 | 117 |
typedef _Item Item; |
| 119 | 118 |
|
| 120 | 119 |
private: |
| 121 | 120 |
const Map& _map; |
| 122 | 121 |
Converter _converter; |
| 123 | 122 |
|
| 124 | 123 |
public: |
| 125 | 124 |
MapStorage(const Map& map, const Converter& converter = Converter()) |
| 126 | 125 |
: _map(map), _converter(converter) {}
|
| 127 | 126 |
virtual ~MapStorage() {}
|
| 128 | 127 |
|
| 129 | 128 |
virtual std::string get(const Item& item) {
|
| 130 | 129 |
return _converter(_map[item]); |
| 131 | 130 |
} |
| 132 | 131 |
virtual void sort(std::vector<Item>& items) {
|
| 133 | 132 |
MapLess<Map> less(_map); |
| 134 | 133 |
std::sort(items.begin(), items.end(), less); |
| 135 | 134 |
} |
| 136 | 135 |
}; |
| 137 | 136 |
|
| 138 | 137 |
template <typename _Graph, bool _dir, typename _Map, |
| 139 | 138 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
| 140 | 139 |
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
|
| 141 | 140 |
public: |
| 142 | 141 |
typedef _Map Map; |
| 143 | 142 |
typedef _Converter Converter; |
| 144 | 143 |
typedef _Graph Graph; |
| 145 | 144 |
typedef typename Graph::Edge Item; |
| 146 | 145 |
static const bool dir = _dir; |
| 147 | 146 |
|
| 148 | 147 |
private: |
| 149 | 148 |
const Graph& _graph; |
| 150 | 149 |
const Map& _map; |
| 151 | 150 |
Converter _converter; |
| 152 | 151 |
|
| 153 | 152 |
public: |
| 154 | 153 |
GraphArcMapStorage(const Graph& graph, const Map& map, |
| 155 | 154 |
const Converter& converter = Converter()) |
| 156 | 155 |
: _graph(graph), _map(map), _converter(converter) {}
|
| 157 | 156 |
virtual ~GraphArcMapStorage() {}
|
| 158 | 157 |
|
| 159 | 158 |
virtual std::string get(const Item& item) {
|
| 160 | 159 |
return _converter(_map[_graph.direct(item, dir)]); |
| 161 | 160 |
} |
| 162 | 161 |
virtual void sort(std::vector<Item>& items) {
|
| 163 | 162 |
GraphArcMapLess<Graph, dir, Map> less(_graph, _map); |
| 164 | 163 |
std::sort(items.begin(), items.end(), less); |
| 165 | 164 |
} |
| 166 | 165 |
}; |
| 167 | 166 |
|
| 168 | 167 |
class ValueStorageBase {
|
| 169 | 168 |
public: |
| 170 | 169 |
ValueStorageBase() {}
|
| 171 | 170 |
virtual ~ValueStorageBase() {}
|
| 172 | 171 |
|
| 173 | 172 |
virtual std::string get() = 0; |
| 174 | 173 |
}; |
| 175 | 174 |
|
| 176 | 175 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
| 177 | 176 |
class ValueStorage : public ValueStorageBase {
|
| 178 | 177 |
public: |
| 179 | 178 |
typedef _Value Value; |
| 180 | 179 |
typedef _Converter Converter; |
| 181 | 180 |
|
| 182 | 181 |
private: |
| 183 | 182 |
const Value& _value; |
| 184 | 183 |
Converter _converter; |
| 185 | 184 |
|
| 186 | 185 |
public: |
| 187 | 186 |
ValueStorage(const Value& value, const Converter& converter = Converter()) |
| 188 | 187 |
: _value(value), _converter(converter) {}
|
| 189 | 188 |
|
| 190 | 189 |
virtual std::string get() {
|
| 191 | 190 |
return _converter(_value); |
| 192 | 191 |
} |
| 193 | 192 |
}; |
| 194 | 193 |
|
| 195 | 194 |
template <typename Value> |
| 196 | 195 |
struct MapLookUpConverter {
|
| 197 | 196 |
const std::map<Value, std::string>& _map; |
| 198 | 197 |
|
| 199 | 198 |
MapLookUpConverter(const std::map<Value, std::string>& map) |
| 200 | 199 |
: _map(map) {}
|
| 201 | 200 |
|
| 202 | 201 |
std::string operator()(const Value& str) {
|
| 203 | 202 |
typename std::map<Value, std::string>::const_iterator it = |
| 204 | 203 |
_map.find(str); |
| 205 | 204 |
if (it == _map.end()) {
|
| 206 | 205 |
throw FormatError("Item not found");
|
| 207 | 206 |
} |
| 208 | 207 |
return it->second; |
| 209 | 208 |
} |
| 210 | 209 |
}; |
| 211 | 210 |
|
| 212 | 211 |
template <typename Graph> |
| 213 | 212 |
struct GraphArcLookUpConverter {
|
| 214 | 213 |
const Graph& _graph; |
| 215 | 214 |
const std::map<typename Graph::Edge, std::string>& _map; |
| 216 | 215 |
|
| 217 | 216 |
GraphArcLookUpConverter(const Graph& graph, |
| 218 | 217 |
const std::map<typename Graph::Edge, |
| 219 | 218 |
std::string>& map) |
| 220 | 219 |
: _graph(graph), _map(map) {}
|
| 221 | 220 |
|
| 222 | 221 |
std::string operator()(const typename Graph::Arc& val) {
|
| 223 | 222 |
typename std::map<typename Graph::Edge, std::string> |
| 224 | 223 |
::const_iterator it = _map.find(val); |
| 225 | 224 |
if (it == _map.end()) {
|
| 226 | 225 |
throw FormatError("Item not found");
|
| 227 | 226 |
} |
| 228 | 227 |
return (_graph.direction(val) ? '+' : '-') + it->second; |
| 229 | 228 |
} |
| 230 | 229 |
}; |
| 231 | 230 |
|
| 232 | 231 |
inline bool isWhiteSpace(char c) {
|
| 233 | 232 |
return c == ' ' || c == '\t' || c == '\v' || |
| 234 | 233 |
c == '\n' || c == '\r' || c == '\f'; |
| 235 | 234 |
} |
| 236 | 235 |
|
| 237 | 236 |
inline bool isEscaped(char c) {
|
| 238 | 237 |
return c == '\\' || c == '\"' || c == '\'' || |
| 239 | 238 |
c == '\a' || c == '\b'; |
| 240 | 239 |
} |
| 241 | 240 |
|
| 242 | 241 |
inline static void writeEscape(std::ostream& os, char c) {
|
| 243 | 242 |
switch (c) {
|
| 244 | 243 |
case '\\': |
| 245 | 244 |
os << "\\\\"; |
| 246 | 245 |
return; |
| 247 | 246 |
case '\"': |
| 248 | 247 |
os << "\\\""; |
| 249 | 248 |
return; |
| 250 | 249 |
case '\a': |
| 251 | 250 |
os << "\\a"; |
| 252 | 251 |
return; |
| 253 | 252 |
case '\b': |
| 254 | 253 |
os << "\\b"; |
| 255 | 254 |
return; |
| 256 | 255 |
case '\f': |
| 257 | 256 |
os << "\\f"; |
| 258 | 257 |
return; |
| 259 | 258 |
case '\r': |
| 260 | 259 |
os << "\\r"; |
| 261 | 260 |
return; |
| 262 | 261 |
case '\n': |
| 263 | 262 |
os << "\\n"; |
| 264 | 263 |
return; |
| 265 | 264 |
case '\t': |
| 266 | 265 |
os << "\\t"; |
| 267 | 266 |
return; |
| 268 | 267 |
case '\v': |
| 269 | 268 |
os << "\\v"; |
| 270 | 269 |
return; |
| 271 | 270 |
default: |
| 272 | 271 |
if (c < 0x20) {
|
| 273 | 272 |
std::ios::fmtflags flags = os.flags(); |
| 274 | 273 |
os << '\\' << std::oct << static_cast<int>(c); |
| 275 | 274 |
os.flags(flags); |
| 276 | 275 |
} else {
|
| 277 | 276 |
os << c; |
| 278 | 277 |
} |
| 279 | 278 |
return; |
| 280 | 279 |
} |
| 281 | 280 |
} |
| 282 | 281 |
|
| 283 | 282 |
inline bool requireEscape(const std::string& str) {
|
| 284 | 283 |
if (str.empty() || str[0] == '@') return true; |
| 285 | 284 |
std::istringstream is(str); |
| 286 | 285 |
char c; |
| 287 | 286 |
while (is.get(c)) {
|
| 288 | 287 |
if (isWhiteSpace(c) || isEscaped(c)) {
|
| 289 | 288 |
return true; |
| 290 | 289 |
} |
| 291 | 290 |
} |
| 292 | 291 |
return false; |
| 293 | 292 |
} |
| 294 | 293 |
|
| 295 | 294 |
inline std::ostream& writeToken(std::ostream& os, const std::string& str) {
|
| 296 | 295 |
|
| 297 | 296 |
if (requireEscape(str)) {
|
| 298 | 297 |
os << '\"'; |
| 299 | 298 |
for (std::string::const_iterator it = str.begin(); |
| 300 | 299 |
it != str.end(); ++it) {
|
| 301 | 300 |
writeEscape(os, *it); |
| 302 | 301 |
} |
| 303 | 302 |
os << '\"'; |
| 304 | 303 |
} else {
|
| 305 | 304 |
os << str; |
| 306 | 305 |
} |
| 307 | 306 |
return os; |
| 308 | 307 |
} |
| 309 | 308 |
|
| 310 | 309 |
class Section {
|
| 311 | 310 |
public: |
| 312 | 311 |
virtual ~Section() {}
|
| 313 | 312 |
virtual void process(std::ostream& os) = 0; |
| 314 | 313 |
}; |
| 315 | 314 |
|
| 316 | 315 |
template <typename Functor> |
| 317 | 316 |
class LineSection : public Section {
|
| 318 | 317 |
private: |
| 319 | 318 |
|
| 320 | 319 |
Functor _functor; |
| 321 | 320 |
|
| 322 | 321 |
public: |
| 323 | 322 |
|
| 324 | 323 |
LineSection(const Functor& functor) : _functor(functor) {}
|
| 325 | 324 |
virtual ~LineSection() {}
|
| 326 | 325 |
|
| 327 | 326 |
virtual void process(std::ostream& os) {
|
| 328 | 327 |
std::string line; |
| 329 | 328 |
while (!(line = _functor()).empty()) os << line << std::endl; |
| 330 | 329 |
} |
| 331 | 330 |
}; |
| 332 | 331 |
|
| 333 | 332 |
template <typename Functor> |
| 334 | 333 |
class StreamSection : public Section {
|
| 335 | 334 |
private: |
| 336 | 335 |
|
| 337 | 336 |
Functor _functor; |
| 338 | 337 |
|
| 339 | 338 |
public: |
| 340 | 339 |
|
| 341 | 340 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
| 342 | 341 |
virtual ~StreamSection() {}
|
| 343 | 342 |
|
| 344 | 343 |
virtual void process(std::ostream& os) {
|
| 345 | 344 |
_functor(os); |
| 346 | 345 |
} |
| 347 | 346 |
}; |
| 348 | 347 |
|
| 349 | 348 |
} |
| 350 | 349 |
|
| 351 | 350 |
template <typename Digraph> |
| 352 | 351 |
class DigraphWriter; |
| 353 | 352 |
|
| 354 | 353 |
/// \brief Return a \ref DigraphWriter class |
| 355 | 354 |
/// |
| 356 | 355 |
/// This function just returns a \ref DigraphWriter class. |
| 357 | 356 |
/// \relates DigraphWriter |
| 358 | 357 |
template <typename Digraph> |
| 359 | 358 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 360 | 359 |
std::ostream& os = std::cout) {
|
| 361 | 360 |
DigraphWriter<Digraph> tmp(digraph, os); |
| 362 | 361 |
return tmp; |
| 363 | 362 |
} |
| 364 | 363 |
|
| 365 | 364 |
/// \brief Return a \ref DigraphWriter class |
| 366 | 365 |
/// |
| 367 | 366 |
/// This function just returns a \ref DigraphWriter class. |
| 368 | 367 |
/// \relates DigraphWriter |
| 369 | 368 |
template <typename Digraph> |
| 370 | 369 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 371 | 370 |
const std::string& fn) {
|
| 372 | 371 |
DigraphWriter<Digraph> tmp(digraph, fn); |
| 373 | 372 |
return tmp; |
| 374 | 373 |
} |
| 375 | 374 |
|
| 376 | 375 |
/// \brief Return a \ref DigraphWriter class |
| 377 | 376 |
/// |
| 378 | 377 |
/// This function just returns a \ref DigraphWriter class. |
| 379 | 378 |
/// \relates DigraphWriter |
| 380 | 379 |
template <typename Digraph> |
| 381 | 380 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
| 382 | 381 |
const char* fn) {
|
| 383 | 382 |
DigraphWriter<Digraph> tmp(digraph, fn); |
| 384 | 383 |
return tmp; |
| 385 | 384 |
} |
| 386 | 385 |
|
| 387 | 386 |
/// \ingroup lemon_io |
| 388 | 387 |
/// |
| 389 | 388 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 390 | 389 |
/// |
| 391 | 390 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 392 | 391 |
/// |
| 393 | 392 |
/// The writing method does a batch processing. The user creates a |
| 394 | 393 |
/// writer object, then various writing rules can be added to the |
| 395 | 394 |
/// writer, and eventually the writing is executed with the \c run() |
| 396 | 395 |
/// member function. A map writing rule can be added to the writer |
| 397 | 396 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 398 | 397 |
/// converter parameter can also be added as a standard functor |
| 399 | 398 |
/// converting from the value type of the map to \c std::string. If it |
| 400 | 399 |
/// is set, it will determine how the value type of the map is written to |
| 401 | 400 |
/// the output stream. If the functor is not set, then a default |
| 402 | 401 |
/// conversion will be used. The \c attribute(), \c node() and \c |
| 403 | 402 |
/// arc() functions are used to add attribute writing rules. |
| 404 | 403 |
/// |
| 405 | 404 |
///\code |
| 406 | 405 |
/// DigraphWriter<Digraph>(digraph, std::cout). |
| 407 | 406 |
/// nodeMap("coordinates", coord_map).
|
| 408 | 407 |
/// nodeMap("size", size).
|
| 409 | 408 |
/// nodeMap("title", title).
|
| 410 | 409 |
/// arcMap("capacity", cap_map).
|
| 411 | 410 |
/// node("source", src).
|
| 412 | 411 |
/// node("target", trg).
|
| 413 | 412 |
/// attribute("caption", caption).
|
| 414 | 413 |
/// run(); |
| 415 | 414 |
///\endcode |
| 416 | 415 |
/// |
| 417 | 416 |
/// |
| 418 | 417 |
/// By default, the writer does not write additional captions to the |
| 419 | 418 |
/// sections, but they can be give as an optional parameter of |
| 420 | 419 |
/// the \c nodes(), \c arcs() or \c |
| 421 | 420 |
/// attributes() functions. |
| 422 | 421 |
/// |
| 423 | 422 |
/// The \c skipNodes() and \c skipArcs() functions forbid the |
| 424 | 423 |
/// writing of the sections. If two arc sections should be written |
| 425 | 424 |
/// to the output, it can be done in two passes, the first pass |
| 426 | 425 |
/// writes the node section and the first arc section, then the |
| 427 | 426 |
/// second pass skips the node section and writes just the arc |
| 428 | 427 |
/// section to the stream. The output stream can be retrieved with |
| 429 | 428 |
/// the \c ostream() function, hence the second pass can append its |
| 430 | 429 |
/// output to the output of the first pass. |
| 431 | 430 |
template <typename _Digraph> |
| 432 | 431 |
class DigraphWriter {
|
| 433 | 432 |
public: |
| 434 | 433 |
|
| 435 | 434 |
typedef _Digraph Digraph; |
| 436 | 435 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 437 | 436 |
|
| 438 | 437 |
private: |
| 439 | 438 |
|
| 440 | 439 |
|
| 441 | 440 |
std::ostream* _os; |
| 442 | 441 |
bool local_os; |
| 443 | 442 |
|
| 444 | 443 |
const Digraph& _digraph; |
| 445 | 444 |
|
| 446 | 445 |
std::string _nodes_caption; |
| 447 | 446 |
std::string _arcs_caption; |
| 448 | 447 |
std::string _attributes_caption; |
| 449 | 448 |
|
| 450 | 449 |
typedef std::map<Node, std::string> NodeIndex; |
| 451 | 450 |
NodeIndex _node_index; |
| 452 | 451 |
typedef std::map<Arc, std::string> ArcIndex; |
| 453 | 452 |
ArcIndex _arc_index; |
| 454 | 453 |
|
| 455 | 454 |
typedef std::vector<std::pair<std::string, |
| 456 | 455 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
| 457 | 456 |
NodeMaps _node_maps; |
| 458 | 457 |
|
| 459 | 458 |
typedef std::vector<std::pair<std::string, |
| 460 | 459 |
_writer_bits::MapStorageBase<Arc>* > >ArcMaps; |
| 461 | 460 |
ArcMaps _arc_maps; |
| 462 | 461 |
|
| 463 | 462 |
typedef std::vector<std::pair<std::string, |
| 464 | 463 |
_writer_bits::ValueStorageBase*> > Attributes; |
| 465 | 464 |
Attributes _attributes; |
| 466 | 465 |
|
| 467 | 466 |
bool _skip_nodes; |
| 468 | 467 |
bool _skip_arcs; |
| 469 | 468 |
|
| 470 | 469 |
public: |
| 471 | 470 |
|
| 472 | 471 |
/// \brief Constructor |
| 473 | 472 |
/// |
| 474 | 473 |
/// Construct a directed graph writer, which writes to the given |
| 475 | 474 |
/// output stream. |
| 476 | 475 |
DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout) |
| 477 | 476 |
: _os(&os), local_os(false), _digraph(digraph), |
| 478 | 477 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 479 | 478 |
|
| 480 | 479 |
/// \brief Constructor |
| 481 | 480 |
/// |
| 482 | 481 |
/// Construct a directed graph writer, which writes to the given |
| 483 | 482 |
/// output file. |
| 484 | 483 |
DigraphWriter(const Digraph& digraph, const std::string& fn) |
| 485 | 484 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), |
| 486 | 485 |
_skip_nodes(false), _skip_arcs(false) {
|
| 487 | 486 |
if (!(*_os)) {
|
| 488 | 487 |
delete _os; |
| 489 | 488 |
throw IoError("Cannot write file", fn);
|
| 490 | 489 |
} |
| 491 | 490 |
} |
| 492 | 491 |
|
| 493 | 492 |
/// \brief Constructor |
| 494 | 493 |
/// |
| 495 | 494 |
/// Construct a directed graph writer, which writes to the given |
| 496 | 495 |
/// output file. |
| 497 | 496 |
DigraphWriter(const Digraph& digraph, const char* fn) |
| 498 | 497 |
: _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), |
| 499 | 498 |
_skip_nodes(false), _skip_arcs(false) {
|
| 500 | 499 |
if (!(*_os)) {
|
| 501 | 500 |
delete _os; |
| 502 | 501 |
throw IoError("Cannot write file", fn);
|
| 503 | 502 |
} |
| 504 | 503 |
} |
| 505 | 504 |
|
| 506 | 505 |
/// \brief Destructor |
| 507 | 506 |
~DigraphWriter() {
|
| 508 | 507 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 509 | 508 |
it != _node_maps.end(); ++it) {
|
| 510 | 509 |
delete it->second; |
| 511 | 510 |
} |
| 512 | 511 |
|
| 513 | 512 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 514 | 513 |
it != _arc_maps.end(); ++it) {
|
| 515 | 514 |
delete it->second; |
| 516 | 515 |
} |
| 517 | 516 |
|
| 518 | 517 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 519 | 518 |
it != _attributes.end(); ++it) {
|
| 520 | 519 |
delete it->second; |
| 521 | 520 |
} |
| 522 | 521 |
|
| 523 | 522 |
if (local_os) {
|
| 524 | 523 |
delete _os; |
| 525 | 524 |
} |
| 526 | 525 |
} |
| 527 | 526 |
|
| 528 | 527 |
private: |
| 529 | 528 |
|
| 530 | 529 |
friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, |
| 531 | 530 |
std::ostream& os); |
| 532 | 531 |
friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, |
| 533 | 532 |
const std::string& fn); |
| 534 | 533 |
friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, |
| 535 | 534 |
const char *fn); |
| 536 | 535 |
|
| 537 | 536 |
DigraphWriter(DigraphWriter& other) |
| 538 | 537 |
: _os(other._os), local_os(other.local_os), _digraph(other._digraph), |
| 539 | 538 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
| 540 | 539 |
|
| 541 | 540 |
other._os = 0; |
| 542 | 541 |
other.local_os = false; |
| 543 | 542 |
|
| 544 | 543 |
_node_index.swap(other._node_index); |
| 545 | 544 |
_arc_index.swap(other._arc_index); |
| 546 | 545 |
|
| 547 | 546 |
_node_maps.swap(other._node_maps); |
| 548 | 547 |
_arc_maps.swap(other._arc_maps); |
| 549 | 548 |
_attributes.swap(other._attributes); |
| 550 | 549 |
|
| 551 | 550 |
_nodes_caption = other._nodes_caption; |
| 552 | 551 |
_arcs_caption = other._arcs_caption; |
| 553 | 552 |
_attributes_caption = other._attributes_caption; |
| 554 | 553 |
} |
| 555 | 554 |
|
| 556 | 555 |
DigraphWriter& operator=(const DigraphWriter&); |
| 557 | 556 |
|
| 558 | 557 |
public: |
| 559 | 558 |
|
| 560 | 559 |
/// \name Writing rules |
| 561 | 560 |
/// @{
|
| 562 | 561 |
|
| 563 | 562 |
/// \brief Node map writing rule |
| 564 | 563 |
/// |
| 565 | 564 |
/// Add a node map writing rule to the writer. |
| 566 | 565 |
template <typename Map> |
| 567 | 566 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
|
| 568 | 567 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 569 | 568 |
_writer_bits::MapStorageBase<Node>* storage = |
| 570 | 569 |
new _writer_bits::MapStorage<Node, Map>(map); |
| 571 | 570 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 572 | 571 |
return *this; |
| 573 | 572 |
} |
| 574 | 573 |
|
| 575 | 574 |
/// \brief Node map writing rule |
| 576 | 575 |
/// |
| 577 | 576 |
/// Add a node map writing rule with specialized converter to the |
| 578 | 577 |
/// writer. |
| 579 | 578 |
template <typename Map, typename Converter> |
| 580 | 579 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map, |
| 581 | 580 |
const Converter& converter = Converter()) {
|
| 582 | 581 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 583 | 582 |
_writer_bits::MapStorageBase<Node>* storage = |
| 584 | 583 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 585 | 584 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 586 | 585 |
return *this; |
| 587 | 586 |
} |
| 588 | 587 |
|
| 589 | 588 |
/// \brief Arc map writing rule |
| 590 | 589 |
/// |
| 591 | 590 |
/// Add an arc map writing rule to the writer. |
| 592 | 591 |
template <typename Map> |
| 593 | 592 |
DigraphWriter& arcMap(const std::string& caption, const Map& map) {
|
| 594 | 593 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 595 | 594 |
_writer_bits::MapStorageBase<Arc>* storage = |
| 596 | 595 |
new _writer_bits::MapStorage<Arc, Map>(map); |
| 597 | 596 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 598 | 597 |
return *this; |
| 599 | 598 |
} |
| 600 | 599 |
|
| 601 | 600 |
/// \brief Arc map writing rule |
| 602 | 601 |
/// |
| 603 | 602 |
/// Add an arc map writing rule with specialized converter to the |
| 604 | 603 |
/// writer. |
| 605 | 604 |
template <typename Map, typename Converter> |
| 606 | 605 |
DigraphWriter& arcMap(const std::string& caption, const Map& map, |
| 607 | 606 |
const Converter& converter = Converter()) {
|
| 608 | 607 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 609 | 608 |
_writer_bits::MapStorageBase<Arc>* storage = |
| 610 | 609 |
new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter); |
| 611 | 610 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 612 | 611 |
return *this; |
| 613 | 612 |
} |
| 614 | 613 |
|
| 615 | 614 |
/// \brief Attribute writing rule |
| 616 | 615 |
/// |
| 617 | 616 |
/// Add an attribute writing rule to the writer. |
| 618 | 617 |
template <typename Value> |
| 619 | 618 |
DigraphWriter& attribute(const std::string& caption, const Value& value) {
|
| 620 | 619 |
_writer_bits::ValueStorageBase* storage = |
| 621 | 620 |
new _writer_bits::ValueStorage<Value>(value); |
| 622 | 621 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 623 | 622 |
return *this; |
| 624 | 623 |
} |
| 625 | 624 |
|
| 626 | 625 |
/// \brief Attribute writing rule |
| 627 | 626 |
/// |
| 628 | 627 |
/// Add an attribute writing rule with specialized converter to the |
| 629 | 628 |
/// writer. |
| 630 | 629 |
template <typename Value, typename Converter> |
| 631 | 630 |
DigraphWriter& attribute(const std::string& caption, const Value& value, |
| 632 | 631 |
const Converter& converter = Converter()) {
|
| 633 | 632 |
_writer_bits::ValueStorageBase* storage = |
| 634 | 633 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
| 635 | 634 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 636 | 635 |
return *this; |
| 637 | 636 |
} |
| 638 | 637 |
|
| 639 | 638 |
/// \brief Node writing rule |
| 640 | 639 |
/// |
| 641 | 640 |
/// Add a node writing rule to the writer. |
| 642 | 641 |
DigraphWriter& node(const std::string& caption, const Node& node) {
|
| 643 | 642 |
typedef _writer_bits::MapLookUpConverter<Node> Converter; |
| 644 | 643 |
Converter converter(_node_index); |
| 645 | 644 |
_writer_bits::ValueStorageBase* storage = |
| 646 | 645 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
| 647 | 646 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 648 | 647 |
return *this; |
| 649 | 648 |
} |
| 650 | 649 |
|
| 651 | 650 |
/// \brief Arc writing rule |
| 652 | 651 |
/// |
| 653 | 652 |
/// Add an arc writing rule to writer. |
| 654 | 653 |
DigraphWriter& arc(const std::string& caption, const Arc& arc) {
|
| 655 | 654 |
typedef _writer_bits::MapLookUpConverter<Arc> Converter; |
| 656 | 655 |
Converter converter(_arc_index); |
| 657 | 656 |
_writer_bits::ValueStorageBase* storage = |
| 658 | 657 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 659 | 658 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 660 | 659 |
return *this; |
| 661 | 660 |
} |
| 662 | 661 |
|
| 663 | 662 |
/// \name Section captions |
| 664 | 663 |
/// @{
|
| 665 | 664 |
|
| 666 | 665 |
/// \brief Add an additional caption to the \c \@nodes section |
| 667 | 666 |
/// |
| 668 | 667 |
/// Add an additional caption to the \c \@nodes section. |
| 669 | 668 |
DigraphWriter& nodes(const std::string& caption) {
|
| 670 | 669 |
_nodes_caption = caption; |
| 671 | 670 |
return *this; |
| 672 | 671 |
} |
| 673 | 672 |
|
| 674 | 673 |
/// \brief Add an additional caption to the \c \@arcs section |
| 675 | 674 |
/// |
| 676 | 675 |
/// Add an additional caption to the \c \@arcs section. |
| 677 | 676 |
DigraphWriter& arcs(const std::string& caption) {
|
| 678 | 677 |
_arcs_caption = caption; |
| 679 | 678 |
return *this; |
| 680 | 679 |
} |
| 681 | 680 |
|
| 682 | 681 |
/// \brief Add an additional caption to the \c \@attributes section |
| 683 | 682 |
/// |
| 684 | 683 |
/// Add an additional caption to the \c \@attributes section. |
| 685 | 684 |
DigraphWriter& attributes(const std::string& caption) {
|
| 686 | 685 |
_attributes_caption = caption; |
| 687 | 686 |
return *this; |
| 688 | 687 |
} |
| 689 | 688 |
|
| 690 | 689 |
/// \name Skipping section |
| 691 | 690 |
/// @{
|
| 692 | 691 |
|
| 693 | 692 |
/// \brief Skip writing the node set |
| 694 | 693 |
/// |
| 695 | 694 |
/// The \c \@nodes section will not be written to the stream. |
| 696 | 695 |
DigraphWriter& skipNodes() {
|
| 697 | 696 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
| 698 | 697 |
_skip_nodes = true; |
| 699 | 698 |
return *this; |
| 700 | 699 |
} |
| 701 | 700 |
|
| 702 | 701 |
/// \brief Skip writing arc set |
| 703 | 702 |
/// |
| 704 | 703 |
/// The \c \@arcs section will not be written to the stream. |
| 705 | 704 |
DigraphWriter& skipArcs() {
|
| 706 | 705 |
LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member"); |
| 707 | 706 |
_skip_arcs = true; |
| 708 | 707 |
return *this; |
| 709 | 708 |
} |
| 710 | 709 |
|
| 711 | 710 |
/// @} |
| 712 | 711 |
|
| 713 | 712 |
private: |
| 714 | 713 |
|
| 715 | 714 |
void writeNodes() {
|
| 716 | 715 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 717 | 716 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 718 | 717 |
it != _node_maps.end(); ++it) {
|
| 719 | 718 |
if (it->first == "label") {
|
| 720 | 719 |
label = it->second; |
| 721 | 720 |
break; |
| 722 | 721 |
} |
| 723 | 722 |
} |
| 724 | 723 |
|
| 725 | 724 |
*_os << "@nodes"; |
| 726 | 725 |
if (!_nodes_caption.empty()) {
|
| 727 | 726 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption); |
| 728 | 727 |
} |
| 729 | 728 |
*_os << std::endl; |
| 730 | 729 |
|
| 731 | 730 |
if (label == 0) {
|
| 732 | 731 |
*_os << "label" << '\t'; |
| 733 | 732 |
} |
| 734 | 733 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 735 | 734 |
it != _node_maps.end(); ++it) {
|
| 736 | 735 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 737 | 736 |
} |
| 738 | 737 |
*_os << std::endl; |
| 739 | 738 |
|
| 740 | 739 |
std::vector<Node> nodes; |
| 741 | 740 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 742 | 741 |
nodes.push_back(n); |
| 743 | 742 |
} |
| 744 | 743 |
|
| 745 | 744 |
if (label == 0) {
|
| 746 | 745 |
IdMap<Digraph, Node> id_map(_digraph); |
| 747 | 746 |
_writer_bits::MapLess<IdMap<Digraph, Node> > id_less(id_map); |
| 748 | 747 |
std::sort(nodes.begin(), nodes.end(), id_less); |
| 749 | 748 |
} else {
|
| 750 | 749 |
label->sort(nodes); |
| 751 | 750 |
} |
| 752 | 751 |
|
| 753 | 752 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
|
| 754 | 753 |
Node n = nodes[i]; |
| 755 | 754 |
if (label == 0) {
|
| 756 | 755 |
std::ostringstream os; |
| 757 | 756 |
os << _digraph.id(n); |
| 758 | 757 |
_writer_bits::writeToken(*_os, os.str()); |
| 759 | 758 |
*_os << '\t'; |
| 760 | 759 |
_node_index.insert(std::make_pair(n, os.str())); |
| 761 | 760 |
} |
| 762 | 761 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 763 | 762 |
it != _node_maps.end(); ++it) {
|
| 764 | 763 |
std::string value = it->second->get(n); |
| 765 | 764 |
_writer_bits::writeToken(*_os, value); |
| 766 | 765 |
if (it->first == "label") {
|
| 767 | 766 |
_node_index.insert(std::make_pair(n, value)); |
| 768 | 767 |
} |
| 769 | 768 |
*_os << '\t'; |
| 770 | 769 |
} |
| 771 | 770 |
*_os << std::endl; |
| 772 | 771 |
} |
| 773 | 772 |
} |
| 774 | 773 |
|
| 775 | 774 |
void createNodeIndex() {
|
| 776 | 775 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 777 | 776 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 778 | 777 |
it != _node_maps.end(); ++it) {
|
| 779 | 778 |
if (it->first == "label") {
|
| 780 | 779 |
label = it->second; |
| 781 | 780 |
break; |
| 782 | 781 |
} |
| 783 | 782 |
} |
| 784 | 783 |
|
| 785 | 784 |
if (label == 0) {
|
| 786 | 785 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 787 | 786 |
std::ostringstream os; |
| 788 | 787 |
os << _digraph.id(n); |
| 789 | 788 |
_node_index.insert(std::make_pair(n, os.str())); |
| 790 | 789 |
} |
| 791 | 790 |
} else {
|
| 792 | 791 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 793 | 792 |
std::string value = label->get(n); |
| 794 | 793 |
_node_index.insert(std::make_pair(n, value)); |
| 795 | 794 |
} |
| 796 | 795 |
} |
| 797 | 796 |
} |
| 798 | 797 |
|
| 799 | 798 |
void writeArcs() {
|
| 800 | 799 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
| 801 | 800 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 802 | 801 |
it != _arc_maps.end(); ++it) {
|
| 803 | 802 |
if (it->first == "label") {
|
| 804 | 803 |
label = it->second; |
| 805 | 804 |
break; |
| 806 | 805 |
} |
| 807 | 806 |
} |
| 808 | 807 |
|
| 809 | 808 |
*_os << "@arcs"; |
| 810 | 809 |
if (!_arcs_caption.empty()) {
|
| 811 | 810 |
_writer_bits::writeToken(*_os << ' ', _arcs_caption); |
| 812 | 811 |
} |
| 813 | 812 |
*_os << std::endl; |
| 814 | 813 |
|
| 815 | 814 |
*_os << '\t' << '\t'; |
| 816 | 815 |
if (label == 0) {
|
| 817 | 816 |
*_os << "label" << '\t'; |
| 818 | 817 |
} |
| 819 | 818 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 820 | 819 |
it != _arc_maps.end(); ++it) {
|
| 821 | 820 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 822 | 821 |
} |
| 823 | 822 |
*_os << std::endl; |
| 824 | 823 |
|
| 825 | 824 |
std::vector<Arc> arcs; |
| 826 | 825 |
for (ArcIt n(_digraph); n != INVALID; ++n) {
|
| 827 | 826 |
arcs.push_back(n); |
| 828 | 827 |
} |
| 829 | 828 |
|
| 830 | 829 |
if (label == 0) {
|
| 831 | 830 |
IdMap<Digraph, Arc> id_map(_digraph); |
| 832 | 831 |
_writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map); |
| 833 | 832 |
std::sort(arcs.begin(), arcs.end(), id_less); |
| 834 | 833 |
} else {
|
| 835 | 834 |
label->sort(arcs); |
| 836 | 835 |
} |
| 837 | 836 |
|
| 838 | 837 |
for (int i = 0; i < static_cast<int>(arcs.size()); ++i) {
|
| 839 | 838 |
Arc a = arcs[i]; |
| 840 | 839 |
_writer_bits::writeToken(*_os, _node_index. |
| 841 | 840 |
find(_digraph.source(a))->second); |
| 842 | 841 |
*_os << '\t'; |
| 843 | 842 |
_writer_bits::writeToken(*_os, _node_index. |
| 844 | 843 |
find(_digraph.target(a))->second); |
| 845 | 844 |
*_os << '\t'; |
| 846 | 845 |
if (label == 0) {
|
| 847 | 846 |
std::ostringstream os; |
| 848 | 847 |
os << _digraph.id(a); |
| 849 | 848 |
_writer_bits::writeToken(*_os, os.str()); |
| 850 | 849 |
*_os << '\t'; |
| 851 | 850 |
_arc_index.insert(std::make_pair(a, os.str())); |
| 852 | 851 |
} |
| 853 | 852 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 854 | 853 |
it != _arc_maps.end(); ++it) {
|
| 855 | 854 |
std::string value = it->second->get(a); |
| 856 | 855 |
_writer_bits::writeToken(*_os, value); |
| 857 | 856 |
if (it->first == "label") {
|
| 858 | 857 |
_arc_index.insert(std::make_pair(a, value)); |
| 859 | 858 |
} |
| 860 | 859 |
*_os << '\t'; |
| 861 | 860 |
} |
| 862 | 861 |
*_os << std::endl; |
| 863 | 862 |
} |
| 864 | 863 |
} |
| 865 | 864 |
|
| 866 | 865 |
void createArcIndex() {
|
| 867 | 866 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
| 868 | 867 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 869 | 868 |
it != _arc_maps.end(); ++it) {
|
| 870 | 869 |
if (it->first == "label") {
|
| 871 | 870 |
label = it->second; |
| 872 | 871 |
break; |
| 873 | 872 |
} |
| 874 | 873 |
} |
| 875 | 874 |
|
| 876 | 875 |
if (label == 0) {
|
| 877 | 876 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 878 | 877 |
std::ostringstream os; |
| 879 | 878 |
os << _digraph.id(a); |
| 880 | 879 |
_arc_index.insert(std::make_pair(a, os.str())); |
| 881 | 880 |
} |
| 882 | 881 |
} else {
|
| 883 | 882 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 884 | 883 |
std::string value = label->get(a); |
| 885 | 884 |
_arc_index.insert(std::make_pair(a, value)); |
| 886 | 885 |
} |
| 887 | 886 |
} |
| 888 | 887 |
} |
| 889 | 888 |
|
| 890 | 889 |
void writeAttributes() {
|
| 891 | 890 |
if (_attributes.empty()) return; |
| 892 | 891 |
*_os << "@attributes"; |
| 893 | 892 |
if (!_attributes_caption.empty()) {
|
| 894 | 893 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
| 895 | 894 |
} |
| 896 | 895 |
*_os << std::endl; |
| 897 | 896 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 898 | 897 |
it != _attributes.end(); ++it) {
|
| 899 | 898 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
| 900 | 899 |
_writer_bits::writeToken(*_os, it->second->get()); |
| 901 | 900 |
*_os << std::endl; |
| 902 | 901 |
} |
| 903 | 902 |
} |
| 904 | 903 |
|
| 905 | 904 |
public: |
| 906 | 905 |
|
| 907 | 906 |
/// \name Execution of the writer |
| 908 | 907 |
/// @{
|
| 909 | 908 |
|
| 910 | 909 |
/// \brief Start the batch processing |
| 911 | 910 |
/// |
| 912 | 911 |
/// This function starts the batch processing. |
| 913 | 912 |
void run() {
|
| 914 | 913 |
if (!_skip_nodes) {
|
| 915 | 914 |
writeNodes(); |
| 916 | 915 |
} else {
|
| 917 | 916 |
createNodeIndex(); |
| 918 | 917 |
} |
| 919 | 918 |
if (!_skip_arcs) {
|
| 920 | 919 |
writeArcs(); |
| 921 | 920 |
} else {
|
| 922 | 921 |
createArcIndex(); |
| 923 | 922 |
} |
| 924 | 923 |
writeAttributes(); |
| 925 | 924 |
} |
| 926 | 925 |
|
| 927 | 926 |
/// \brief Give back the stream of the writer |
| 928 | 927 |
/// |
| 929 | 928 |
/// Give back the stream of the writer. |
| 930 | 929 |
std::ostream& ostream() {
|
| 931 | 930 |
return *_os; |
| 932 | 931 |
} |
| 933 | 932 |
|
| 934 | 933 |
/// @} |
| 935 | 934 |
}; |
| 936 | 935 |
|
| 937 | 936 |
template <typename Graph> |
| 938 | 937 |
class GraphWriter; |
| 939 | 938 |
|
| 940 | 939 |
/// \brief Return a \ref GraphWriter class |
| 941 | 940 |
/// |
| 942 | 941 |
/// This function just returns a \ref GraphWriter class. |
| 943 | 942 |
/// \relates GraphWriter |
| 944 | 943 |
template <typename Graph> |
| 945 | 944 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
| 946 | 945 |
std::ostream& os = std::cout) {
|
| 947 | 946 |
GraphWriter<Graph> tmp(graph, os); |
| 948 | 947 |
return tmp; |
| 949 | 948 |
} |
| 950 | 949 |
|
| 951 | 950 |
/// \brief Return a \ref GraphWriter class |
| 952 | 951 |
/// |
| 953 | 952 |
/// This function just returns a \ref GraphWriter class. |
| 954 | 953 |
/// \relates GraphWriter |
| 955 | 954 |
template <typename Graph> |
| 956 | 955 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) {
|
| 957 | 956 |
GraphWriter<Graph> tmp(graph, fn); |
| 958 | 957 |
return tmp; |
| 959 | 958 |
} |
| 960 | 959 |
|
| 961 | 960 |
/// \brief Return a \ref GraphWriter class |
| 962 | 961 |
/// |
| 963 | 962 |
/// This function just returns a \ref GraphWriter class. |
| 964 | 963 |
/// \relates GraphWriter |
| 965 | 964 |
template <typename Graph> |
| 966 | 965 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
|
| 967 | 966 |
GraphWriter<Graph> tmp(graph, fn); |
| 968 | 967 |
return tmp; |
| 969 | 968 |
} |
| 970 | 969 |
|
| 971 | 970 |
/// \ingroup lemon_io |
| 972 | 971 |
/// |
| 973 | 972 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 974 | 973 |
/// |
| 975 | 974 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 976 | 975 |
/// |
| 977 | 976 |
/// It can be used almost the same way as \c DigraphWriter. |
| 978 | 977 |
/// The only difference is that this class can handle edges and |
| 979 | 978 |
/// edge maps as well as arcs and arc maps. |
| 980 | 979 |
/// |
| 981 | 980 |
/// The arc maps are written into the file as two columns, the |
| 982 | 981 |
/// caption of the columns are the name of the map prefixed with \c |
| 983 | 982 |
/// '+' and \c '-'. The arcs are written into the \c \@attributes |
| 984 | 983 |
/// section as a \c '+' or a \c '-' prefix (depends on the direction |
| 985 | 984 |
/// of the arc) and the label of corresponding edge. |
| 986 | 985 |
template <typename _Graph> |
| 987 | 986 |
class GraphWriter {
|
| 988 | 987 |
public: |
| 989 | 988 |
|
| 990 | 989 |
typedef _Graph Graph; |
| 991 | 990 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 992 | 991 |
|
| 993 | 992 |
private: |
| 994 | 993 |
|
| 995 | 994 |
|
| 996 | 995 |
std::ostream* _os; |
| 997 | 996 |
bool local_os; |
| 998 | 997 |
|
| 999 | 998 |
const Graph& _graph; |
| 1000 | 999 |
|
| 1001 | 1000 |
std::string _nodes_caption; |
| 1002 | 1001 |
std::string _edges_caption; |
| 1003 | 1002 |
std::string _attributes_caption; |
| 1004 | 1003 |
|
| 1005 | 1004 |
typedef std::map<Node, std::string> NodeIndex; |
| 1006 | 1005 |
NodeIndex _node_index; |
| 1007 | 1006 |
typedef std::map<Edge, std::string> EdgeIndex; |
| 1008 | 1007 |
EdgeIndex _edge_index; |
| 1009 | 1008 |
|
| 1010 | 1009 |
typedef std::vector<std::pair<std::string, |
| 1011 | 1010 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
| 1012 | 1011 |
NodeMaps _node_maps; |
| 1013 | 1012 |
|
| 1014 | 1013 |
typedef std::vector<std::pair<std::string, |
| 1015 | 1014 |
_writer_bits::MapStorageBase<Edge>* > >EdgeMaps; |
| 1016 | 1015 |
EdgeMaps _edge_maps; |
| 1017 | 1016 |
|
| 1018 | 1017 |
typedef std::vector<std::pair<std::string, |
| 1019 | 1018 |
_writer_bits::ValueStorageBase*> > Attributes; |
| 1020 | 1019 |
Attributes _attributes; |
| 1021 | 1020 |
|
| 1022 | 1021 |
bool _skip_nodes; |
| 1023 | 1022 |
bool _skip_edges; |
| 1024 | 1023 |
|
| 1025 | 1024 |
public: |
| 1026 | 1025 |
|
| 1027 | 1026 |
/// \brief Constructor |
| 1028 | 1027 |
/// |
| 1029 | 1028 |
/// Construct a directed graph writer, which writes to the given |
| 1030 | 1029 |
/// output stream. |
| 1031 | 1030 |
GraphWriter(const Graph& graph, std::ostream& os = std::cout) |
| 1032 | 1031 |
: _os(&os), local_os(false), _graph(graph), |
| 1033 | 1032 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1034 | 1033 |
|
| 1035 | 1034 |
/// \brief Constructor |
| 1036 | 1035 |
/// |
| 1037 | 1036 |
/// Construct a directed graph writer, which writes to the given |
| 1038 | 1037 |
/// output file. |
| 1039 | 1038 |
GraphWriter(const Graph& graph, const std::string& fn) |
| 1040 | 1039 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), |
| 1041 | 1040 |
_skip_nodes(false), _skip_edges(false) {
|
| 1042 | 1041 |
if (!(*_os)) {
|
| 1043 | 1042 |
delete _os; |
| 1044 | 1043 |
throw IoError("Cannot write file", fn);
|
| 1045 | 1044 |
} |
| 1046 | 1045 |
} |
| 1047 | 1046 |
|
| 1048 | 1047 |
/// \brief Constructor |
| 1049 | 1048 |
/// |
| 1050 | 1049 |
/// Construct a directed graph writer, which writes to the given |
| 1051 | 1050 |
/// output file. |
| 1052 | 1051 |
GraphWriter(const Graph& graph, const char* fn) |
| 1053 | 1052 |
: _os(new std::ofstream(fn)), local_os(true), _graph(graph), |
| 1054 | 1053 |
_skip_nodes(false), _skip_edges(false) {
|
| 1055 | 1054 |
if (!(*_os)) {
|
| 1056 | 1055 |
delete _os; |
| 1057 | 1056 |
throw IoError("Cannot write file", fn);
|
| 1058 | 1057 |
} |
| 1059 | 1058 |
} |
| 1060 | 1059 |
|
| 1061 | 1060 |
/// \brief Destructor |
| 1062 | 1061 |
~GraphWriter() {
|
| 1063 | 1062 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1064 | 1063 |
it != _node_maps.end(); ++it) {
|
| 1065 | 1064 |
delete it->second; |
| 1066 | 1065 |
} |
| 1067 | 1066 |
|
| 1068 | 1067 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1069 | 1068 |
it != _edge_maps.end(); ++it) {
|
| 1070 | 1069 |
delete it->second; |
| 1071 | 1070 |
} |
| 1072 | 1071 |
|
| 1073 | 1072 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1074 | 1073 |
it != _attributes.end(); ++it) {
|
| 1075 | 1074 |
delete it->second; |
| 1076 | 1075 |
} |
| 1077 | 1076 |
|
| 1078 | 1077 |
if (local_os) {
|
| 1079 | 1078 |
delete _os; |
| 1080 | 1079 |
} |
| 1081 | 1080 |
} |
| 1082 | 1081 |
|
| 1083 | 1082 |
private: |
| 1084 | 1083 |
|
| 1085 | 1084 |
friend GraphWriter<Graph> graphWriter<>(const Graph& graph, |
| 1086 | 1085 |
std::ostream& os); |
| 1087 | 1086 |
friend GraphWriter<Graph> graphWriter<>(const Graph& graph, |
| 1088 | 1087 |
const std::string& fn); |
| 1089 | 1088 |
friend GraphWriter<Graph> graphWriter<>(const Graph& graph, |
| 1090 | 1089 |
const char *fn); |
| 1091 | 1090 |
|
| 1092 | 1091 |
GraphWriter(GraphWriter& other) |
| 1093 | 1092 |
: _os(other._os), local_os(other.local_os), _graph(other._graph), |
| 1094 | 1093 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
| 1095 | 1094 |
|
| 1096 | 1095 |
other._os = 0; |
| 1097 | 1096 |
other.local_os = false; |
| 1098 | 1097 |
|
| 1099 | 1098 |
_node_index.swap(other._node_index); |
| 1100 | 1099 |
_edge_index.swap(other._edge_index); |
| 1101 | 1100 |
|
| 1102 | 1101 |
_node_maps.swap(other._node_maps); |
| 1103 | 1102 |
_edge_maps.swap(other._edge_maps); |
| 1104 | 1103 |
_attributes.swap(other._attributes); |
| 1105 | 1104 |
|
| 1106 | 1105 |
_nodes_caption = other._nodes_caption; |
| 1107 | 1106 |
_edges_caption = other._edges_caption; |
| 1108 | 1107 |
_attributes_caption = other._attributes_caption; |
| 1109 | 1108 |
} |
| 1110 | 1109 |
|
| 1111 | 1110 |
GraphWriter& operator=(const GraphWriter&); |
| 1112 | 1111 |
|
| 1113 | 1112 |
public: |
| 1114 | 1113 |
|
| 1115 | 1114 |
/// \name Writing rules |
| 1116 | 1115 |
/// @{
|
| 1117 | 1116 |
|
| 1118 | 1117 |
/// \brief Node map writing rule |
| 1119 | 1118 |
/// |
| 1120 | 1119 |
/// Add a node map writing rule to the writer. |
| 1121 | 1120 |
template <typename Map> |
| 1122 | 1121 |
GraphWriter& nodeMap(const std::string& caption, const Map& map) {
|
| 1123 | 1122 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1124 | 1123 |
_writer_bits::MapStorageBase<Node>* storage = |
| 1125 | 1124 |
new _writer_bits::MapStorage<Node, Map>(map); |
| 1126 | 1125 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1127 | 1126 |
return *this; |
| 1128 | 1127 |
} |
| 1129 | 1128 |
|
| 1130 | 1129 |
/// \brief Node map writing rule |
| 1131 | 1130 |
/// |
| 1132 | 1131 |
/// Add a node map writing rule with specialized converter to the |
| 1133 | 1132 |
/// writer. |
| 1134 | 1133 |
template <typename Map, typename Converter> |
| 1135 | 1134 |
GraphWriter& nodeMap(const std::string& caption, const Map& map, |
| 1136 | 1135 |
const Converter& converter = Converter()) {
|
| 1137 | 1136 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1138 | 1137 |
_writer_bits::MapStorageBase<Node>* storage = |
| 1139 | 1138 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 1140 | 1139 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1141 | 1140 |
return *this; |
| 1142 | 1141 |
} |
| 1143 | 1142 |
|
| 1144 | 1143 |
/// \brief Edge map writing rule |
| 1145 | 1144 |
/// |
| 1146 | 1145 |
/// Add an edge map writing rule to the writer. |
| 1147 | 1146 |
template <typename Map> |
| 1148 | 1147 |
GraphWriter& edgeMap(const std::string& caption, const Map& map) {
|
| 1149 | 1148 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1150 | 1149 |
_writer_bits::MapStorageBase<Edge>* storage = |
| 1151 | 1150 |
new _writer_bits::MapStorage<Edge, Map>(map); |
| 1152 | 1151 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1153 | 1152 |
return *this; |
| 1154 | 1153 |
} |
| 1155 | 1154 |
|
| 1156 | 1155 |
/// \brief Edge map writing rule |
| 1157 | 1156 |
/// |
| 1158 | 1157 |
/// Add an edge map writing rule with specialized converter to the |
| 1159 | 1158 |
/// writer. |
| 1160 | 1159 |
template <typename Map, typename Converter> |
| 1161 | 1160 |
GraphWriter& edgeMap(const std::string& caption, const Map& map, |
| 1162 | 1161 |
const Converter& converter = Converter()) {
|
| 1163 | 1162 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1164 | 1163 |
_writer_bits::MapStorageBase<Edge>* storage = |
| 1165 | 1164 |
new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter); |
| 1166 | 1165 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1167 | 1166 |
return *this; |
| 1168 | 1167 |
} |
| 1169 | 1168 |
|
| 1170 | 1169 |
/// \brief Arc map writing rule |
| 1171 | 1170 |
/// |
| 1172 | 1171 |
/// Add an arc map writing rule to the writer. |
| 1173 | 1172 |
template <typename Map> |
| 1174 | 1173 |
GraphWriter& arcMap(const std::string& caption, const Map& map) {
|
| 1175 | 1174 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 1176 | 1175 |
_writer_bits::MapStorageBase<Edge>* forward_storage = |
| 1177 | 1176 |
new _writer_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
| 1178 | 1177 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1179 | 1178 |
_writer_bits::MapStorageBase<Edge>* backward_storage = |
| 1180 | 1179 |
new _writer_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map); |
| 1181 | 1180 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1182 | 1181 |
return *this; |
| 1183 | 1182 |
} |
| 1184 | 1183 |
|
| 1185 | 1184 |
/// \brief Arc map writing rule |
| 1186 | 1185 |
/// |
| 1187 | 1186 |
/// Add an arc map writing rule with specialized converter to the |
| 1188 | 1187 |
/// writer. |
| 1189 | 1188 |
template <typename Map, typename Converter> |
| 1190 | 1189 |
GraphWriter& arcMap(const std::string& caption, const Map& map, |
| 1191 | 1190 |
const Converter& converter = Converter()) {
|
| 1192 | 1191 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 1193 | 1192 |
_writer_bits::MapStorageBase<Edge>* forward_storage = |
| 1194 | 1193 |
new _writer_bits::GraphArcMapStorage<Graph, true, Map, Converter> |
| 1195 | 1194 |
(_graph, map, converter); |
| 1196 | 1195 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1197 | 1196 |
_writer_bits::MapStorageBase<Edge>* backward_storage = |
| 1198 | 1197 |
new _writer_bits::GraphArcMapStorage<Graph, false, Map, Converter> |
| 1199 | 1198 |
(_graph, map, converter); |
| 1200 | 1199 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1201 | 1200 |
return *this; |
| 1202 | 1201 |
} |
| 1203 | 1202 |
|
| 1204 | 1203 |
/// \brief Attribute writing rule |
| 1205 | 1204 |
/// |
| 1206 | 1205 |
/// Add an attribute writing rule to the writer. |
| 1207 | 1206 |
template <typename Value> |
| 1208 | 1207 |
GraphWriter& attribute(const std::string& caption, const Value& value) {
|
| 1209 | 1208 |
_writer_bits::ValueStorageBase* storage = |
| 1210 | 1209 |
new _writer_bits::ValueStorage<Value>(value); |
| 1211 | 1210 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1212 | 1211 |
return *this; |
| 1213 | 1212 |
} |
| 1214 | 1213 |
|
| 1215 | 1214 |
/// \brief Attribute writing rule |
| 1216 | 1215 |
/// |
| 1217 | 1216 |
/// Add an attribute writing rule with specialized converter to the |
| 1218 | 1217 |
/// writer. |
| 1219 | 1218 |
template <typename Value, typename Converter> |
| 1220 | 1219 |
GraphWriter& attribute(const std::string& caption, const Value& value, |
| 1221 | 1220 |
const Converter& converter = Converter()) {
|
| 1222 | 1221 |
_writer_bits::ValueStorageBase* storage = |
| 1223 | 1222 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
| 1224 | 1223 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1225 | 1224 |
return *this; |
| 1226 | 1225 |
} |
| 1227 | 1226 |
|
| 1228 | 1227 |
/// \brief Node writing rule |
| 1229 | 1228 |
/// |
| 1230 | 1229 |
/// Add a node writing rule to the writer. |
| 1231 | 1230 |
GraphWriter& node(const std::string& caption, const Node& node) {
|
| 1232 | 1231 |
typedef _writer_bits::MapLookUpConverter<Node> Converter; |
| 1233 | 1232 |
Converter converter(_node_index); |
| 1234 | 1233 |
_writer_bits::ValueStorageBase* storage = |
| 1235 | 1234 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
| 1236 | 1235 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1237 | 1236 |
return *this; |
| 1238 | 1237 |
} |
| 1239 | 1238 |
|
| 1240 | 1239 |
/// \brief Edge writing rule |
| 1241 | 1240 |
/// |
| 1242 | 1241 |
/// Add an edge writing rule to writer. |
| 1243 | 1242 |
GraphWriter& edge(const std::string& caption, const Edge& edge) {
|
| 1244 | 1243 |
typedef _writer_bits::MapLookUpConverter<Edge> Converter; |
| 1245 | 1244 |
Converter converter(_edge_index); |
| 1246 | 1245 |
_writer_bits::ValueStorageBase* storage = |
| 1247 | 1246 |
new _writer_bits::ValueStorage<Edge, Converter>(edge, converter); |
| 1248 | 1247 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1249 | 1248 |
return *this; |
| 1250 | 1249 |
} |
| 1251 | 1250 |
|
| 1252 | 1251 |
/// \brief Arc writing rule |
| 1253 | 1252 |
/// |
| 1254 | 1253 |
/// Add an arc writing rule to writer. |
| 1255 | 1254 |
GraphWriter& arc(const std::string& caption, const Arc& arc) {
|
| 1256 | 1255 |
typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter; |
| 1257 | 1256 |
Converter converter(_graph, _edge_index); |
| 1258 | 1257 |
_writer_bits::ValueStorageBase* storage = |
| 1259 | 1258 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 1260 | 1259 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1261 | 1260 |
return *this; |
| 1262 | 1261 |
} |
| 1263 | 1262 |
|
| 1264 | 1263 |
/// \name Section captions |
| 1265 | 1264 |
/// @{
|
| 1266 | 1265 |
|
| 1267 | 1266 |
/// \brief Add an additional caption to the \c \@nodes section |
| 1268 | 1267 |
/// |
| 1269 | 1268 |
/// Add an additional caption to the \c \@nodes section. |
| 1270 | 1269 |
GraphWriter& nodes(const std::string& caption) {
|
| 1271 | 1270 |
_nodes_caption = caption; |
| 1272 | 1271 |
return *this; |
| 1273 | 1272 |
} |
| 1274 | 1273 |
|
| 1275 | 1274 |
/// \brief Add an additional caption to the \c \@arcs section |
| 1276 | 1275 |
/// |
| 1277 | 1276 |
/// Add an additional caption to the \c \@arcs section. |
| 1278 | 1277 |
GraphWriter& edges(const std::string& caption) {
|
| 1279 | 1278 |
_edges_caption = caption; |
| 1280 | 1279 |
return *this; |
| 1281 | 1280 |
} |
| 1282 | 1281 |
|
| 1283 | 1282 |
/// \brief Add an additional caption to the \c \@attributes section |
| 1284 | 1283 |
/// |
| 1285 | 1284 |
/// Add an additional caption to the \c \@attributes section. |
| 1286 | 1285 |
GraphWriter& attributes(const std::string& caption) {
|
| 1287 | 1286 |
_attributes_caption = caption; |
| 1288 | 1287 |
return *this; |
| 1289 | 1288 |
} |
| 1290 | 1289 |
|
| 1291 | 1290 |
/// \name Skipping section |
| 1292 | 1291 |
/// @{
|
| 1293 | 1292 |
|
| 1294 | 1293 |
/// \brief Skip writing the node set |
| 1295 | 1294 |
/// |
| 1296 | 1295 |
/// The \c \@nodes section will not be written to the stream. |
| 1297 | 1296 |
GraphWriter& skipNodes() {
|
| 1298 | 1297 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
| 1299 | 1298 |
_skip_nodes = true; |
| 1300 | 1299 |
return *this; |
| 1301 | 1300 |
} |
| 1302 | 1301 |
|
| 1303 | 1302 |
/// \brief Skip writing edge set |
| 1304 | 1303 |
/// |
| 1305 | 1304 |
/// The \c \@edges section will not be written to the stream. |
| 1306 | 1305 |
GraphWriter& skipEdges() {
|
| 1307 | 1306 |
LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member"); |
| 1308 | 1307 |
_skip_edges = true; |
| 1309 | 1308 |
return *this; |
| 1310 | 1309 |
} |
| 1311 | 1310 |
|
| 1312 | 1311 |
/// @} |
| 1313 | 1312 |
|
| 1314 | 1313 |
private: |
| 1315 | 1314 |
|
| 1316 | 1315 |
void writeNodes() {
|
| 1317 | 1316 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 1318 | 1317 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1319 | 1318 |
it != _node_maps.end(); ++it) {
|
| 1320 | 1319 |
if (it->first == "label") {
|
| 1321 | 1320 |
label = it->second; |
| 1322 | 1321 |
break; |
| 1323 | 1322 |
} |
| 1324 | 1323 |
} |
| 1325 | 1324 |
|
| 1326 | 1325 |
*_os << "@nodes"; |
| 1327 | 1326 |
if (!_nodes_caption.empty()) {
|
| 1328 | 1327 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption); |
| 1329 | 1328 |
} |
| 1330 | 1329 |
*_os << std::endl; |
| 1331 | 1330 |
|
| 1332 | 1331 |
if (label == 0) {
|
| 1333 | 1332 |
*_os << "label" << '\t'; |
| 1334 | 1333 |
} |
| 1335 | 1334 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1336 | 1335 |
it != _node_maps.end(); ++it) {
|
| 1337 | 1336 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 1338 | 1337 |
} |
| 1339 | 1338 |
*_os << std::endl; |
| 1340 | 1339 |
|
| 1341 | 1340 |
std::vector<Node> nodes; |
| 1342 | 1341 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1343 | 1342 |
nodes.push_back(n); |
| 1344 | 1343 |
} |
| 1345 | 1344 |
|
| 1346 | 1345 |
if (label == 0) {
|
| 1347 | 1346 |
IdMap<Graph, Node> id_map(_graph); |
| 1348 | 1347 |
_writer_bits::MapLess<IdMap<Graph, Node> > id_less(id_map); |
| 1349 | 1348 |
std::sort(nodes.begin(), nodes.end(), id_less); |
| 1350 | 1349 |
} else {
|
| 1351 | 1350 |
label->sort(nodes); |
| 1352 | 1351 |
} |
| 1353 | 1352 |
|
| 1354 | 1353 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
|
| 1355 | 1354 |
Node n = nodes[i]; |
| 1356 | 1355 |
if (label == 0) {
|
| 1357 | 1356 |
std::ostringstream os; |
| 1358 | 1357 |
os << _graph.id(n); |
| 1359 | 1358 |
_writer_bits::writeToken(*_os, os.str()); |
| 1360 | 1359 |
*_os << '\t'; |
| 1361 | 1360 |
_node_index.insert(std::make_pair(n, os.str())); |
| 1362 | 1361 |
} |
| 1363 | 1362 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1364 | 1363 |
it != _node_maps.end(); ++it) {
|
| 1365 | 1364 |
std::string value = it->second->get(n); |
| 1366 | 1365 |
_writer_bits::writeToken(*_os, value); |
| 1367 | 1366 |
if (it->first == "label") {
|
| 1368 | 1367 |
_node_index.insert(std::make_pair(n, value)); |
| 1369 | 1368 |
} |
| 1370 | 1369 |
*_os << '\t'; |
| 1371 | 1370 |
} |
| 1372 | 1371 |
*_os << std::endl; |
| 1373 | 1372 |
} |
| 1374 | 1373 |
} |
| 1375 | 1374 |
|
| 1376 | 1375 |
void createNodeIndex() {
|
| 1377 | 1376 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 1378 | 1377 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1379 | 1378 |
it != _node_maps.end(); ++it) {
|
| 1380 | 1379 |
if (it->first == "label") {
|
| 1381 | 1380 |
label = it->second; |
| 1382 | 1381 |
break; |
| 1383 | 1382 |
} |
| 1384 | 1383 |
} |
| 1385 | 1384 |
|
| 1386 | 1385 |
if (label == 0) {
|
| 1387 | 1386 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1388 | 1387 |
std::ostringstream os; |
| 1389 | 1388 |
os << _graph.id(n); |
| 1390 | 1389 |
_node_index.insert(std::make_pair(n, os.str())); |
| 1391 | 1390 |
} |
| 1392 | 1391 |
} else {
|
| 1393 | 1392 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1394 | 1393 |
std::string value = label->get(n); |
| 1395 | 1394 |
_node_index.insert(std::make_pair(n, value)); |
| 1396 | 1395 |
} |
| 1397 | 1396 |
} |
| 1398 | 1397 |
} |
| 1399 | 1398 |
|
| 1400 | 1399 |
void writeEdges() {
|
| 1401 | 1400 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
| 1402 | 1401 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1403 | 1402 |
it != _edge_maps.end(); ++it) {
|
| 1404 | 1403 |
if (it->first == "label") {
|
| 1405 | 1404 |
label = it->second; |
| 1406 | 1405 |
break; |
| 1407 | 1406 |
} |
| 1408 | 1407 |
} |
| 1409 | 1408 |
|
| 1410 | 1409 |
*_os << "@edges"; |
| 1411 | 1410 |
if (!_edges_caption.empty()) {
|
| 1412 | 1411 |
_writer_bits::writeToken(*_os << ' ', _edges_caption); |
| 1413 | 1412 |
} |
| 1414 | 1413 |
*_os << std::endl; |
| 1415 | 1414 |
|
| 1416 | 1415 |
*_os << '\t' << '\t'; |
| 1417 | 1416 |
if (label == 0) {
|
| 1418 | 1417 |
*_os << "label" << '\t'; |
| 1419 | 1418 |
} |
| 1420 | 1419 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1421 | 1420 |
it != _edge_maps.end(); ++it) {
|
| 1422 | 1421 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 1423 | 1422 |
} |
| 1424 | 1423 |
*_os << std::endl; |
| 1425 | 1424 |
|
| 1426 | 1425 |
std::vector<Edge> edges; |
| 1427 | 1426 |
for (EdgeIt n(_graph); n != INVALID; ++n) {
|
| 1428 | 1427 |
edges.push_back(n); |
| 1429 | 1428 |
} |
| 1430 | 1429 |
|
| 1431 | 1430 |
if (label == 0) {
|
| 1432 | 1431 |
IdMap<Graph, Edge> id_map(_graph); |
| 1433 | 1432 |
_writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map); |
| 1434 | 1433 |
std::sort(edges.begin(), edges.end(), id_less); |
| 1435 | 1434 |
} else {
|
| 1436 | 1435 |
label->sort(edges); |
| 1437 | 1436 |
} |
| 1438 | 1437 |
|
| 1439 | 1438 |
for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
|
| 1440 | 1439 |
Edge e = edges[i]; |
| 1441 | 1440 |
_writer_bits::writeToken(*_os, _node_index. |
| 1442 | 1441 |
find(_graph.u(e))->second); |
| 1443 | 1442 |
*_os << '\t'; |
| 1444 | 1443 |
_writer_bits::writeToken(*_os, _node_index. |
| 1445 | 1444 |
find(_graph.v(e))->second); |
| 1446 | 1445 |
*_os << '\t'; |
| 1447 | 1446 |
if (label == 0) {
|
| 1448 | 1447 |
std::ostringstream os; |
| 1449 | 1448 |
os << _graph.id(e); |
| 1450 | 1449 |
_writer_bits::writeToken(*_os, os.str()); |
| 1451 | 1450 |
*_os << '\t'; |
| 1452 | 1451 |
_edge_index.insert(std::make_pair(e, os.str())); |
| 1453 | 1452 |
} |
| 1454 | 1453 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1455 | 1454 |
it != _edge_maps.end(); ++it) {
|
| 1456 | 1455 |
std::string value = it->second->get(e); |
| 1457 | 1456 |
_writer_bits::writeToken(*_os, value); |
| 1458 | 1457 |
if (it->first == "label") {
|
| 1459 | 1458 |
_edge_index.insert(std::make_pair(e, value)); |
| 1460 | 1459 |
} |
| 1461 | 1460 |
*_os << '\t'; |
| 1462 | 1461 |
} |
| 1463 | 1462 |
*_os << std::endl; |
| 1464 | 1463 |
} |
| 1465 | 1464 |
} |
| 1466 | 1465 |
|
| 1467 | 1466 |
void createEdgeIndex() {
|
| 1468 | 1467 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
| 1469 | 1468 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1470 | 1469 |
it != _edge_maps.end(); ++it) {
|
| 1471 | 1470 |
if (it->first == "label") {
|
| 1472 | 1471 |
label = it->second; |
| 1473 | 1472 |
break; |
| 1474 | 1473 |
} |
| 1475 | 1474 |
} |
| 1476 | 1475 |
|
| 1477 | 1476 |
if (label == 0) {
|
| 1478 | 1477 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1479 | 1478 |
std::ostringstream os; |
| 1480 | 1479 |
os << _graph.id(e); |
| 1481 | 1480 |
_edge_index.insert(std::make_pair(e, os.str())); |
| 1482 | 1481 |
} |
| 1483 | 1482 |
} else {
|
| 1484 | 1483 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1485 | 1484 |
std::string value = label->get(e); |
| 1486 | 1485 |
_edge_index.insert(std::make_pair(e, value)); |
| 1487 | 1486 |
} |
| 1488 | 1487 |
} |
| 1489 | 1488 |
} |
| 1490 | 1489 |
|
| 1491 | 1490 |
void writeAttributes() {
|
| 1492 | 1491 |
if (_attributes.empty()) return; |
| 1493 | 1492 |
*_os << "@attributes"; |
| 1494 | 1493 |
if (!_attributes_caption.empty()) {
|
| 1495 | 1494 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
| 1496 | 1495 |
} |
| 1497 | 1496 |
*_os << std::endl; |
| 1498 | 1497 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1499 | 1498 |
it != _attributes.end(); ++it) {
|
| 1500 | 1499 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
| 1501 | 1500 |
_writer_bits::writeToken(*_os, it->second->get()); |
| 1502 | 1501 |
*_os << std::endl; |
| 1503 | 1502 |
} |
| 1504 | 1503 |
} |
| 1505 | 1504 |
|
| 1506 | 1505 |
public: |
| 1507 | 1506 |
|
| 1508 | 1507 |
/// \name Execution of the writer |
| 1509 | 1508 |
/// @{
|
| 1510 | 1509 |
|
| 1511 | 1510 |
/// \brief Start the batch processing |
| 1512 | 1511 |
/// |
| 1513 | 1512 |
/// This function starts the batch processing. |
| 1514 | 1513 |
void run() {
|
| 1515 | 1514 |
if (!_skip_nodes) {
|
| 1516 | 1515 |
writeNodes(); |
| 1517 | 1516 |
} else {
|
| 1518 | 1517 |
createNodeIndex(); |
| 1519 | 1518 |
} |
| 1520 | 1519 |
if (!_skip_edges) {
|
| 1521 | 1520 |
writeEdges(); |
| 1522 | 1521 |
} else {
|
| 1523 | 1522 |
createEdgeIndex(); |
| 1524 | 1523 |
} |
| 1525 | 1524 |
writeAttributes(); |
| 1526 | 1525 |
} |
| 1527 | 1526 |
|
| 1528 | 1527 |
/// \brief Give back the stream of the writer |
| 1529 | 1528 |
/// |
| 1530 | 1529 |
/// Give back the stream of the writer |
| 1531 | 1530 |
std::ostream& ostream() {
|
| 1532 | 1531 |
return *_os; |
| 1533 | 1532 |
} |
| 1534 | 1533 |
|
| 1535 | 1534 |
/// @} |
| 1536 | 1535 |
}; |
| 1537 | 1536 |
|
| 1538 | 1537 |
class SectionWriter; |
| 1539 | 1538 |
|
| 1540 | 1539 |
SectionWriter sectionWriter(std::istream& is); |
| 1541 | 1540 |
SectionWriter sectionWriter(const std::string& fn); |
| 1542 | 1541 |
SectionWriter sectionWriter(const char* fn); |
| 1543 | 1542 |
|
| 1544 | 1543 |
/// \ingroup lemon_io |
| 1545 | 1544 |
/// |
| 1546 | 1545 |
/// \brief Section writer class |
| 1547 | 1546 |
/// |
| 1548 | 1547 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
| 1549 | 1548 |
/// which contain any data in arbitrary format. Such sections can be |
| 1550 | 1549 |
/// written with this class. A writing rule can be added to the |
| 1551 | 1550 |
/// class with two different functions. With the \c sectionLines() |
| 1552 | 1551 |
/// function a generator can write the section line-by-line, while |
| 1553 | 1552 |
/// with the \c sectionStream() member the section can be written to |
| 1554 | 1553 |
/// an output stream. |
| 1555 | 1554 |
class SectionWriter {
|
| 1556 | 1555 |
private: |
| 1557 | 1556 |
|
| 1558 | 1557 |
std::ostream* _os; |
| 1559 | 1558 |
bool local_os; |
| 1560 | 1559 |
|
| 1561 | 1560 |
typedef std::vector<std::pair<std::string, _writer_bits::Section*> > |
| 1562 | 1561 |
Sections; |
| 1563 | 1562 |
|
| 1564 | 1563 |
Sections _sections; |
| 1565 | 1564 |
|
| 1566 | 1565 |
public: |
| 1567 | 1566 |
|
| 1568 | 1567 |
/// \brief Constructor |
| 1569 | 1568 |
/// |
| 1570 | 1569 |
/// Construct a section writer, which writes to the given output |
| 1571 | 1570 |
/// stream. |
| 1572 | 1571 |
SectionWriter(std::ostream& os) |
| 1573 | 1572 |
: _os(&os), local_os(false) {}
|
| 1574 | 1573 |
|
| 1575 | 1574 |
/// \brief Constructor |
| 1576 | 1575 |
/// |
| 1577 | 1576 |
/// Construct a section writer, which writes into the given file. |
| 1578 | 1577 |
SectionWriter(const std::string& fn) |
| 1579 | 1578 |
: _os(new std::ofstream(fn.c_str())), local_os(true) {
|
| 1580 | 1579 |
if (!(*_os)) {
|
| 1581 | 1580 |
delete _os; |
| 1582 | 1581 |
throw IoError("Cannot write file", fn);
|
| 1583 | 1582 |
} |
| 1584 | 1583 |
} |
| 1585 | 1584 |
|
| 1586 | 1585 |
/// \brief Constructor |
| 1587 | 1586 |
/// |
| 1588 | 1587 |
/// Construct a section writer, which writes into the given file. |
| 1589 | 1588 |
SectionWriter(const char* fn) |
| 1590 | 1589 |
: _os(new std::ofstream(fn)), local_os(true) {
|
| 1591 | 1590 |
if (!(*_os)) {
|
| 1592 | 1591 |
delete _os; |
| 1593 | 1592 |
throw IoError("Cannot write file", fn);
|
| 1594 | 1593 |
} |
| 1595 | 1594 |
} |
| 1596 | 1595 |
|
| 1597 | 1596 |
/// \brief Destructor |
| 1598 | 1597 |
~SectionWriter() {
|
| 1599 | 1598 |
for (Sections::iterator it = _sections.begin(); |
| 1600 | 1599 |
it != _sections.end(); ++it) {
|
| 1601 | 1600 |
delete it->second; |
| 1602 | 1601 |
} |
| 1603 | 1602 |
|
| 1604 | 1603 |
if (local_os) {
|
| 1605 | 1604 |
delete _os; |
| 1606 | 1605 |
} |
| 1607 | 1606 |
|
| 1608 | 1607 |
} |
| 1609 | 1608 |
|
| 1610 | 1609 |
private: |
| 1611 | 1610 |
|
| 1612 | 1611 |
friend SectionWriter sectionWriter(std::ostream& os); |
| 1613 | 1612 |
friend SectionWriter sectionWriter(const std::string& fn); |
| 1614 | 1613 |
friend SectionWriter sectionWriter(const char* fn); |
| 1615 | 1614 |
|
| 1616 | 1615 |
SectionWriter(SectionWriter& other) |
| 1617 | 1616 |
: _os(other._os), local_os(other.local_os) {
|
| 1618 | 1617 |
|
| 1619 | 1618 |
other._os = 0; |
| 1620 | 1619 |
other.local_os = false; |
| 1621 | 1620 |
|
| 1622 | 1621 |
_sections.swap(other._sections); |
| 1623 | 1622 |
} |
| 1624 | 1623 |
|
| 1625 | 1624 |
SectionWriter& operator=(const SectionWriter&); |
| 1626 | 1625 |
|
| 1627 | 1626 |
public: |
| 1628 | 1627 |
|
| 1629 | 1628 |
/// \name Section writers |
| 1630 | 1629 |
/// @{
|
| 1631 | 1630 |
|
| 1632 | 1631 |
/// \brief Add a section writer with line oriented writing |
| 1633 | 1632 |
/// |
| 1634 | 1633 |
/// The first parameter is the type descriptor of the section, the |
| 1635 | 1634 |
/// second is a generator with std::string values. At the writing |
| 1636 | 1635 |
/// process, the returned \c std::string will be written into the |
| 1637 | 1636 |
/// output file until it is an empty string. |
| 1638 | 1637 |
/// |
| 1639 | 1638 |
/// For example, an integer vector is written into a section. |
| 1640 | 1639 |
///\code |
| 1641 | 1640 |
/// @numbers |
| 1642 | 1641 |
/// 12 45 23 78 |
| 1643 | 1642 |
/// 4 28 38 28 |
| 1644 | 1643 |
/// 23 6 16 |
| 1645 | 1644 |
///\endcode |
| 1646 | 1645 |
/// |
| 1647 | 1646 |
/// The generator is implemented as a struct. |
| 1648 | 1647 |
///\code |
| 1649 | 1648 |
/// struct NumberSection {
|
| 1650 | 1649 |
/// std::vector<int>::const_iterator _it, _end; |
| 1651 | 1650 |
/// NumberSection(const std::vector<int>& data) |
| 1652 | 1651 |
/// : _it(data.begin()), _end(data.end()) {}
|
| 1653 | 1652 |
/// std::string operator()() {
|
| 1654 | 1653 |
/// int rem_in_line = 4; |
| 1655 | 1654 |
/// std::ostringstream ls; |
| 1656 | 1655 |
/// while (rem_in_line > 0 && _it != _end) {
|
| 1657 | 1656 |
/// ls << *(_it++) << ' '; |
| 1658 | 1657 |
/// --rem_in_line; |
| 1659 | 1658 |
/// } |
| 1660 | 1659 |
/// return ls.str(); |
| 1661 | 1660 |
/// } |
| 1662 | 1661 |
/// }; |
| 1663 | 1662 |
/// |
| 1664 | 1663 |
/// // ... |
| 1665 | 1664 |
/// |
| 1666 | 1665 |
/// writer.sectionLines("numbers", NumberSection(vec));
|
| 1667 | 1666 |
///\endcode |
| 1668 | 1667 |
template <typename Functor> |
| 1669 | 1668 |
SectionWriter& sectionLines(const std::string& type, Functor functor) {
|
| 1670 | 1669 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 1671 | 1670 |
_sections.push_back(std::make_pair(type, |
| 1672 | 1671 |
new _writer_bits::LineSection<Functor>(functor))); |
| 1673 | 1672 |
return *this; |
| 1674 | 1673 |
} |
| 1675 | 1674 |
|
| 1676 | 1675 |
|
| 1677 | 1676 |
/// \brief Add a section writer with stream oriented writing |
| 1678 | 1677 |
/// |
| 1679 | 1678 |
/// The first parameter is the type of the section, the second is |
| 1680 | 1679 |
/// a functor, which takes a \c std::ostream& parameter. The |
| 1681 | 1680 |
/// functor writes the section to the output stream. |
| 1682 | 1681 |
/// \warning The last line must be closed with end-line character. |
| 1683 | 1682 |
template <typename Functor> |
| 1684 | 1683 |
SectionWriter& sectionStream(const std::string& type, Functor functor) {
|
| 1685 | 1684 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 1686 | 1685 |
_sections.push_back(std::make_pair(type, |
| 1687 | 1686 |
new _writer_bits::StreamSection<Functor>(functor))); |
| 1688 | 1687 |
return *this; |
| 1689 | 1688 |
} |
| 1690 | 1689 |
|
| 1691 | 1690 |
/// @} |
| 1692 | 1691 |
|
| 1693 | 1692 |
public: |
| 1694 | 1693 |
|
| 1695 | 1694 |
|
| 1696 | 1695 |
/// \name Execution of the writer |
| 1697 | 1696 |
/// @{
|
| 1698 | 1697 |
|
| 1699 | 1698 |
/// \brief Start the batch processing |
| 1700 | 1699 |
/// |
| 1701 | 1700 |
/// This function starts the batch processing. |
| 1702 | 1701 |
void run() {
|
| 1703 | 1702 |
|
| 1704 | 1703 |
LEMON_ASSERT(_os != 0, "This writer is assigned to an other writer"); |
| 1705 | 1704 |
|
| 1706 | 1705 |
for (Sections::iterator it = _sections.begin(); |
| 1707 | 1706 |
it != _sections.end(); ++it) {
|
| 1708 | 1707 |
(*_os) << '@' << it->first << std::endl; |
| 1709 | 1708 |
it->second->process(*_os); |
| 1710 | 1709 |
} |
| 1711 | 1710 |
} |
| 1712 | 1711 |
|
| 1713 | 1712 |
/// \brief Give back the stream of the writer |
| 1714 | 1713 |
/// |
| 1715 | 1714 |
/// Returns the stream of the writer |
| 1716 | 1715 |
std::ostream& ostream() {
|
| 1717 | 1716 |
return *_os; |
| 1718 | 1717 |
} |
| 1719 | 1718 |
|
| 1720 | 1719 |
/// @} |
| 1721 | 1720 |
|
| 1722 | 1721 |
}; |
| 1723 | 1722 |
|
| 1724 | 1723 |
/// \brief Return a \ref SectionWriter class |
| 1725 | 1724 |
/// |
| 1726 | 1725 |
/// This function just returns a \ref SectionWriter class. |
| 1727 | 1726 |
/// \relates SectionWriter |
| 1728 | 1727 |
inline SectionWriter sectionWriter(std::ostream& os) {
|
| 1729 | 1728 |
SectionWriter tmp(os); |
| 1730 | 1729 |
return tmp; |
| 1731 | 1730 |
} |
| 1732 | 1731 |
|
| 1733 | 1732 |
/// \brief Return a \ref SectionWriter class |
| 1734 | 1733 |
/// |
| 1735 | 1734 |
/// This function just returns a \ref SectionWriter class. |
| 1736 | 1735 |
/// \relates SectionWriter |
| 1737 | 1736 |
inline SectionWriter sectionWriter(const std::string& fn) {
|
| 1738 | 1737 |
SectionWriter tmp(fn); |
| 1739 | 1738 |
return tmp; |
| 1740 | 1739 |
} |
| 1741 | 1740 |
|
| 1742 | 1741 |
/// \brief Return a \ref SectionWriter class |
| 1743 | 1742 |
/// |
| 1744 | 1743 |
/// This function just returns a \ref SectionWriter class. |
| 1745 | 1744 |
/// \relates SectionWriter |
| 1746 | 1745 |
inline SectionWriter sectionWriter(const char* fn) {
|
| 1747 | 1746 |
SectionWriter tmp(fn); |
| 1748 | 1747 |
return tmp; |
| 1749 | 1748 |
} |
| 1750 | 1749 |
} |
| 1751 | 1750 |
|
| 1752 | 1751 |
#endif |
0 comments (0 inline)