src/lemon/smart_graph.h
author deba
Mon, 04 Oct 2004 17:13:21 +0000
changeset 937 d4e911acef3d
parent 921 818510fa3d99
child 946 c94ef40a22ce
permissions -rw-r--r--
Revert backport changes -r1230.
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/smart_graph.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@105
    16
alpar@921
    17
#ifndef LEMON_SMART_GRAPH_H
alpar@921
    18
#define LEMON_SMART_GRAPH_H
alpar@104
    19
klao@491
    20
///\ingroup graphs
alpar@242
    21
///\file
alpar@242
    22
///\brief SmartGraph and SymSmartGraph classes.
alpar@242
    23
alpar@104
    24
#include <vector>
deba@782
    25
#include <climits>
alpar@104
    26
alpar@921
    27
#include <lemon/invalid.h>
alpar@157
    28
deba@937
    29
alpar@921
    30
#include <lemon/array_map.h>
alpar@919
    31
alpar@921
    32
#include <lemon/map_registry.h>
deba@782
    33
alpar@921
    34
#include <lemon/map_defines.h>
deba@782
    35
alpar@921
    36
namespace lemon {
alpar@104
    37
alpar@407
    38
/// \addtogroup graphs
alpar@407
    39
/// @{
deba@782
    40
//  class SymSmartGraph;
alpar@185
    41
alpar@186
    42
  ///A smart graph class.
alpar@186
    43
alpar@186
    44
  ///This is a simple and fast graph implementation.
alpar@186
    45
  ///It is also quite memory efficient, but at the price
alpar@186
    46
  ///that <b> it does not support node and edge deletion</b>.
alpar@880
    47
  ///It conforms to 
alpar@880
    48
  ///the \ref skeleton::ExtendableGraph "ExtendableGraph" concept.
alpar@880
    49
  ///\sa skeleton::ExtendableGraph.
alpar@402
    50
  ///
alpar@402
    51
  ///\todo Some member functions could be \c static.
alpar@753
    52
  ///
alpar@753
    53
  ///\todo A possibly useful functionality: a function saveState() would
alpar@753
    54
  ///give back a data sturcture X and then the function restoreState(X)
alpar@753
    55
  ///would remove the nodes and edges added after the call of saveState().
alpar@753
    56
  ///Of course it should be used as a stack. (Maybe X is not necessary.)
alpar@753
    57
  ///
alpar@456
    58
  ///\author Alpar Juttner
alpar@104
    59
  class SmartGraph {
alpar@104
    60
alpar@104
    61
    struct NodeT 
alpar@104
    62
    {
alpar@104
    63
      int first_in,first_out;      
alpar@157
    64
      NodeT() : first_in(-1), first_out(-1) {}
alpar@104
    65
    };
alpar@104
    66
    struct EdgeT 
alpar@104
    67
    {
alpar@104
    68
      int head, tail, next_in, next_out;      
alpar@104
    69
      //FIXME: is this necessary?
alpar@157
    70
      EdgeT() : next_in(-1), next_out(-1) {}  
alpar@104
    71
    };
alpar@104
    72
alpar@104
    73
    std::vector<NodeT> nodes;
alpar@129
    74
alpar@104
    75
    std::vector<EdgeT> edges;
alpar@104
    76
    
alpar@185
    77
    
alpar@104
    78
  public:
deba@782
    79
deba@782
    80
    typedef SmartGraph Graph;
alpar@104
    81
alpar@164
    82
    class Node;
alpar@164
    83
    class Edge;
alpar@108
    84
alpar@164
    85
    class NodeIt;
alpar@164
    86
    class EdgeIt;
alpar@104
    87
    class OutEdgeIt;
alpar@104
    88
    class InEdgeIt;
alpar@104
    89
    
alpar@904
    90
    // Create map registries.
deba@782
    91
    CREATE_MAP_REGISTRIES;
alpar@904
    92
    // Create node and edge maps.
deba@897
    93
    CREATE_MAPS(ArrayMap);
alpar@104
    94
    
alpar@104
    95
  public:
alpar@104
    96
alpar@104
    97
    SmartGraph() : nodes(), edges() { }
alpar@136
    98
    SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
alpar@104
    99
    
alpar@813
   100
    ///Number of nodes.
alpar@813
   101
    int nodeNum() const { return nodes.size(); }
alpar@813
   102
    ///Number of edges.
alpar@813
   103
    int edgeNum() const { return edges.size(); }
alpar@104
   104
alpar@813
   105
    /// Maximum node ID.
alpar@813
   106
    
alpar@813
   107
    /// Maximum node ID.
alpar@813
   108
    ///\sa id(Node)
alpar@813
   109
    int maxNodeId() const { return nodes.size()-1; }
alpar@813
   110
    /// Maximum edge ID.
alpar@813
   111
    
alpar@813
   112
    /// Maximum edge ID.
alpar@813
   113
    ///\sa id(Edge)
alpar@813
   114
    int maxEdgeId() const { return edges.size()-1; }
alpar@108
   115
alpar@164
   116
    Node tail(Edge e) const { return edges[e.n].tail; }
alpar@164
   117
    Node head(Edge e) const { return edges[e.n].head; }
alpar@104
   118
alpar@164
   119
    NodeIt& first(NodeIt& v) const { 
alpar@164
   120
      v=NodeIt(*this); return v; }
alpar@164
   121
    EdgeIt& first(EdgeIt& e) const { 
alpar@164
   122
      e=EdgeIt(*this); return e; }
alpar@164
   123
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
alpar@104
   124
      e=OutEdgeIt(*this,v); return e; }
alpar@164
   125
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
alpar@104
   126
      e=InEdgeIt(*this,v); return e; }
