src/hugo/list_graph.h
author marci
Mon, 20 Sep 2004 17:53:33 +0000
changeset 890 3a48bc350e0f
parent 877 66dd225ca128
child 891 74589d20dbc3
permissions -rw-r--r--
Specialized ConstMap for defining constant maps at compile time, by klao.
Time comparision of the generic and specialized maps.
alpar@395
     1
// -*- mode:C++ -*-
alpar@395
     2
alpar@405
     3
#ifndef HUGO_LIST_GRAPH_H
alpar@405
     4
#define HUGO_LIST_GRAPH_H
alpar@395
     5
klao@491
     6
///\ingroup graphs
alpar@395
     7
///\file
alpar@405
     8
///\brief ListGraph, SymListGraph, NodeSet and EdgeSet classes.
alpar@395
     9
alpar@395
    10
#include <vector>
deba@782
    11
#include <climits>
alpar@395
    12
ladanyi@542
    13
#include <hugo/invalid.h>
alpar@395
    14
deba@782
    15
#include <hugo/map_registry.h>
deba@822
    16
#include <hugo/default_map.h>
deba@782
    17
deba@822
    18
#include <hugo/sym_map.h>
deba@782
    19
deba@782
    20
#include <hugo/map_defines.h>
deba@782
    21
deba@782
    22
alpar@395
    23
namespace hugo {
alpar@395
    24
alpar@406
    25
/// \addtogroup graphs
alpar@406
    26
/// @{
alpar@406
    27
alpar@401
    28
  ///A list graph class.
alpar@395
    29
alpar@397
    30
  ///This is a simple and fast erasable graph implementation.
alpar@397
    31
  ///
alpar@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@822
    83
    CREATE_MAPS(DefaultMap);
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
    /// Importing maps from the base class ListGraph.
deba@822
   446
    KEEP_MAPS(ListGraph, SymListGraph);
deba@782
   447
deba@822
   448
    /// Creating symmetric map registry.
deba@782
   449
    CREATE_SYM_EDGE_MAP_REGISTRY;
deba@822
   450
    /// Creating symmetric edge map.
deba@822
   451
    CREATE_SYM_EDGE_MAP(DefaultMap);
alpar@395
   452
alpar@397
   453
    SymListGraph() : ListGraph() { }
alpar@397
   454
    SymListGraph(const ListGraph &_g) : ListGraph(_g) { }
alpar@397
   455
    ///Adds a pair of oppositely directed edges to the graph.
alpar@395
   456
    Edge addEdge(Node u, Node v)
alpar@395
   457
    {
alpar@397
   458
      Edge e = ListGraph::addEdge(u,v);
deba@782
   459
      Edge f = ListGraph::addEdge(v,u);
deba@782
   460
      sym_edge_maps.add(e);
deba@782
   461
      sym_edge_maps.add(f);
deba@782
   462
      
alpar@395
   463
      return e;
alpar@395
   464
    }
alpar@395
   465
deba@782
   466
    void erase(Node n) { ListGraph::erase(n);}
alpar@395
   467
    ///The oppositely directed edge.
alpar@395
   468
alpar@395
   469
    ///Returns the oppositely directed
alpar@395
   470
    ///pair of the edge \c e.
alpar@713
   471
    static Edge opposite(Edge e)
alpar@395
   472
    {
alpar@395
   473
      Edge f;
alpar@395
   474
      f.idref() = e.idref() - 2*(e.idref()%2) + 1;
alpar@395
   475
      return f;
alpar@395
   476
    }
alpar@395
   477
    
alpar@397
   478
    ///Removes a pair of oppositely directed edges to the graph.
alpar@397
   479
    void erase(Edge e) {
deba@782
   480
      Edge f = opposite(e);
deba@782
   481
      sym_edge_maps.erase(e);
deba@782
   482
      sym_edge_maps.erase(f);
deba@782
   483
      ListGraph::erase(f);
alpar@397
   484
      ListGraph::erase(e);
deba@782
   485
    }    
deba@782
   486
  };
alpar@395
   487
alpar@400
   488
alpar@401
   489
  ///A graph class containing only nodes.
alpar@400
   490
alpar@401
   491
  ///This class implements a graph structure without edges.
alpar@401
   492
  ///The most useful application of this class is to be the node set of an
alpar@401
   493
  ///\ref EdgeSet class.
alpar@400
   494
  ///
alpar@880
   495
  ///It conforms to 
alpar@880
   496
  ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept
alpar@880
   497
  ///with the exception that you cannot
alpar@401
   498
  ///add (or delete) edges. The usual edge iterators are exists, but they are
alpar@401
   499
  ///always \ref INVALID.
