src/hugo/list_graph.h
author athos
Thu, 16 Sep 2004 10:26:14 +0000
changeset 860 3577b3db6089
parent 827 6433f69dfc6b
child 877 66dd225ca128
permissions -rw-r--r--
Completed documentation for mincostflows and minlengthpaths.
alpar@395
     1
// -*- mode:C++ -*-
alpar@395
     2
alpar@405
     3
#ifndef HUGO_LIST_GRAPH_H
alpar@405
     4
#define HUGO_LIST_GRAPH_H
alpar@395
     5
klao@491
     6
///\ingroup graphs
alpar@395
     7
///\file
alpar@405
     8
///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes.
alpar@395
     9
alpar@395
    10
#include <vector>
deba@782
    11
#include <climits>
alpar@395
    12
ladanyi@542
    13
#include <hugo/invalid.h>
alpar@395
    14
deba@782
    15
#include <hugo/map_registry.h>
deba@822
    16
#include <hugo/default_map.h>
deba@782
    17
deba@822
    18
#include <hugo/sym_map.h>
deba@782
    19
deba@782
    20
#include <hugo/map_defines.h>
deba@782
    21
deba@782
    22
alpar@395
    23
namespace hugo {
alpar@395
    24
alpar@406
    25
/// \addtogroup graphs
alpar@406
    26
/// @{
alpar@406
    27
alpar@401
    28
  ///A list graph class.
alpar@395
    29
alpar@397
    30
  ///This is a simple and fast erasable graph implementation.
alpar@397
    31
  ///
alpar@395
    32
  ///It conforms to the graph interface documented under
alpar@827
    33
  ///the description of
alpar@827
    34
  ///\ref skeleton::ErasableGraphSkeleton "ErasableGraphSkeleton".
alpar@827
    35
  ///\sa skeleton::ErasableGraphSkeleton.
alpar@397
    36
  class ListGraph {
alpar@395
    37
alpar@397
    38
    //Nodes are double linked.
alpar@397
    39
    //The free nodes are only single linked using the "next" field.
alpar@395
    40
    struct NodeT 
alpar@395
    41
    {
alpar@397
    42
      int first_in,first_out;
alpar@397
    43
      int prev, next;
alpar@395
    44
    };
alpar@397
    45
    //Edges are double linked.
alpar@397
    46
    //The free edges are only single linked using the "next_in" field.
alpar@395
    47
    struct EdgeT 
alpar@395
    48
    {
alpar@397
    49
      int head, tail;
alpar@397
    50
      int prev_in, prev_out;
alpar@397
    51
      int next_in, next_out;
alpar@395
    52
    };
alpar@395
    53
alpar@395
    54
    std::vector<NodeT> nodes;
alpar@397
    55
    //The first node
alpar@397
    56
    int first_node;
alpar@397
    57
    //The first free node
alpar@397
    58
    int first_free_node;
alpar@395
    59
    std::vector<EdgeT> edges;
alpar@397
    60
    //The first free edge
alpar@397
    61
    int first_free_edge;
alpar@395
    62
    
deba@782
    63
  public:
alpar@395
    64
    
deba@782
    65
    typedef ListGraph Graph;
alpar@397
    66
    
alpar@395
    67
    class Node;
alpar@395
    68
    class Edge;
alpar@395
    69
alpar@395
    70
    
alpar@395
    71
  public:
alpar@395
    72
alpar@395
    73
    class NodeIt;
alpar@395
    74
    class EdgeIt;
alpar@395
    75
    class OutEdgeIt;
alpar@395
    76
    class InEdgeIt;
deba@782
    77
deba@822
    78
    /// Creating map registries.
deba@782
    79
    CREATE_MAP_REGISTRIES;
deba@822
    80
    /// Creating node and edge maps.
alpar@827
    81
alpar@827
    82
    /// \todo
alpar@827
    83
    /// It apears in the documentation as if it were a function definition.
deba@822
    84
    CREATE_MAPS(DefaultMap);
deba@782
    85
alpar@395
    86
  public:
alpar@395
    87
deba@782
    88
    ListGraph() 
deba@782
    89
      : nodes(), first_node(-1),
deba@782
    90
	first_free_node(-1), edges(), first_free_edge(-1) {}
deba@782
    91
deba@782
    92
    ListGraph(const ListGraph &_g) 
deba@782
    93
      : nodes(_g.nodes), first_node(_g.first_node),
deba@782
    94
	first_free_node(_g.first_free_node), edges(_g.edges),
deba@782
    95
	first_free_edge(_g.first_free_edge) {}
alpar@395
    96
    
alpar@813
    97
    ///Number of nodes.
alpar@813
    98
    int nodeNum() const { return nodes.size(); }
alpar@813
    99
    ///Number of edges.
alpar@813
   100
    int edgeNum() const { return edges.size(); }
alpar@395
   101
alpar@813
   102
    ///Set the expected maximum number of edges.
alpar@695
   103
alpar@695
   104
    ///With this function, it is possible to set the expected number of edges.
alpar@695
   105
    ///The use of this fasten the building of the graph and makes
alpar@695
   106
    ///it possible to avoid the superfluous memory allocation.
alpar@695
   107
    void reserveEdge(int n) { edges.reserve(n); };
alpar@695
   108
    
alpar@813
   109
    /// Maximum node ID.
alpar@813
   110
    
alpar@813
   111
    /// Maximum node ID.
alpar@813
   112
    ///\sa id(Node)
alpar@813
   113
    int maxNodeId() const { return nodes.size()-1; } 
alpar@813
   114
    /// Maximum edge ID.
alpar@813
   115
    
alpar@813
   116
    /// Maximum edge ID.
alpar@813
   117
    ///\sa id(Edge)
alpar@813
   118
    int maxEdgeId() const { return edges.size()-1; }
alpar@395
   119
alpar@395
   120
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@395
   121
    Node head(Edge e) const { return edges[e.n].head; }
alpar@395
   122
alpar@713
   123
    NodeIt& first(NodeIt& v) const { 
alpar@395
   124
      v=NodeIt(*this); return v; }
alpar@713
   125
    EdgeIt& first(EdgeIt& e) const { 
alpar@395
   126
      e=EdgeIt(*this); return e; }
alpar@713
   127
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@395
   128
      e=OutEdgeIt(*this,v); return e; }
alpar@713
   129
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@395
   130
      e=InEdgeIt(*this,v); return e; }
alpar@395
   131
alpar@813
   132
    /// Node ID.
alpar@813
   133
    
alpar@813
   134
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   135
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   136
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   137
    ///
alpar@813
   138
    /// The ID of the \ref INVALID node is -1.
alpar@813
   139
    ///\return The ID of the node \c v. 
alpar@713
   140
    static int id(Node v) { return v.n; }
alpar@813
   141
    /// Edge ID.
alpar@813
   142
    
alpar@813
   143
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   144
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   145
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   146
    ///
alpar@813
   147
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   148
    ///\return The ID of the edge \c e. 