alpar@104
   127
alpar@813
   128
    /// Node ID.
alpar@813
   129
    
alpar@813
   130
    /// The ID of a valid Node is a nonnegative integer not greater than
alpar@813
   131
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
alpar@813
   132
    /// and the greatest node ID can be actually less then \ref maxNodeId().
alpar@813
   133
    ///
alpar@813
   134
    /// The ID of the \ref INVALID node is -1.
alpar@813
   135
    ///\return The ID of the node \c v. 
alpar@713
   136
    static int id(Node v) { return v.n; }
alpar@813
   137
    /// Edge ID.
alpar@813
   138
    
alpar@813
   139
    /// The ID of a valid Edge is a nonnegative integer not greater than
alpar@813
   140
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
alpar@813
   141
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
alpar@813
   142
    ///
alpar@813
   143
    /// The ID of the \ref INVALID edge is -1.
alpar@813
   144
    ///\return The ID of the edge \c e. 
alpar@713
   145
    static int id(Edge e) { return e.n; }
alpar@104
   146
alpar@164
   147
    Node addNode() {
alpar@164
   148
      Node n; n.n=nodes.size();
alpar@104
   149
      nodes.push_back(NodeT()); //FIXME: Hmmm...
alpar@108
   150
deba@782
   151
      
deba@782
   152
      node_maps.add(n);
alpar@104
   153
      return n;
alpar@104
   154
    }
alpar@108
   155
    
alpar@164
   156
    Edge addEdge(Node u, Node v) {
alpar@164
   157
      Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
alpar@104
   158
      edges[e.n].tail=u.n; edges[e.n].head=v.n;
alpar@104
   159
      edges[e.n].next_out=nodes[u.n].first_out;
alpar@104
   160
      edges[e.n].next_in=nodes[v.n].first_in;
alpar@104
   161
      nodes[u.n].first_out=nodes[v.n].first_in=e.n;
alpar@108
   162
deba@782
   163
      edge_maps.add(e);
alpar@108
   164
alpar@104
   165
      return e;
alpar@104
   166
    }
alpar@104
   167
alpar@774
   168
    /// Finds an edge between two nodes.
alpar@774
   169
alpar@774
   170
    /// Finds an edge from node \c u to node \c v.
alpar@774
   171
    ///
alpar@774
   172
    /// If \c prev is \ref INVALID (this is the default value), then
alpar@774
   173
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
alpar@774
   174
    /// the next edge from \c u to \c v after \c prev.
alpar@774
   175
    /// \return The found edge or INVALID if there is no such an edge.
alpar@774
   176
    Edge findEdge(Node u,Node v, Edge prev = INVALID) 
alpar@774
   177
    {
alpar@774
   178
      int e = (prev.n==-1)? nodes[u.n].first_out : edges[prev.n].next_out;
alpar@774
   179
      while(e!=-1 && edges[e].tail!=v.n) e = edges[e].next_out;
alpar@774
   180
      prev.n=e;
alpar@774
   181
      return prev;
alpar@774
   182
    }
alpar@774
   183
    
deba@782
   184
    void clear() {
deba@782
   185
      edge_maps.clear();
deba@782
   186
      edges.clear();
deba@782
   187
      node_maps.clear();
deba@782
   188
      nodes.clear();
deba@782
   189
    }
alpar@104
   190
alpar@164
   191
    class Node {
alpar@104
   192
      friend class SmartGraph;
alpar@104
   193
      template <typename T> friend class NodeMap;
alpar@104
   194
      
alpar@164
   195
      friend class Edge;
alpar@104
   196
      friend class OutEdgeIt;
alpar@104
   197
      friend class InEdgeIt;
alpar@164
   198
      friend class SymEdge;
alpar@104
   199
alpar@104
   200
    protected:
alpar@104
   201
      int n;
alpar@722
   202
      friend int SmartGraph::id(Node v); 
alpar@164
   203
      Node(int nn) {n=nn;}
alpar@104
   204
    public:
alpar@164
   205
      Node() {}
alpar@503
   206
      Node (Invalid) { n=-1; }
alpar@164
   207
      bool operator==(const Node i) const {return n==i.n;}
alpar@164
   208
      bool operator!=(const Node i) const {return n!=i.n;}
alpar@164
   209
      bool operator<(const Node i) const {return n<i.n;}
alpar@774
   210
      //      ///Validity check
alpar@774
   211
      //      operator bool() { return n!=-1; }
alpar@104
   212
    };