alpar@880
   500
  ///\sa skeleton::ExtendableGraph
alpar@880
   501
  ///\sa EdgeSet
alpar@400
   502
  class NodeSet {
alpar@400
   503
alpar@400
   504
    //Nodes are double linked.
alpar@400
   505
    //The free nodes are only single linked using the "next" field.
alpar@400
   506
    struct NodeT 
alpar@400
   507
    {
alpar@400
   508
      int first_in,first_out;
alpar@400
   509
      int prev, next;
alpar@400
   510
      //      NodeT() {}
alpar@400
   511
    };
alpar@400
   512
alpar@400
   513
    std::vector<NodeT> nodes;
alpar@400
   514
    //The first node
alpar@400
   515
    int first_node;
alpar@400
   516
    //The first free node
alpar@400
   517
    int first_free_node;
alpar@400
   518
    
alpar@400
   519
  public:
deba@782
   520
deba@782
   521
    typedef NodeSet Graph;
alpar@400
   522
    
alpar@400
   523
    class Node;
alpar@400
   524
    class Edge;
alpar@400
   525
alpar@400
   526
  public:
alpar@400
   527
alpar@400
   528
    class NodeIt;
alpar@400
   529
    class EdgeIt;
alpar@400
   530
    class OutEdgeIt;
alpar@400
   531
    class InEdgeIt;
alpar@400
   532
    
deba@822
   533
    /// Creating node map registry.
deba@822
   534
    CREATE_NODE_MAP_REGISTRY;
deba@822
   535
    /// Creating node maps.
deba@822
   536
    CREATE_NODE_MAP(DefaultMap);
deba@822
   537
deba@822
   538
    /// Creating empty map structure for edges.
deba@822
   539
    template <typename Value>
deba@822
   540
    class EdgeMap {
deba@822
   541
    public:
deba@822
   542
      EdgeMap() {}
deba@822
   543
      EdgeMap(const Graph&) {}
deba@822
   544
      EdgeMap(const Graph&, const Value&) {}
deba@822
   545
deba@822
   546
      EdgeMap(const EdgeMap&) {}
deba@822
   547
      template <typename CMap> EdgeMap(const CMap&) {}
deba@822
   548
deba@822
   549
      EdgeMap& operator=(const EdgeMap&) {}
deba@822
   550
      template <typename CMap> EdgeMap& operator=(const CMap&) {}
deba@822
   551
      
deba@822
   552
      class ConstIterator {
deba@822
   553
      public:
deba@822
   554
	bool operator==(const ConstIterator&) {return true;}
deba@822
   555
	bool operator!=(const ConstIterator&) {return false;}
deba@822
   556
      };
deba@822
   557
deba@822
   558
      typedef ConstIterator Iterator;
deba@822
   559
      
deba@822
   560
      Iterator begin() { return Iterator();}
deba@822
   561
      Iterator end() { return Iterator();}
deba@822
   562
deba@822
   563
      ConstIterator begin() const { return ConstIterator();}
deba@822
   564
      ConstIterator end() const { return ConstIterator();}
deba@822
   565
deba@822
   566
    };
alpar@400
   567
    
alpar@400
   568
  public:
alpar@400
   569
alpar@408
   570
    ///Default constructor
deba@782
   571
    NodeSet() 
deba@782
   572
      : nodes(), first_node(-1), first_free_node(-1) {}
alpar@408
   573
    ///Copy constructor
deba@782
   574
    NodeSet(const NodeSet &_g) 
deba@782
   575
      : nodes(_g.nodes), first_node(_g.first_node),
deba@782
   576
	first_free_node(_g.first_free_node) {}
alpar@400
   577
    
alpar@813
   578
    ///Number of nodes.
alpar@813
   579
    int nodeNum() const { return nodes.size(); }
alpar@813
   580
    ///Number of edges.
alpar@813
   581
    int edgeNum() const { return 0; }
alpar@400
   582
alpar@813
   583
    /// Maximum node ID.
alpar@813
   584
    
alpar@813
   585
    /// Maximum node ID.
alpar@813
   586
    ///\sa id(Node)
alpar@813
   587
    int maxNodeId() const { return nodes.size()-1; }
alpar@813
   588
    /// Maximum edge ID.
alpar@813
   589
    
alpar@813
   590
    /// Maximum edge ID.
alpar@813
   591
    ///\sa id(Edge)
alpar@813
   592
    int maxEdgeId() const { return 0; }
alpar@400
   593
alpar@400
   594
    Node tail(Edge e) const { return INVALID; }
alpar@400
   595
    Node head(Edge e) const { return INVALID; }
alpar@400
   596
alpar@400
   597
    NodeIt& first(NodeIt& v) const { 
alpar@400
   598
      v=NodeIt(*this); return v; }
alpar@400
   599
    EdgeIt& first(EdgeIt& e) const { 
alpar@400
   600
      e=EdgeIt(*this); return e; }
alpar@400
   601
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@400
   602
      e=OutEdgeIt(*this,v); return e; }
