lemon/bits/graph_extender.h
author ladanyi
Thu, 27 Jul 2006 11:49:15 +0000
changeset 2165 947d36fb3b49
parent 2115 4cd528a30ec1
child 2203 5f1a83b565fb
permissions -rw-r--r--
Maybe useful, feel free to remove if not.
deba@1791
     1
/* -*- C++ -*-
deba@1791
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1956
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@1791
     8
 *
deba@1791
     9
 * Permission to use, modify and distribute this software is granted
deba@1791
    10
 * provided that this copyright notice appears in all copies. For
deba@1791
    11
 * precise terms see the accompanying LICENSE file.
deba@1791
    12
 *
deba@1791
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@1791
    14
 * express or implied, and with no claim as to its suitability for any
deba@1791
    15
 * purpose.
deba@1791
    16
 *
deba@1791
    17
 */
deba@1791
    18
deba@1996
    19
#ifndef LEMON_BITS_GRAPH_EXTENDER_H
deba@1996
    20
#define LEMON_BITS_GRAPH_EXTENDER_H
deba@1791
    21
deba@1993
    22
#include <lemon/bits/invalid.h>
deba@2116
    23
#include <lemon/error.h>
deba@1791
    24
deba@1999
    25
#include <lemon/bits/map_extender.h>
deba@1979
    26
#include <lemon/bits/default_map.h>
deba@1979
    27
deba@2116
    28
#include <lemon/concept_check.h>
deba@2116
    29
#include <lemon/concept/maps.h>
deba@2116
    30
deba@1996
    31
///\ingroup graphbits
deba@1996
    32
///\file
deba@1996
    33
///\brief Extenders for the graph types
deba@1791
    34
namespace lemon {
deba@1791
    35
deba@1996
    36
  /// \ingroup graphbits
deba@1996
    37
  ///
deba@1996
    38
  /// \brief Extender for the Graphs
deba@1979
    39
  template <typename Base>
deba@1979
    40
  class GraphExtender : public Base {
deba@1791
    41
  public:
deba@1791
    42
deba@1979
    43
    typedef Base Parent;
deba@1791
    44
    typedef GraphExtender Graph;
deba@1791
    45
deba@1979
    46
    // Base extensions
deba@1979
    47
deba@1791
    48
    typedef typename Parent::Node Node;
deba@1791
    49
    typedef typename Parent::Edge Edge;
deba@1791
    50
deba@1791
    51
    int maxId(Node) const {
deba@1791
    52
      return Parent::maxNodeId();
deba@1791
    53
    }
deba@1791
    54
deba@1791
    55
    int maxId(Edge) const {
deba@1791
    56
      return Parent::maxEdgeId();
deba@1791
    57
    }
deba@1791
    58
deba@1791
    59
    Node fromId(int id, Node) const {
deba@1791
    60
      return Parent::nodeFromId(id);
deba@1791
    61
    }
deba@1791
    62
deba@1791
    63
    Edge fromId(int id, Edge) const {
deba@1791
    64
      return Parent::edgeFromId(id);
deba@1791
    65
    }
deba@1791
    66
deba@1791
    67
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@1791
    68
      if (n == Parent::source(e))
deba@1791
    69
	return Parent::target(e);
deba@1791
    70
      else if(n==Parent::target(e))
deba@1791
    71
	return Parent::source(e);
deba@1791
    72
      else
deba@1791
    73
	return INVALID;
deba@1791
    74
    }
deba@1791
    75
deba@1979
    76
    // Alterable extension
deba@1791
    77
deba@1999
    78
    typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
deba@1999
    79
    typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
deba@1979
    80
deba@1979
    81
deba@1979
    82
  protected:
deba@1979
    83
deba@1979
    84
    mutable NodeNotifier node_notifier;
deba@1979
    85
    mutable EdgeNotifier edge_notifier;
deba@1791
    86
deba@1791
    87
  public:
deba@1791
    88
deba@1979
    89
    NodeNotifier& getNotifier(Node) const {
deba@1979
    90
      return node_notifier;
deba@1979
    91
    }
deba@1979
    92
    
deba@1979
    93
    EdgeNotifier& getNotifier(Edge) const {
deba@1979
    94
      return edge_notifier;
deba@1979
    95
    }
deba@1979
    96
deba@1979
    97
    class NodeIt : public Node { 
deba@1979
    98
      const Graph* graph;
deba@1979
    99
    public:
deba@1979
   100
deba@1979
   101
      NodeIt() {}
deba@1979
   102
deba@1979
   103
      NodeIt(Invalid i) : Node(i) { }
deba@1979
   104
deba@1979
   105
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@2031
   106
	_graph.first(static_cast<Node&>(*this));
deba@1979
   107
      }
deba@1979
   108
deba@1979
   109
      NodeIt(const Graph& _graph, const Node& node) 
deba@1979
   110
	: Node(node), graph(&_graph) {}
deba@1979
   111
deba@1979
   112
      NodeIt& operator++() { 
deba@1979
   113
	graph->next(*this);
deba@1979
   114
	return *this; 
deba@1979
   115
      }
deba@1979
   116
deba@1979
   117
    };
deba@1979
   118
deba@1979
   119
deba@1979
   120
    class EdgeIt : public Edge { 
deba@1979
   121
      const Graph* graph;
deba@1979
   122
    public:
deba@1979
   123
deba@1979
   124
      EdgeIt() { }
deba@1979
   125
deba@1979
   126
      EdgeIt(Invalid i) : Edge(i) { }
deba@1979
   127
deba@1979
   128
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2031
   129
	_graph.first(static_cast<Edge&>(*this));
deba@1979
   130
      }
deba@1979
   131
deba@1979
   132
      EdgeIt(const Graph& _graph, const Edge& e) : 
deba@1979
   133
	Edge(e), graph(&_graph) { }
deba@1979
   134
deba@1979
   135
      EdgeIt& operator++() { 
deba@1979
   136
	graph->next(*this);
deba@1979
   137
	return *this; 
deba@1979
   138
      }
deba@1979
   139
deba@1979
   140
    };
deba@1979
   141
deba@1979
   142
deba@1979
   143
    class OutEdgeIt : public Edge { 
deba@1979
   144
      const Graph* graph;
deba@1979
   145
    public:
deba@1979
   146
deba@1979
   147
      OutEdgeIt() { }
deba@1979
   148
deba@1979
   149
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   150
deba@1979
   151
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   152
	: graph(&_graph) {
deba@1979
   153
	_graph.firstOut(*this, node);
deba@1979
   154
      }
deba@1979
   155
deba@1979
   156
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@1979
   157
	: Edge(edge), graph(&_graph) {}
deba@1979
   158
deba@1979
   159
      OutEdgeIt& operator++() { 
deba@1979
   160
	graph->nextOut(*this);
deba@1979
   161
	return *this; 
deba@1979
   162
      }
deba@1979
   163
deba@1979
   164
    };
deba@1979
   165
deba@1979
   166
deba@1979
   167
    class InEdgeIt : public Edge { 
deba@1979
   168
      const Graph* graph;
deba@1979
   169
    public:
deba@1979
   170
deba@1979
   171
      InEdgeIt() { }
deba@1979
   172
deba@1979
   173
      InEdgeIt(Invalid i) : Edge(i) { }
deba@1979
   174
deba@1979
   175
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@1979
   176
	: graph(&_graph) {
deba@1979
   177
	_graph.firstIn(*this, node);
deba@1979
   178
      }
deba@1979
   179
deba@1979
   180
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@1979
   181
	Edge(edge), graph(&_graph) {}
deba@1979
   182
deba@1979
   183
      InEdgeIt& operator++() { 
deba@1979
   184
	graph->nextIn(*this);
deba@1979
   185
	return *this; 
deba@1979
   186
      }
deba@1979
   187
deba@1979
   188
    };
deba@1979
   189
deba@1979
   190
    /// \brief Base node of the iterator
deba@1979
   191
    ///
athos@2102
   192
    /// Returns the base node (i.e. the source in this case) of the iterator
deba@1979
   193
    Node baseNode(const OutEdgeIt &e) const {
deba@1979
   194
      return Parent::source(e);
deba@1979
   195
    }
deba@1979
   196
    /// \brief Running node of the iterator
deba@1979
   197
    ///
athos@2102
   198
    /// Returns the running node (i.e. the target in this case) of the
deba@1979
   199
    /// iterator
deba@1979
   200
    Node runningNode(const OutEdgeIt &e) const {
deba@1979
   201
      return Parent::target(e);
deba@1979
   202
    }
deba@1979
   203
deba@1979
   204
    /// \brief Base node of the iterator
deba@1979
   205
    ///
athos@2102
   206
    /// Returns the base node (i.e. the target in this case) of the iterator
deba@1979
   207
    Node baseNode(const InEdgeIt &e) const {
deba@1979
   208
      return Parent::target(e);
deba@1979
   209
    }
deba@1979
   210
    /// \brief Running node of the iterator
deba@1979
   211
    ///
