lemon/path_utils.h
changeset 2335 27aa03cd3121
child 2350 eb371753e814
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/path_utils.h	Mon Jan 08 10:39:59 2007 +0000
     1.3 @@ -0,0 +1,140 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2006
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +
    1.23 +///\ingroup paths
    1.24 +///\file
    1.25 +///\brief Classes for representing paths in graphs.
    1.26 +///
    1.27 +
    1.28 +#ifndef LEMON_PATH_UTILS_H
    1.29 +#define LEMON_PATH_UTILS_H
    1.30 +
    1.31 +#include <lemon/concepts/path.h>
    1.32 +
    1.33 +namespace lemon {
    1.34 +
    1.35 +  namespace _path_bits {
    1.36 +
    1.37 +    template <typename Path, typename Enable = void>
    1.38 +    struct RevTagIndicator {
    1.39 +      static const bool value = false;
    1.40 +    };
    1.41 +
    1.42 +    template <typename Graph>
    1.43 +    struct RevTagIndicator<
    1.44 +      Graph, 
    1.45 +      typename enable_if<typename Graph::RevTag, void>::type
    1.46 +    > {
    1.47 +      static const bool value = true;
    1.48 +    };
    1.49 +
    1.50 +    template <typename Target, typename Source,
    1.51 +              typename BuildEnable = void, typename RevEnable = void>
    1.52 +    struct PathCopySelector {
    1.53 +      static void copy(Target& target, const Source& source) {
    1.54 +        source.clear();
    1.55 +        for (typename Source::EdgeIt it(source); it != INVALID; ++it) {
    1.56 +          target.addBack(it);
    1.57 +        }
    1.58 +      }
    1.59 +    };
    1.60 +
    1.61 +    template <typename Target, typename Source, typename BuildEnable>
    1.62 +    struct PathCopySelector<
    1.63 +      Target, Source, BuildEnable, 
    1.64 +      typename enable_if<typename Source::RevPathTag, void>::type> {
    1.65 +      static void copy(Target& target, const Source& source) {
    1.66 +        source.clear();
    1.67 +        for (typename Source::RevIt it(source); it != INVALID; ++it) {
    1.68 +          target.addFront(it);
    1.69 +        }
    1.70 +      }
    1.71 +    };
    1.72 +
    1.73 +    template <typename Target, typename Source, typename RevEnable>
    1.74 +    struct PathCopySelector<
    1.75 +      Target, Source, 
    1.76 +      typename enable_if<typename Target::BuildTag, void>::type, RevEnable> {
    1.77 +      static void copy(Target& target, const Source& source) {
    1.78 +        target.clear();
    1.79 +        target.build(source);
    1.80 +      }
    1.81 +    };
    1.82 +
    1.83 +    template <typename Target, typename Source>
    1.84 +    struct PathCopySelector<
    1.85 +      Target, Source, 
    1.86 +      typename enable_if<typename Target::BuildTag, void>::type,
    1.87 +      typename enable_if<typename Source::RevPathTag, void>::type> {
    1.88 +      static void copy(Target& target, const Source& source) {
    1.89 +        target.clear();
    1.90 +        target.buildRev(source);
    1.91 +      }
    1.92 +    };
    1.93 +
    1.94 +  }
    1.95 +
    1.96 +
    1.97 +  /// \brief Make of copy of a path.
    1.98 +  ///
    1.99 +  ///  Make of copy of a path.
   1.100 +  template <typename Target, typename Source>
   1.101 +  void copyPath(Target& target, const Source& source) {
   1.102 +    checkConcept<concepts::PathDumper<typename Source::Graph>, Source>();
   1.103 +    _path_bits::PathCopySelector<Target, Source>::copy(target, source);
   1.104 +  }
   1.105 +
   1.106 +  /// \brief Checks the path's consistency.
   1.107 +  ///
   1.108 +  /// Checks that each edge's target is the next's source. 
   1.109 +  /// \Checks the path's consistency.
   1.110 +  ///
   1.111 +  /// Checks that each edge's target is the next's source. 
   1.112 +  template <typename Graph, typename Path>
   1.113 +  bool checkPath(const Graph& graph, const Path& path) {
   1.114 +    typename Path::EdgeIt it(path);
   1.115 +    if (it == INVALID) return true;
   1.116 +    typename Graph::Node node = graph.target(it);
   1.117 +    ++it;
   1.118 +    while (it != INVALID) {
   1.119 +      if (graph.source(it) != node) return false;
   1.120 +      node = graph.target(it);
   1.121 +      ++it;
   1.122 +    }
   1.123 +    return true;
   1.124 +  }
   1.125 +
   1.126 +  /// \brief Gives back the source of the path
   1.127 +  ///
   1.128 +  /// Gives back the source of the path.
   1.129 +  template <typename Graph, typename Path>
   1.130 +  typename Graph::Node pathSource(const Graph& graph, const Path& path) {
   1.131 +    return graph.source(path.front());
   1.132 +  }
   1.133 +
   1.134 +  /// \brief Gives back the target of the path
   1.135 +  ///
   1.136 +  /// Gives back the target of the path.
   1.137 +  template <typename Graph, typename Path>
   1.138 +  typename Graph::Node pathTarget(const Graph& graph, const Path& path) {
   1.139 +    return graph.target(path.back());
   1.140 +  }
   1.141 +}
   1.142 +
   1.143 +#endif