alpar@400
   603
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@400
   604
      e=InEdgeIt(*this,v); return e; }
alpar@400
   605
alpar@813
   606
    /// Node ID.
alpar@813
   607
    
alpar@813
   608
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   609
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   610
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   611
    ///
alpar@813
   612
    /// The ID of the \ref INVALID node is -1.
alpar@813
   613
    ///\return The ID of the node \c v. 
alpar@400
   614
    int id(Node v) const { return v.n; }
alpar@813
   615
    /// Edge ID.
alpar@813
   616
    
alpar@813
   617
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   618
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   619
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   620
    ///
alpar@813
   621
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   622
    ///\return The ID of the edge \c e. 
alpar@400
   623
    int id(Edge e) const { return -1; }
alpar@400
   624
alpar@400
   625
    /// Adds a new node to the graph.
alpar@400
   626
alpar@813
   627
    /// \warning It adds the new node to the front of the list.
alpar@400
   628
    /// (i.e. the lastly added node becomes the first.)
alpar@400
   629
    Node addNode() {
alpar@400
   630
      int n;
alpar@400
   631
      
alpar@400
   632
      if(first_free_node==-1)
alpar@400
   633
	{
alpar@400
   634
	  n = nodes.size();
alpar@400
   635
	  nodes.push_back(NodeT());
alpar@400
   636
	}
alpar@400
   637
      else {
alpar@400
   638
	n = first_free_node;
alpar@400
   639
	first_free_node = nodes[n].next;
alpar@400
   640
      }
alpar@400
   641
      
alpar@400
   642
      nodes[n].next = first_node;
alpar@400
   643
      if(first_node != -1) nodes[first_node].prev = n;
alpar@400
   644
      first_node = n;
alpar@400
   645
      nodes[n].prev = -1;
alpar@400
   646
      
alpar@400
   647
      nodes[n].first_in = nodes[n].first_out = -1;
alpar@400
   648
      
alpar@400
   649
      Node nn; nn.n=n;
alpar@400
   650
alpar@400
   651
      //Update dynamic maps
deba@782
   652
      node_maps.add(nn);
alpar@400
   653
alpar@400
   654
      return nn;
alpar@400
   655
    }
alpar@400
   656
    
alpar@400
   657
    void erase(Node nn) {
alpar@400
   658
      int n=nn.n;
alpar@400
   659
      
alpar@400
   660
      if(nodes[n].next != -1) nodes[nodes[n].next].prev = nodes[n].prev;
alpar@400
   661
      if(nodes[n].prev != -1) nodes[nodes[n].prev].next = nodes[n].next;
alpar@400
   662
      else first_node = nodes[n].next;
alpar@400
   663
      
alpar@400
   664
      nodes[n].next = first_free_node;
alpar@400
   665
      first_free_node = n;
alpar@400
   666
alpar@400
   667
      //Update dynamic maps
deba@782
   668
      node_maps.erase(nn);
alpar@400
   669
    }
alpar@400
   670
    
alpar@774
   671
        
alpar@774
   672
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   673
    {
alpar@774
   674
      return INVALID;
alpar@774
   675
    }
alpar@774
   676
    
alpar@400
   677
    void clear() {
deba@782
   678
      node_maps.clear();
alpar@400
   679
      nodes.clear();
alpar@400
   680
      first_node = first_free_node = -1;
alpar@400
   681
    }
alpar@400
   682
alpar@400
   683
    class Node {
alpar@400
   684
      friend class NodeSet;
alpar@400
   685
      template <typename T> friend class NodeMap;
alpar@400
   686
      
alpar@400
   687
      friend class Edge;
alpar@400
   688
      friend class OutEdgeIt;
alpar@400
   689
      friend class InEdgeIt;
alpar@400
   690
alpar@400
   691
    protected:
alpar@400
   692
      int n;
alpar@400
   693
      friend int NodeSet::id(Node v) const; 
alpar@400
   694
      Node(int nn) {n=nn;}
alpar@400
   695
    public:
alpar@400
   696
      Node() {}
alpar@400
   697
      Node (Invalid i) { n=-1; }
alpar@400
   698
      bool operator==(const Node i) const {return n==i.n;}
alpar@400
   699
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@400
   700
      bool operator<(const Node i) const {return n<i.n;}
alpar@400
   701
    };