alpar@713
   149
    static int id(Edge e) { return e.n; }
alpar@395
   150
alpar@397
   151
    /// Adds a new node to the graph.
alpar@397
   152
alpar@813
   153
    /// \warning It adds the new node to the front of the list.
alpar@397
   154
    /// (i.e. the lastly added node becomes the first.)
alpar@395
   155
    Node addNode() {
alpar@397
   156
      int n;
alpar@397
   157
      
alpar@397
   158
      if(first_free_node==-1)
alpar@397
   159
	{
alpar@397
   160
	  n = nodes.size();
alpar@397
   161
	  nodes.push_back(NodeT());
alpar@397
   162
	}
alpar@397
   163
      else {
alpar@397
   164
	n = first_free_node;
alpar@397
   165
	first_free_node = nodes[n].next;
alpar@397
   166
      }
alpar@397
   167
      
alpar@397
   168
      nodes[n].next = first_node;
alpar@397
   169
      if(first_node != -1) nodes[first_node].prev = n;
alpar@397
   170
      first_node = n;
alpar@397
   171
      nodes[n].prev = -1;
alpar@397
   172
      
alpar@397
   173
      nodes[n].first_in = nodes[n].first_out = -1;
alpar@397
   174
      
alpar@397
   175
      Node nn; nn.n=n;
alpar@395
   176
alpar@397
   177
      //Update dynamic maps
deba@782
   178
      node_maps.add(nn);
alpar@395
   179
alpar@397
   180
      return nn;
alpar@395
   181
    }
alpar@395
   182
    
alpar@395
   183
    Edge addEdge(Node u, Node v) {
alpar@397
   184
      int n;
alpar@397
   185
      
alpar@397
   186
      if(first_free_edge==-1)
alpar@397
   187
	{
alpar@397
   188
	  n = edges.size();
alpar@397
   189
	  edges.push_back(EdgeT());
alpar@397
   190
	}
alpar@397
   191
      else {
alpar@397
   192
	n = first_free_edge;
alpar@397
   193
	first_free_edge = edges[n].next_in;
alpar@397
   194
      }
alpar@397
   195
      
alpar@397
   196
      edges[n].tail = u.n; edges[n].head = v.n;
alpar@395
   197
alpar@397
   198
      edges[n].next_out = nodes[u.n].first_out;
alpar@397
   199
      if(nodes[u.n].first_out != -1) edges[nodes[u.n].first_out].prev_out = n;
alpar@397
   200
      edges[n].next_in = nodes[v.n].first_in;
alpar@397
   201
      if(nodes[v.n].first_in != -1) edges[nodes[v.n].first_in].prev_in = n;
alpar@397
   202
      edges[n].prev_in = edges[n].prev_out = -1;
alpar@397
   203
	
alpar@397
   204
      nodes[u.n].first_out = nodes[v.n].first_in = n;
alpar@397
   205
alpar@397
   206
      Edge e; e.n=n;
alpar@397
   207
alpar@397
   208
      //Update dynamic maps
deba@782
   209
      edge_maps.add(e);
alpar@395
   210
alpar@395
   211
      return e;
alpar@395
   212
    }
alpar@774
   213
    
alpar@774
   214
    /// Finds an edge between two nodes.
alpar@395
   215
alpar@774
   216
    /// Finds an edge from node \c u to node \c v.
alpar@774
   217
    ///
alpar@774
   218
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   219
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   220
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   221
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   222
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   223
    {
alpar@774
   224
      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
alpar@774
   225
      while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
alpar@774
   226
      prev.n=e;
alpar@774
   227
      return prev;
alpar@774
   228
    }
alpar@774
   229
    
alpar@397
   230
  private:
alpar@397
   231
    void eraseEdge(int n) {
alpar@397
   232
      
alpar@397
   233
      if(edges[n].next_in!=-1)
alpar@397
   234
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
alpar@397
   235
      if(edges[n].prev_in!=-1)
alpar@397
   236
	edges[edges[n].prev_in].next_in = edges[n].next_in;
alpar@397
   237
      else nodes[edges[n].head].first_in = edges[n].next_in;
alpar@397
   238
      
alpar@397
   239
      if(edges[n].next_out!=-1)
alpar@397
   240
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
alpar@397
   241
      if(edges[n].prev_out!=-1)
alpar@397
   242
	edges[edges[n].prev_out].next_out = edges[n].next_out;
alpar@397
   243
      else nodes[edges[n].tail].first_out = edges[n].next_out;
alpar@397
   244
      
alpar@397
   245
      edges[n].next_in = first_free_edge;
alpar@695
   246
      first_free_edge = n;      
alpar@397
   247
alpar@397
   248
      //Update dynamic maps
alpar@397
   249
      Edge e; e.n=n;
deba@782
   250
      edge_maps.erase(e);
deba@782
   251
alpar@397
   252
    }
alpar@397
   253
      
alpar@397
   254
  public:
alpar@397
   255
alpar@397
   256
    void erase(Node nn) {
alpar@397
   257
      int n=nn.n;
alpar@397
   258
      
alpar@397
   259
      int m;
alpar@397
   260
      while((m=nodes[n].first_in)!=-1) eraseEdge(m);
alpar@397
   261
      while((m=nodes[n].first_out)!=-1) eraseEdge(m);
alpar@397
   262
alpar@397
   263
      if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
alpar@397
   264
      if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
alpar@397
   265
      else first_node = nodes[n].next;
alpar@397
   266
      
alpar@397
   267
      nodes[n].next = first_free_node;
alpar@397
   268
      first_free_node = n;
alpar@397
   269
alpar@397
   270
      //Update dynamic maps
deba@782
   271
      node_maps.erase(nn);
deba@782
   272
alpar@397
   273
    }
alpar@397
   274
    
alpar@397
   275
    void erase(Edge e) { eraseEdge(e.n); }
alpar@397
   276
alpar@397
   277
    void clear() {
deba@782
   278
      edge_maps.clear();
deba@782
   279
      edges.clear();
deba@782
   280
      node_maps.clear();
deba@782
   281
      nodes.clear();
alpar@397
   282
      first_node=first_free_node=first_free_edge=-1;
alpar@397
   283
    }
alpar@395
   284
alpar@395
   285
    class Node {
alpar@397
   286
      friend class ListGraph;
alpar@395
   287
      template <typename T> friend class NodeMap;
alpar@400
   288
       
alpar@395
   289
      friend class Edge;
alpar@395
   290
      friend class OutEdgeIt;
alpar@395
   291
      friend class InEdgeIt;
alpar@395
   292
      friend class SymEdge;
alpar@395
   293
alpar@395
   294
    protected:
alpar@395
   295
      int n;
alpar@722
   296
      friend int ListGraph::id(Node v); 
alpar@395
   297
      Node(int nn) {n=nn;}
alpar@395
   298
    public:
alpar@395
   299
      Node() {}
alpar@503
   300
      Node (Invalid) { n=-1; }
alpar@395
   301
      bool operator==(const Node i) const {return n==i.n;}
alpar@395
   302
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@395
   303
      bool operator<(const Node i) const {return n<i.n;}
alpar@774
   304
      //      ///Validity check
alpar@774
   305
      //      operator bool() { return n!=-1; }
alpar@395
   306
    };
