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