alpar@400
   702
    
alpar@400
   703
    class NodeIt : public Node {
alpar@774
   704
      const NodeSet *G;
alpar@400
   705
      friend class NodeSet;
alpar@400
   706
    public:
alpar@579
   707
      NodeIt() : Node() { }
alpar@774
   708
      NodeIt(const NodeSet& _G,Node n) : Node(n), G(&_G) { }
alpar@579
   709
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   710
      NodeIt(const NodeSet& _G) : Node(_G.first_node), G(&_G) { }
alpar@774
   711
      NodeIt &operator++() {
alpar@774
   712
	n=G->nodes[n].next; 
alpar@774
   713
	return *this; 
alpar@774
   714
      }
alpar@400
   715
    };
alpar@400
   716
alpar@400
   717
    class Edge {
alpar@400
   718
      //friend class NodeSet;
alpar@400
   719
      //template <typename T> friend class EdgeMap;
alpar@400
   720
alpar@400
   721
      //template <typename T> friend class SymNodeSet::SymEdgeMap;      
alpar@400
   722
      //friend Edge SymNodeSet::opposite(Edge) const;
alpar@400
   723
      
alpar@400
   724
      //      friend class Node;
alpar@400
   725
      //      friend class NodeIt;
alpar@400
   726
    protected:
alpar@400
   727
      //friend int NodeSet::id(Edge e) const;
alpar@400
   728
      //      Edge(int nn) {}
alpar@400
   729
    public:
alpar@400
   730
      Edge() { }
alpar@400
   731
      Edge (Invalid) { }
alpar@400
   732
      bool operator==(const Edge i) const {return true;}
alpar@400
   733
      bool operator!=(const Edge i) const {return false;}
alpar@400
   734
      bool operator<(const Edge i) const {return false;}
alpar@400
   735
      ///\bug This is a workaround until somebody tells me how to
alpar@400
   736
      ///make class \c SymNodeSet::SymEdgeMap friend of Edge
alpar@400
   737
      //      int idref() {return -1;}
alpar@400
   738
      //      int idref() const {return -1;}
alpar@400
   739
    };
alpar@400
   740
    
alpar@400
   741
    class EdgeIt : public Edge {
alpar@400
   742
      //friend class NodeSet;
alpar@400
   743
    public:
alpar@400
   744
      EdgeIt(const NodeSet& G) : Edge() { }
alpar@774
   745
      EdgeIt(const NodeSet&, Edge) : Edge() { }
alpar@400
   746
      EdgeIt (Invalid i) : Edge(i) { }
alpar@400
   747
      EdgeIt() : Edge() { }
alpar@400
   748
      ///\bug This is a workaround until somebody tells me how to
alpar@400
   749
      ///make class \c SymNodeSet::SymEdgeMap friend of Edge
alpar@400
   750
      //      int idref() {return -1;}
alpar@774
   751
      EdgeIt operator++() { return INVALID; }
alpar@400
   752
    };
alpar@400
   753
    
alpar@400
   754
    class OutEdgeIt : public Edge {
alpar@400
   755
      friend class NodeSet;
alpar@400
   756
    public: 
alpar@400
   757
      OutEdgeIt() : Edge() { }
alpar@774
   758
      OutEdgeIt(const NodeSet&, Edge) : Edge() { }
alpar@400
   759
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@400
   760
      OutEdgeIt(const NodeSet& G,const Node v)	: Edge() {}
alpar@774
   761
      OutEdgeIt operator++() { return INVALID; }
alpar@400
   762
    };
alpar@400
   763
    
alpar@400
   764
    class InEdgeIt : public Edge {
alpar@400
   765
      friend class NodeSet;
alpar@400
   766
    public: 
alpar@400
   767
      InEdgeIt() : Edge() { }
alpar@774
   768
      InEdgeIt(const NodeSet&, Edge) : Edge() { }
alpar@400
   769
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@400
   770
      InEdgeIt(const NodeSet& G,Node v) :Edge() {}
alpar@774
   771
      InEdgeIt operator++() { return INVALID; }
alpar@400
   772
    };
alpar@400
   773
alpar@400
   774
  };
alpar@400
   775
alpar@400
   776
alpar@400
   777
alpar@401
   778
  ///Graph structure using a node set of another graph.
alpar@401
   779
alpar@401
   780
  ///This structure can be used to establish another graph over a node set
alpar@401
   781
  /// of an existing one. The node iterator will go through the nodes of the
alpar@401
   782
  /// original graph, and the NodeMap's of both graphs will convert to
alpar@401
   783
  /// each other.
alpar@401
   784
  ///
