lemon/bits/path_dump.h
author alpar
Sat, 03 Mar 2007 12:00:32 +0000
changeset 2388 c6d537888fe5
parent 2335 27aa03cd3121
child 2391 14a343be7a5a
permissions -rw-r--r--
More doc.
deba@2357
     1
deba@2335
     2
/* -*- C++ -*-
deba@2335
     3
 *
deba@2335
     4
 * This file is a part of LEMON, a generic C++ optimization library
deba@2335
     5
 *
deba@2335
     6
 * Copyright (C) 2003-2006
deba@2335
     7
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@2335
     8
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@2335
     9
 *
deba@2335
    10
 * Permission to use, modify and distribute this software is granted
deba@2335
    11
 * provided that this copyright notice appears in all copies. For
deba@2335
    12
 * precise terms see the accompanying LICENSE file.
deba@2335
    13
 *
deba@2335
    14
 * This software is provided "AS IS" with no warranty of any kind,
deba@2335
    15
 * express or implied, and with no claim as to its suitability for any
deba@2335
    16
 * purpose.
deba@2335
    17
 *
deba@2335
    18
 */
deba@2335
    19
deba@2335
    20
#ifndef LEMON_BITS_PRED_MAP_PATH_H
deba@2335
    21
#define LEMON_BITS_PRED_MAP_PATH_H
deba@2335
    22
deba@2335
    23
