lemon/static_graph.h
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 25 Aug 2009 11:09:02 +0200
changeset 773 cf360f758f25
child 774 f4b5c2d5449d
permissions -rw-r--r--
Port StaticDigraph from SVN -r3524 (#68)
kpeter@773
     1
/* -*- C++ -*-
kpeter@773
     2
 *
kpeter@773
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@773
     4
 *
kpeter@773
     5
 * Copyright (C) 2003-2008
kpeter@773
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@773
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@773
     8
 *
kpeter@773
     9
 * Permission to use, modify and distribute this software is granted
kpeter@773
    10
 * provided that this copyright notice appears in all copies. For
kpeter@773
    11
 * precise terms see the accompanying LICENSE file.
kpeter@773
    12
 *
kpeter@773
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@773
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@773
    15
 * purpose.
kpeter@773
    16
 *
kpeter@773
    17
 */
kpeter@773
    18
kpeter@773
    19
#ifndef LEMON_STATIC_GRAPH_H
kpeter@773
    20
#define LEMON_STATIC_GRAPH_H
kpeter@773
    21
kpeter@773
    22
///\ingroup graphs
kpeter@773
    23
///\file
kpeter@773
    24
///\brief StaticDigraph class.
kpeter@773
    25
kpeter@773
    26
#include <lemon/core.h>
kpeter@773
    27
#include <lemon/bits/graph_extender.h>
kpeter@773
    28
kpeter@773
    29
namespace lemon {
kpeter@773
    30
kpeter@773
    31
  class StaticDigraphBase {
kpeter@773
    32
  public:
kpeter@773
    33
kpeter@773
    34
    StaticDigraphBase() 
kpeter@773
    35
      : node_num(-1), arc_num(0), 
kpeter@773
    36
        node_first_out(NULL), node_first_in(NULL),
kpeter@773
    37
        arc_source(NULL), arc_target(NULL), 
kpeter@773
    38
        arc_next_in(NULL), arc_next_out(NULL) {}
kpeter@773
    39
    
kpeter@773
    40
    ~StaticDigraphBase() {
kpeter@773
    41
      if (node_num != -1) {
kpeter@773
    42
        delete[] node_first_out;
kpeter@773
    43
        delete[] node_first_in;
kpeter@773
    44
        delete[] arc_source;
kpeter@773
    45
        delete[] arc_target;
kpeter@773
    46
        delete[] arc_next_out;
kpeter@773
    47
        delete[] arc_next_in;
kpeter@773
    48
      }
kpeter@773
    49
    }
kpeter@773
    50
kpeter@773
    51
    class Node {
kpeter@773
    52
      friend class StaticDigraphBase;
kpeter@773
    53
    protected:
kpeter@773
    54
      int id;
kpeter@773
    55
      Node(int _id) : id(_id) {}
kpeter@773
    56
    public:
kpeter@773
    57
      Node() {}
kpeter@773
    58
      Node (Invalid) : id(-1) {}
kpeter@773
    59
      bool operator==(const Node& node) const { return id == node.id; }
kpeter@773
    60
      bool operator!=(const Node& node) const { return id != node.id; }
kpeter@773
    61
      bool operator<(const Node& node) const { return id < node.id; }
kpeter@773
    62
    };
kpeter@773
    63
kpeter@773
    64
    class Arc {
kpeter@773
    65
      friend class StaticDigraphBase;      
kpeter@773
    66
    protected:
kpeter@773
    67
      int id;
kpeter@773
    68
      Arc(int _id) : id(_id) {}
kpeter@773
    69
    public:
kpeter@773
    70
      Arc() { }
kpeter@773
    71
      Arc (Invalid) : id(-1) {}
kpeter@773
    72
      bool operator==(const Arc& arc) const { return id == arc.id; }
kpeter@773
    73
      bool operator!=(const Arc& arc) const { return id != arc.id; }
kpeter@773
    74
      bool operator<(const Arc& arc) const { return id < arc.id; }
kpeter@773
    75
    };
kpeter@773
    76
kpeter@773
    77
    Node source(const Arc& e) const { return Node(arc_source[e.id]); }
kpeter@773
    78
    Node target(const Arc& e) const { return Node(arc_target[e.id]); }
kpeter@773
    79
kpeter@773
    80
    void first(Node& n) const { n.id = node_num - 1; }
kpeter@773
    81
    static void next(Node& n) { --n.id; }
kpeter@773
    82
kpeter@773
    83
    void first(Arc& e) const { e.id = arc_num - 1; }
kpeter@773
    84
    static void next(Arc& e) { --e.id; }
kpeter@773
    85
kpeter@773
    86
    void firstOut(Arc& e, const Node& n) const { 
kpeter@773
    87
      e.id = node_first_out[n.id] != node_first_out[n.id + 1] ? 
kpeter@773
    88
        node_first_out[n.id] : -1;
kpeter@773
    89
    }
kpeter@773
    90
    void nextOut(Arc& e) const { e.id = arc_next_out[e.id]; }
kpeter@773
    91
kpeter@773
    92
    void firstIn(Arc& e, const Node& n) const { e.id = node_first_in[n.id]; }
kpeter@773
    93
    void nextIn(Arc& e) const { e.id = arc_next_in[e.id]; }
kpeter@773
    94
kpeter@773
    95
    int id(const Node& n) const { return n.id; }
kpeter@773
    96
    Node nodeFromId(int id) const { return Node(id); }
kpeter@773
    97
    int maxNodeId() const { return node_num - 1; }
kpeter@773
    98
kpeter@773
    99
    int id(const Arc& e) const { return e.id; }
kpeter@773
   100
    Arc arcFromId(int id) const { return Arc(id); }
kpeter@773
   101
    int maxArcId() const { return arc_num - 1; }
kpeter@773
   102
kpeter@773
   103
    typedef True NodeNumTag;
kpeter@773
   104
    typedef True ArcNumTag;
kpeter@773
   105
kpeter@773
   106
    int nodeNum() const { return node_num; }
kpeter@773
   107
    int arcNum() const { return arc_num; }
kpeter@773
   108
kpeter@773
   109
  private:
kpeter@773
   110
kpeter@773
   111
    template <typename Digraph, typename NodeRefMap>
kpeter@773
   112
    class ArcLess {
kpeter@773
   113
    public:
kpeter@773
   114
      typedef typename Digraph::Arc Arc;
kpeter@773
   115
kpeter@773
   116
      ArcLess(const Digraph &_graph, const NodeRefMap& _nodeRef) 
kpeter@773
   117
        : digraph(_graph), nodeRef(_nodeRef) {}
kpeter@773
   118
      
kpeter@773
   119
      bool operator()(const Arc& left, const Arc& right) const {
kpeter@773
   120
	return nodeRef[digraph.target(left)] < nodeRef[digraph.target(right)];
kpeter@773
   121
      }
kpeter@773
   122
    private:
kpeter@773
   123
      const Digraph& digraph;
kpeter@773
   124
      const NodeRefMap& nodeRef;
kpeter@773
   125
    };
kpeter@773
   126
    
kpeter@773
   127
  public:
kpeter@773
   128
kpeter@773
   129
    typedef True BuildTag;
kpeter@773
   130
    
kpeter@773
   131
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
kpeter@773
   132
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
kpeter@773
   133
kpeter@773
   134
      if (node_num != -1) {
kpeter@773
   135
        delete[] node_first_out;
kpeter@773
   136
        delete[] node_first_in;
kpeter@773
   137
        delete[] arc_source;
kpeter@773
   138
        delete[] arc_target;
kpeter@773
   139
        delete[] arc_next_out;
kpeter@773
   140
        delete[] arc_next_in;
kpeter@773
   141
      }
kpeter@773
   142
kpeter@773
   143
      typedef typename Digraph::Node GNode;
kpeter@773
   144
      typedef typename Digraph::Arc GArc;
kpeter@773
   145
kpeter@773
   146
      node_num = countNodes(digraph);
kpeter@773
   147
      arc_num = countArcs(digraph);
kpeter@773
   148
kpeter@773
   149
      node_first_out = new int[node_num + 1];
kpeter@773
   150
      node_first_in = new int[node_num];
kpeter@773
   151
kpeter@773
   152
      arc_source = new int[arc_num];
kpeter@773
   153
      arc_target = new int[arc_num];
kpeter@773
   154
      arc_next_out = new int[arc_num];
kpeter@773
   155
      arc_next_in = new int[arc_num];
kpeter@773
   156
kpeter@773
   157
      int node_index = 0;
kpeter@773
   158
      for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
kpeter@773
   159
        nodeRef[n] = Node(node_index);
kpeter@773
   160
        node_first_in[node_index] = -1;
kpeter@773
   161
        ++node_index;
kpeter@773
   162
      }
kpeter@773
   163
kpeter@773
   164
      ArcLess<Digraph, NodeRefMap> arcLess(digraph, nodeRef);
kpeter@773
   165
kpeter@773
   166
      int arc_index = 0;
kpeter@773
   167
      for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
kpeter@773
   168
        int source = nodeRef[n].id;
kpeter@773
   169
        std::vector<GArc> arcs;
kpeter@773
   170
        for (typename Digraph::OutArcIt e(digraph, n); e != INVALID; ++e) {
kpeter@773
   171
          arcs.push_back(e);
kpeter@773
   172
        }
kpeter@773
   173
        if (!arcs.empty()) {
kpeter@773
   174
          node_first_out[source] = arc_index;
kpeter@773
   175
          std::sort(arcs.begin(), arcs.end(), arcLess);
kpeter@773
   176
          for (typename std::vector<GArc>::iterator it = arcs.begin();
kpeter@773
   177
               it != arcs.end(); ++it) {
kpeter@773
   178
            int target = nodeRef[digraph.target(*it)].id;
kpeter@773
   179
            arcRef[*it] = Arc(arc_index);
kpeter@773
   180
            arc_source[arc_index] = source; 
kpeter@773
   181
            arc_target[arc_index] = target;
kpeter@773
   182
            arc_next_in[arc_index] = node_first_in[target];
kpeter@773
   183
            node_first_in[target] = arc_index;
kpeter@773
   184
            arc_next_out[arc_index] = arc_index + 1;
kpeter@773
   185
            ++arc_index;
kpeter@773
   186
          }
kpeter@773
   187
          arc_next_out[arc_index - 1] = -1;
kpeter@773
   188
        } else {
kpeter@773
   189
          node_first_out[source] = arc_index;
kpeter@773
   190
        }
kpeter@773
   191
      }
kpeter@773
   192
      node_first_out[node_num] = arc_num;
kpeter@773
   193
    }