alpar@404
   785
  ///\warning Adding or deleting nodes from the graph is not safe if an
alpar@404
   786
  ///\ref EdgeSet is currently attached to it!
alpar@404
   787
  ///
alpar@404
   788
  ///\todo Make it possible to add/delete edges from the base graph
alpar@404
   789
  ///(and from \ref EdgeSet, as well)
alpar@404
   790
  ///
alpar@401
   791
  ///\param GG The type of the graph which shares its node set with this class.
alpar@880
   792
  ///Its interface must conform to the
alpar@880
   793
  ///\ref skeleton::StaticGraph "StaticGraph" concept.
alpar@400
   794
  ///
alpar@880
   795
  ///It conforms to the 
alpar@880
   796
  ///\ref skeleton::ExtendableGraph "ExtendableGraph" concept.
alpar@880
   797
  ///\sa skeleton::ExtendableGraph.
alpar@880
   798
  ///\sa NodeSet.
alpar@400
   799
  template<typename GG>
alpar@400
   800
  class EdgeSet {
alpar@400
   801
alpar@400
   802
    typedef GG NodeGraphType;
alpar@400
   803
alpar@400
   804
    NodeGraphType &G;
alpar@400
   805
alpar@515
   806
  public:
deba@782
   807
alpar@400
   808
    class Node;
alpar@705
   809
    class Edge;
alpar@705
   810
    class OutEdgeIt;
alpar@705
   811
    class InEdgeIt;
alpar@705
   812
    class SymEdge;
deba@782
   813
deba@782
   814
    typedef EdgeSet Graph;
deba@782
   815
alpar@531
   816
    int id(Node v) const; 
alpar@531
   817
alpar@531
   818
    class Node : public NodeGraphType::Node {
alpar@531
   819
      friend class EdgeSet;
alpar@531
   820
      //      template <typename T> friend class NodeMap;
alpar@531
   821
      
alpar@531
   822
      friend class Edge;
alpar@531
   823
      friend class OutEdgeIt;
alpar@531
   824
      friend class InEdgeIt;
alpar@531
   825
      friend class SymEdge;
alpar@531
   826
alpar@531
   827
    public:
alpar@531
   828
      friend int EdgeSet::id(Node v) const; 
alpar@531
   829
      //      Node(int nn) {n=nn;}
alpar@531
   830
    public:
alpar@531
   831
      Node() : NodeGraphType::Node() {}
alpar@531
   832
      Node (Invalid i) : NodeGraphType::Node(i) {}
alpar@531
   833
      Node(const typename NodeGraphType::Node &n) : NodeGraphType::Node(n) {}
alpar@531
   834
    };
alpar@531
   835
    
alpar@531
   836
    class NodeIt : public NodeGraphType::NodeIt {
alpar@531
   837
      friend class EdgeSet;
alpar@531
   838
    public:
alpar@531
   839
      NodeIt() : NodeGraphType::NodeIt() { }
alpar@774
   840
      NodeIt(const EdgeSet& _G,Node n) : NodeGraphType::NodeIt(_G.G,n) { }
alpar@531
   841
      NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {}
alpar@531
   842
      NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { }
alpar@531
   843
      NodeIt(const typename NodeGraphType::NodeIt &n)
alpar@531
   844
	: NodeGraphType::NodeIt(n) {}
alpar@579
   845
alpar@531
   846
      operator Node() { return Node(*this);}
alpar@774
   847
      NodeIt &operator++()
alpar@774
   848
      { this->NodeGraphType::NodeIt::operator++(); return *this;} 
alpar@531
   849
    };
alpar@515
   850
alpar@515
   851
  private:
alpar@400
   852
    //Edges are double linked.
alpar@400
   853
    //The free edges are only single linked using the "next_in" field.
alpar@400
   854
    struct NodeT 
alpar@400
   855
    {
alpar@400
   856
      int first_in,first_out;
alpar@400
   857
      NodeT() : first_in(-1), first_out(-1) { }
alpar@400
   858
    };
alpar@400
   859
alpar@400
   860
    struct EdgeT 
alpar@400
   861
    {
alpar@400
   862
      Node head, tail;
alpar@400
   863
      int prev_in, prev_out;
alpar@400
   864
      int next_in, next_out;
alpar@400
   865
    };
alpar@400
   866
alpar@400
   867
    
alpar@515
   868
    typename NodeGraphType::template NodeMap<NodeT> nodes;
alpar@400
   869
    
alpar@400
   870
    std::vector<EdgeT> edges;
alpar@400
   871
    //The first free edge
alpar@400
   872
    int first_free_edge;
alpar@400
   873
    
alpar@400
   874
  public:
alpar@400
   875
    
alpar@400
   876
    class Node;
alpar@400
   877
    class Edge;
alpar@400
   878
alpar@400
   879
    class NodeIt;
alpar@400
   880
    class EdgeIt;
alpar@400
   881
    class OutEdgeIt;
alpar@400
   882
    class InEdgeIt;
deba@782
   883
deba@782
   884
deba@877
   885
    /// Creates edge map registry.
deba@782
   886
    CREATE_EDGE_MAP_REGISTRY;
deba@877
   887
    /// Creates edge maps.
deba@822
   888
    CREATE_EDGE_MAP(DefaultMap);
deba@822
   889
deba@877
   890
    /// Imports node maps from the NodeGraphType.
deba@822
   891
    IMPORT_NODE_MAP(NodeGraphType, graph.G, EdgeSet, graph);
alpar@400
   892
    
alpar@400
   893
    
alpar@400
   894
  public:
alpar@400
   895
alpar@408
   896
    ///Constructor
alpar@408
   897
    
alpar@408
   898
    ///Construates a new graph based on the nodeset of an existing one.
alpar@408
   899
    ///\param _G the base graph.
alpar@880
   900
    explicit EdgeSet(NodeGraphType &_G) 
deba@782
   901
      : G(_G), nodes(_G), edges(),
deba@782
   902
	first_free_edge(-1) {}
alpar@408
   903
    ///Copy constructor
alpar@408
   904
alpar@408
   905
    ///Makes a copy of an EdgeSet.
alpar@408
   906
    ///It will be based on the same graph.
alpar@880
   907
    explicit EdgeSet(const EdgeSet &_g) 
deba@782
   908
      : G(_g.G), nodes(_g.G), edges(_g.edges),
deba@782
   909
	first_free_edge(_g.first_free_edge) {}
alpar@400
   910
    
alpar@813
   911
    ///Number of nodes.
alpar@813
   912
    int nodeNum() const { return G.nodeNum(); }
alpar@813
   913
    ///Number of edges.
alpar@813
   914
    int edgeNum() const { return edges.size(); }
alpar@400
   915
alpar@813
   916
    /// Maximum node ID.
alpar@813
   917
    
alpar@813
   918
    /// Maximum node ID.
alpar@813
   919
    ///\sa id(Node)
alpar@813
   920
    int maxNodeId() const { return G.maxNodeId(); }
alpar@813
   921
    /// Maximum edge ID.
alpar@813
   922
    
alpar@813
   923
    /// Maximum edge ID.
alpar@813
   924
    ///\sa id(Edge)
alpar@813
   925
    int maxEdgeId() const { return edges.size()-1; }
alpar@400
   926
alpar@400
   927
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@400
   928
    Node head(Edge e) const { return edges[e.n].head; }
alpar@400
   929
alpar@400
   930
    NodeIt& first(NodeIt& v) const { 
alpar@400
   931
      v=NodeIt(*this); return v; }
alpar@400
   932
    EdgeIt& first(EdgeIt& e) const { 
alpar@400
   933
      e=EdgeIt(*this); return e; }
alpar@400
   934
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@400
   935
      e=OutEdgeIt(*this,v); return e; }