alpar@395
   307
    
alpar@395
   308
    class NodeIt : public Node {
alpar@774
   309
      const ListGraph *G;
alpar@397
   310
      friend class ListGraph;
alpar@395
   311
    public:
alpar@400
   312
      NodeIt() : Node() { }
alpar@400
   313
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   314
      NodeIt(const ListGraph& _G) : Node(_G.first_node), G(&_G) { }
alpar@579
   315
      ///\todo Undocumented conversion Node -\> NodeIt.
alpar@774
   316
      NodeIt(const ListGraph& _G,Node n) : Node(n), G(&_G) { }
alpar@774
   317
      NodeIt &operator++() {
alpar@774
   318
	n=G->nodes[n].next; 
alpar@774
   319
	return *this; 
alpar@774
   320
      }
alpar@774
   321
      //      ///Validity check
alpar@774
   322
      //      operator bool() { return Node::operator bool(); }      
alpar@395
   323
    };
alpar@395
   324
alpar@395
   325
    class Edge {
alpar@397
   326
      friend class ListGraph;
alpar@395
   327
      template <typename T> friend class EdgeMap;
alpar@395
   328
alpar@397
   329
      //template <typename T> friend class SymListGraph::SymEdgeMap;      
alpar@397
   330
      //friend Edge SymListGraph::opposite(Edge) const;
alpar@395
   331
      
alpar@395
   332
      friend class Node;
alpar@395
   333
      friend class NodeIt;
alpar@395
   334
    protected:
alpar@395
   335
      int n;
alpar@722
   336
      friend int ListGraph::id(Edge e);
alpar@395
   337
alpar@706
   338
    public:
alpar@706
   339
      /// An Edge with id \c n.
alpar@706
   340
alpar@706
   341
      /// \bug It should be
alpar@706
   342
      /// obtained by a member function of the Graph.
alpar@395
   343
      Edge(int nn) {n=nn;}
alpar@706
   344
alpar@395
   345
      Edge() { }
alpar@395
   346
      Edge (Invalid) { n=-1; }
alpar@395
   347
      bool operator==(const Edge i) const {return n==i.n;}
alpar@395
   348
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@395
   349
      bool operator<(const Edge i) const {return n<i.n;}
alpar@395
   350
      ///\bug This is a workaround until somebody tells me how to
alpar@397
   351
      ///make class \c SymListGraph::SymEdgeMap friend of Edge
alpar@395
   352
      int &idref() {return n;}
alpar@774
   353
      const int &idref() const {return n;} 
alpar@774
   354
      //      ///Validity check
alpar@774
   355
      //      operator bool() { return n!=-1; }
alpar@774
   356
   };
alpar@395
   357
    
alpar@395
   358
    class EdgeIt : public Edge {
alpar@774
   359
      const ListGraph *G;
alpar@397
   360
      friend class ListGraph;
alpar@395
   361
    public:
alpar@774
   362
      EdgeIt(const ListGraph& _G) : Edge(), G(&_G) {
alpar@397
   363
      	int m;
alpar@774
   364
	for(m=_G.first_node;
alpar@774
   365
	    m!=-1 && _G.nodes[m].first_in == -1; m = _G.nodes[m].next);
alpar@774
   366
	n = (m==-1)?-1:_G.nodes[m].first_in;
alpar@397
   367
      }
alpar@395
   368
      EdgeIt (Invalid i) : Edge(i) { }
alpar@774
   369
      EdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@395
   370
      EdgeIt() : Edge() { }
alpar@395
   371
      ///\bug This is a workaround until somebody tells me how to
alpar@397
   372
      ///make class \c SymListGraph::SymEdgeMap friend of Edge
alpar@395
   373
      int &idref() {return n;}
alpar@774
   374
      EdgeIt &operator++() {
alpar@774
   375
	if(G->edges[n].next_in!=-1) n=G->edges[n].next_in;
alpar@774
   376
	else {
alpar@774
   377
	  int nn;
alpar@774
   378
	  for(nn=G->nodes[G->edges[n].head].next;
alpar@774
   379
	      nn!=-1 && G->nodes[nn].first_in == -1;
alpar@774
   380
	      nn = G->nodes[nn].next) ;
alpar@774
   381
	  n = (nn==-1)?-1:G->nodes[nn].first_in;
alpar@774
   382
	}
alpar@774
   383
	return *this;
alpar@774
   384
      }
alpar@774
   385
      //      ///Validity check
alpar@774
   386
      //      operator bool() { return Edge::operator bool(); }      
alpar@395
   387
    };
alpar@395
   388
    
alpar@395
   389
    class OutEdgeIt : public Edge {
alpar@774
   390
      const ListGraph *G;
alpar@397
   391
      friend class ListGraph;
alpar@395
   392
    public: 
alpar@395
   393
      OutEdgeIt() : Edge() { }
alpar@774
   394
      OutEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@395
   395
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@395
   396
alpar@774
   397
      OutEdgeIt(const ListGraph& _G,const Node v)
alpar@774
   398
	: Edge(_G.nodes[v.n].first_out), G(&_G) {}
alpar@774
   399
      OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
alpar@774
   400
      //      ///Validity check
alpar@774
   401
      //      operator bool() { return Edge::operator bool(); }      
alpar@395
   402
    };
alpar@395
   403
    
alpar@395
   404
    class InEdgeIt : public Edge {
alpar@774
   405
      const ListGraph *G;
alpar@397
   406
      friend class ListGraph;
alpar@395
   407
    public: 
alpar@395
   408
      InEdgeIt() : Edge() { }
alpar@774
   409
      InEdgeIt(const ListGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@395
   410
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@774
   411
      InEdgeIt(const ListGraph& _G,Node v)
alpar@774
   412
	: Edge(_G.nodes[v.n].first_in), G(&_G) { }
alpar@774
   413
      InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
alpar@774
   414
      //      ///Validity check
alpar@774
   415
      //      operator bool() { return Edge::operator bool(); }      
alpar@395
   416
    };
alpar@395
   417
  };
alpar@395
   418
alpar@395
   419
  ///Graph for bidirectional edges.
alpar@395
   420
alpar@395
   421
  ///The purpose of this graph structure is to handle graphs
alpar@395
   422
  ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
alpar@395
   423
  ///of oppositely directed edges.
alpar@395
   424
  ///There is a new edge map type called
alpar@397
   425
  ///\ref SymListGraph::SymEdgeMap "SymEdgeMap"
alpar@395
   426
  ///that complements this
alpar@395
   427
  ///feature by
alpar@395
   428
  ///storing shared values for the edge pairs. The usual
alpar@395
   429
  ///\ref GraphSkeleton::EdgeMap "EdgeMap"
alpar@395
   430
  ///can be used