athos@2102
   212
    /// Returns the running node (i.e. the source in this case) of the
deba@1979
   213
    /// iterator
deba@1979
   214
    Node runningNode(const InEdgeIt &e) const {
deba@1979
   215
      return Parent::source(e);
deba@1979
   216
    }
deba@1979
   217
deba@1979
   218
    
deba@1979
   219
    template <typename _Value>
deba@1979
   220
    class NodeMap 
deba@1999
   221
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
deba@1979
   222
    public:
deba@1979
   223
      typedef GraphExtender Graph;
deba@1999
   224
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
deba@1979
   225
klao@2046
   226
      explicit NodeMap(const Graph& graph) 
deba@1999
   227
	: Parent(graph) {}
deba@1999
   228
      NodeMap(const Graph& graph, const _Value& value) 
deba@1999
   229
	: Parent(graph, value) {}
deba@1979
   230
deba@1979
   231
      NodeMap& operator=(const NodeMap& cmap) {
deba@1979
   232
	return operator=<NodeMap>(cmap);
deba@1979
   233
      }
deba@1979
   234
deba@1979
   235
      template <typename CMap>
deba@1979
   236
      NodeMap& operator=(const CMap& cmap) {
deba@2031
   237
        Parent::operator=(cmap);
deba@1979
   238
	return *this;
deba@1979
   239
      }
deba@1979
   240
deba@1979
   241
    };
deba@1979
   242
deba@1979
   243
    template <typename _Value>
deba@1979
   244
    class EdgeMap 
deba@1999
   245
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
deba@1979
   246
    public:
deba@1979
   247
      typedef GraphExtender Graph;
deba@1999
   248
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
deba@1979
   249
klao@2046
   250
      explicit EdgeMap(const Graph& graph) 
deba@1999
   251
	: Parent(graph) {}
deba@1999
   252
      EdgeMap(const Graph& graph, const _Value& value) 
deba@1999
   253
	: Parent(graph, value) {}
deba@1979
   254
deba@1979
   255
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@1979
   256
	return operator=<EdgeMap>(cmap);
deba@1979
   257
      }
deba@1979
   258
deba@1979
   259
      template <typename CMap>
deba@1979
   260
      EdgeMap& operator=(const CMap& cmap) {
deba@2031
   261
        Parent::operator=(cmap);
deba@1979
   262
	return *this;
deba@1979
   263
      }
deba@1979
   264
    };
deba@1979
   265
deba@1979
   266
deba@1979
   267
    Node addNode() {
deba@1979
   268
      Node node = Parent::addNode();
deba@1979
   269
      getNotifier(Node()).add(node);
deba@1979
   270
      return node;
deba@1979
   271
    }
deba@1979
   272
    
deba@1979
   273
    Edge addEdge(const Node& from, const Node& to) {
deba@1979
   274
      Edge edge = Parent::addEdge(from, to);
deba@1979
   275
      getNotifier(Edge()).add(edge);
deba@1979
   276
      return edge;
deba@1979
   277
    }
deba@1979
   278
deba@1979
   279
    void clear() {
deba@1979
   280
      getNotifier(Edge()).clear();
deba@1979
   281
      getNotifier(Node()).clear();
deba@1979
   282
      Parent::clear();
deba@1979
   283
    }
deba@1979
   284
deba@1979
   285
deba@1979
   286
    void erase(const Node& node) {
deba@1979
   287
      Edge edge;
deba@1979
   288
      Parent::firstOut(edge, node);
deba@1979
   289
      while (edge != INVALID ) {
deba@1979
   290
	erase(edge);
deba@1979
   291
	Parent::firstOut(edge, node);
deba@1979
   292
      } 
deba@1979
   293
deba@1979
   294
      Parent::firstIn(edge, node);
deba@1979
   295
      while (edge != INVALID ) {
deba@1979
   296
	erase(edge);
deba@1979
   297
	Parent::firstIn(edge, node);
deba@1979
   298
      }
deba@1979
   299
deba@1979
   300
      getNotifier(Node()).erase(node);
deba@1979
   301
      Parent::erase(node);
deba@1979
   302
    }
deba@1979
   303
    
deba@1979
   304
    void erase(const Edge& edge) {
deba@1979
   305
      getNotifier(Edge()).erase(edge);
deba@1979
   306
      Parent::erase(edge);
deba@1979
   307
    }
deba@1979
   308
deba@1999
   309
    GraphExtender() {
deba@1999
   310
      node_notifier.setContainer(*this);
deba@1999
   311
      edge_notifier.setContainer(*this);
deba@1999
   312
    } 
deba@1999
   313
    
deba@1979
   314
deba@1979
   315
    ~GraphExtender() {
deba@1999
   316
      edge_notifier.clear();
deba@1999
   317
      node_notifier.clear();
deba@1979
   318
    }
deba@1979
   319
  };
deba@1979
   320
deba@2116
   321
  /// \ingroup graphbits
deba@2116
   322
  ///
deba@2116
   323
  /// \brief Extender for the UGraphs
deba@2116
   324
  template <typename Base> 
deba@2116
   325
  class UGraphExtender : public Base {
deba@2116
   326
  public:
deba@2116
   327
    
deba@2116
   328
    typedef Base Parent;
deba@2116
   329
    typedef UGraphExtender Graph;
deba@2116
   330
deba@2116
   331
    typedef typename Parent::Node Node;
deba@2116
   332
    typedef typename Parent::Edge Edge;
deba@2116
   333
    typedef typename Parent::UEdge UEdge;
deba@2116
   334
deba@2116
   335
    // UGraph extension    
deba@2116
   336
deba@2116
   337
    int maxId(Node) const {
deba@2116
   338
      return Parent::maxNodeId();
deba@2116
   339
    }
deba@2116
   340
deba@2116
   341
    int maxId(Edge) const {
deba@2116
   342
      return Parent::maxEdgeId();
deba@2116
   343
    }
deba@2116
   344
deba@2116
   345
    int maxId(UEdge) const {
deba@2116
   346
      return Parent::maxUEdgeId();
deba@2116
   347
    }
deba@2116
   348
deba@2116
   349
    Node fromId(int id, Node) const {
deba@2116
   350
      return Parent::nodeFromId(id);
deba@2116
   351
    }
deba@2116
   352
deba@2116
   353
    Edge fromId(int id, Edge) const {
deba@2116
   354
      return Parent::edgeFromId(id);
deba@2116
   355
    }
deba@2116
   356
deba@2116
   357
    UEdge fromId(int id, UEdge) const {
deba@2116
   358
      return Parent::uEdgeFromId(id);
deba@2116
   359
    }
deba@2116
   360
deba@2116
   361
    Node oppositeNode(const Node &n, const UEdge &e) const {
deba@2116
   362
      if( n == Parent::source(e))
deba@2116
   363
	return Parent::target(e);
deba@2116
   364
      else if( n == Parent::target(e))
deba@2116
   365
	return Parent::source(e);
deba@2116
   366
      else
deba@2116
   367
	return INVALID;
deba@2116
   368
    }
deba@2116
   369
deba@2116
   370
    Edge oppositeEdge(const Edge &e) const {
deba@2116
   371
      return Parent::direct(e, !Parent::direction(e));
deba@2116
   372
    }
deba@2116
   373
deba@2116
   374
    using Parent::direct;
deba@2116
   375
    Edge direct(const UEdge &ue, const Node &s) const {
deba@2116
   376
      return Parent::direct(ue, Parent::source(ue) == s);
deba@2116
   377
    }
deba@2116
   378
deba@2116
   379
    // Alterable extension
deba@2116
   380
deba@2116
   381
    typedef AlterationNotifier<UGraphExtender, Node> NodeNotifier;
deba@2116
   382
    typedef AlterationNotifier<UGraphExtender, Edge> EdgeNotifier;
deba@2116
   383
    typedef AlterationNotifier<UGraphExtender, UEdge> UEdgeNotifier;
deba@2116
   384
deba@2116
   385
deba@2116
   386
  protected:
deba@2116
   387
deba@2116
   388
    mutable NodeNotifier node_notifier;
deba@2116
   389
    mutable EdgeNotifier edge_notifier;
deba@2116
   390
    mutable UEdgeNotifier uedge_notifier;
deba@2116
   391
deba@2116
   392
  public:
deba@2116
   393
deba@2116
   394
    NodeNotifier& getNotifier(Node) const {
deba@2116
   395
      return node_notifier;
deba@2116
   396
    }
deba@2116
   397
    
deba@2116
   398
    EdgeNotifier& getNotifier(Edge) const {
deba@2116
   399
      return edge_notifier;
deba@2116
   400
    }
deba@2116
   401
deba@2116
   402
    UEdgeNotifier& getNotifier(UEdge) const {
deba@2116
   403
      return uedge_notifier;
deba@2116
   404
    }
deba@2116
   405
deba@2116
   406
deba@2116
   407
deba@2116
   408
    class NodeIt : public Node { 
deba@2116
   409
      const Graph* graph;
deba@2116
   410
    public:
deba@2116
   411
deba@2116
   412
      NodeIt() {}
deba@2116
   413
deba@2116
   414
      NodeIt(Invalid i) : Node(i) { }
deba@2116
   415
deba@2116
   416
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   417
	_graph.first(static_cast<Node&>(*this));
deba@2116
   418
      }
deba@2116
   419
deba@2116
   420
      NodeIt(const Graph& _graph, const Node& node) 
deba@2116
   421
	: Node(node), graph(&_graph) {}
deba@2116
   422
deba@2116
   423
      NodeIt& operator++() { 
deba@2116
   424
	graph->next(*this);
deba@2116
   425
	return *this; 
deba@2116
   426
      }
deba@2116
   427
deba@2116
   428
    };