alpar@400
   936
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@400
   937
      e=InEdgeIt(*this,v); return e; }
alpar@400
   938
alpar@813
   939
    /// Node ID.
alpar@813
   940
    
alpar@813
   941
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   942
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   943
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   944
    ///
alpar@813
   945
    /// The ID of the \ref INVALID node is -1.
alpar@813
   946
    ///\return The ID of the node \c v. 
alpar@813
   947
    int id(Node v) { return G.id(v); }
alpar@813
   948
    /// Edge ID.
alpar@813
   949
    
alpar@813
   950
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   951
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   952
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   953
    ///
alpar@813
   954
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   955
    ///\return The ID of the edge \c e. 
alpar@400
   956
    int id(Edge e) const { return e.n; }
alpar@400
   957
alpar@400
   958
    /// Adds a new node to the graph.
alpar@579
   959
    Node addNode() { return G.addNode(); }
alpar@400
   960
    
alpar@400
   961
    Edge addEdge(Node u, Node v) {
alpar@400
   962
      int n;
alpar@400
   963
      
alpar@400
   964
      if(first_free_edge==-1)
alpar@400
   965
	{
alpar@400
   966
	  n = edges.size();
alpar@400
   967
	  edges.push_back(EdgeT());
alpar@400
   968
	}
alpar@400
   969
      else {
alpar@400
   970
	n = first_free_edge;
alpar@400
   971
	first_free_edge = edges[n].next_in;
alpar@400
   972
      }
alpar@400
   973
      
alpar@401
   974
      edges[n].tail = u; edges[n].head = v;
alpar@400
   975
alpar@401
   976
      edges[n].next_out = nodes[u].first_out;
alpar@401
   977
      if(nodes[u].first_out != -1) edges[nodes[u].first_out].prev_out = n;
alpar@401
   978
      edges[n].next_in = nodes[v].first_in;
alpar@401
   979
      if(nodes[v].first_in != -1) edges[nodes[v].first_in].prev_in = n;
alpar@400
   980
      edges[n].prev_in = edges[n].prev_out = -1;
alpar@400
   981
	
alpar@401
   982
      nodes[u].first_out = nodes[v].first_in = n;
alpar@400
   983
alpar@400
   984
      Edge e; e.n=n;
alpar@400
   985
alpar@400
   986
      //Update dynamic maps
deba@782
   987
      edge_maps.add(e);
alpar@400
   988
alpar@400
   989
      return e;
alpar@400
   990
    }
