lemon/static_graph.h
author Peter Kovacs <kpeter@inf.elte.hu>
Tue, 29 Sep 2009 13:03:34 +0200
changeset 825 a143f19f465b
parent 823 eff1caf6d32e
child 826 c160bf9f18ef
permissions -rw-r--r--
Make some graph member functions static (#311, #68)
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_STATIC_GRAPH_H
    20 #define LEMON_STATIC_GRAPH_H
    21 
    22 ///\ingroup graphs
    23 ///\file
    24 ///\brief StaticDigraph class.
    25 
    26 #include <lemon/core.h>
    27 #include <lemon/bits/graph_extender.h>
    28 
    29 namespace lemon {
    30 
    31   class StaticDigraphBase {
    32   public:
    33 
    34     StaticDigraphBase() 
    35       : built(false), node_num(0), arc_num(0), 
    36         node_first_out(NULL), node_first_in(NULL),
    37         arc_source(NULL), arc_target(NULL), 
    38         arc_next_in(NULL), arc_next_out(NULL) {}
    39     
    40     ~StaticDigraphBase() {
    41       if (built) {
    42         delete[] node_first_out;
    43         delete[] node_first_in;
    44         delete[] arc_source;
    45         delete[] arc_target;
    46         delete[] arc_next_out;
    47         delete[] arc_next_in;
    48       }
    49     }
    50 
    51     class Node {
    52       friend class StaticDigraphBase;
    53     protected:
    54       int id;
    55       Node(int _id) : id(_id) {}
    56     public:
    57       Node() {}
    58       Node (Invalid) : id(-1) {}
    59       bool operator==(const Node& node) const { return id == node.id; }
    60       bool operator!=(const Node& node) const { return id != node.id; }
    61       bool operator<(const Node& node) const { return id < node.id; }
    62     };
    63 
    64     class Arc {
    65       friend class StaticDigraphBase;      
    66     protected:
    67       int id;
    68       Arc(int _id) : id(_id) {}
    69     public:
    70       Arc() { }
    71       Arc (Invalid) : id(-1) {}
    72       bool operator==(const Arc& arc) const { return id == arc.id; }
    73       bool operator!=(const Arc& arc) const { return id != arc.id; }
    74       bool operator<(const Arc& arc) const { return id < arc.id; }
    75     };
    76 
    77     Node source(const Arc& e) const { return Node(arc_source[e.id]); }
    78     Node target(const Arc& e) const { return Node(arc_target[e.id]); }
    79 
    80     void first(Node& n) const { n.id = node_num - 1; }
    81     static void next(Node& n) { --n.id; }
    82 
    83     void first(Arc& e) const { e.id = arc_num - 1; }
    84     static void next(Arc& e) { --e.id; }
    85 
    86     void firstOut(Arc& e, const Node& n) const { 
    87       e.id = node_first_out[n.id] != node_first_out[n.id + 1] ? 
    88         node_first_out[n.id] : -1;
    89     }
    90     void nextOut(Arc& e) const { e.id = arc_next_out[e.id]; }
    91 
    92     void firstIn(Arc& e, const Node& n) const { e.id = node_first_in[n.id]; }
    93     void nextIn(Arc& e) const { e.id = arc_next_in[e.id]; }
    94 
    95     static int id(const Node& n) { return n.id; }
    96     static Node nodeFromId(int id) { return Node(id); }
    97     int maxNodeId() const { return node_num - 1; }
    98 
    99     static int id(const Arc& e) { return e.id; }
   100     static Arc arcFromId(int id) { return Arc(id); }
   101     int maxArcId() const { return arc_num - 1; }
   102 
   103     typedef True NodeNumTag;
   104     typedef True ArcNumTag;
   105 
   106     int nodeNum() const { return node_num; }
   107     int arcNum() const { return arc_num; }
   108 
   109   private:
   110 
   111     template <typename Digraph, typename NodeRefMap>
   112     class ArcLess {
   113     public:
   114       typedef typename Digraph::Arc Arc;
   115 
   116       ArcLess(const Digraph &_graph, const NodeRefMap& _nodeRef) 
   117         : digraph(_graph), nodeRef(_nodeRef) {}
   118       
   119       bool operator()(const Arc& left, const Arc& right) const {
   120 	return nodeRef[digraph.target(left)] < nodeRef[digraph.target(right)];
   121       }
   122     private:
   123       const Digraph& digraph;
   124       const NodeRefMap& nodeRef;
   125     };
   126     
   127   public:
   128 
   129     typedef True BuildTag;
   130     
   131     void clear() {
   132       if (built) {
   133         delete[] node_first_out;
   134         delete[] node_first_in;
   135         delete[] arc_source;
   136         delete[] arc_target;
   137         delete[] arc_next_out;
   138         delete[] arc_next_in;
   139       }
   140       built = false;
   141       node_num = 0;
   142       arc_num = 0;
   143     }
   144     
   145     template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
   146     void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
   147       typedef typename Digraph::Node GNode;
   148       typedef typename Digraph::Arc GArc;
   149 
   150       built = true;
   151 
   152       node_num = countNodes(digraph);
   153       arc_num = countArcs(digraph);
   154 
   155       node_first_out = new int[node_num + 1];
   156       node_first_in = new int[node_num];
   157 
   158       arc_source = new int[arc_num];
   159       arc_target = new int[arc_num];
   160       arc_next_out = new int[arc_num];
   161       arc_next_in = new int[arc_num];
   162 
   163       int node_index = 0;
   164       for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
   165         nodeRef[n] = Node(node_index);
   166         node_first_in[node_index] = -1;
   167         ++node_index;
   168       }
   169 
   170       ArcLess<Digraph, NodeRefMap> arcLess(digraph, nodeRef);
   171 
   172       int arc_index = 0;
   173       for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
   174         int source = nodeRef[n].id;
   175         std::vector<GArc> arcs;
   176         for (typename Digraph::OutArcIt e(digraph, n); e != INVALID; ++e) {
   177           arcs.push_back(e);
   178         }
   179         if (!arcs.empty()) {
   180           node_first_out[source] = arc_index;
   181           std::sort(arcs.begin(), arcs.end(), arcLess);
   182           for (typename std::vector<GArc>::iterator it = arcs.begin();
   183                it != arcs.end(); ++it) {
   184             int target = nodeRef[digraph.target(*it)].id;
   185             arcRef[*it] = Arc(arc_index);
   186             arc_source[arc_index] = source; 
   187             arc_target[arc_index] = target;
   188             arc_next_in[arc_index] = node_first_in[target];
   189             node_first_in[target] = arc_index;
   190             arc_next_out[arc_index] = arc_index + 1;
   191             ++arc_index;
   192           }
   193           arc_next_out[arc_index - 1] = -1;
   194         } else {
   195           node_first_out[source] = arc_index;
   196         }
   197       }
   198       node_first_out[node_num] = arc_num;
   199     }
   200 
   201   protected:
   202 
   203     void fastFirstOut(Arc& e, const Node& n) const {
   204       e.id = node_first_out[n.id];
   205     }
   206 
   207     static void fastNextOut(Arc& e) {
   208       ++e.id;
   209     }
   210     void fastLastOut(Arc& e, const Node& n) const {
   211       e.id = node_first_out[n.id + 1];
   212     }
   213 
   214   protected:
   215     bool built;
   216     int node_num;
   217     int arc_num;
   218     int *node_first_out;
   219     int *node_first_in;
   220     int *arc_source;
   221     int *arc_target;
   222     int *arc_next_in;
   223     int *arc_next_out;
   224   };
   225 
   226   typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase;
   227 
   228 
   229   /// \ingroup graphs
   230   ///
   231   /// \brief A static directed graph class.
   232   ///
   233   /// \ref StaticDigraph is a highly efficient digraph implementation,
   234   /// but it is fully static.
   235   /// It stores only two \c int values for each node and only four \c int
   236   /// values for each arc. Moreover it provides faster item iteration than
   237   /// \ref ListDigraph and \ref SmartDigraph, especially using \c OutArcIt
   238   /// iterators, since its arcs are stored in an appropriate order.
   239   /// However it only provides build() and clear() functions and does not
   240   /// support any other modification of the digraph.
   241   ///
   242   /// Since this digraph structure is completely static, its nodes and arcs
   243   /// can be indexed with integers from the ranges <tt>[0..nodeNum()-1]</tt>
   244   /// and <tt>[0..arcNum()-1]</tt>, respectively. 
   245   /// The index of an item is the same as its ID, it can be obtained
   246   /// using the corresponding \ref index() or \ref concepts::Digraph::id()
   247   /// "id()" function. A node or arc with a certain index can be obtained
   248   /// using node() or arc().
   249   ///
   250   /// This type fully conforms to the \ref concepts::Digraph "Digraph concept".
   251   /// Most of its member functions and nested classes are documented
   252   /// only in the concept class.
   253   ///
   254   /// \sa concepts::Digraph
   255   class StaticDigraph : public ExtendedStaticDigraphBase {
   256   public:
   257 
   258     typedef ExtendedStaticDigraphBase Parent;
   259   
   260   public:
   261   
   262     /// \brief Constructor
   263     ///
   264     /// Default constructor.
   265     StaticDigraph() : Parent() {}
   266 
   267     /// \brief The node with the given index.
   268     ///
   269     /// This function returns the node with the given index.
   270     /// \sa index()
   271     static Node node(int ix) { return Parent::nodeFromId(ix); }
   272 
   273     /// \brief The arc with the given index.
   274     ///
   275     /// This function returns the arc with the given index.
   276     /// \sa index()
   277     static Arc arc(int ix) { return Parent::arcFromId(ix); }
   278 
   279     /// \brief The index of the given node.
   280     ///
   281     /// This function returns the index of the the given node.
   282     /// \sa node()
   283     static int index(Node node) { return Parent::id(node); }
   284 
   285     /// \brief The index of the given arc.
   286     ///
   287     /// This function returns the index of the the given arc.
   288     /// \sa arc()
   289     static int index(Arc arc) { return Parent::id(arc); }
   290 
   291     /// \brief Number of nodes.
   292     ///
   293     /// This function returns the number of nodes.
   294     int nodeNum() const { return node_num; }
   295 
   296     /// \brief Number of arcs.
   297     ///
   298     /// This function returns the number of arcs.
   299     int arcNum() const { return arc_num; }
   300 
   301     /// \brief Build the digraph copying another digraph.
   302     ///
   303     /// This function builds the digraph copying another digraph of any
   304     /// kind. It can be called more than once, but in such case, the whole
   305     /// structure and all maps will be cleared and rebuilt.
   306     ///
   307     /// This method also makes possible to copy a digraph to a StaticDigraph
   308     /// structure using \ref DigraphCopy.
   309     /// 
   310     /// \param digraph An existing digraph to be copied.
   311     /// \param nodeRef The node references will be copied into this map.
   312     /// Its key type must be \c Digraph::Node and its value type must be
   313     /// \c StaticDigraph::Node.
   314     /// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap"
   315     /// concept.
   316     /// \param arcRef The arc references will be copied into this map.
   317     /// Its key type must be \c Digraph::Arc and its value type must be
   318     /// \c StaticDigraph::Arc.
   319     /// It must conform to the \ref concepts::WriteMap "WriteMap" concept.
   320     ///
   321     /// \note If you do not need the arc references, then you could use
   322     /// \ref NullMap for the last parameter. However the node references
   323     /// are required by the function itself, thus they must be readable
   324     /// from the map.
   325     template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
   326     void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
   327       if (built) Parent::clear();
   328       Parent::build(digraph, nodeRef, arcRef);
   329     }
   330   
   331     /// \brief Clear the digraph.
   332     ///
   333     /// This function erases all nodes and arcs from the digraph.
   334     void clear() {
   335       Parent::clear();
   336     }
   337 
   338   protected:
   339 
   340     using Parent::fastFirstOut;
   341     using Parent::fastNextOut;
   342     using Parent::fastLastOut;
   343     
   344   public:
   345 
   346     class OutArcIt : public Arc {
   347     public:
   348 
   349       OutArcIt() { }
   350 
   351       OutArcIt(Invalid i) : Arc(i) { }
   352 
   353       OutArcIt(const StaticDigraph& digraph, const Node& node) {
   354 	digraph.fastFirstOut(*this, node);
   355 	digraph.fastLastOut(last, node);
   356         if (last == *this) *this = INVALID;
   357       }
   358 
   359       OutArcIt(const StaticDigraph& digraph, const Arc& arc) : Arc(arc) {
   360         if (arc != INVALID) {
   361           digraph.fastLastOut(last, digraph.source(arc));
   362         }
   363       }
   364 
   365       OutArcIt& operator++() { 
   366         StaticDigraph::fastNextOut(*this);
   367         if (last == *this) *this = INVALID;
   368         return *this; 
   369       }
   370 
   371     private:
   372       Arc last;
   373     };
   374 
   375     Node baseNode(const OutArcIt &arc) const {
   376       return Parent::source(static_cast<const Arc&>(arc));
   377     }
   378 
   379     Node runningNode(const OutArcIt &arc) const {
   380       return Parent::target(static_cast<const Arc&>(arc));
   381     }
   382 
   383     Node baseNode(const InArcIt &arc) const {
   384       return Parent::target(static_cast<const Arc&>(arc));
   385     }
   386 
   387     Node runningNode(const InArcIt &arc) const {
   388       return Parent::source(static_cast<const Arc&>(arc));
   389     }
   390 
   391   };
   392 
   393 }
   394 
   395 #endif