deba@2116
   429
deba@2116
   430
deba@2116
   431
    class EdgeIt : public Edge { 
deba@2116
   432
      const Graph* graph;
deba@2116
   433
    public:
deba@2116
   434
deba@2116
   435
      EdgeIt() { }
deba@2116
   436
deba@2116
   437
      EdgeIt(Invalid i) : Edge(i) { }
deba@2116
   438
deba@2116
   439
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   440
	_graph.first(static_cast<Edge&>(*this));
deba@2116
   441
      }
deba@2116
   442
deba@2116
   443
      EdgeIt(const Graph& _graph, const Edge& e) : 
deba@2116
   444
	Edge(e), graph(&_graph) { }
deba@2116
   445
deba@2116
   446
      EdgeIt& operator++() { 
deba@2116
   447
	graph->next(*this);
deba@2116
   448
	return *this; 
deba@2116
   449
      }
deba@2116
   450
deba@2116
   451
    };
deba@2116
   452
deba@2116
   453
deba@2116
   454
    class OutEdgeIt : public Edge { 
deba@2116
   455
      const Graph* graph;
deba@2116
   456
    public:
deba@2116
   457
deba@2116
   458
      OutEdgeIt() { }
deba@2116
   459
deba@2116
   460
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@2116
   461
deba@2116
   462
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@2116
   463
	: graph(&_graph) {
deba@2116
   464
	_graph.firstOut(*this, node);
deba@2116
   465
      }
deba@2116
   466
deba@2116
   467
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@2116
   468
	: Edge(edge), graph(&_graph) {}
deba@2116
   469
deba@2116
   470
      OutEdgeIt& operator++() { 
deba@2116
   471
	graph->nextOut(*this);
deba@2116
   472
	return *this; 
deba@2116
   473
      }
deba@2116
   474
deba@2116
   475
    };
deba@2116
   476
deba@2116
   477
deba@2116
   478
    class InEdgeIt : public Edge { 
deba@2116
   479
      const Graph* graph;
deba@2116
   480
    public:
deba@2116
   481
deba@2116
   482
      InEdgeIt() { }
deba@2116
   483
deba@2116
   484
      InEdgeIt(Invalid i) : Edge(i) { }
deba@2116
   485
deba@2116
   486
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@2116
   487
	: graph(&_graph) {
deba@2116
   488
	_graph.firstIn(*this, node);
deba@2116
   489
      }
deba@2116
   490
deba@2116
   491
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@2116
   492
	Edge(edge), graph(&_graph) {}
deba@2116
   493
deba@2116
   494
      InEdgeIt& operator++() { 
deba@2116
   495
	graph->nextIn(*this);
deba@2116
   496
	return *this; 
deba@2116
   497
      }
deba@2116
   498
deba@2116
   499
    };
deba@2116
   500
deba@2116
   501
deba@2116
   502
    class UEdgeIt : public Parent::UEdge { 
deba@2116
   503
      const Graph* graph;
deba@2116
   504
    public:
deba@2116
   505
deba@2116
   506
      UEdgeIt() { }
deba@2116
   507
deba@2116
   508
      UEdgeIt(Invalid i) : UEdge(i) { }
deba@2116
   509
deba@2116
   510
      explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   511
	_graph.first(static_cast<UEdge&>(*this));
deba@2116
   512
      }
deba@2116
   513
deba@2116
   514
      UEdgeIt(const Graph& _graph, const UEdge& e) : 
deba@2116
   515
	UEdge(e), graph(&_graph) { }
deba@2116
   516
deba@2116
   517
      UEdgeIt& operator++() { 
deba@2116
   518
	graph->next(*this);
deba@2116
   519
	return *this; 
deba@2116
   520
      }
deba@2116
   521
deba@2116
   522
    };
deba@2116
   523
deba@2116
   524
    class IncEdgeIt : public Parent::UEdge {
deba@2116
   525
      friend class UGraphExtender;
deba@2116
   526
      const Graph* graph;
deba@2116
   527
      bool direction;
deba@2116
   528
    public:
deba@2116
   529
deba@2116
   530
      IncEdgeIt() { }
deba@2116
   531
deba@2116
   532
      IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
deba@2116
   533
deba@2116
   534
      IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
deba@2116
   535
	_graph.firstInc(*this, direction, n);
deba@2116
   536
      }
deba@2116
   537
deba@2116
   538
      IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
deba@2116
   539
	: graph(&_graph), UEdge(ue) {
deba@2116
   540
	direction = (_graph.source(ue) == n);
deba@2116
   541
      }
deba@2116
   542
deba@2116
   543
      IncEdgeIt& operator++() {
deba@2116
   544
	graph->nextInc(*this, direction);
deba@2116
   545
	return *this; 
deba@2116
   546
      }
deba@2116
   547
    };
deba@2116
   548
deba@2116
   549
    /// \brief Base node of the iterator
deba@2116
   550
    ///
deba@2116
   551
    /// Returns the base node (ie. the source in this case) of the iterator
deba@2116
   552
    Node baseNode(const OutEdgeIt &e) const {
deba@2116
   553
      return Parent::source((Edge)e);
deba@2116
   554
    }
deba@2116
   555
    /// \brief Running node of the iterator
deba@2116
   556
    ///
deba@2116
   557
    /// Returns the running node (ie. the target in this case) of the
deba@2116
   558
    /// iterator
deba@2116
   559
    Node runningNode(const OutEdgeIt &e) const {
deba@2116
   560
      return Parent::target((Edge)e);
deba@2116
   561
    }
deba@2116
   562
deba@2116
   563
    /// \brief Base node of the iterator
deba@2116
   564
    ///
deba@2116
   565
    /// Returns the base node (ie. the target in this case) of the iterator
deba@2116
   566
    Node baseNode(const InEdgeIt &e) const {
deba@2116
   567
      return Parent::target((Edge)e);
deba@2116
   568
    }
deba@2116
   569
    /// \brief Running node of the iterator
deba@2116
   570
    ///
deba@2116
   571
    /// Returns the running node (ie. the source in this case) of the
deba@2116
   572
    /// iterator
deba@2116
   573
    Node runningNode(const InEdgeIt &e) const {
deba@2116
   574
      return Parent::source((Edge)e);
deba@2116
   575
    }
deba@2116
   576
deba@2116
   577
    /// Base node of the iterator
deba@2116
   578
    ///
deba@2116
   579
    /// Returns the base node of the iterator
deba@2116
   580
    Node baseNode(const IncEdgeIt &e) const {
deba@2116
   581
      return e.direction ? source(e) : target(e);
deba@2116
   582
    }
deba@2116
   583
    /// Running node of the iterator
deba@2116
   584
    ///
deba@2116
   585
    /// Returns the running node of the iterator
deba@2116
   586
    Node runningNode(const IncEdgeIt &e) const {
deba@2116
   587
      return e.direction ? target(e) : source(e);
deba@2116
   588
    }
deba@2116
   589
deba@2116
   590
    // Mappable extension
deba@2116
   591
deba@2116
   592
    template <typename _Value>
deba@2116
   593
    class NodeMap 
deba@2116
   594
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
deba@2116
   595
    public:
deba@2116
   596
      typedef UGraphExtender Graph;
deba@2116
   597
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
deba@2116
   598
deba@2116
   599
      NodeMap(const Graph& graph) 
deba@2116
   600
	: Parent(graph) {}
deba@2116
   601
      NodeMap(const Graph& graph, const _Value& value) 
deba@2116
   602
	: Parent(graph, value) {}
deba@2116
   603
deba@2116
   604
      NodeMap& operator=(const NodeMap& cmap) {
deba@2116
   605
	return operator=<NodeMap>(cmap);
deba@2116
   606
      }
deba@2116
   607
deba@2116
   608
      template <typename CMap>
deba@2116
   609
      NodeMap& operator=(const CMap& cmap) {
deba@2116
   610
        Parent::operator=(cmap);
deba@2116
   611
	return *this;
deba@2116
   612
      }
deba@2116
   613
deba@2116
   614
    };
deba@2116
   615
deba@2116
   616
    template <typename _Value>
deba@2116
   617
    class EdgeMap 
deba@2116
   618
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
deba@2116
   619
    public:
deba@2116
   620
      typedef UGraphExtender Graph;
deba@2116
   621
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
deba@2116
   622
deba@2116
   623
      EdgeMap(const Graph& graph) 
deba@2116
   624
	: Parent(graph) {}
deba@2116
   625
      EdgeMap(const Graph& graph, const _Value& value) 
deba@2116
   626
	: Parent(graph, value) {}
deba@2116
   627
deba@2116
   628
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2116
   629
	return operator=<EdgeMap>(cmap);
deba@2116
   630
      }
deba@2116
   631
deba@2116
   632
      template <typename CMap>
deba@2116
   633
      EdgeMap& operator=(const CMap& cmap) {
deba@2116
   634
        Parent::operator=(cmap);
deba@2116
   635
	return *this;
deba@2116
   636
      }
deba@2116
   637
    };
deba@2116
   638
deba@2116
   639
deba@2116
   640
    template <typename _Value>
deba@2116
   641
    class UEdgeMap 
deba@2116
   642
      : public MapExtender<DefaultMap<Graph, UEdge, _Value> > {
deba@2116
   643
    public:
deba@2116
   644
      typedef UGraphExtender Graph;
deba@2116
   645
      typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
deba@2116
   646
deba@2116
   647
      UEdgeMap(const Graph& graph) 
deba@2116
   648
	: Parent(graph) {}
deba@2116
   649
deba@2116
   650
      UEdgeMap(const Graph& graph, const _Value& value) 
deba@2116
   651
	: Parent(graph, value) {}
deba@2116
   652
deba@2116
   653
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2116
   654
	return operator=<UEdgeMap>(cmap);
deba@2116
   655
      }
deba@2116
   656
deba@2116
   657
      template <typename CMap>
deba@2116
   658
      UEdgeMap& operator=(const CMap& cmap) {
deba@2116
   659
        Parent::operator=(cmap);
deba@2116
   660
	return *this;
deba@2116
   661
      }
deba@2116
   662
deba@2116
   663
    };
deba@2116
   664
deba@2116
   665
    // Alteration extension
deba@2116
   666
deba@2116
   667
    Node addNode() {
deba@2116
   668
      Node node = Parent::addNode();
deba@2116
   669
      getNotifier(Node()).add(node);
deba@2116
   670
      return node;
deba@2116
   671
    }
deba@2116
   672
deba@2116
   673
    UEdge addEdge(const Node& from, const Node& to) {
deba@2116
   674
      UEdge uedge = Parent::addEdge(from, to);
deba@2116
   675
      getNotifier(UEdge()).add(uedge);
deba@2116
   676
      getNotifier(Edge()).add(Parent::direct(uedge, true));
deba@2116
   677
      getNotifier(Edge()).add(Parent::direct(uedge, false));
deba@2116
   678
      return uedge;
deba@2116
   679
    }
deba@2116
   680
    
deba@2116
   681
    void clear() {
deba@2116
   682
      getNotifier(Edge()).clear();
deba@2116
   683
      getNotifier(UEdge()).clear();
deba@2116
   684
      getNotifier(Node()).clear();
deba@2116
   685
      Parent::clear();
deba@2116
   686
    }
deba@2116
   687
deba@2116
   688
    void erase(const Node& node) {
deba@2116
   689
      Edge edge;
deba@2116
   690
      Parent::firstOut(edge, node);
deba@2116
   691
      while (edge != INVALID ) {
deba@2116
   692
	erase(edge);
deba@2116
   693
	Parent::firstOut(edge, node);
deba@2116
   694
      } 
deba@2116
   695
deba@2116
   696
      Parent::firstIn(edge, node);
deba@2116
   697
      while (edge != INVALID ) {
deba@2116
   698
	erase(edge);
deba@2116
   699
	Parent::firstIn(edge, node);
deba@2116
   700
      }
deba@2116
   701
deba@2116
   702
      getNotifier(Node()).erase(node);
deba@2116
   703
      Parent::erase(node);
deba@2116
   704
    }
deba@2116
   705
deba@2116
   706
    void erase(const UEdge& uedge) {
deba@2116
   707
      getNotifier(Edge()).erase(Parent::direct(uedge, true));
deba@2116
   708
      getNotifier(Edge()).erase(Parent::direct(uedge, false));
deba@2116
   709
      getNotifier(UEdge()).erase(uedge);
deba@2116
   710
      Parent::erase(uedge);
deba@2116
   711
    }
deba@2116
   712
deba@2116
   713
    UGraphExtender() {
deba@2116
   714
      node_notifier.setContainer(*this); 
deba@2116
   715
      edge_notifier.setContainer(*this);
deba@2116
   716
      uedge_notifier.setContainer(*this);
deba@2116
   717
    } 
deba@2116
   718
deba@2116
   719
    ~UGraphExtender() {
deba@2116
   720
      uedge_notifier.clear();
deba@2116
   721
      edge_notifier.clear();
deba@2116
   722
      node_notifier.clear(); 
deba@2116
   723
    } 
deba@2116
   724
deba@2116
   725
  };
deba@2116
   726
deba@2116
   727
  /// \ingroup graphbits
deba@2116
   728
  ///
deba@2116
   729
  /// \brief Extender for the BpUGraphs
deba@2116
   730
  template <typename Base>