alpar@104
   213
    
alpar@164
   214
    class NodeIt : public Node {
alpar@774
   215
      const SmartGraph *G;
alpar@104
   216
      friend class SmartGraph;
alpar@104
   217
    public:
alpar@402
   218
      NodeIt() : Node() { }
alpar@774
   219
      NodeIt(const SmartGraph& _G,Node n) : Node(n), G(&_G) { }
alpar@402
   220
      NodeIt(Invalid i) : Node(i) { }
alpar@774
   221
      NodeIt(const SmartGraph& _G) : Node(_G.nodes.size()?0:-1), G(&_G) { }
alpar@774
   222
      NodeIt &operator++() {
alpar@774
   223
	n=(n+2)%(G->nodes.size()+1)-1; 
alpar@774
   224
	return *this; 
alpar@774
   225
      }
alpar@774
   226
//       ///Validity check
alpar@774
   227
//       operator bool() { return Node::operator bool(); }      
alpar@104
   228
    };
alpar@104
   229
alpar@164
   230
    class Edge {
alpar@104
   231
      friend class SmartGraph;
alpar@104
   232
      template <typename T> friend class EdgeMap;
alpar@185
   233
alpar@905
   234
      friend class SymSmartGraph;
alpar@104
   235
      
alpar@164
   236
      friend class Node;
alpar@104
   237
      friend class NodeIt;
alpar@104
   238
    protected:
alpar@104
   239
      int n;
alpar@722
   240
      friend int SmartGraph::id(Edge e);
alpar@905
   241
      Edge(int nn) {n=nn;}
alpar@706
   242
    public:
alpar@706
   243
      /// An Edge with id \c n.
alpar@706
   244
alpar@164
   245
      Edge() { }
marci@174
   246
      Edge (Invalid) { n=-1; }
alpar@164
   247
      bool operator==(const Edge i) const {return n==i.n;}
alpar@164
   248
      bool operator!=(const Edge i) const {return n!=i.n;}
alpar@164
   249
      bool operator<(const Edge i) const {return n<i.n;}
alpar@774
   250
//       ///Validity check
alpar@774
   251
//       operator bool() { return n!=-1; }
alpar@905
   252
alpar@905
   253
      ///Set the edge to that have ID \c ID.
alpar@905
   254
      void setToId(int id) { n=id; }
alpar@774
   255
   };
alpar@104
   256
    
alpar@164
   257
    class EdgeIt : public Edge {
alpar@774
   258
      const SmartGraph *G;
alpar@104
   259
      friend class SmartGraph;
alpar@104
   260
    public:
alpar@774
   261
      EdgeIt(const SmartGraph& _G) : Edge(_G.edges.size()-1), G(&_G) { }
alpar@774
   262
      EdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@164
   263
      EdgeIt (Invalid i) : Edge(i) { }
alpar@164
   264
      EdgeIt() : Edge() { }
alpar@774
   265
      EdgeIt &operator++() { --n; return *this; }
alpar@774
   266
//       ///Validity check
alpar@774
   267
//       operator bool() { return Edge::operator bool(); }      
alpar@104
   268
    };
alpar@104
   269
    
alpar@164
   270
    class OutEdgeIt : public Edge {
alpar@774
   271
      const SmartGraph *G;
alpar@104
   272
      friend class SmartGraph;
alpar@104
   273
    public: 
alpar@164
   274
      OutEdgeIt() : Edge() { }
alpar@774
   275
      OutEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@164
   276
      OutEdgeIt (Invalid i) : Edge(i) { }
alpar@157
   277
alpar@774
   278
      OutEdgeIt(const SmartGraph& _G,const Node v)
alpar@774
   279
	: Edge(_G.nodes[v.n].first_out), G(&_G) {}
alpar@774
   280
      OutEdgeIt &operator++() { n=G->edges[n].next_out; return *this; }
alpar@774
   281
//       ///Validity check
alpar@774
   282
//       operator bool() { return Edge::operator bool(); }      
alpar@104
   283
    };
alpar@104
   284
    