alpar@400
   991
alpar@774
   992
    /// Finds an edge between two nodes.
alpar@774
   993
alpar@774
   994
    /// Finds an edge from node \c u to node \c v.
alpar@774
   995
    ///
alpar@774
   996
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   997
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   998
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   999
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
  1000
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
  1001
    {
alpar@774
  1002
      int e = (prev.n==-1)? nodes[u].first_out : edges[prev.n].next_out;
alpar@774
  1003
      while(e!=-1 && edges[e].tail!=v) e = edges[e].next_out;
alpar@774
  1004
      prev.n=e;
alpar@774
  1005
      return prev;
alpar@774
  1006
    }
alpar@774
  1007
    
alpar@400
  1008
  private:
alpar@400
  1009
    void eraseEdge(int n) {
alpar@400
  1010
      
alpar@400
  1011
      if(edges[n].next_in!=-1)
alpar@400
  1012
	edges[edges[n].next_in].prev_in = edges[n].prev_in;
alpar@400
  1013
      if(edges[n].prev_in!=-1)
alpar@400
  1014
	edges[edges[n].prev_in].next_in = edges[n].next_in;
alpar@400
  1015
      else nodes[edges[n].head].first_in = edges[n].next_in;
alpar@400
  1016
      
alpar@400
  1017
      if(edges[n].next_out!=-1)
alpar@400
  1018
	edges[edges[n].next_out].prev_out = edges[n].prev_out;
alpar@400
  1019
      if(edges[n].prev_out!=-1)
alpar@400
  1020
	edges[edges[n].prev_out].next_out = edges[n].next_out;
alpar@400
  1021
      else nodes[edges[n].tail].first_out = edges[n].next_out;
alpar@400
  1022
      
alpar@400
  1023
      edges[n].next_in = first_free_edge;
alpar@400
  1024
      first_free_edge = -1;      
alpar@400
  1025
alpar@400
  1026
      //Update dynamic maps
deba@782
  1027
      Edge e; e.n = n;
deba@782
  1028
      edge_maps.erase(e);
alpar@400
  1029
    }
alpar@400
  1030
      
alpar@400
  1031
  public:
alpar@400
  1032
alpar@400
  1033
//     void erase(Node nn) {
alpar@400
  1034
//       int n=nn.n;
alpar@400
  1035
//       int m;
alpar@400
  1036
//       while((m=nodes[n].first_in)!=-1) eraseEdge(m);
alpar@400
  1037
//       while((m=nodes[n].first_out)!=-1) eraseEdge(m);
alpar@400
  1038
//     }
alpar@400
  1039
    
alpar@400
  1040
    void erase(Edge e) { eraseEdge(e.n); }
alpar@400
  1041
alpar@579
  1042
    ///Clear all edges. (Doesn't clear the nodes!)
alpar@579
  1043
    void clear() {
deba@782
  1044
      edge_maps.clear();
alpar@579
  1045
      edges.clear();
alpar@579
  1046
      first_free_edge=-1;
alpar@579
  1047
    }
alpar@579
  1048
alpar@579
  1049