alpar@395
   431
  ///as well.
alpar@395
   432
  ///
alpar@395
   433
  ///The oppositely directed edge can also be obtained easily
alpar@395
   434
  ///using \ref opposite.
alpar@397
   435
  ///
alpar@397
   436
  ///Here erase(Edge) deletes a pair of edges.
alpar@397
   437
  ///
alpar@397
   438
  ///\todo this date structure need some reconsiderations. Maybe it
alpar@397
   439
  ///should be implemented independently from ListGraph.
deba@782
   440
  
alpar@397
   441
  class SymListGraph : public ListGraph
alpar@395
   442
  {
alpar@395
   443
  public:
deba@782
   444
deba@782
   445
    typedef SymListGraph Graph;
deba@782
   446
deba@822
   447
    /// Importing maps from the base class ListGraph.
deba@822
   448
    KEEP_MAPS(ListGraph, SymListGraph);
deba@782
   449
deba@822
   450
    /// Creating symmetric map registry.
deba@782
   451
    CREATE_SYM_EDGE_MAP_REGISTRY;
deba@822
   452
    /// Creating symmetric edge map.
deba@822
   453
    CREATE_SYM_EDGE_MAP(DefaultMap);
alpar@395
   454
alpar@397
   455
    SymListGraph() : ListGraph() { }
alpar@397
   456
    SymListGraph(const ListGraph &_g) : ListGraph(_g) { }
alpar@397
   457
    ///Adds a pair of oppositely directed edges to the graph.
alpar@395
   458
    Edge addEdge(Node u, Node v)
alpar@395
   459
    {
alpar@397
   460
      Edge e = ListGraph::addEdge(u,v);
deba@782
   461
      Edge f = ListGraph::addEdge(v,u);
deba@782
   462
      sym_edge_maps.add(e);
deba@782
   463
      sym_edge_maps.add(f);
deba@782
   464
      
alpar@395
   465
      return e;
alpar@395
   466
    }
alpar@395
   467
deba@782
   468
    void erase(Node n) { ListGraph::erase(n);}
alpar@395
   469
    ///The oppositely directed edge.
alpar@395
   470
alpar@395
   471
    ///Returns the oppositely directed
alpar@395
   472
    ///pair of the edge \c e.
alpar@713
   473
    static Edge opposite(Edge e)
alpar@395
   474
    {
alpar@395
   475
      Edge f;
alpar@395
   476
      f.idref() = e.idref() - 2*(e.idref()%2) + 1;
alpar@395
   477
      return f;
alpar@395
   478
    }
alpar@395
   479
    
alpar@397
   480
    ///Removes a pair of oppositely directed edges to the graph.
alpar@397
   481
    void erase(Edge e) {
deba@782
   482
      Edge f = opposite(e);
deba@782
   483
      sym_edge_maps.erase(e);
deba@782
   484
      sym_edge_maps.erase(f);
deba@782
   485
      ListGraph::erase(f);
alpar@397
   486
      ListGraph::erase(e);
deba@782
   487
    }    
deba@782
   488
  };
alpar@395
   489
alpar@400
   490
alpar@401
   491
  ///A graph class containing only nodes.
alpar@400
   492
alpar@401
   493
  ///This class implements a graph structure without edges.
alpar@401
   494
  ///The most useful application of this class is to be the node set of an
alpar@401
   495
  ///\ref EdgeSet class.
alpar@400
   496
  ///
alpar@400
   497
  ///It conforms to the graph interface documented under
alpar@401
   498
  ///the description of \ref GraphSkeleton with the exception that you cannot
alpar@401
   499
  ///add (or delete) edges. The usual edge iterators are exists, but they are
alpar@401
   500
  ///always \ref INVALID.
alpar@401
   501
  ///\sa \ref GraphSkeleton
alpar@508
   502
  ///\sa \ref EdgeSet
alpar@400
   503
  class NodeSet {
alpar@400
   504
alpar@400
   505
    //Nodes are double linked.
alpar@400
   506
    //The free nodes are only single linked using the "next" field.
alpar@400
   507
    struct NodeT 
alpar@400
   508
    {
alpar@400
   509
      int first_in,first_out;
alpar@400
   510
      int prev, next;
alpar@400
   511
      //      NodeT() {}
alpar@400
   512
    };
alpar@400
   513
alpar@400
   514
    std::vector<NodeT> nodes;
alpar@400
   515
    //The first node
alpar@400
   516
    int first_node;
alpar@400
   517
    //The first free node
alpar@400
   518
    int first_free_node;
alpar@400
   519
    
alpar@400
   520
  public:
deba@782
   521
deba@782
   522
    typedef NodeSet Graph;
alpar@400
   523
    
alpar@400
   524
    class Node;
alpar@400
   525
    class Edge;
alpar@400
   526
alpar@400
   527
  public:
alpar@400
   528
alpar@400
   529
    class NodeIt;
alpar@400
   530
    class EdgeIt;
alpar@400
   531
    class OutEdgeIt;
alpar@400
   532
    class InEdgeIt;
alpar@400
   533
    
deba@822
   534
    /// Creating node map registry.
deba@822
   535
    CREATE_NODE_MAP_REGISTRY;
deba@822
   536
    /// Creating node maps.
deba@822
   537
    CREATE_NODE_MAP(DefaultMap);
deba@822
   538
deba@822
   539
    /// Creating empty map structure for edges.
deba@822
   540
    template <typename Value>
deba@822
   541
    class EdgeMap {
deba@822
   542
    public:
deba@822
   543
      EdgeMap() {}
deba@822
   544
      EdgeMap(const Graph&) {}
deba@822
   545
      EdgeMap(const Graph&, const Value&) {}
deba@822
   546
deba@822
   547
      EdgeMap(const EdgeMap&) {}
deba@822
   548
      template <typename CMap> EdgeMap(const CMap&) {}
deba@822
   549
deba@822
   550
      EdgeMap& operator=(const EdgeMap&) {}
deba@822
   551
      template <typename CMap> EdgeMap& operator=(const CMap&) {}
deba@822
   552
      
deba@822
   553
      class ConstIterator {
deba@822
   554
      public:
deba@822
   555
	bool operator==(const ConstIterator&) {return true;}
deba@822
   556
	bool operator!=(const ConstIterator&) {return false;}
deba@822
   557
      };
deba@822
   558
deba@822
   559
      typedef ConstIterator Iterator;
deba@822
   560
      
deba@822
   561
      Iterator begin() { return Iterator();}
deba@822
   562
      Iterator end() { return Iterator();}
deba@822
   563
deba@822
   564
      ConstIterator begin() const { return ConstIterator();}
deba@822
   565
      ConstIterator end() const { return ConstIterator();}
deba@822
   566
deba@822
   567
    };
alpar@400
   568
    
alpar@400
   569
  public:
alpar@400
   570
alpar@408
   571
    ///Default constructor
deba@782
   572
    NodeSet() 
deba@782
   573
      : nodes(), first_node(-1), first_free_node(-1) {}
alpar@408
   574
    ///Copy constructor
deba@782
   575
    NodeSet(const NodeSet &_g) 
deba@782
   576
      : nodes(_g.nodes), first_node(_g.first_node),
deba@782
   577
	first_free_node(_g.first_free_node) {}
alpar@400
   578
    
alpar@813
   579
    ///Number of nodes.
alpar@813
   580
    int nodeNum() const { return nodes.size(); }
alpar@813
   581
    ///Number of edges.
alpar@813
   582
    int edgeNum() const { return 0; }
alpar@400
   583
alpar@813
   584
    /// Maximum node ID.
alpar@813
   585
    
alpar@813
   586
    /// Maximum node ID.
alpar@813
   587
    ///\sa id(Node)
alpar@813
   588
    int maxNodeId() const { return nodes.size()-1; }
alpar@813
   589
    /// Maximum edge ID.
alpar@813
   590
    
alpar@813
   591
    /// Maximum edge ID.
alpar@813
   592
    ///\sa id(Edge)
alpar@813
   593
    int maxEdgeId() const { return 0; }
alpar@400
   594
alpar@400
   595
    Node tail(Edge e) const { return INVALID; }
alpar@400
   596
    Node head(Edge e) const { return INVALID; }
alpar@400
   597
alpar@400
   598
    NodeIt& first(NodeIt& v) const { 
alpar@400
   599
      v=NodeIt(*this); return v; }
alpar@400
   600
    EdgeIt& first(EdgeIt& e) const { 
alpar@400
   601
      e=EdgeIt(*this); return e; }
alpar@400
   602
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@400
   603
      e=OutEdgeIt(*this,v); return e; }
