3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
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
21 ///\brief Classes for representing paths in digraphs.
24 #ifndef LEMON_PATH_UTILS_H
25 #define LEMON_PATH_UTILS_H
27 #include <lemon/concepts/path.h>
31 namespace _path_bits {
33 template <typename Path, typename Enable = void>
34 struct RevTagIndicator {
35 static const bool value = false;
38 template <typename Digraph>
39 struct RevTagIndicator<
41 typename enable_if<typename Digraph::RevTag, void>::type
43 static const bool value = true;
46 template <typename Target, typename Source,
47 typename BuildEnable = void, typename RevEnable = void>
48 struct PathCopySelector {
49 static void copy(Target& target, const Source& source) {
51 for (typename Source::ArcIt it(source); it != INVALID; ++it) {
57 template <typename Target, typename Source, typename BuildEnable>
58 struct PathCopySelector<
59 Target, Source, BuildEnable,
60 typename enable_if<typename Source::RevPathTag, void>::type> {
61 static void copy(Target& target, const Source& source) {
63 for (typename Source::RevArcIt it(source); it != INVALID; ++it) {
69 template <typename Target, typename Source, typename RevEnable>
70 struct PathCopySelector<
72 typename enable_if<typename Target::BuildTag, void>::type, RevEnable> {
73 static void copy(Target& target, const Source& source) {
79 template <typename Target, typename Source>
80 struct PathCopySelector<
82 typename enable_if<typename Target::BuildTag, void>::type,
83 typename enable_if<typename Source::RevPathTag, void>::type> {
84 static void copy(Target& target, const Source& source) {
86 target.buildRev(source);
93 /// \brief Make a copy of a path.
95 /// This function makes a copy of a path.
96 template <typename Target, typename Source>
97 void copyPath(Target& target, const Source& source) {
98 checkConcept<concepts::PathDumper<typename Source::Digraph>, Source>();
99 _path_bits::PathCopySelector<Target, Source>::copy(target, source);
102 /// \brief Check the consistency of a path.
104 /// This function checks that the target of each arc is the same
105 /// as the source of the next one.
107 template <typename Digraph, typename Path>
108 bool checkPath(const Digraph& digraph, const Path& path) {
109 typename Path::ArcIt it(path);
110 if (it == INVALID) return true;
111 typename Digraph::Node node = digraph.target(it);
113 while (it != INVALID) {
114 if (digraph.source(it) != node) return false;
115 node = digraph.target(it);
121 /// \brief The source of a path
123 /// This function returns the source of the given path.
124 template <typename Digraph, typename Path>
125 typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
126 return digraph.source(path.front());
129 /// \brief The target of a path
131 /// This function returns the target of the given path.
132 template <typename Digraph, typename Path>
133 typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
134 return digraph.target(path.back());
137 /// \brief Class which helps to iterate through the nodes of a path
139 /// In a sense, the path can be treated as a list of arcs. The
140 /// lemon path type stores only this list. As a consequence, it
141 /// cannot enumerate the nodes in the path and the zero length paths
142 /// cannot have a source node.
144 /// This class implements the node iterator of a path structure. To
145 /// provide this feature, the underlying digraph should be passed to
146 /// the constructor of the iterator.
147 template <typename Path>
150 const typename Path::Digraph *_digraph;
151 typename Path::ArcIt _it;
152 typename Path::Digraph::Node _nd;
156 typedef typename Path::Digraph Digraph;
157 typedef typename Digraph::Node Node;
159 /// Default constructor
161 /// Invalid constructor
163 : _digraph(0), _it(INVALID), _nd(INVALID) {}
165 PathNodeIt(const Digraph& digraph, const Path& path)
166 : _digraph(&digraph), _it(path) {
167 _nd = (_it != INVALID ? _digraph->source(_it) : INVALID);
170 PathNodeIt(const Digraph& digraph, const Path& path, const Node& src)
171 : _digraph(&digraph), _it(path), _nd(src) {}
173 ///Conversion to Digraph::Node
174 operator Node() const {
179 PathNodeIt& operator++() {
180 if (_it == INVALID) _nd = INVALID;
182 _nd = _digraph->target(_it);
188 /// Comparison operator
189 bool operator==(const PathNodeIt& n) const {
190 return _it == n._it && _nd == n._nd;
192 /// Comparison operator
193 bool operator!=(const PathNodeIt& n) const {
194 return _it != n._it || _nd != n._nd;
196 /// Comparison operator
197 bool operator<(const PathNodeIt& n) const {
198 return (_it < n._it && _nd != INVALID);