lemon/path_utils.h
author alpar
Sat, 03 Mar 2007 16:30:37 +0000
changeset 2391 14a343be7a5a
parent 2357 5365600a7a5c
child 2467 2025a571895e
permissions -rw-r--r--
Happy New Year to all source files!
deba@2335
     1
/* -*- C++ -*-
deba@2335
     2
 *
deba@2335
     3
 * This file is a part of LEMON, a generic C++ optimization library
deba@2335
     4
 *
alpar@2391
     5
 * Copyright (C) 2003-2007
deba@2335
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2335
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2335
     8
 *
deba@2335
     9
 * Permission to use, modify and distribute this software is granted
deba@2335
    10
 * provided that this copyright notice appears in all copies. For
deba@2335
    11
 * precise terms see the accompanying LICENSE file.
deba@2335
    12
 *
deba@2335
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@2335
    14
 * express or implied, and with no claim as to its suitability for any
deba@2335
    15
 * purpose.
deba@2335
    16
 *
deba@2335
    17
 */
deba@2335
    18
deba@2335
    19
///\ingroup paths
deba@2335
    20
///\file
deba@2335
    21
///\brief Classes for representing paths in graphs.
deba@2335
    22
///
deba@2335
    23
deba@2335
    24
#ifndef LEMON_PATH_UTILS_H
deba@2335
    25
#define LEMON_PATH_UTILS_H
deba@2335
    26
deba@2335
    27
#include <lemon/concepts/path.h>
deba@2335
    28
deba@2335
    29
namespace lemon {
deba@2335
    30
deba@2335
    31
  namespace _path_bits {
deba@2335
    32
deba@2335
    33
    template <typename Path, typename Enable = void>
deba@2335
    34
    struct RevTagIndicator {
deba@2335
    35
      static const bool value = false;
deba@2335
    36
    };
deba@2335
    37
deba@2335
    38
    template <typename Graph>
deba@2335
    39
    struct RevTagIndicator<
deba@2335
    40
      Graph, 
deba@2335
    41
      typename enable_if<typename Graph::RevTag, void>::type
deba@2335
    42
    > {
deba@2335
    43
      static const bool value = true;
deba@2335
    44
    };
deba@2335
    45
deba@2335
    46
    template <typename Target, typename Source,
deba@2335
    47
              typename BuildEnable = void, typename RevEnable = void>
deba@2335
    48
    struct PathCopySelector {
deba@2335
    49
      static void copy(Target& target, const Source& source) {
deba@2357
    50
        target.clear();
deba@2335
    51
        for (typename Source::EdgeIt it(source); it != INVALID; ++it) {
deba@2335
    52
          target.addBack(it);
deba@2335
    53
        }
deba@2335
    54
      }
deba@2335
    55
    };
deba@2335
    56
deba@2335
    57
    template <typename Target, typename Source, typename BuildEnable>
deba@2335
    58
    struct PathCopySelector<
deba@2335
    59
      Target, Source, BuildEnable, 
deba@2335
    60
      typename enable_if<typename Source::RevPathTag, void>::type> {
deba@2335
    61
      static void copy(Target& target, const Source& source) {
deba@2357
    62
        target.clear();
deba@2357
    63
        for (typename Source::RevEdgeIt it(source); it != INVALID; ++it) {
deba@2335
    64
          target.addFront(it);
deba@2335
    65
        }
deba@2335
    66
      }
deba@2335
    67
    };
deba@2335
    68
deba@2335
    69
    template <typename Target, typename Source, typename RevEnable>
deba@2335
    70
    struct PathCopySelector<
deba@2335
    71
      Target, Source, 
deba@2335
    72
      typename enable_if<typename Target::BuildTag, void>::type, RevEnable> {
deba@2335
    73
      static void copy(Target& target, const Source& source) {
deba@2335
    74
        target.clear();
deba@2335
    75
        target.build(source);
deba@2335
    76
      }
deba@2335
    77
    };
deba@2335
    78
deba@2335
    79
    template <typename Target, typename Source>
deba@2335
    80
    struct PathCopySelector<
deba@2335
    81
      Target, Source, 
deba@2335
    82
      typename enable_if<typename Target::BuildTag, void>::type,
deba@2335
    83
      typename enable_if<typename Source::RevPathTag, void>::type> {
deba@2335
    84
      static void copy(Target& target, const Source& source) {
deba@2335
    85
        target.clear();
deba@2335
    86
        target.buildRev(source);
deba@2335
    87
      }
deba@2335
    88
    };
deba@2335
    89
deba@2335
    90
  }
deba@2335
    91
deba@2335
    92
deba@2335
    93
  /// \brief Make of copy of a path.
deba@2335
    94
  ///
deba@2335
    95
  ///  Make of copy of a path.
deba@2335
    96
  template <typename Target, typename Source>
deba@2335
    97
  void copyPath(Target& target, const Source& source) {
deba@2335
    98
    checkConcept<concepts::PathDumper<typename Source::Graph>, Source>();
deba@2335
    99
    _path_bits::PathCopySelector<Target, Source>::copy(target, source);
deba@2335
   100
  }
deba@2335
   101
deba@2335
   102
  /// \brief Checks the path's consistency.
deba@2335
   103
  ///
deba@2335
   104
  /// Checks that each edge's target is the next's source. 
alpar@2350
   105
  /// 
deba@2335
   106
  template <typename Graph, typename Path>
deba@2335
   107
  bool checkPath(const Graph& graph, const Path& path) {
deba@2335
   108
    typename Path::EdgeIt it(path);
deba@2335
   109
    if (it == INVALID) return true;
deba@2335
   110
    typename Graph::Node node = graph.target(it);
deba@2335
   111
    ++it;
deba@2335
   112
    while (it != INVALID) {
deba@2335
   113
      if (graph.source(it) != node) return false;
deba@2335
   114
      node = graph.target(it);
deba@2335
   115
      ++it;
deba@2335
   116
    }
deba@2335
   117
    return true;
deba@2335
   118
  }
deba@2335
   119
deba@2335
   120
  /// \brief Gives back the source of the path
deba@2335
   121
  ///
deba@2335
   122
  /// Gives back the source of the path.
deba@2335
   123
  template <typename Graph, typename Path>
deba@2335
   124
  typename Graph::Node pathSource(const Graph& graph, const Path& path) {
deba@2335
   125
    return graph.source(path.front());
deba@2335
   126
  }
deba@2335
   127
deba@2335
   128
  /// \brief Gives back the target of the path
deba@2335
   129
  ///
deba@2335
   130
  /// Gives back the target of the path.
deba@2335
   131
  template <typename Graph, typename Path>
deba@2335
   132
  typename Graph::Node pathTarget(const Graph& graph, const Path& path) {
deba@2335
   133
    return graph.target(path.back());
deba@2335
   134
  }
deba@2335
   135
}
deba@2335
   136
deba@2335
   137
#endif