alpar@400
   604
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@400
   605
      e=InEdgeIt(*this,v); return e; }
alpar@400
   606
alpar@813
   607
    /// Node ID.
alpar@813
   608
    
alpar@813
   609
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   610
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   611
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   612
    ///
alpar@813
   613
    /// The ID of the \ref INVALID node is -1.
alpar@813
   614
    ///\return The ID of the node \c v. 
alpar@400
   615
    int id(Node v) const { return v.n; }
alpar@813
   616
    /// Edge ID.
alpar@813
   617
    
alpar@813
   618
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   619
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   620
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   621
    ///
alpar@813
   622
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   623
    ///\return The ID of the edge \c e. 
alpar@400
   624
    int id(Edge e) const { return -1; }
alpar@400
   625
alpar@400
   626
    /// Adds a new node to the graph.
alpar@400
   627
alpar@813
   628
    /// \warning It adds the new node to the front of the list.
alpar@400
   629
    /// (i.e. the lastly added node becomes the first.)
alpar@400
   630
    Node addNode() {
alpar@400
   631
      int n;
alpar@400
   632
      
alpar@400
   633
      if(first_free_node==-1)
alpar@400
   634
	{
alpar@400
   635
	  n = nodes.size();
alpar@400
   636
	  nodes.push_back(NodeT());
alpar@400
   637
	}
alpar@400
   638
      else {
alpar@400
   639
	n = first_free_node;
alpar@400
   640
	first_free_node = nodes[n].next;
alpar@400
   641
      }
alpar@400
   642
      
alpar@400
   643
      nodes[n].next = first_node;
alpar@400
   644
      if(first_node != -1) nodes[first_node].prev = n;
alpar@400
   645
      first_node = n;
alpar@400
   646
      nodes[n].prev = -1;
alpar@400
   647
      
alpar@400
   648
      nodes[n].first_in = nodes[n].first_out = -1;
alpar@400
   649
      
alpar@400
   650
      Node nn; nn.n=n;
alpar@400
   651
alpar@400
   652
      //Update dynamic maps
deba@782
   653
      node_maps.add(nn);
alpar@400
   654
alpar@400
   655
      return nn;
alpar@400
   656
    }
alpar@400
   657
    
alpar@400
   658
    void erase(Node nn) {
alpar@400
   659
      int n=nn.n;
alpar@400
   660
      
alpar@400
   661
      if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
alpar@400
   662
      if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
alpar@400
   663
      else first_node = nodes[n].next;
alpar@400
   664
      
alpar@400
   665
      nodes[n].next = first_free_node;
alpar@400
   666
      first_free_node = n;
alpar@400
   667
alpar@400
   668
      //Update dynamic maps
deba@782
   669
      node_maps.erase(nn);
alpar@400
   670
    }
alpar@400
   671
    
alpar@774
   672
        
alpar@774
   673
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   674
    {
alpar@774
   675
      return INVALID;
alpar@774
   676
    }
alpar@774
   677
    
alpar@400
   678
    void clear() {
deba@782
   679
      node_maps.clear();
alpar@400
   680
      nodes.clear();
alpar@400
   681
      first_node = first_free_node = -1;
alpar@400
   682
    }
alpar@400
   683
alpar@400
   684
    class Node {
alpar@400
   685
      friend class NodeSet;
alpar@400
   686
      template <typename T> friend class NodeMap;
alpar@400
   687
      
alpar@400
   688
      friend class Edge;
alpar@400
   689
      friend class OutEdgeIt;
alpar@400
   690
      friend class InEdgeIt;
alpar@400
   691
alpar@400
   692
    protected:
alpar@400
   693
      int n;
alpar@400
   694
      friend int NodeSet::id(Node v) const; 
alpar@400
   695
      Node(int nn) {n=nn;}
alpar@400
   696
    public:
alpar@400
   697
      Node() {}
alpar@400
   698
      Node (Invalid i) { n=-1; }
alpar@400
   699
      bool operator==(const Node i) const {return n==i.n;}
alpar@400
   700
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@400
   701
      bool operator<(const Node i) const {return n<i.n;}
alpar@400
   702
    };
alpar@400
   703
    
alpar@400
   704
    class NodeIt : public Node {
alpar@774
   705
      const NodeSet *G;
alpar@400
   706
      friend class NodeSet;
alpar@400
   707
    public:
alpar@579
   708
      NodeIt() : Node() { }
alpar@774
   709
      NodeIt(const NodeSet& _G,Node n) : Node(n), G(&_G) { }
alpar@579
   710
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   711
      NodeIt(const NodeSet& _G) : Node(_G.first_node), G(&_G) { }
alpar@774
   712
      NodeIt &operator++() {
alpar@774
   713
	n=G->nodes[n].next; 
alpar@774
   714
	return *this; 
alpar@774
   715
      }
alpar@400
   716
    };
alpar@400
   717
