3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
22 ///\brief Classes for representing paths in graphs.
25 #ifndef LEMON_PATH_UTILS_H
26 #define LEMON_PATH_UTILS_H
28 #include <lemon/concepts/path.h>
32 namespace _path_bits {
34 template <typename Path, typename Enable = void>
35 struct RevTagIndicator {
36 static const bool value = false;
39 template <typename Graph>
40 struct RevTagIndicator<
42 typename enable_if<typename Graph::RevTag, void>::type
44 static const bool value = true;
47 template <typename Target, typename Source,
48 typename BuildEnable = void, typename RevEnable = void>
49 struct PathCopySelector {
50 static void copy(Target& target, const Source& source) {
52 for (typename Source::EdgeIt it(source); it != INVALID; ++it) {
58 template <typename Target, typename Source, typename BuildEnable>
59 struct PathCopySelector<
60 Target, Source, BuildEnable,
61 typename enable_if<typename Source::RevPathTag, void>::type> {
62 static void copy(Target& target, const Source& source) {
64 for (typename Source::RevEdgeIt it(source); it != INVALID; ++it) {
70 template <typename Target, typename Source, typename RevEnable>
71 struct PathCopySelector<
73 typename enable_if<typename Target::BuildTag, void>::type, RevEnable> {
74 static void copy(Target& target, const Source& source) {
80 template <typename Target, typename Source>
81 struct PathCopySelector<
83 typename enable_if<typename Target::BuildTag, void>::type,
84 typename enable_if<typename Source::RevPathTag, void>::type> {
85 static void copy(Target& target, const Source& source) {
87 target.buildRev(source);
94 /// \brief Make of copy of a path.
96 /// Make of copy of a path.
97 template <typename Target, typename Source>
98 void copyPath(Target& target, const Source& source) {
99 checkConcept<concepts::PathDumper<typename Source::Graph>, Source>();
100 _path_bits::PathCopySelector<Target, Source>::copy(target, source);
103 /// \brief Checks the path's consistency.
105 /// Checks that each edge's target is the next's source.
107 template <typename Graph, typename Path>
108 bool checkPath(const Graph& graph, const Path& path) {
109 typename Path::EdgeIt it(path);
110 if (it == INVALID) return true;
111 typename Graph::Node node = graph.target(it);
113 while (it != INVALID) {
114 if (graph.source(it) != node) return false;
115 node = graph.target(it);
121 /// \brief Gives back the source of the path
123 /// Gives back the source of the path.
124 template <typename Graph, typename Path>
125 typename Graph::Node pathSource(const Graph& graph, const Path& path) {
126 return graph.source(path.front());
129 /// \brief Gives back the target of the path
131 /// Gives back the target of the path.
132 template <typename Graph, typename Path>
133 typename Graph::Node pathTarget(const Graph& graph, const Path& path) {
134 return graph.target(path.back());