deba@2116
   731
  class BpUGraphExtender : public Base {
deba@2116
   732
  public:
deba@2116
   733
    typedef Base Parent;
deba@2116
   734
    typedef BpUGraphExtender Graph;
deba@2116
   735
deba@2116
   736
    typedef typename Parent::Node Node;
deba@2116
   737
    typedef typename Parent::UEdge UEdge;
deba@2116
   738
deba@2116
   739
deba@2116
   740
    using Parent::first;
deba@2116
   741
    using Parent::next;
deba@2116
   742
deba@2116
   743
    using Parent::id;
deba@2116
   744
deba@2116
   745
    class ANode : public Node {
deba@2116
   746
      friend class BpUGraphExtender;
deba@2116
   747
    public:
deba@2116
   748
      ANode() {}
deba@2116
   749
      ANode(const Node& node) : Node(node) {
deba@2116
   750
	LEMON_ASSERT(Parent::aNode(node) || node == INVALID, 
deba@2116
   751
		     typename Parent::NodeSetError());
deba@2116
   752
      }
deba@2116
   753
      ANode(Invalid) : Node(INVALID) {}
deba@2116
   754
    };
deba@2116
   755
deba@2116
   756
    void first(ANode& node) const {
deba@2116
   757
      Parent::firstANode(static_cast<Node&>(node));
deba@2116
   758
    }
deba@2116
   759
    void next(ANode& node) const {
deba@2116
   760
      Parent::nextANode(static_cast<Node&>(node));
deba@2116
   761
    }
deba@2116
   762
deba@2116
   763
    int id(const ANode& node) const {
deba@2116
   764
      return Parent::aNodeId(node);
deba@2116
   765
    }
deba@2116
   766
deba@2116
   767
    class BNode : public Node {
deba@2116
   768
      friend class BpUGraphExtender;
deba@2116
   769
    public:
deba@2116
   770
      BNode() {}
deba@2116
   771
      BNode(const Node& node) : Node(node) {
deba@2116
   772
	LEMON_ASSERT(Parent::bNode(node) || node == INVALID,
deba@2116
   773
		     typename Parent::NodeSetError());
deba@2116
   774
      }
deba@2116
   775
      BNode(Invalid) : Node(INVALID) {}
deba@2116
   776
    };
deba@2116
   777
deba@2116
   778
    void first(BNode& node) const {
deba@2116
   779
      Parent::firstBNode(static_cast<Node&>(node));
deba@2116
   780
    }
deba@2116
   781
    void next(BNode& node) const {
deba@2116
   782
      Parent::nextBNode(static_cast<Node&>(node));
deba@2116
   783
    }
deba@2116
   784
  
deba@2116
   785
    int id(const BNode& node) const {
deba@2116
   786
      return Parent::aNodeId(node);
deba@2116
   787
    }
deba@2116
   788
deba@2116
   789
    Node source(const UEdge& edge) const {
deba@2116
   790
      return aNode(edge);
deba@2116
   791
    }
deba@2116
   792
    Node target(const UEdge& edge) const {
deba@2116
   793
      return bNode(edge);
deba@2116
   794
    }
deba@2116
   795
deba@2116
   796
    void firstInc(UEdge& edge, bool& direction, const Node& node) const {
deba@2116
   797
      if (Parent::aNode(node)) {
deba@2116
   798
	Parent::firstFromANode(edge, node);
deba@2116
   799
	direction = true;
deba@2116
   800
      } else {
deba@2116
   801
	Parent::firstFromBNode(edge, node);
deba@2116
   802
	direction = static_cast<UEdge&>(edge) == INVALID;
deba@2116
   803
      }
deba@2116
   804
    }
deba@2116
   805
    void nextInc(UEdge& edge, bool& direction) const {
deba@2116
   806
      if (direction) {
deba@2116
   807
	Parent::nextFromANode(edge);
deba@2116
   808
      } else {
deba@2116
   809
	Parent::nextFromBNode(edge);
deba@2116
   810
	if (edge == INVALID) direction = true;
deba@2116
   811
      }
deba@2116
   812
    }
deba@2116
   813
deba@2116
   814
    class Edge : public UEdge {
deba@2116
   815
      friend class BpUGraphExtender;
deba@2116
   816
    protected:
deba@2116
   817
      bool forward;
deba@2116
   818
deba@2116
   819
      Edge(const UEdge& edge, bool _forward)
deba@2116
   820
	: UEdge(edge), forward(_forward) {}
deba@2116
   821
deba@2116
   822
    public:
deba@2116
   823
      Edge() {}
deba@2116
   824
      Edge (Invalid) : UEdge(INVALID), forward(true) {}
deba@2116
   825
      bool operator==(const Edge& i) const {
deba@2116
   826
	return UEdge::operator==(i) && forward == i.forward;
deba@2116
   827
      }
deba@2116
   828
      bool operator!=(const Edge& i) const {
deba@2116
   829
	return UEdge::operator!=(i) || forward != i.forward;
deba@2116
   830
      }
deba@2116
   831
      bool operator<(const Edge& i) const {
deba@2116
   832
	return UEdge::operator<(i) || 
deba@2116
   833
	  (!(i.forward<forward) && UEdge(*this)<UEdge(i));
deba@2116
   834
      }
deba@2116
   835
    };
deba@2116
   836
deba@2116
   837
    void first(Edge& edge) const {
deba@2116
   838
      Parent::first(static_cast<UEdge&>(edge));
deba@2116
   839
      edge.forward = true;
deba@2116
   840
    }
deba@2116
   841
deba@2116
   842
    void next(Edge& edge) const {
deba@2116
   843
      if (!edge.forward) {
deba@2116
   844
	Parent::next(static_cast<UEdge&>(edge));
deba@2116
   845
      }
deba@2116
   846
      edge.forward = !edge.forward;
deba@2116
   847
    }
deba@2116
   848
deba@2116
   849
    void firstOut(Edge& edge, const Node& node) const {
deba@2116
   850
      if (Parent::aNode(node)) {
deba@2116
   851
	Parent::firstFromANode(edge, node);
deba@2116
   852
	edge.forward = true;
deba@2116
   853
      } else {
deba@2116
   854
	Parent::firstFromBNode(edge, node);
deba@2116
   855
	edge.forward = static_cast<UEdge&>(edge) == INVALID;
deba@2116
   856
      }
deba@2116
   857
    }
deba@2116
   858
    void nextOut(Edge& edge) const {
deba@2116
   859
      if (edge.forward) {
deba@2116
   860
	Parent::nextFromANode(edge);
deba@2116
   861
      } else {
deba@2116
   862
	Parent::nextFromBNode(edge);
deba@2116
   863
        edge.forward = static_cast<UEdge&>(edge) == INVALID;
deba@2116
   864
      }
deba@2116
   865
    }
deba@2116
   866
deba@2116
   867
    void firstIn(Edge& edge, const Node& node) const {
deba@2116
   868
      if (Parent::bNode(node)) {
deba@2116
   869
	Parent::firstFromBNode(edge, node);
deba@2116
   870
	edge.forward = true;	
deba@2116
   871
      } else {
deba@2116
   872
	Parent::firstFromANode(edge, node);
deba@2116
   873
	edge.forward = static_cast<UEdge&>(edge) == INVALID;
deba@2116
   874
      }
deba@2116
   875
    }
deba@2116
   876
    void nextIn(Edge& edge) const {
deba@2116
   877
      if (edge.forward) {
deba@2116
   878
	Parent::nextFromBNode(edge);
deba@2116
   879
      } else {
deba@2116
   880
	Parent::nextFromANode(edge);
deba@2116
   881
	edge.forward = static_cast<UEdge&>(edge) == INVALID;
deba@2116
   882
      }
deba@2116
   883
    }
deba@2116
   884
deba@2116
   885
    Node source(const Edge& edge) const {
deba@2116
   886
      return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge);
deba@2116
   887
    }
deba@2116
   888
    Node target(const Edge& edge) const {
deba@2116
   889
      return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge);
deba@2116
   890
    }
deba@2116
   891
deba@2116
   892
    int id(const Edge& edge) const {
deba@2116
   893
      return (Parent::id(static_cast<const UEdge&>(edge)) << 1) + 
deba@2116
   894
        (edge.forward ? 0 : 1);
deba@2116
   895
    }
deba@2116
   896
    Edge edgeFromId(int id) const {
deba@2116
   897
      return Edge(Parent::fromUEdgeId(id >> 1), (id & 1) == 0);
deba@2116
   898
    }
deba@2116
   899
    int maxEdgeId() const {
deba@2116
   900
      return (Parent::maxUEdgeId(UEdge()) << 1) + 1;
deba@2116
   901
    }
deba@2116
   902
deba@2116
   903
    bool direction(const Edge& edge) const {
deba@2116
   904
      return edge.forward;
deba@2116
   905
    }
deba@2116
   906
deba@2116
   907
    Edge direct(const UEdge& edge, bool direction) const {
deba@2116
   908
      return Edge(edge, direction);
deba@2116
   909
    }
deba@2116
   910
deba@2116
   911
    int edgeNum() const {
deba@2116
   912
      return 2 * Parent::uEdgeNum();
deba@2116
   913
    }
deba@2116
   914
deba@2116
   915
    int uEdgeNum() const {
deba@2116
   916
      return Parent::uEdgeNum();
deba@2116
   917
    }
deba@2116
   918
deba@2116
   919
    Node oppositeNode(const UEdge& edge, const Node& node) const {
deba@2116
   920
      return source(edge) == node ? 
deba@2116
   921
	target(edge) : source(edge);
deba@2116
   922
    }
deba@2116
   923
deba@2116
   924
    Edge direct(const UEdge& edge, const Node& node) const {
deba@2116
   925
      return Edge(edge, node == Parent::source(edge));
deba@2116
   926
    }
deba@2116
   927
deba@2116
   928
    Edge oppositeEdge(const Edge& edge) const {
deba@2116
   929
      return Parent::direct(edge, !Parent::direction(edge));
deba@2116
   930
    }
deba@2116
   931
deba@2116
   932
deba@2116
   933
    int maxId(Node) const {
deba@2116
   934
      return Parent::maxNodeId();
deba@2116
   935
    }
deba@2116
   936
    int maxId(BNode) const {
deba@2116
   937
      return Parent::maxBNodeId();
deba@2116
   938
    }
deba@2116
   939
    int maxId(ANode) const {
deba@2116
   940
      return Parent::maxANodeId();
deba@2116
   941
    }
deba@2116
   942
    int maxId(Edge) const {
deba@2116
   943
      return maxEdgeId();
deba@2116
   944
    }
deba@2116
   945
    int maxId(UEdge) const {
deba@2116
   946
      return Parent::maxUEdgeId();
deba@2116
   947
    }
deba@2116
   948
deba@2116
   949
deba@2116
   950
    Node fromId(int id, Node) const {
deba@2116
   951
      return Parent::nodeFromId(id);
deba@2116
   952
    }
deba@2116
   953
    ANode fromId(int id, ANode) const {
deba@2116
   954
      return Parent::fromANodeId(id);
deba@2116
   955
    }
deba@2116
   956
    BNode fromId(int id, BNode) const {
deba@2116
   957
      return Parent::fromBNodeId(id);
deba@2116
   958
    }
deba@2116
   959
    Edge fromId(int id, Edge) const {
deba@2116
   960
      return Parent::edgeFromId(id);
deba@2116
   961
    }
deba@2116
   962
    UEdge fromId(int id, UEdge) const {
deba@2116
   963
      return Parent::uEdgeFromId(id);
deba@2116
   964
    }  
deba@2116
   965
  
deba@2116
   966
    typedef AlterationNotifier<BpUGraphExtender, ANode> ANodeNotifier;
deba@2116
   967
    typedef AlterationNotifier<BpUGraphExtender, BNode> BNodeNotifier;
deba@2116
   968
    typedef AlterationNotifier<BpUGraphExtender, Node> NodeNotifier;
deba@2116
   969
    typedef AlterationNotifier<BpUGraphExtender, Edge> EdgeNotifier;
deba@2116
   970
    typedef AlterationNotifier<BpUGraphExtender, UEdge> UEdgeNotifier;
deba@2116
   971
deba@2116
   972
  protected:
deba@2116
   973
deba@2116
   974
    mutable ANodeNotifier anode_notifier;
deba@2116
   975
    mutable BNodeNotifier bnode_notifier;
deba@2116
   976
    mutable NodeNotifier node_notifier;
deba@2116
   977
    mutable EdgeNotifier edge_notifier;
deba@2116
   978
    mutable UEdgeNotifier uedge_notifier;
deba@2116
   979