alpar@400
   718
    class Edge {
alpar@400
   719
      //friend class NodeSet;
alpar@400
   720
      //template <typename T> friend class EdgeMap;
alpar@400
   721
alpar@400
   722
      //template <typename T> friend class SymNodeSet::SymEdgeMap;      
alpar@400
   723
      //friend Edge SymNodeSet::opposite(Edge) const;
alpar@400
   724
      
alpar@400
   725
      //      friend class Node;
alpar@400
   726
      //      friend class NodeIt;
alpar@400
   727
    protected:
alpar@400
   728
      //friend int NodeSet::id(Edge e) const;
alpar@400
   729
      //      Edge(int nn) {}
alpar@400
   730
    public:
alpar@400
   731
      Edge() { }
alpar@400
   732
      Edge (Invalid) { }
alpar@400
   733
      bool operator==(const Edge i) const {return true;}
alpar@400
   734
      bool operator!=(const Edge i) const {return false;}
alpar@400
   735
      bool operator<(const Edge i) const {return false;}
alpar@400
   736
      ///\bug This is a workaround until somebody tells me how to
alpar@400
   737
      ///make class \c SymNodeSet::SymEdgeMap friend of Edge
alpar@400
   738
      //      int idref() {return -1;}
alpar@400
   739
      //      int idref() const {return -1;}
alpar@400
   740
    };
alpar@400
   741
    
alpar@400
   742
    class EdgeIt : public Edge {
alpar@400
   743
      //friend class NodeSet;
alpar@400
   744
    public:
alpar@400
   745
      EdgeIt(const NodeSet& G) : Edge() { }
alpar@774
   746
      EdgeIt(const NodeSet&, Edge) : Edge() { }
alpar@400
   747
      EdgeIt (Invalid i) : Edge(i) { }
alpar@400
   748
      EdgeIt() : Edge() { }
alpar@400
   749
      ///\bug This is a workaround until somebody tells me how to
alpar@400
   750
      ///make class \c SymNodeSet::SymEdgeMap friend of Edge
alpar@400
   751
      //      int idref() {return -1;}
alpar@774
   752
      EdgeIt operator++() { return INVALID; }
alpar@400
   753
    };
alpar@400
   754
    
alpar@400
   755
    class OutEdgeIt : public Edge {
alpar@400
   756
      friend class NodeSet;
alpar@400
   757
    public: 
alpar@400
   758
      OutEdgeIt() : Edge() { }
alpar@774
   759
      OutEdgeIt(const NodeSet&, Edge) : Edge() { }
alpar@400
   760
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@400
   761
      OutEdgeIt(const NodeSet& G,const Node v)	: Edge() {}
alpar@774
   762
      OutEdgeIt operator++() { return INVALID; }
alpar@400
   763
    };
alpar@400
   764
    
alpar@400
   765
    class InEdgeIt : public Edge {
alpar@400
   766
      friend class NodeSet;
alpar@400
   767
    public: 
alpar@400
   768
      InEdgeIt() : Edge() { }
alpar@774
   769
      InEdgeIt(const NodeSet&, Edge) : Edge() { }
alpar@400
   770
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@400
   771
      InEdgeIt(const NodeSet& G,Node v) :Edge() {}
alpar@774
   772
      InEdgeIt operator++() { return INVALID; }
alpar@400
   773
    };
alpar@400
   774
alpar@400
   775
  };
alpar@400
   776
alpar@400
   777
alpar@400
   778
alpar@401
   779
  ///Graph structure using a node set of another graph.
alpar@401
   780
alpar@401
   781
  ///This structure can be used to establish another graph over a node set
alpar@401
   782
  /// of an existing one. The node iterator will go through the nodes of the
alpar@401
   783
  /// original graph, and the NodeMap's of both graphs will convert to
alpar@401
   784
  /// each other.
alpar@401
   785
  ///
alpar@404
   786
  ///\warning Adding or deleting nodes from the graph is not safe if an
alpar@404
   787
  ///\ref EdgeSet is currently attached to it!
alpar@404
   788
  ///
alpar@404
   789
  ///\todo Make it possible to add/delete edges from the base graph
alpar@404
   790
  ///(and from \ref EdgeSet, as well)
alpar@404
   791
  ///
alpar@401
   792
  ///\param GG The type of the graph which shares its node set with this class.
alpar@401
   793
  ///Its interface must conform with \ref GraphSkeleton.
alpar@400
   794
  ///
alpar@400
   795
  ///It conforms to the graph interface documented under
alpar@400
   796
  ///the description of \ref GraphSkeleton.
alpar@400
   797
  ///\sa \ref GraphSkeleton.
alpar@401
   798
  ///\sa \ref NodeSet.
alpar@400
   799
  template<typename GG>