namespace lemon {
deba@2335
    24
deba@2335
    25
  template <typename _Graph, typename _PredMap>
deba@2335
    26
  class PredMapPath {
deba@2335
    27
  public:
deba@2335
    28
    typedef True RevPathTag;
deba@2335
    29
deba@2335
    30
    typedef _Graph Graph;
deba@2335
    31
    typedef typename Graph::Edge Edge;
deba@2335
    32
    typedef _PredMap PredMap;
deba@2335
    33
deba@2335
    34
    PredMapPath(const Graph& _graph, const PredMap& _predMap,
deba@2335
    35
                typename Graph::Node _target)
deba@2335
    36
      : graph(_graph), predMap(_predMap), target(_target) {}
deba@2335
    37
deba@2335
    38
    int length() const {
deba@2335
    39
      int len = 0;
deba@2335
    40
      typename Graph::Node node = target;
deba@2335
    41
      typename Graph::Edge edge;
deba@2335
    42
      while ((edge = predMap[node]) != INVALID) {
deba@2335
    43
        node = graph.source(edge);
deba@2335
    44
        ++len;
deba@2335
    45
      }
deba@2335
    46
      return len;
deba@2335
    47
    }
deba@2335
    48
deba@2335
    49
    bool empty() const {
deba@2335
    50
      return predMap[target] != INVALID;
deba@2335
    51
    }
deba@2335
    52
deba@2357
    53
    class RevEdgeIt {
deba@2335
    54
    public:
deba@2357
    55
      RevEdgeIt() {}
deba@2357
    56
      RevEdgeIt(Invalid) : path(0), current(INVALID) {}
deba@2357
    57
      RevEdgeIt(const PredMapPath& _path) 
deba@2335
    58
        : path(&_path), current(_path.target) {}
deba@2335
    59
deba@2335
    60
      operator const typename Graph::Edge() const {
deba@2335
    61
        return path->predMap[current];
deba@2335
    62
      }
deba@2335
    63
deba@2357
    64
      RevEdgeIt& operator++() {
deba@2335
    65
        current = path->graph.source(path->predMap[current]);
deba@2335
    66
        if (path->predMap[current] == INVALID) current = INVALID;
deba@2335
    67
        return *this;
deba@2335
    68
      }
deba@2335
    69
deba@2357
    70
      bool operator==(const RevEdgeIt& e) const { 
deba@2335
    71
        return current == e.current; 
deba@2335
    72
      }
deba@2335
    73
deba@2357
    74
      bool operator!=(const RevEdgeIt& e) const {
deba@2335
    75
        return current != e.current; 
deba@2335
    76
      }
deba@2335
    77
deba@2357
    78
      bool operator<(const RevEdgeIt& e) const { 
deba@2335
    79
        return current < e.current; 
deba@2335
    80
      }
deba@2335
    81
      
deba@2335
    82
    private:
deba@2335
    83
      const PredMapPath* path;
deba@2335
    84
      typename Graph::Node current;
deba@2335
    85
    };
deba@2335
    86
deba@2335
    87
  private:
deba@2335
    88
    const Graph& graph;
deba@2335
    89
    const PredMap& predMap;
deba@2335
    90
    typename Graph::Node target;
deba@2335
    91
  };
deba@2335
    92
deba@2335
    93
deba@2335
    94
  template <typename _Graph, typename _PredMatrixMap>
deba@2335
    95
  class PredMatrixMapPath {
deba@2335
    96
  public:
deba@2335
    97
    typedef True RevPathTag;
deba@2335
    98
deba@2335
    99
    typedef _Graph Graph;
deba@2335
   100
    typedef typename Graph::Edge Edge;
deba@2335
   101
    typedef _PredMatrixMap PredMatrixMap;
deba@2335
   102
deba@2335
   103
    PredMatrixMapPath(const Graph& _graph, 
deba@2335
   104
                      const PredMatrixMap& _predMatrixMap,
deba@2335
   105
                      typename Graph::Node _source, 
deba@2335
   106
                      typename Graph::Node _target)
deba@2335
   107
      : graph(_graph), predMatrixMap(_predMatrixMap), 
deba@2335
   108
        source(_source), target(_target) {}
deba@2335
   109
deba@2335
   110
    int length() const {
deba@2335
   111
      int len = 0;
deba@2335
   112
      typename Graph::Node node = target;
deba@2335
   113
      typename Graph::Edge edge;
deba@2335
   114
      while ((edge = predMatrixMap(source, node)) != INVALID) {
deba@2335
   115
        node = graph.source(edge);
deba@2335
   116
        ++len;
deba@2335
   117
      }
deba@2335
   118
      return len;
deba@2335
   119
    }
deba@2335
   120
deba@2335
   121
    bool empty() const {
deba@2335
   122
      return source != target;
deba@2335
   123
    }
deba@2335
   124
deba@2357
   125
    class RevEdgeIt {
deba@2335
   126
    public:
deba@2357
   127
      RevEdgeIt() {}
deba@2357
   128
      RevEdgeIt(Invalid) : path(0), current(INVALID) {}
deba@2357
   129
      RevEdgeIt(const PredMatrixMapPath& _path) 
deba@2335
   130
        : path(&_path), current(_path.target) {}
deba@2335
   131
deba@2335
   132
      operator const typename Graph::Edge() const {
deba@2335
   133
        return path->predMatrixMap(path->source, current);
deba@2335
   134
      }
deba@2335
   135
deba@2357
   136
      RevEdgeIt& operator++() {
deba@2335
   137
        current = 
deba@2335
   138
          path->graph.source(path->predMatrixMap(path->source, current));
deba@2335
   139
        if (path->predMatrixMap(path->source, current) == INVALID) 
deba@2335
   140
          current = INVALID;
deba@2335
   141
        return *this;
deba@2335
   142
      }
deba@2335
   143
deba@2357
   144
      bool operator==(const RevEdgeIt& e) const { 
deba@2335
   145
        return current == e.current; 
deba@2335
   146
      }
deba@2335
   147
deba@2357
   148
      bool operator!=(const RevEdgeIt& e) const {
deba@2335
   149
        return current != e.current; 
deba@2335
   150
      }
deba@2335
   151
deba@2357
   152
      bool operator<(const RevEdgeIt& e) const { 
deba@2335
   153
        return current < e.current; 
deba@2335
   154
      }
deba@2335
   155
      
deba@2335
   156
    private:
deba@2335
   157
      const PredMatrixMapPath* path;
deba@2335
   158
      typename Graph::Node current;
deba@2335
   159
    };
deba@2335
   160
deba@2335
   161
  private:
deba@2335
   162
    const Graph& graph;
deba@2335
   163
    const PredMatrixMap& predMatrixMap;
deba@2335
   164
    typename Graph::Node source;
deba@2335
   165
    typename Graph::Node target;
deba@2335
   166
  };
deba@2335
   167
deba@2335
   168
}
deba@2335
   169
deba@2335
   170
#endif