deba@2116
   980
  public:
deba@2116
   981
deba@2116
   982
    NodeNotifier& getNotifier(Node) const {
deba@2116
   983
      return node_notifier;
deba@2116
   984
    }
deba@2116
   985
deba@2116
   986
    ANodeNotifier& getNotifier(ANode) const {
deba@2116
   987
      return anode_notifier;
deba@2116
   988
    }
deba@2116
   989
deba@2116
   990
    BNodeNotifier& getNotifier(BNode) const {
deba@2116
   991
      return bnode_notifier;
deba@2116
   992
    }
deba@2116
   993
deba@2116
   994
    EdgeNotifier& getNotifier(Edge) const {
deba@2116
   995
      return edge_notifier;
deba@2116
   996
    }
deba@2116
   997
deba@2116
   998
    UEdgeNotifier& getNotifier(UEdge) const {
deba@2116
   999
      return uedge_notifier;
deba@2116
  1000
    }
deba@2116
  1001
deba@2116
  1002
    class NodeIt : public Node { 
deba@2116
  1003
      const Graph* graph;
deba@2116
  1004
    public:
deba@2116
  1005
    
deba@2116
  1006
      NodeIt() { }
deba@2116
  1007
    
deba@2116
  1008
      NodeIt(Invalid i) : Node(INVALID) { }
deba@2116
  1009
    
deba@2116
  1010
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
  1011
	graph->first(static_cast<Node&>(*this));
deba@2116
  1012
      }
deba@2116
  1013
deba@2116
  1014
      NodeIt(const Graph& _graph, const Node& node) 
deba@2116
  1015
	: Node(node), graph(&_graph) { }
deba@2116
  1016
    
deba@2116
  1017
      NodeIt& operator++() { 
deba@2116
  1018
	graph->next(*this);
deba@2116
  1019
	return *this; 
deba@2116
  1020
      }
deba@2116
  1021
deba@2116
  1022
    };
deba@2116
  1023
deba@2116
  1024
    class ANodeIt : public Node { 
deba@2116
  1025
      friend class BpUGraphExtender;
deba@2116
  1026
      const Graph* graph;
deba@2116
  1027
    public:
deba@2116
  1028
    
deba@2116
  1029
      ANodeIt() { }
deba@2116
  1030
    
deba@2116
  1031
      ANodeIt(Invalid i) : Node(INVALID) { }
deba@2116
  1032
    
deba@2116
  1033
      explicit ANodeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
  1034
	graph->firstANode(static_cast<Node&>(*this));
deba@2116
  1035
      }
deba@2116
  1036
deba@2116
  1037
      ANodeIt(const Graph& _graph, const Node& node) 
deba@2116
  1038
	: Node(node), graph(&_graph) {}
deba@2116
  1039
    
deba@2116
  1040
      ANodeIt& operator++() { 
deba@2116
  1041
	graph->nextANode(*this);
deba@2116
  1042
	return *this; 
deba@2116
  1043
      }
deba@2116
  1044
    };
deba@2116
  1045
deba@2116
  1046
    class BNodeIt : public Node { 
deba@2116
  1047
      friend class BpUGraphExtender;
deba@2116
  1048
      const Graph* graph;
deba@2116
  1049
    public:
deba@2116
  1050
    
deba@2116
  1051
      BNodeIt() { }
deba@2116
  1052
    
deba@2116
  1053
      BNodeIt(Invalid i) : Node(INVALID) { }
deba@2116
  1054
    
deba@2116
  1055
      explicit BNodeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
  1056
	graph->firstBNode(static_cast<Node&>(*this));
deba@2116
  1057
      }
deba@2116
  1058
deba@2116
  1059
      BNodeIt(const Graph& _graph, const Node& node) 
deba@2116
  1060
	: Node(node), graph(&_graph) {}
deba@2116
  1061
    
deba@2116
  1062
      BNodeIt& operator++() { 
deba@2116
  1063
	graph->nextBNode(*this);
deba@2116
  1064
	return *this; 
deba@2116
  1065
      }
deba@2116
  1066
    };
deba@2116
  1067
deba@2116
  1068
    class EdgeIt : public Edge { 
deba@2116
  1069
      friend class BpUGraphExtender;
deba@2116
  1070
      const Graph* graph;
deba@2116
  1071
    public:
deba@2116
  1072
    
deba@2116
  1073
      EdgeIt() { }
deba@2116
  1074
    
deba@2116
  1075
      EdgeIt(Invalid i) : Edge(INVALID) { }
deba@2116
  1076
    
deba@2116
  1077
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
  1078
	graph->first(static_cast<Edge&>(*this));
deba@2116
  1079
      }
deba@2116
  1080
deba@2116
  1081
      EdgeIt(const Graph& _graph, const Edge& edge) 
deba@2116
  1082
	: Edge(edge), graph(&_graph) { }
deba@2116
  1083
    
deba@2116
  1084
      EdgeIt& operator++() { 
deba@2116
  1085
	graph->next(*this);
deba@2116
  1086
	return *this; 
deba@2116
  1087
      }
deba@2116
  1088
deba@2116
  1089
    };
deba@2116
  1090
deba@2116
  1091
    class UEdgeIt : public UEdge { 
deba@2116
  1092
      friend class BpUGraphExtender;
deba@2116
  1093
      const Graph* graph;
deba@2116
  1094
    public:
deba@2116
  1095
    
deba@2116
  1096
      UEdgeIt() { }
deba@2116
  1097
    
deba@2116
  1098
      UEdgeIt(Invalid i) : UEdge(INVALID) { }
deba@2116
  1099
    
deba@2116
  1100
      explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
  1101
	graph->first(static_cast<UEdge&>(*this));
deba@2116
  1102
      }
deba@2116
  1103
deba@2116
  1104
      UEdgeIt(const Graph& _graph, const UEdge& edge) 
deba@2116
  1105
	: UEdge(edge), graph(&_graph) { }
deba@2116
  1106
    
deba@2116
  1107
      UEdgeIt& operator++() { 
deba@2116
  1108
	graph->next(*this);
deba@2116
  1109
	return *this; 
deba@2116
  1110
      }
deba@2116
  1111
    };
deba@2116
  1112
deba@2116
  1113
    class OutEdgeIt : public Edge { 
deba@2116
  1114
      friend class BpUGraphExtender;
deba@2116
  1115
      const Graph* graph;
deba@2116
  1116
    public:
deba@2116
  1117
    
deba@2116
  1118
      OutEdgeIt() { }
deba@2116
  1119
    
deba@2116
  1120
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@2116
  1121
    
deba@2116
  1122
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@2116
  1123
	: graph(&_graph) {
deba@2116
  1124
	graph->firstOut(*this, node);
deba@2116
  1125
      }
deba@2116
  1126
    
deba@2116
  1127
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@2116
  1128
	: Edge(edge), graph(&_graph) {}
deba@2116
  1129
    
deba@2116
  1130
      OutEdgeIt& operator++() { 
deba@2116
  1131
	graph->nextOut(*this);
deba@2116
  1132
	return *this; 
deba@2116
  1133
      }
deba@2116
  1134
deba@2116
  1135
    };
deba@2116
  1136
deba@2116
  1137
deba@2116
  1138
    class InEdgeIt : public Edge { 
deba@2116
  1139
      friend class BpUGraphExtender;
deba@2116
  1140
      const Graph* graph;
deba@2116
  1141
    public:
deba@2116
  1142
    
deba@2116
  1143
      InEdgeIt() { }
deba@2116
  1144
    
deba@2116
  1145
      InEdgeIt(Invalid i) : Edge(i) { }
deba@2116
  1146
    
deba@2116
  1147
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@2116
  1148
	: graph(&_graph) {
deba@2116
  1149
	graph->firstIn(*this, node);
deba@2116
  1150
      }
deba@2116
  1151
    
deba@2116
  1152
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@2116
  1153
	Edge(edge), graph(&_graph) {}
deba@2116
  1154
    
deba@2116
  1155
      InEdgeIt& operator++() { 
deba@2116
  1156
	graph->nextIn(*this);
deba@2116
  1157
	return *this; 
deba@2116
  1158
      }
deba@2116
  1159
deba@2116
  1160
    };
deba@2116
  1161
  
deba@2116
  1162
    /// \brief Base node of the iterator
deba@2116
  1163
    ///
deba@2116
  1164
    /// Returns the base node (ie. the source in this case) of the iterator
deba@2116
  1165
    Node baseNode(const OutEdgeIt &e) const {
deba@2116
  1166
      return Parent::source((Edge&)e);
deba@2116
  1167
    }
deba@2116
  1168
    /// \brief Running node of the iterator
deba@2116
  1169
    ///
deba@2116
  1170
    /// Returns the running node (ie. the target in this case) of the
deba@2116
  1171
    /// iterator
deba@2116
  1172
    Node runningNode(const OutEdgeIt &e) const {
deba@2116
  1173
      return Parent::target((Edge&)e);
deba@2116
  1174
    }