alpar@400
  1050
    class Edge {
alpar@579
  1051
    public:
alpar@400
  1052
      friend class EdgeSet;
alpar@400
  1053
      template <typename T> friend class EdgeMap;
alpar@400
  1054
alpar@400
  1055
      friend class Node;
alpar@400
  1056
      friend class NodeIt;
alpar@579
  1057
    public:
alpar@774
  1058
      ///\bug It should be at least protected
alpar@579
  1059
      ///
alpar@579
  1060
      int n;
alpar@400
  1061
    protected:
alpar@400
  1062
      friend int EdgeSet::id(Edge e) const;
alpar@400
  1063
alpar@400
  1064
      Edge(int nn) {n=nn;}
alpar@400
  1065
    public:
alpar@400
  1066
      Edge() { }
alpar@400
  1067
      Edge (Invalid) { n=-1; }
alpar@400
  1068
      bool operator==(const Edge i) const {return n==i.n;}
alpar@400
  1069
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@400
  1070
      bool operator<(const Edge i) const {return n<i.n;}
alpar@400
  1071
      ///\bug This is a workaround until somebody tells me how to
alpar@400
  1072
      ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
alpar@400
  1073
      int &idref() {return n;}
alpar@400
  1074
      const int &idref() const {return n;}
alpar@400
  1075
    };
alpar@400
  1076
    
alpar@400
  1077
    class EdgeIt : public Edge {
alpar@400
  1078
      friend class EdgeSet;
alpar@579
  1079
      template <typename T> friend class EdgeMap;
alpar@579
  1080
    
alpar@774
  1081
      const EdgeSet *G;
alpar@400
  1082
    public:
alpar@774
  1083
      EdgeIt(const EdgeSet& _G) : Edge(), G(&_G) {
alpar@503
  1084
	//      	typename NodeGraphType::Node m;
alpar@503
  1085
        NodeIt m;
alpar@774
  1086
	for(G->first(m);
alpar@774
  1087
	    m!=INVALID && G->nodes[m].first_in == -1;  ++m);
alpar@774
  1088
	///\bug AJJAJ! This is a non sense!!!!!!!
alpar@774
  1089
	this->n = m!=INVALID?-1:G->nodes[m].first_in;
alpar@400
  1090
      }
alpar@774
  1091
      EdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
alpar@400
  1092
      EdgeIt (Invalid i) : Edge(i) { }
alpar@400
  1093
      EdgeIt() : Edge() { }
alpar@774
  1094
      ///.
alpar@774
  1095
      
alpar@774
  1096
      ///\bug UNIMPLEMENTED!!!!!
alpar@774
  1097
      //
alpar@774
  1098
      EdgeIt &operator++() {
alpar@774
  1099
	return *this;
alpar@774
  1100
      }
alpar@774
  1101
       ///\bug This is a workaround until somebody tells me how to
alpar@400
  1102
      ///make class \c SymEdgeSet::SymEdgeMap friend of Edge
alpar@515
  1103
      int &idref() {return this->n;}
alpar@400
  1104
    };
alpar@400
  1105
    
alpar@400
  1106
    class OutEdgeIt : public Edge {
alpar@774
  1107
      const EdgeSet *G;
alpar@400
  1108
      friend class EdgeSet;
alpar@400
  1109
    public: 
alpar@400
  1110
      OutEdgeIt() : Edge() { }
alpar@400
  1111
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@774
  1112
      OutEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
alpar@400
  1113
alpar@774
  1114
      OutEdgeIt(const EdgeSet& _G,const Node v) :
alpar@774
  1115
	Edge(_G.nodes[v].first_out), G(&_G) { }
deba@844
  1116
      OutEdgeIt &operator++() { 
deba@844
  1117
	Edge::n = G->edges[Edge::n].next_out;
deba@844
  1118
	return *this; 
deba@844
  1119
      }
alpar@400
  1120
    };
alpar@400
  1121
    
alpar@400
  1122
    class InEdgeIt : public Edge {
alpar@774
  1123
      const EdgeSet *G;
alpar@400
  1124
      friend class EdgeSet;
alpar@400
  1125
    public: 
alpar@400
  1126
      InEdgeIt() : Edge() { }
alpar@400
  1127
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@774
  1128
      InEdgeIt(const EdgeSet& _G, Edge e) : Edge(e), G(&_G) { }
alpar@774
  1129
      InEdgeIt(const EdgeSet& _G,Node v)
alpar@774
  1130
	: Edge(_G.nodes[v].first_in), G(&_G) { }
deba@844
  1131
      InEdgeIt &operator++() { 
deba@844
  1132
	Edge::n = G->edges[Edge::n].next_in; 
deba@844
  1133
	return *this; 
deba@844
  1134
      }
alpar@400
  1135
    };
deba@782
  1136
    
alpar@400
  1137
  };
alpar@406
  1138
alpar@579
  1139
  template<typename GG>
alpar@579
  1140
  inline int EdgeSet<GG>::id(Node v) const { return G.id(v); }
alpar@531
  1141
alpar@406
  1142
/// @}  
alpar@406
  1143
alpar@395
  1144
} //namespace hugo
alpar@395
  1145
alpar@405
  1146
#endif //HUGO_LIST_GRAPH_H