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