deba@2116
  1175
  
deba@2116
  1176
    /// \brief Base node of the iterator
deba@2116
  1177
    ///
deba@2116
  1178
    /// Returns the base node (ie. the target in this case) of the iterator
deba@2116
  1179
    Node baseNode(const InEdgeIt &e) const {
deba@2116
  1180
      return Parent::target((Edge&)e);
deba@2116
  1181
    }
deba@2116
  1182
    /// \brief Running node of the iterator
deba@2116
  1183
    ///
deba@2116
  1184
    /// Returns the running node (ie. the source in this case) of the
deba@2116
  1185
    /// iterator
deba@2116
  1186
    Node runningNode(const InEdgeIt &e) const {
deba@2116
  1187
      return Parent::source((Edge&)e);
deba@2116
  1188
    }
deba@2116
  1189
  
deba@2116
  1190
    class IncEdgeIt : public Parent::UEdge { 
deba@2116
  1191
      friend class BpUGraphExtender;
deba@2116
  1192
      const Graph* graph;
deba@2116
  1193
      bool direction;
deba@2116
  1194
    public:
deba@2116
  1195
    
deba@2116
  1196
      IncEdgeIt() { }
deba@2116
  1197
    
deba@2116
  1198
      IncEdgeIt(Invalid i) : UEdge(i), direction(true) { }
deba@2116
  1199
    
deba@2116
  1200
      IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
deba@2116
  1201
	graph->firstInc(*this, direction, n);
deba@2116
  1202
      }
deba@2116
  1203
deba@2116
  1204
      IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
deba@2116
  1205
	: graph(&_graph), UEdge(ue) {
deba@2116
  1206
	direction = (graph->source(ue) == n);
deba@2116
  1207
      }
deba@2116
  1208
deba@2116
  1209
      IncEdgeIt& operator++() {
deba@2116
  1210
	graph->nextInc(*this, direction);
deba@2116
  1211
	return *this; 
deba@2116
  1212
      }
deba@2116
  1213
    };
deba@2116
  1214
  
deba@2116
  1215
deba@2116
  1216
    /// Base node of the iterator
deba@2116
  1217
    ///
deba@2116
  1218
    /// Returns the base node of the iterator
deba@2116
  1219
    Node baseNode(const IncEdgeIt &e) const {
deba@2116
  1220
      return e.direction ? source(e) : target(e);
deba@2116
  1221
    }
deba@2116
  1222
deba@2116
  1223
    /// Running node of the iterator
deba@2116
  1224
    ///
deba@2116
  1225
    /// Returns the running node of the iterator
deba@2116
  1226
    Node runningNode(const IncEdgeIt &e) const {
deba@2116
  1227
      return e.direction ? target(e) : source(e);
deba@2116
  1228
    }
deba@2116
  1229
deba@2116
  1230
    template <typename _Value>
deba@2116
  1231
    class ANodeMap 
deba@2116
  1232
      : public MapExtender<DefaultMap<Graph, ANode, _Value> > {
deba@2116
  1233
    public:
deba@2116
  1234
      typedef BpUGraphExtender Graph;
deba@2116
  1235
      typedef MapExtender<DefaultMap<Graph, ANode, _Value> > Parent;
deba@2116
  1236
    
deba@2116
  1237
      ANodeMap(const Graph& graph) 
deba@2116
  1238
	: Parent(graph) {}
deba@2116
  1239
      ANodeMap(const Graph& graph, const _Value& value) 
deba@2116
  1240
	: Parent(graph, value) {}
deba@2116
  1241
    
deba@2116
  1242
      ANodeMap& operator=(const ANodeMap& cmap) {
deba@2116
  1243
	return operator=<ANodeMap>(cmap);
deba@2116
  1244
      }
deba@2116
  1245
    
deba@2116
  1246
      template <typename CMap>
deba@2116
  1247
      ANodeMap& operator=(const CMap& cmap) {
deba@2116
  1248
        Parent::operator=(cmap);
deba@2116
  1249
	return *this;
deba@2116
  1250
      }
deba@2116
  1251
    
deba@2116
  1252
    };
deba@2116
  1253
deba@2116
  1254
    template <typename _Value>
deba@2116
  1255
    class BNodeMap 
deba@2116
  1256
      : public MapExtender<DefaultMap<Graph, BNode, _Value> > {
deba@2116
  1257
    public:
deba@2116
  1258
      typedef BpUGraphExtender Graph;
deba@2116
  1259
      typedef MapExtender<DefaultMap<Graph, BNode, _Value> > Parent;
deba@2116
  1260
    
deba@2116
  1261
      BNodeMap(const Graph& graph) 
deba@2116
  1262
	: Parent(graph) {}
deba@2116
  1263
      BNodeMap(const Graph& graph, const _Value& value) 
deba@2116
  1264
	: Parent(graph, value) {}
deba@2116
  1265
    
deba@2116
  1266
      BNodeMap& operator=(const BNodeMap& cmap) {
deba@2116
  1267
	return operator=<BNodeMap>(cmap);
deba@2116
  1268
      }
deba@2116
  1269
    
deba@2116
  1270
      template <typename CMap>
deba@2116
  1271
      BNodeMap& operator=(const CMap& cmap) {
deba@2116
  1272
        Parent::operator=(cmap);
deba@2116
  1273
	return *this;
deba@2116
  1274
      }
deba@2116
  1275
    
deba@2116
  1276
    };
deba@2116
  1277
deba@2116
  1278
  public:
deba@2116
  1279
deba@2116
  1280
    template <typename _Value>
deba@2116
  1281
    class NodeMap {
deba@2116
  1282
    public:
deba@2116
  1283
      typedef BpUGraphExtender Graph;
deba@2116
  1284
deba@2116
  1285
      typedef Node Key;
deba@2116
  1286
      typedef _Value Value;
deba@2116
  1287
deba@2116
  1288
      /// The reference type of the map;
deba@2116
  1289
      typedef typename ANodeMap<_Value>::Reference Reference;
deba@2116
  1290
      /// The const reference type of the map;
deba@2116
  1291
      typedef typename ANodeMap<_Value>::ConstReference ConstReference;
deba@2116
  1292
deba@2116
  1293
      typedef True ReferenceMapTag;
deba@2116
  1294
deba@2116
  1295
      NodeMap(const Graph& _graph) 
deba@2116
  1296
	: graph(_graph), aNodeMap(_graph), bNodeMap(_graph) {}
deba@2116
  1297
      NodeMap(const Graph& _graph, const _Value& _value) 
deba@2116
  1298
	: graph(_graph), aNodeMap(_graph, _value), bNodeMap(_graph, _value) {}
deba@2116
  1299
deba@2116
  1300
      NodeMap& operator=(const NodeMap& cmap) {
deba@2116
  1301
	return operator=<NodeMap>(cmap);
deba@2116
  1302
      }
deba@2116
  1303
    
deba@2116
  1304
      template <typename CMap>
deba@2116
  1305
      NodeMap& operator=(const CMap& cmap) {
deba@2116
  1306
	checkConcept<concept::ReadMap<Node, _Value>, CMap>();
deba@2116
  1307
	const typename Parent::Notifier* notifier = Parent::getNotifier();
deba@2116
  1308
	Edge it;
deba@2116
  1309
	for (graph.first(it); it != INVALID; graph.next(it)) {
deba@2116
  1310
	  Parent::set(it, cmap[it]);
deba@2116
  1311
	}
deba@2116
  1312
	return *this;
deba@2116
  1313
      }
deba@2116
  1314
deba@2116
  1315
      ConstReference operator[](const Key& node) const {
deba@2116
  1316
	if (Parent::aNode(node)) {
deba@2116
  1317
	  return aNodeMap[node];
deba@2116
  1318
	} else {
deba@2116
  1319
	  return bNodeMap[node];
deba@2116
  1320
	}
deba@2116
  1321
      } 
deba@2116
  1322
deba@2116
  1323
      Reference operator[](const Key& node) {
deba@2116
  1324
	if (Parent::aNode(node)) {
deba@2116
  1325
	  return aNodeMap[node];
deba@2116
  1326
	} else {
deba@2116
  1327
	  return bNodeMap[node];
deba@2116
  1328
	}
deba@2116
  1329
      }
deba@2116
  1330
deba@2116
  1331
      void set(const Key& node, const Value& value) {
deba@2116
  1332
	if (Parent::aNode(node)) {
deba@2116
  1333
	  aNodeMap.set(node, value);
deba@2116
  1334
	} else {
deba@2116
  1335
	  bNodeMap.set(node, value);
deba@2116
  1336
	}
deba@2116
  1337
      }
deba@2116
  1338
deba@2116
  1339
      class MapIt : public NodeIt {
deba@2116
  1340
      public:
deba@2116
  1341
deba@2116
  1342
        typedef NodeIt Parent;
deba@2116
  1343
        
deba@2116
  1344
        explicit MapIt(NodeMap& _map) 
deba@2116
  1345
          : Parent(_map.graph), map(_map) {}
deba@2116
  1346
        
deba@2116
  1347
        typename MapTraits<NodeMap>::ConstReturnValue operator*() const {
deba@2116
  1348
          return map[*this];
deba@2116
  1349
        }
deba@2116
  1350
        
deba@2116
  1351
        typename MapTraits<NodeMap>::ReturnValue operator*() {
deba@2116
  1352
          return map[*this];
deba@2116
  1353
        }
deba@2116
  1354
        
deba@2116
  1355
        void set(const Value& value) {
deba@2116
  1356
          map.set(*this, value);
deba@2116
  1357
        }
deba@2116
  1358
deba@2116
  1359
      private:
deba@2116
  1360
        NodeMap& map;
deba@2116
  1361
      };
deba@2116
  1362
deba@2116
  1363
      class ConstMapIt : public NodeIt {
deba@2116
  1364
      public:
deba@2116
  1365
deba@2116
  1366
        typedef NodeIt Parent;
deba@2116
  1367
        
deba@2116
  1368
        explicit ConstMapIt(const NodeMap& _map) 
deba@2116
  1369
          : Parent(_map.graph), map(_map) {}
deba@2116
  1370
        
deba@2116
  1371
        typename MapTraits<NodeMap>::ConstReturnValue operator*() const {
deba@2116
  1372
          return map[*this];
deba@2116
  1373
        }
deba@2116
  1374
        
deba@2116
  1375
      private:
deba@2116
  1376
        const NodeMap& map;
deba@2116
  1377
      };
deba@2116
  1378
deba@2116
  1379
      class ItemIt : public NodeIt {
deba@2116
  1380
      public:
deba@2116
  1381
deba@2116
  1382
        typedef NodeIt Parent;
deba@2116
  1383
deba@2116
  1384
        explicit ItemIt(const NodeMap& _map)
deba@2116
  1385
          : Parent(_map.graph) {}
deba@2116
  1386
        
deba@2116
  1387
      };
deba@2116
  1388
      
deba@2116
  1389
    private:
deba@2116
  1390
      const Graph& graph;
deba@2116
  1391
      ANodeMap<_Value> aNodeMap;
deba@2116
  1392
      BNodeMap<_Value> bNodeMap;
deba@2116
  1393
    };