alpar@164
   285
    class InEdgeIt : public Edge {
alpar@774
   286
      const SmartGraph *G;
alpar@104
   287
      friend class SmartGraph;
alpar@104
   288
    public: 
alpar@164
   289
      InEdgeIt() : Edge() { }
alpar@774
   290
      InEdgeIt(const SmartGraph& _G, Edge e) : Edge(e), G(&_G) { }
alpar@164
   291
      InEdgeIt (Invalid i) : Edge(i) { }
alpar@774
   292
      InEdgeIt(const SmartGraph& _G,Node v)
alpar@774
   293
	: Edge(_G.nodes[v.n].first_in), G(&_G) { }
alpar@774
   294
      InEdgeIt &operator++() { n=G->edges[n].next_in; return *this; }
alpar@774
   295
//       ///Validity check
alpar@774
   296
//       operator bool() { return Edge::operator bool(); }      
alpar@104
   297
    };
alpar@105
   298
alpar@104
   299
  };
alpar@185
   300
deba@937
   301
deba@937
   302
deba@937
   303
  class SymSmartGraph : public SmartGraph {
deba@937
   304
    typedef SmartGraph Parent;
deba@937
   305
  public:
deba@937
   306
deba@937
   307
    typedef SymSmartGraph Graph;
deba@937
   308
deba@937
   309
    typedef SmartGraph::Node Node;
deba@937
   310
    typedef SmartGraph::NodeIt NodeIt;
deba@937
   311
deba@937
   312
    class SymEdge;
deba@937
   313
    class SymEdgeIt;
deba@937
   314
deba@937
   315
    class Edge;
deba@937
   316
    class EdgeIt;
deba@937
   317
    class OutEdgeIt;
deba@937
   318
    class InEdgeIt;
deba@937
   319
deba@937
   320
    template <typename Value>
deba@937
   321
    class NodeMap : public Parent::NodeMap<Value> {      
deba@937
   322
    public:
deba@937
   323
      NodeMap(const SymSmartGraph& g) 
deba@937
   324
	: SymSmartGraph::Parent::NodeMap<Value>(g) {}
deba@937
   325
      NodeMap(const SymSmartGraph& g, Value v) 
deba@937
   326
	: SymSmartGraph::Parent::NodeMap<Value>(g, v) {}
deba@937
   327
      template<typename TT> 
deba@937
   328
      NodeMap(const NodeMap<TT>& copy) 
deba@937
   329
	: SymSmartGraph::Parent::NodeMap<Value>(copy) { }            
deba@937
   330
    };
deba@937
   331
deba@937
   332
    template <typename Value>
deba@937
   333
    class SymEdgeMap : public Parent::EdgeMap<Value> {
deba@937
   334
    public:
deba@937
   335
      typedef SymEdge KeyType;
deba@937
   336
deba@937
   337
      SymEdgeMap(const SymSmartGraph& g) 
deba@937
   338
	: SymSmartGraph::Parent::EdgeMap<Value>(g) {}
deba@937
   339
      SymEdgeMap(const SymSmartGraph& g, Value v) 
deba@937
   340
	: SymSmartGraph::Parent::EdgeMap<Value>(g, v) {}
deba@937
   341
      template<typename TT> 
deba@937
   342
      SymEdgeMap(const SymEdgeMap<TT>& copy) 
deba@937
   343
	: SymSmartGraph::Parent::EdgeMap<Value>(copy) { }
deba@937
   344
      
deba@937
   345
    };
deba@937
   346
deba@937
   347
    // Create edge map registry.
deba@937
   348
    CREATE_EDGE_MAP_REGISTRY;
deba@937
   349
    // Create edge maps.
deba@937
   350
    CREATE_EDGE_MAP(ArrayMap);
deba@937
   351
deba@937
   352
    class Edge {
deba@937
   353
      friend class SymSmartGraph;
deba@937
   354
      friend class SymSmartGraph::EdgeIt;
deba@937
   355
      friend class SymSmartGraph::OutEdgeIt;
deba@937
   356
      friend class SymSmartGraph::InEdgeIt;
deba@937
   357
      
deba@937
   358
    protected:
deba@937
   359
      int id;
deba@937
   360
deba@937
   361
      Edge(int pid) { id = pid; }
deba@937
   362
deba@937
   363
    public:
deba@937
   364
      /// An Edge with id \c n.
deba@937
   365
deba@937
   366
      Edge() { }
deba@937
   367
      Edge (Invalid) { id = -1; }
deba@937
   368
deba@937
   369
      operator SymEdge(){ return SymEdge(id >> 1);}
deba@937
   370
      
deba@937
   371
      bool operator==(const Edge i) const {return id == i.id;}
deba@937
   372
      bool operator!=(const Edge i) const {return id != i.id;}
deba@937
   373
      bool operator<(const Edge i) const {return id < i.id;}
deba@937
   374
      //      ///Validity check
deba@937
   375
      //      operator bool() { return n!=-1; }
deba@937
   376
    };
deba@937
   377
deba@937
   378
    class SymEdge : public SmartGraph::Edge {
deba@937
   379
      friend class SymSmartGraph;
deba@937
   380
      friend class SymSmartGraph::Edge;
deba@937
   381
      typedef SmartGraph::Edge Parent;
deba@937
   382
deba@937
   383
    protected:      
deba@937
   384
      SymEdge(int pid) : Parent(pid) {}
deba@937
   385
    public:
deba@937
   386
deba@937
   387
      SymEdge() { }
deba@937
   388
      SymEdge(const SmartGraph::Edge& i) : Parent(i) {} 
deba@937
   389
      SymEdge (Invalid) : Parent(INVALID) {}
deba@937
   390
deba@937
   391
    };
deba@937
   392
deba@937
   393
    class OutEdgeIt {
deba@937
   394
      Parent::OutEdgeIt out;
deba@937
   395
      Parent::InEdgeIt in;      
deba@937
   396
    public: 
deba@937
   397
      OutEdgeIt() {}
deba@937
   398
      OutEdgeIt(const SymSmartGraph& g, Edge e) { 
deba@937
   399
	if ((e.id & 1) == 0) {	
deba@937
   400
	  out = Parent::OutEdgeIt(g, SymEdge(e));
deba@937
   401
	  in = Parent::InEdgeIt(g, g.tail(e));
deba@937
   402
	} else {
deba@937
   403
	  out = Parent::OutEdgeIt(INVALID);
deba@937
   404
	  in = Parent::InEdgeIt(g, SymEdge(e));
deba@937
   405
	}
deba@937
   406
      }
deba@937
   407
      OutEdgeIt (Invalid i) : out(INVALID), in(INVALID) { }
deba@937
   408
deba@937
   409
      OutEdgeIt(const SymSmartGraph& g, const Node v)
deba@937
   410
	: out(g, v), in(g, v) {}
deba@937
   411
      OutEdgeIt &operator++() { 
deba@937
   412
	if (out != INVALID) {
deba@937
   413
	  ++out;
deba@937
   414
	} else {
deba@937
   415
	  ++in;
deba@937
   416
	}
deba@937
   417
	return *this; 
deba@937
   418
      }
deba@937
   419
deba@937
   420
      operator Edge() const {
deba@937
   421
	if (out == INVALID && in == INVALID) return INVALID;
deba@937
   422
	return out != INVALID ? forward(out) : backward(in);
deba@937
   423
      }
deba@937
   424
deba@937
   425
      bool operator==(const Edge i) const {return Edge(*this) == i;}
deba@937
   426
      bool operator!=(const Edge i) const {return Edge(*this) != i;}
deba@937
   427
      bool operator<(const Edge i) const {return Edge(*this) < i;}
deba@937
   428
    };
deba@937
   429
deba@937
   430
    class InEdgeIt {
deba@937
   431
      Parent::OutEdgeIt out;
deba@937
   432
      Parent::InEdgeIt in;      
deba@937
   433
    public: 
deba@937
   434
      InEdgeIt() {}
deba@937
   435
      InEdgeIt(const SymSmartGraph& g, Edge e) { 
deba@937
   436
	if ((e.id & 1) == 0) {	
deba@937
   437
	  out = Parent::OutEdgeIt(g, SymEdge(e));
deba@937
   438
	  in = Parent::InEdgeIt(g, g.tail(e));
deba@937
   439
	} else {
deba@937
   440
	  out = Parent::OutEdgeIt(INVALID);
deba@937
   441
	  in = Parent::InEdgeIt(g, SymEdge(e));
deba@937
   442
	}
deba@937
   443
      }
deba@937
   444
      InEdgeIt (Invalid i) : out(INVALID), in(INVALID) { }
deba@937
   445
deba@937
   446
      InEdgeIt(const SymSmartGraph& g, const Node v)
deba@937
   447
	: out(g, v), in(g, v) {}
deba@937
   448
deba@937
   449
      InEdgeIt &operator++() { 
deba@937
   450
	if (out != INVALID) {
deba@937
   451
	  ++out;
deba@937
   452
	} else {
deba@937
   453
	  ++in;
deba@937
   454
	}
deba@937
   455
	return *this; 
deba@937
   456
      }
deba@937
   457
deba@937
   458
      operator Edge() const {
deba@937
   459
	if (out == INVALID && in == INVALID) return INVALID;
deba@937
   460
	return out != INVALID ? backward(out) : forward(in);
deba@937
   461
      }
deba@937
   462
deba@937
   463
      bool operator==(const Edge i) const {return Edge(*this) == i;}
deba@937
   464
      bool operator!=(const Edge i) const {return Edge(*this) != i;}
deba@937
   465
      bool operator<(const Edge i) const {return Edge(*this) < i;}
deba@937
   466
    };
deba@937
   467
deba@937
   468
    class SymEdgeIt : public Parent::EdgeIt {
deba@937
   469
deba@937
   470
    public:
deba@937
   471
      SymEdgeIt() {}
deba@937
   472
deba@937
   473
      SymEdgeIt(const SymSmartGraph& g) 
deba@937
   474
	: SymSmartGraph::Parent::EdgeIt(g) {}
deba@937
   475
deba@937
   476
      SymEdgeIt(const SymSmartGraph& g, SymEdge e) 
deba@937
   477
	: SymSmartGraph::Parent::EdgeIt(g, e) {}
deba@937
   478
deba@937
   479
      SymEdgeIt(Invalid i) 
deba@937
   480
	: SymSmartGraph::Parent::EdgeIt(INVALID) {}
deba@937
   481
deba@937
   482
      SymEdgeIt& operator++() {
deba@937
   483
	SymSmartGraph::Parent::EdgeIt::operator++();
deba@937
   484
	return *this;
deba@937
   485
      }
deba@937
   486
deba@937
   487
      operator SymEdge() const {
deba@937
   488
	return SymEdge
deba@937
   489
	  (static_cast<const SymSmartGraph::Parent::EdgeIt&>(*this));
deba@937
   490
      }
deba@937
   491
      bool operator==(const SymEdge i) const {return SymEdge(*this) == i;}
deba@937
   492
      bool operator!=(const SymEdge i) const {return SymEdge(*this) != i;}
deba@937
   493
      bool operator<(const SymEdge i) const {return SymEdge(*this) < i;}
deba@937
   494
    };
deba@937
   495
deba@937
   496
    class EdgeIt {
deba@937
   497
      SymEdgeIt it;
deba@937
   498
      bool fw;
deba@937
   499
    public:
deba@937
   500
      EdgeIt(const SymSmartGraph& g) : it(g), fw(true) {}
deba@937
   501
      EdgeIt (Invalid i) : it(i) { }
deba@937
   502
      EdgeIt(const SymSmartGraph& g, Edge e) 
deba@937
   503
	: it(g, SymEdge(e)), fw(id(e) & 1 == 0) { }
deba@937
   504
      EdgeIt() { }
deba@937
   505
      EdgeIt& operator++() {
deba@937
   506
	fw = !fw;
deba@937
   507
	if (fw) ++it;
deba@937
   508
	return *this;
deba@937
   509
      }
deba@937
   510
      operator Edge() const {
deba@937
   511
	if (it == INVALID) return INVALID;
deba@937
   512
	return fw ? forward(it) : backward(it);
deba@937
   513
      }
deba@937
   514
      bool operator==(const Edge i) const {return Edge(*this) == i;}
deba@937
   515
      bool operator!=(const Edge i) const {return Edge(*this) != i;}
deba@937
   516
      bool operator<(const Edge i) const {return Edge(*this) < i;}
deba@937
   517
deba@937
   518
    };
deba@937
   519
deba@937
   520
    ///Number of nodes.
deba@937
   521
    int nodeNum() const { return Parent::nodeNum(); }
deba@937
   522
    ///Number of edges.
deba@937
   523
    int edgeNum() const { return 2*Parent::edgeNum(); }
deba@937
   524
    ///Number of symmetric edges.
deba@937
   525
    int symEdgeNum() const { return Parent::edgeNum(); }
deba@937
   526
deba@937
   527
    /// Maximum node ID.
deba@937
   528
    
deba@937
   529
    /// Maximum node ID.
deba@937
   530
    ///\sa id(Node)
deba@937
   531
    int maxNodeId() const { return Parent::maxNodeId(); } 
deba@937
   532
    /// Maximum edge ID.
deba@937
   533
    
deba@937
   534
    /// Maximum edge ID.
deba@937
   535
    ///\sa id(Edge)
deba@937
   536
    int maxEdgeId() const { return 2*Parent::maxEdgeId(); }
deba@937
   537
    /// Maximum symmetric edge ID.
deba@937
   538
    
deba@937
   539
    /// Maximum symmetric edge ID.
deba@937
   540
    ///\sa id(SymEdge)
deba@937
   541
    int maxSymEdgeId() const { return Parent::maxEdgeId(); }
deba@937
   542
deba@937
   543
deba@937
   544
    Node tail(Edge e) const { 
deba@937
   545
      return (e.id & 1) == 0 ? 
deba@937
   546
	Parent::tail(SymEdge(e)) : Parent::head(SymEdge(e)); 
deba@937
   547
    }
deba@937
   548
deba@937
   549
    Node head(Edge e) const { 
deba@937
   550
      return (e.id & 1) == 0 ? 
deba@937
   551
	Parent::head(SymEdge(e)) : Parent::tail(SymEdge(e)); 
deba@937
   552
    }
deba@937
   553
deba@937
   554
    Node tail(SymEdge e) const { 
deba@937
   555
      return Parent::tail(e); 
deba@937
   556
    }
deba@937
   557
deba@937
   558
    Node head(SymEdge e) const { 
deba@937
   559
      return Parent::head(e); 
deba@937
   560
    }
deba@937
   561
deba@937
   562
    NodeIt& first(NodeIt& v) const { 
deba@937
   563
      v=NodeIt(*this); return v; }
deba@937
   564
    EdgeIt& first(EdgeIt& e) const { 
deba@937
   565
      e=EdgeIt(*this); return e; }
deba@937
   566
    SymEdgeIt& first(SymEdgeIt& e) const {
deba@937
   567
      e=SymEdgeIt(*this); return e; }
deba@937
   568
    OutEdgeIt& first(OutEdgeIt& e, const Node v) const { 
deba@937
   569
      e=OutEdgeIt(*this,v); return e; }
deba@937
   570
    InEdgeIt& first(InEdgeIt& e, const Node v) const { 
deba@937
   571
      e=InEdgeIt(*this,v); return e; }
deba@937
   572
deba@937
   573
    /// Node ID.
deba@937
   574
    
deba@937
   575
    /// The ID of a valid Node is a nonnegative integer not greater than
deba@937
   576
    /// \ref maxNodeId(). The range of the ID's is not surely continuous
deba@937
   577
    /// and the greatest node ID can be actually less then \ref maxNodeId().
deba@937
   578
    ///
deba@937
   579
    /// The ID of the \ref INVALID node is -1.
deba@937
   580
    ///\return The ID of the node \c v. 
deba@937
   581
    static int id(Node v) { return Parent::id(v); }
deba@937
   582
    /// Edge ID.
deba@937
   583
    
deba@937
   584
    /// The ID of a valid Edge is a nonnegative integer not greater than
deba@937
   585
    /// \ref maxEdgeId(). The range of the ID's is not surely continuous
deba@937
   586
    /// and the greatest edge ID can be actually less then \ref maxEdgeId().
deba@937
   587
    ///
deba@937
   588
    /// The ID of the \ref INVALID edge is -1.
deba@937
   589
    ///\return The ID of the edge \c e. 
deba@937
   590
    static int id(Edge e) { return e.id; }
deba@937
   591
deba@937
   592
    /// The ID of a valid SymEdge is a nonnegative integer not greater than
deba@937
   593
    /// \ref maxSymEdgeId(). The range of the ID's is not surely continuous
deba@937
   594
    /// and the greatest edge ID can be actually less then \ref maxSymEdgeId().
deba@937
   595
    ///
deba@937
   596
    /// The ID of the \ref INVALID symmetric edge is -1.
deba@937
   597
    ///\return The ID of the edge \c e. 
deba@937
   598
    static int id(SymEdge e) { return Parent::id(e); }
deba@937
   599
deba@937
   600
    /// Adds a new node to the graph.
deba@937
   601
deba@937
   602
    /// \warning It adds the new node to the front of the list.
deba@937
   603
    /// (i.e. the lastly added node becomes the first.)
deba@937
   604
    Node addNode() {
deba@937
   605
      return Parent::addNode();
deba@937
   606
    }
deba@937
   607
    
deba@937
   608
    SymEdge addEdge(Node u, Node v) {
deba@937
   609
      SymEdge se = Parent::addEdge(u, v);
deba@937
   610
      edge_maps.add(forward(se));
deba@937
   611
      edge_maps.add(backward(se));
deba@937
   612
      return se;
deba@937
   613
    }
deba@937
   614
    
deba@937
   615
    /// Finds an edge between two nodes.
deba@937
   616
deba@937
   617
    /// Finds an edge from node \c u to node \c v.
deba@937
   618
    ///
deba@937
   619
    /// If \c prev is \ref INVALID (this is the default value), then
deba@937
   620
    /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@937
   621
    /// the next edge from \c u to \c v after \c prev.
deba@937
   622
    /// \return The found edge or INVALID if there is no such an edge.
deba@937
   623
    Edge findEdge(Node u, Node v, Edge prev = INVALID) 
deba@937
   624
    {     
deba@937
   625
      if (prev == INVALID || id(prev) & 1 == 0) {
deba@937
   626
	SymEdge se = Parent::findEdge(u, v, SymEdge(prev));
deba@937
   627
	if (se != INVALID) return forward(se);
deba@937
   628
      } else {
deba@937
   629
	SymEdge se = Parent::findEdge(v, u, SymEdge(prev));
deba@937
   630
	if (se != INVALID) return backward(se);	
deba@937
   631
      }
deba@937
   632
      return INVALID;
deba@937
   633
    }
deba@937
   634
deba@937
   635
//     /// Finds an symmetric edge between two nodes.
deba@937
   636
deba@937
   637
//     /// Finds an symmetric edge from node \c u to node \c v.
deba@937
   638
//     ///
deba@937
   639
//     /// If \c prev is \ref INVALID (this is the default value), then
deba@937
   640
//     /// It finds the first edge from \c u to \c v. Otherwise it looks for
deba@937
   641
//     /// the next edge from \c u to \c v after \c prev.
deba@937
   642
//     /// \return The found edge or INVALID if there is no such an edge.
deba@937
   643
deba@937
   644
//     SymEdge findEdge(Node u, Node v, SymEdge prev = INVALID) 
deba@937
   645
//     {     
deba@937
   646
//       if (prev == INVALID || id(prev) & 1 == 0) {
deba@937
   647
// 	SymEdge se = Parent::findEdge(u, v, SymEdge(prev));
deba@937
   648
// 	if (se != INVALID) return se;
deba@937
   649
//       } else {
deba@937
   650
// 	SymEdge se = Parent::findEdge(v, u, SymEdge(prev));
deba@937
   651
// 	if (se != INVALID) return se;	
deba@937
   652
//       }
deba@937
   653
//       return INVALID;
deba@937
   654
//     }
deba@937
   655
    
deba@937
   656
  public:
deba@937
   657
deba@937
   658
    void clear() {
deba@937
   659
      edge_maps.clear();
deba@937
   660
      Parent::clear();
deba@937
   661
    }
deba@937
   662
deba@937
   663
    static Edge opposite(Edge e) {
deba@937
   664
      return Edge(id(e) ^ 1);
deba@937
   665
    }
deba@937
   666
deba@937
   667
    static Edge forward(SymEdge e) {
deba@937
   668
      return Edge(id(e) << 1);
deba@937
   669
    }
deba@937
   670
deba@937
   671
    static Edge backward(SymEdge e) {
deba@937
   672
      return Edge((id(e) << 1) | 1);
deba@937
   673
    }
deba@937
   674
deba@937
   675
  };
