lemon/bits/path_dump.h
author alpar
Tue, 05 Jun 2007 14:48:20 +0000
changeset 2448 ab899ae3505f
parent 2357 5365600a7a5c
child 2467 2025a571895e
permissions -rw-r--r--
Bugfix and improvement in -tsp2 algorithm
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
#ifndef LEMON_BITS_PRED_MAP_PATH_H
deba@2335
    20
#define LEMON_BITS_PRED_MAP_PATH_H
deba@2335
    21
deba@2335
    22
namespace lemon {
deba@2335
    23
deba@2335
    24
  template <typename _Graph, typename _PredMap>
deba@2335
    25
  class PredMapPath {
deba@2335
    26
  public:
deba@2335
    27
    typedef True RevPathTag;
deba@2335
    28
deba@2335
    29
    typedef _Graph Graph;
deba@2335
    30
    typedef typename Graph::Edge Edge;
deba@2335
    31
    typedef _PredMap PredMap;
deba@2335
    32
deba@2335
    33
    PredMapPath(const Graph& _graph, const PredMap& _predMap,
deba@2335
    34
                typename Graph::Node _target)
deba@2335
    35
      : graph(_graph), predMap(_predMap), target(_target) {}
deba@2335
    36
deba@2335
    37
    int length() const {
deba@2335
    38
      int len = 0;
deba@2335
    39
      typename Graph::Node node = target;
deba@2335
    40
      typename Graph::Edge edge;
deba@2335
    41
      while ((edge = predMap[node]) != INVALID) {
deba@2335
    42
        node = graph.source(edge);
deba@2335
    43
        ++len;
deba@2335
    44
      }
deba@2335
    45
      return len;
deba@2335
    46
    }
deba@2335
    47
deba@2335
    48
    bool empty() const {
deba@2335
    49
      return predMap[target] != INVALID;
deba@2335
    50
    }
deba@2335
    51
deba@2357
    52
    class RevEdgeIt {
deba@2335
    53
    public:
deba@2357
    54
      RevEdgeIt() {}
deba@2357
    55
      RevEdgeIt(Invalid) : path(0), current(INVALID) {}
deba@2357
    56
      RevEdgeIt(const PredMapPath& _path) 
deba@2335
    57
        : path(&_path), current(_path.target) {}
deba@2335
    58
deba@2335
    59
      operator const typename Graph::Edge() const {
deba@2335
    60
        return path->predMap[current];
deba@2335
    61
      }
deba@2335
    62
deba@2357
    63
      RevEdgeIt& operator++() {
deba@2335
    64
        current = path->graph.source(path->predMap[current]);
deba@2335
    65
        if (path->predMap[current] == INVALID) current = INVALID;
deba@2335
    66
        return *this;
deba@2335
    67
      }
deba@2335
    68
deba@2357
    69
      bool operator==(const RevEdgeIt& e) const { 
deba@2335
    70
        return current == e.current; 
deba@2335
    71
      }
deba@2335
    72
deba@2357
    73
      bool operator!=(const RevEdgeIt& e) const {
deba@2335
    74
        return current != e.current; 
deba@2335
    75
      }
deba@2335
    76
deba@2357
    77
      bool operator<(const RevEdgeIt& e) const { 
deba@2335
    78
        return current < e.current; 
deba@2335
    79
      }
deba@2335
    80
      
deba@2335
    81
    private:
deba@2335
    82
      const PredMapPath* path;
deba@2335
    83
      typename Graph::Node current;
deba@2335
    84
    };
deba@2335
    85
deba@2335
    86
  private:
deba@2335
    87
    const Graph& graph;
deba@2335
    88
    const PredMap& predMap;
deba@2335
    89
    typename Graph::Node target;
deba@2335
    90
  };
deba@2335
    91
deba@2335
    92
deba@2335
    93
  template <typename _Graph, typename _PredMatrixMap>
deba@2335
    94
  class PredMatrixMapPath {
deba@2335
    95
  public:
deba@2335
    96
    typedef True RevPathTag;
deba@2335
    97
deba@2335
    98
    typedef _Graph Graph;
deba@2335
    99
    typedef typename Graph::Edge Edge;
deba@2335
   100
    typedef _PredMatrixMap PredMatrixMap;
deba@2335
   101
deba@2335
   102
    PredMatrixMapPath(const Graph& _graph, 
deba@2335
   103
                      const PredMatrixMap& _predMatrixMap,
deba@2335
   104
                      typename Graph::Node _source, 
deba@2335
   105
                      typename Graph::Node _target)
deba@2335
   106
      : graph(_graph), predMatrixMap(_predMatrixMap), 
deba@2335
   107
        source(_source), target(_target) {}
deba@2335
   108
deba@2335
   109
    int length() const {
deba@2335
   110
      int len = 0;
deba@2335
   111
      typename Graph::Node node = target;
deba@2335
   112
      typename Graph::Edge edge;
deba@2335
   113
      while ((edge = predMatrixMap(source, node)) != INVALID) {
deba@2335
   114
        node = graph.source(edge);
deba@2335
   115
        ++len;
deba@2335
   116
      }
deba@2335
   117
      return len;
deba@2335
   118
    }
deba@2335
   119
deba@2335
   120
    bool empty() const {
deba@2335
   121
      return source != target;
deba@2335
   122
    }
deba@2335
   123
deba@2357
   124
    class RevEdgeIt {
deba@2335
   125
    public:
deba@2357
   126
      RevEdgeIt() {}
deba@2357
   127
      RevEdgeIt(Invalid) : path(0), current(INVALID) {}
deba@2357
   128
      RevEdgeIt(const PredMatrixMapPath& _path) 
deba@2335
   129
        : path(&_path), current(_path.target) {}
deba@2335
   130
deba@2335
   131
      operator const typename Graph::Edge() const {
deba@2335
   132
        return path->predMatrixMap(path->source, current);
deba@2335
   133
      }
deba@2335
   134
deba@2357
   135
      RevEdgeIt& operator++() {
deba@2335
   136
        current = 
deba@2335
   137
          path->graph.source(path->predMatrixMap(path->source, current));
deba@2335
   138
        if (path->predMatrixMap(path->source, current) == INVALID) 
deba@2335
   139
          current = INVALID;
deba@2335
   140
        return *this;
deba@2335
   141
      }
deba@2335
   142
deba@2357
   143
      bool operator==(const RevEdgeIt& e) const { 
deba@2335
   144
        return current == e.current; 
deba@2335
   145
      }
deba@2335
   146
deba@2357
   147
      bool operator!=(const RevEdgeIt& e) const {
deba@2335
   148
        return current != e.current; 
deba@2335
   149
      }
deba@2335
   150
deba@2357
   151
      bool operator<(const RevEdgeIt& e) const { 
deba@2335
   152
        return current < e.current; 
deba@2335
   153
      }
deba@2335
   154
      
deba@2335
   155
    private:
deba@2335
   156
      const PredMatrixMapPath* path;
deba@2335
   157
      typename Graph::Node current;
deba@2335
   158
    };
deba@2335
   159
deba@2335
   160
  private:
deba@2335
   161
    const Graph& graph;
deba@2335
   162
    const PredMatrixMap& predMatrixMap;
deba@2335
   163
    typename Graph::Node source;
deba@2335
   164
    typename Graph::Node target;
deba@2335
   165
  };
deba@2335
   166
deba@2335
   167
}
deba@2335
   168
deba@2335
   169
#endif