deba@2116
  1394
    
deba@2116
  1395
deba@2116
  1396
    template <typename _Value>
deba@2116
  1397
    class EdgeMap 
deba@2116
  1398
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
deba@2116
  1399
    public:
deba@2116
  1400
      typedef BpUGraphExtender Graph;
deba@2116
  1401
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
deba@2116
  1402
    
deba@2116
  1403
      EdgeMap(const Graph& graph) 
deba@2116
  1404
	: Parent(graph) {}
deba@2116
  1405
      EdgeMap(const Graph& graph, const _Value& value) 
deba@2116
  1406
	: Parent(graph, value) {}
deba@2116
  1407
    
deba@2116
  1408
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2116
  1409
	return operator=<EdgeMap>(cmap);
deba@2116
  1410
      }
deba@2116
  1411
    
deba@2116
  1412
      template <typename CMap>
deba@2116
  1413
      EdgeMap& operator=(const CMap& cmap) {
deba@2116
  1414
        Parent::operator=(cmap);
deba@2116
  1415
	return *this;
deba@2116
  1416
      }
deba@2116
  1417
    };
deba@2116
  1418
deba@2116
  1419
    template <typename _Value>
deba@2116
  1420
    class UEdgeMap 
deba@2116
  1421
      : public MapExtender<DefaultMap<Graph, UEdge, _Value> > {
deba@2116
  1422
    public:
deba@2116
  1423
      typedef BpUGraphExtender Graph;
deba@2116
  1424
      typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
deba@2116
  1425
    
deba@2116
  1426
      UEdgeMap(const Graph& graph) 
deba@2116
  1427
	: Parent(graph) {}
deba@2116
  1428
      UEdgeMap(const Graph& graph, const _Value& value) 
deba@2116
  1429
	: Parent(graph, value) {}
deba@2116
  1430
    
deba@2116
  1431
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2116
  1432
	return operator=<UEdgeMap>(cmap);
deba@2116
  1433
      }
deba@2116
  1434
    
deba@2116
  1435
      template <typename CMap>
deba@2116
  1436
      UEdgeMap& operator=(const CMap& cmap) {
deba@2116
  1437
        Parent::operator=(cmap);
deba@2116
  1438
	return *this;
deba@2116
  1439
      }
deba@2116
  1440
    };
deba@2116
  1441
deba@2116
  1442
  
deba@2116
  1443
    Node addANode() {
deba@2116
  1444
      Node node = Parent::addANode();
deba@2116
  1445
      getNotifier(ANode()).add(node);
deba@2116
  1446
      getNotifier(Node()).add(node);
deba@2116
  1447
      return node;
deba@2116
  1448
    }
deba@2116
  1449
deba@2116
  1450
    Node addBNode() {
deba@2116
  1451
      Node node = Parent::addBNode();
deba@2116
  1452
      getNotifier(BNode()).add(node);
deba@2116
  1453
      getNotifier(Node()).add(node);
deba@2116
  1454
      return node;
deba@2116
  1455
    }
deba@2116
  1456
  
deba@2116
  1457
    UEdge addEdge(const Node& source, const Node& target) {
deba@2116
  1458
      UEdge uedge = Parent::addEdge(source, target);
deba@2116
  1459
      getNotifier(UEdge()).add(uedge);
deba@2116
  1460
    
deba@2116
  1461
      std::vector<Edge> edges;
deba@2116
  1462
      edges.push_back(direct(uedge, true));
deba@2116
  1463
      edges.push_back(direct(uedge, false));
deba@2116
  1464
      getNotifier(Edge()).add(edges);
deba@2116
  1465
    
deba@2116
  1466
      return uedge;
deba@2116
  1467
    }
deba@2116
  1468
deba@2116
  1469
    void clear() {
deba@2116
  1470
      getNotifier(Edge()).clear();
deba@2116
  1471
      getNotifier(UEdge()).clear();
deba@2116
  1472
      getNotifier(Node()).clear();
deba@2116
  1473
      getNotifier(BNode()).clear();
deba@2116
  1474
      getNotifier(ANode()).clear();
deba@2116
  1475
      Parent::clear();
deba@2116
  1476
    }
deba@2116
  1477
deba@2116
  1478
    void erase(const Node& node) {
deba@2116
  1479
      UEdge uedge;
deba@2116
  1480
      if (Parent::aNode(node)) {
deba@2116
  1481
        Parent::firstFromANode(uedge, node);
deba@2116
  1482
        while (uedge != INVALID) {
deba@2116
  1483
          erase(uedge);
deba@2116
  1484
          Parent::firstFromANode(uedge, node);
deba@2116
  1485
        }
deba@2116
  1486
      } else {
deba@2116
  1487
        Parent::firstFromBNode(uedge, node);
deba@2116
  1488
        while (uedge != INVALID) {
deba@2116
  1489
          erase(uedge);
deba@2116
  1490
          Parent::firstFromBNode(uedge, node);
deba@2116
  1491
        }
deba@2116
  1492
      }
deba@2116
  1493
deba@2116
  1494
      getNotifier(Node()).erase(node);
deba@2116
  1495
      Parent::erase(node);
deba@2116
  1496
    }
deba@2116
  1497
    
deba@2116
  1498
    void erase(const UEdge& uedge) {
deba@2116
  1499
      std::vector<Edge> edges;
deba@2116
  1500
      edges.push_back(direct(uedge, true));
deba@2116
  1501
      edges.push_back(direct(uedge, false));
deba@2116
  1502
      getNotifier(Edge()).erase(edges);
deba@2116
  1503
      getNotifier(UEdge()).erase(uedge);
deba@2116
  1504
      Parent::erase(uedge);
deba@2116
  1505
    }
deba@2116
  1506
deba@2116
  1507
deba@2116
  1508
    BpUGraphExtender() {
deba@2116
  1509
      anode_notifier.setContainer(*this); 
deba@2116
  1510
      bnode_notifier.setContainer(*this); 
deba@2116
  1511
      node_notifier.setContainer(*this); 
deba@2116
  1512
      edge_notifier.setContainer(*this); 
deba@2116
  1513
      uedge_notifier.setContainer(*this);
deba@2116
  1514
    } 
deba@2116
  1515
deba@2116
  1516
    ~BpUGraphExtender() {
deba@2116
  1517
      uedge_notifier.clear();
deba@2116
  1518
      edge_notifier.clear(); 
deba@2116
  1519
      node_notifier.clear(); 
deba@2116
  1520
      anode_notifier.clear(); 
deba@2116
  1521
      bnode_notifier.clear(); 
deba@2116
  1522
    }
deba@2116
  1523
deba@2116
  1524
deba@2116
  1525
  };
deba@2116
  1526
deba@1791
  1527
}
deba@1791
  1528
deba@1996
  1529
#endif