alpar@185
   676
  ///Graph for bidirectional edges.
alpar@185
   677
alpar@185
   678
  ///The purpose of this graph structure is to handle graphs
alpar@185
   679
  ///having bidirectional edges. Here the function \c addEdge(u,v) adds a pair
alpar@186
   680
  ///of oppositely directed edges.
alpar@186
   681
  ///There is a new edge map type called
alpar@186
   682
  ///\ref SymSmartGraph::SymEdgeMap "SymEdgeMap"
alpar@186
   683
  ///that complements this
alpar@186
   684
  ///feature by
alpar@186
   685
  ///storing shared values for the edge pairs. The usual
alpar@880
   686
  ///\ref Graph::EdgeMap "EdgeMap"
alpar@186
   687
  ///can be used
alpar@185
   688
  ///as well.
alpar@185
   689
  ///
alpar@186
   690
  ///The oppositely directed edge can also be obtained easily
alpar@186
   691
  ///using \ref opposite.
alpar@186
   692
  ///\warning It shares the similarity with \ref SmartGraph that
alpar@186
   693
  ///it is not possible to delete edges or nodes from the graph.
alpar@880
   694
  //\sa SmartGraph.
alpar@185
   695
deba@937
   696
  /*  class SymSmartGraph : public SmartGraph
alpar@185
   697
  {
alpar@185
   698
  public:
deba@782
   699
    typedef SymSmartGraph Graph;
deba@782
   700
alpar@904
   701
    // Create symmetric map registry.
deba@782
   702
    CREATE_SYM_EDGE_MAP_REGISTRY;
alpar@904
   703
    // Create symmetric edge map.
deba@897
   704
    CREATE_SYM_EDGE_MAP(ArrayMap);
deba@822
   705
alpar@186
   706
alpar@185
   707
    SymSmartGraph() : SmartGraph() { }
alpar@185
   708
    SymSmartGraph(const SmartGraph &_g) : SmartGraph(_g) { }
alpar@398
   709
    ///Adds a pair of oppositely directed edges to the graph.
alpar@185
   710
    Edge addEdge(Node u, Node v)
alpar@185
   711
    {
alpar@185
   712
      Edge e = SmartGraph::addEdge(u,v);
deba@798
   713
      Edge f = SmartGraph::addEdge(v,u);
deba@798
   714
      sym_edge_maps.add(e);
deba@798
   715
      sym_edge_maps.add(f);
alpar@185
   716
      return e;
alpar@185
   717
    }
alpar@185
   718
alpar@186
   719
    ///The oppositely directed edge.
alpar@186
   720
alpar@186
   721
    ///Returns the oppositely directed
alpar@186
   722
    ///pair of the edge \c e.
alpar@713
   723
    static Edge opposite(Edge e)
alpar@185
   724
    {
alpar@185
   725
      Edge f;
alpar@905
   726
      f.n = e.n - 2*(e.n%2) + 1;
alpar@185
   727
      return f;
alpar@185
   728
    }
alpar@185
   729
    
alpar@185
   730
deba@937
   731
    };*/
alpar@185
   732
  
alpar@407
   733
  /// @}  
alpar@921
   734
} //namespace lemon
alpar@104
   735
alpar@157
   736
alpar@157
   737
alpar@157
   738
alpar@921
   739
#endif //LEMON_SMART_GRAPH_H