kpeter@773
   194
kpeter@773
   195
  protected:
kpeter@773
   196
kpeter@773
   197
    void fastFirstOut(Arc& e, const Node& n) const {
kpeter@773
   198
      e.id = node_first_out[n.id];
kpeter@773
   199
    }
kpeter@773
   200
kpeter@773
   201
    static void fastNextOut(Arc& e) {
kpeter@773
   202
      ++e.id;
kpeter@773
   203
    }
kpeter@773
   204
    void fastLastOut(Arc& e, const Node& n) const {
kpeter@773
   205
      e.id = node_first_out[n.id + 1];
kpeter@773
   206
    }
kpeter@773
   207
kpeter@773
   208
  private:
kpeter@773
   209
    int node_num;
kpeter@773
   210
    int arc_num;
kpeter@773
   211
    int *node_first_out;
kpeter@773
   212
    int *node_first_in;
kpeter@773
   213
    int *arc_source;
kpeter@773
   214
    int *arc_target;
kpeter@773
   215
    int *arc_next_in;
kpeter@773
   216
    int *arc_next_out;
kpeter@773
   217
  };
kpeter@773
   218
kpeter@773
   219
  typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase;
kpeter@773
   220
kpeter@773
   221
kpeter@773
   222
  class StaticDigraph : public ExtendedStaticDigraphBase {
kpeter@773
   223
  public:
kpeter@773
   224
kpeter@773
   225
    typedef ExtendedStaticDigraphBase Parent;
kpeter@773
   226
kpeter@773
   227
  protected:
kpeter@773
   228
kpeter@773
   229
    using Parent::fastFirstOut;
kpeter@773
   230
    using Parent::fastNextOut;
kpeter@773
   231
    using Parent::fastLastOut;
kpeter@773
   232
    
kpeter@773
   233
  public:
kpeter@773
   234
kpeter@773
   235
    class OutArcIt : public Arc {
kpeter@773
   236
    public:
kpeter@773
   237
kpeter@773
   238
      OutArcIt() { }
kpeter@773
   239
kpeter@773
   240
      OutArcIt(Invalid i) : Arc(i) { }
kpeter@773
   241
kpeter@773
   242
      OutArcIt(const StaticDigraph& digraph, const Node& node) {
kpeter@773
   243
	digraph.fastFirstOut(*this, node);
kpeter@773
   244
	digraph.fastLastOut(last, node);
kpeter@773
   245
        if (last == *this) *this = INVALID;
kpeter@773
   246
      }
kpeter@773
   247
kpeter@773
   248
      OutArcIt(const StaticDigraph& digraph, const Arc& arc) : Arc(arc) {
kpeter@773
   249
        if (arc != INVALID) {
kpeter@773
   250
          digraph.fastLastOut(last, digraph.source(arc));
kpeter@773
   251
        }
kpeter@773
   252
      }
kpeter@773
   253
kpeter@773
   254
      OutArcIt& operator++() { 
kpeter@773
   255
        StaticDigraph::fastNextOut(*this);
kpeter@773
   256
        if (last == *this) *this = INVALID;
kpeter@773
   257
        return *this; 
kpeter@773
   258
      }
kpeter@773
   259
kpeter@773
   260
    private:
kpeter@773
   261
      Arc last;
kpeter@773
   262
    };
kpeter@773
   263
kpeter@773
   264
  };
kpeter@773
   265
kpeter@773
   266
}
kpeter@773
   267
kpeter@773
   268
#endif