alpar@400
   800
  class EdgeSet {
alpar@400
   801
alpar@400
   802
    typedef GG NodeGraphType;
alpar@400
   803
alpar@400
   804
    NodeGraphType &G;
alpar@400
   805
alpar@515
   806
  public:
deba@782
   807
alpar@400
   808
    class Node;
alpar@705
   809
    class Edge;
alpar@705
   810
    class OutEdgeIt;
alpar@705
   811
    class InEdgeIt;
alpar@705
   812
    class SymEdge;
deba@782
   813
deba@782
   814
    typedef EdgeSet Graph;
deba@782
   815
alpar@531
   816
    int id(Node v) const; 
alpar@531
   817
alpar@531
   818
    class Node : public NodeGraphType::Node {
alpar@531
   819
      friend class EdgeSet;
alpar@531
   820
      //      template <typename T> friend class NodeMap;
alpar@531
   821
      
alpar@531
   822
      friend class Edge;
alpar@531
   823
      friend class OutEdgeIt;
alpar@531
   824
      friend class InEdgeIt;
alpar@531
   825
      friend class SymEdge;
alpar@531
   826
alpar@531
   827
    public:
alpar@531
   828
      friend int EdgeSet::id(Node v) const; 
alpar@531
   829
      //      Node(int nn) {n=nn;}
alpar@531
   830
    public:
alpar@531
   831
      Node() : NodeGraphType::Node() {}
alpar@531
   832
      Node (Invalid i) : NodeGraphType::Node(i) {}
alpar@531
   833
      Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
alpar@531
   834
    };
alpar@531
   835
    
alpar@531
   836
    class NodeIt : public NodeGraphType::NodeIt {
alpar@531
   837
      friend class EdgeSet;
alpar@531
   838
    public:
alpar@531
   839
      NodeIt() : NodeGraphType::NodeIt() { }
alpar@774
   840
      NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { }
alpar@531
   841
      NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
alpar@531
   842
      NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { }
alpar@531
   843
      NodeIt(const typename NodeGraphType::NodeIt &n)
alpar@531
   844
	: NodeGraphType::NodeIt(n) {}
alpar@579
   845
alpar@531
   846
      operator Node() { return Node(*this);}
alpar@774
   847
      NodeIt &operator++()
alpar@774
   848
      { this->NodeGraphType::NodeIt::operator++(); return *this;} 
alpar@531
   849
    };
alpar@515
   850
alpar@515
   851
  private:
alpar@400
   852
    //Edges are double linked.
alpar@400
   853
    //The free edges are only single linked using the "next_in" field.
alpar@400
   854
    struct NodeT 
alpar@400
   855
    {
alpar@400
   856
      int first_in,first_out;
alpar@400
   857
      NodeT() : first_in(-1), first_out(-1) { }
alpar@400
   858
    };
alpar@400
   859
alpar@400
   860
    struct EdgeT 
alpar@400
   861
    {
alpar@400
   862
      Node head, tail;
alpar@400
   863
      int prev_in, prev_out;
alpar@400
   864
      int next_in, next_out;
alpar@400
   865
    };
alpar@400
   866
alpar@400
   867
    
alpar@515
   868
    typename NodeGraphType::template NodeMap<NodeT> nodes;
alpar@400
   869
    
alpar@400
   870
    std::vector<EdgeT> edges;
alpar@400
   871
    //The first free edge
alpar@400
   872
    int first_free_edge;
alpar@400
   873
    
alpar@400
   874
  public:
alpar@400
   875
    
alpar@400
   876
    class Node;
alpar@400
   877
    class Edge;
alpar@400
   878
alpar@400
   879
    class NodeIt;
alpar@400
   880
    class EdgeIt;
alpar@400
   881
    class OutEdgeIt;
alpar@400
   882
    class InEdgeIt;
deba@782
   883
deba@782
   884
deba@822
   885
    /// Creating edge map registry.
deba@782
   886
    CREATE_EDGE_MAP_REGISTRY;
deba@822
   887
    /// Creating edge maps.
deba@822
   888
    CREATE_EDGE_MAP(DefaultMap);
deba@822
   889
deba@822
   890
    /// Importing node maps from the NodeGraphType.
deba@822
   891
    IMPORT_NODE_MAP(NodeGraphType, graph.G, EdgeSet, graph);
alpar@400
   892
    
alpar@400
   893
    
alpar@400
   894
  public:
alpar@400
   895
alpar@408
   896
    ///Constructor
alpar@408
   897
    
alpar@408
   898
    ///Construates a new graph based on the nodeset of an existing one.
alpar@408
   899
    ///\param _G the base graph.
alpar@408
   900
    ///\todo It looks like a copy constructor, but it isn't.
deba@782
   901
    EdgeSet(NodeGraphType &_G) 
deba@782
   902
      : G(_G), nodes(_G), edges(),
deba@782
   903
	first_free_edge(-1) {}
alpar@408
   904
    ///Copy constructor
alpar@408
   905
alpar@408
   906
    ///Makes a copy of an EdgeSet.
alpar@408
   907
    ///It will be based on the same graph.
deba@782
   908
    EdgeSet(const EdgeSet &_g) 
deba@782
   909
      : G(_g.G), nodes(_g.G), edges(_g.edges),
deba@782
   910
	first_free_edge(_g.first_free_edge) {}
alpar@400
   911
    
alpar@813
   912
    ///Number of nodes.
alpar@813
   913
    int nodeNum() const { return G.nodeNum(); }
alpar@813
   914
    ///Number of edges.
alpar@813
   915
    int edgeNum() const { return edges.size(); }
alpar@400
   916
alpar@813
   917
    /// Maximum node ID.
alpar@813
   918
    
alpar@813
   919
    /// Maximum node ID.
alpar@813
   920
    ///\sa id(Node)
alpar@813
   921
    int maxNodeId() const { return G.maxNodeId(); }
alpar@813
   922
    /// Maximum edge ID.
alpar@813
   923
    
alpar@813
   924
    /// Maximum edge ID.
alpar@813
   925
    ///\sa id(Edge)
alpar@813
   926
    int maxEdgeId() const { return edges.size()-1; }
alpar@400
   927
alpar@400
   928
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@400
   929
    Node head(Edge e) const { return edges[e.n].head; }
alpar@400
   930
alpar@400
   931
    NodeIt& first(NodeIt& v) const { 
alpar@400
   932
      v=NodeIt(*this); return v; }
alpar@400
   933
    EdgeIt& first(EdgeIt& e) const { 
alpar@400
   934
      e=EdgeIt(*this); return e; }
alpar@400
   935
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@400
   936
      e=OutEdgeIt(*this,v); return e; }
alpar@400
   937
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@400
   938
      e=InEdgeIt(*this,v); return e; }
alpar@400
   939
alpar@813
   940
    /// Node ID.
alpar@813
   941
    
alpar@813
   942
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   943
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   944
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   945
    ///
alpar@813
   946
    /// The ID of the \ref INVALID node is -1.
alpar@813
   947
    ///\return The ID of the node \c v. 
alpar@813
   948
    int id(Node v) { return G.id(v); }
alpar@813
   949
    /// Edge ID.
alpar@813
   950
    
alpar@813
   951
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   952
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   953
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   954
    ///
alpar@813
   955
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   956
    ///\return The ID of the edge \c e. 
alpar@400
   957
    int id(Edge e) const { return e.n; }
alpar@400
   958
alpar@400
   959
    /// Adds a new node to the graph.
alpar@579
   960
    Node addNode() { return G.addNode(); }
alpar@400
   961
    
alpar@400
   962
    Edge addEdge(Node u, Node v) {
alpar@400
   963
      int n;
alpar@400
   964
      
alpar@400
   965
      if(first_free_edge==-1)
alpar@400
   966
	{
alpar@400
   967
	  n = edges.size();
alpar@400
   968
	  edges.push_back(EdgeT());
alpar@400
   969
	}
alpar@400
   970
      else {
alpar@400
   971
	n = first_free_edge;
alpar@400
   972
	first_free_edge = edges[n].next_in;
alpar@400
   973
      }
alpar@400
   974
      
alpar@401
   975
      edges[n].tail = u; edges[n].head = v;
alpar@400
   976
alpar@401
   977
      edges[n].next_out = nodes[u].first_out;
alpar@401
   978
      if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n;
alpar@401
   979
      edges[n].next_in = nodes[v].first_in;
alpar@401
   980
      if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n;
alpar@400
   981
      edges[n].prev_in = edges[n].prev_out = -1;
alpar@400
   982
	
alpar@401
   983
      nodes[u].first_out = nodes[v].first_in = n;
alpar@400
   984
alpar@400
   985
      Edge e; e.n=n;
alpar@400
   986
alpar@400
   987
      //Update dynamic maps
deba@782
   988
      edge_maps.add(e);
alpar@400
   989
alpar@400
   990
      return e;
alpar@400
   991
    }
alpar@400
   992
alpar@774
   993
    /// Finds an edge between two nodes.
alpar@774
   994
alpar@774
   995
    /// Finds an edge from node \c u to node \c v.
alpar@774
   996
    ///
alpar@774
   997
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   998
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   999
    /// the next edge from \c u to \c v after \c prev.
alpar@774
  1000
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
  1001
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
  1002
    {
alpar@774
  1003
      int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out;
alpar@774
  1004
      while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out;
alpar@774
  1005
      prev.n=e;
alpar@774
  1006
      return prev;
alpar@774
  1007
    }
alpar@774
  1008
    
alpar@400
  1009
  private:
alpar@400
  1010
    void eraseEdge(int n) {
alpar@400
  1011
      
alpar@400
  1012
      if(edges[n].next_in!=-1)
alpar@400
  1013
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
alpar@400
  1014
      if(edges[n].prev_in!=-1)
alpar@400
  1015
	edges[edges[n].prev_in].next_in = edges[n].next_in;
alpar@400
  1016
      else nodes[edges[n].head].first_in = edges[n].next_in;
alpar@400
  1017
      
alpar@400
  1018
      if(edges[n].next_out!=-1)
alpar@400
  1019
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
alpar@400
  1020
      if(edges[n].prev_out!=-1)
alpar@400
  1021
	edges[edges[n].prev_out].next_out = edges[n].next_out;
alpar@400
  1022
      else nodes[edges[n].tail].first_out = edges[n].next_out;
alpar@400
  1023
      
alpar@400
  1024
      edges[n].next_in = first_free_edge;
alpar@400
  1025
      first_free_edge = -1;      
alpar@400
  1026
alpar@400
  1027
      //Update dynamic maps
deba@782
  1028
      Edge e; e.n = n;
deba@782
  1029
      edge_maps.erase(e);
alpar@400
  1030
    }
alpar@400
  1031
      
alpar@400
  1032
  public:
alpar@400
  1033
alpar@400
  1034
//     void erase(Node nn) {
alpar@400
  1035
//       int n=nn.n;
alpar@400
  1036
//       int m;
alpar@400
  1037
//       while((m=nodes[n].first_in)!=-1) eraseEdge(m);
alpar@400
  1038
//       while((m=nodes[n].first_out)!=-1) eraseEdge(m);
alpar@400
  1039
//     }
alpar@400
  1040
    
alpar@400
  1041
    void erase(Edge e) { eraseEdge(e.n); }
alpar@400
  1042
alpar@579
  1043
    ///Clear all edges. (Doesn't clear the nodes!)
alpar@579
  1044
    void clear() {
deba@782
  1045
      edge_maps.clear();
alpar@579
  1046
      edges.clear();
alpar@579
  1047
      first_free_edge=-1;
alpar@579
  1048
    }
alpar@579
  1049
alpar@579
  1050
alpar@400
  1051
    class Edge {
alpar@579
  1052
    public:
alpar@400
  1053
      friend class EdgeSet;
alpar@400
  1054
      template <typename T> friend class EdgeMap;
alpar@400
  1055
alpar@400
  1056
      friend class Node;
alpar@400
  1057
      friend class NodeIt;
alpar@579
  1058
    public:
alpar@774
  1059
      ///\bug It should be at least protected
alpar@579
  1060
      ///
alpar@579
  1061
      int n;
alpar@400
  1062
    protected:
alpar@400
  1063
      friend int EdgeSet::id(Edge e) const;
alpar@400
  1064
alpar@400
  1065
      Edge(int nn) {n=nn;}
alpar@400
  1066
    public:
alpar@400
  1067
      Edge() { }
alpar@400
  1068
      Edge (Invalid) { n=-1; }
alpar@400
  1069
      bool operator==(const Edge i) const {return n==i.n;}
alpar@400
  1070
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@400
  1071
      bool operator<(const Edge i) const {return n<i.n;}
alpar@400
  1072
      ///\bug This is a workaround until somebody tells me how to
alpar@400
  1073
      ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
alpar@400
  1074
      int &idref() {return n;}
alpar@400
  1075
      const int &idref() const {return n;}
alpar@400
  1076
    };
alpar@400
  1077
    
alpar@400
  1078
    class EdgeIt : public Edge {
alpar@400
  1079
      friend class EdgeSet;
alpar@579
  1080
      template <typename T> friend class EdgeMap;
alpar@579
  1081
    
alpar@774
  1082
      const EdgeSet *G;
alpar@400
  1083
    public:
alpar@774
  1084
      EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) {
alpar@503
  1085
	//      	typename NodeGraphType::Node m;
alpar@503
  1086
        NodeIt m;
alpar@774
  1087
	for(G->first(m);
alpar@774
  1088
	    m!=INVALID && G->nodes[m].first_in == -1;  ++m);
alpar@774
  1089
	///\bug AJJAJ! This is a non sense!!!!!!!
alpar@774
  1090
	this->n = m!=INVALID?-1:G->nodes[m].first_in;
alpar@400
  1091
      }
alpar@774
  1092
      EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
alpar@400
  1093
      EdgeIt (Invalid i) : Edge(i) { }
alpar@400
  1094
      EdgeIt() : Edge() { }
alpar@774
  1095
      ///.
alpar@774
  1096
      
alpar@774
  1097
      ///\bug UNIMPLEMENTED!!!!!
alpar@774
  1098
      //
alpar@774
  1099
      EdgeIt &operator++() {
alpar@774
  1100
	return *this;
alpar@774
  1101
      }
alpar@774
  1102
       ///\bug This is a workaround until somebody tells me how to
alpar@400
  1103
      ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
alpar@515
  1104
      int &idref() {return this->n;}
alpar@400
  1105
    };
alpar@400
  1106
    
alpar@400
  1107
    class OutEdgeIt : public Edge {
alpar@774
  1108
      const EdgeSet *G;
alpar@400
  1109
      friend class EdgeSet;
alpar@400
  1110
    public: 
alpar@400
  1111
      OutEdgeIt() : Edge() { }
alpar@400
  1112
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@774
  1113
      OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
alpar@400
  1114
alpar@774
  1115
      OutEdgeIt(const EdgeSet& _G,const Node v) :
alpar@774
  1116
	Edge(_G.nodes[v].first_out), G(&_G) { }
deba@844
  1117
      OutEdgeIt &operator++() { 
deba@844
  1118
	Edge::n = G->edges[Edge::n].next_out;
deba@844
  1119
	return *this; 
deba@844
  1120
      }
alpar@400
  1121
    };
alpar@400
  1122
    
alpar@400
  1123
    class InEdgeIt : public Edge {
alpar@774
  1124
      const EdgeSet *G;
alpar@400
  1125
      friend class EdgeSet;
alpar@400
  1126
    public: 
alpar@400
  1127
      InEdgeIt() : Edge() { }
alpar@400
  1128
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@774
  1129
      InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
alpar@774
  1130
      InEdgeIt(const EdgeSet& _G,Node v)
alpar@774
  1131
	: Edge(_G.nodes[v].first_in), G(&_G) { }
deba@844
  1132
      InEdgeIt &operator++() { 
deba@844
  1133
	Edge::n = G->edges[Edge::n].next_in; 
deba@844
  1134
	return *this; 
deba@844
  1135
      }
alpar@400
  1136
    };
deba@782
  1137
    
alpar@400
  1138
  };
alpar@406
  1139
alpar@579
  1140
  template<typename GG>
alpar@579
  1141
  inline int EdgeSet<GG>::id(Node v) const { return G.id(v); }
alpar@531
  1142
alpar@406
  1143
/// @}  
alpar@406
  1144
alpar@395
  1145
} //namespace hugo
alpar@395
  1146
alpar@405
  1147
#endif //HUGO_LIST_GRAPH_H