lemon/bits/graph_extender.h
author deba
Tue, 30 Oct 2007 20:44:53 +0000
changeset 2506 216c6bd5c18c
parent 2386 81b47fc5c444
child 2553 bfced05fa852
permissions -rw-r--r--
Change to new union-find interface
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@2391
     5
 * Copyright (C) 2003-2007
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>
alpar@2260
    29
#include <lemon/concepts/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@2384
    89
    NodeNotifier& notifier(Node) const {
deba@1979
    90
      return node_notifier;
deba@1979
    91
    }
deba@1979
    92
    
deba@2384
    93
    EdgeNotifier& notifier(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@2384
   269
      notifier(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@2384
   275
      notifier(Edge()).add(edge);
deba@1979
   276
      return edge;
deba@1979
   277
    }
deba@1979
   278
deba@1979
   279
    void clear() {
deba@2384
   280
      notifier(Edge()).clear();
deba@2384
   281
      notifier(Node()).clear();
deba@1979
   282
      Parent::clear();
deba@1979
   283
    }
deba@1979
   284
deba@2290
   285
    template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
deba@2329
   286
    void build(const Graph& graph, NodeRefMap& nodeRef, EdgeRefMap& edgeRef) {
deba@2329
   287
      Parent::build(graph, nodeRef, edgeRef);
deba@2384
   288
      notifier(Node()).build();
deba@2384
   289
      notifier(Edge()).build();
deba@2290
   290
    }
deba@1979
   291
deba@1979
   292
    void erase(const Node& node) {
deba@1979
   293
      Edge edge;
deba@1979
   294
      Parent::firstOut(edge, node);
deba@1979
   295
      while (edge != INVALID ) {
deba@1979
   296
	erase(edge);
deba@1979
   297
	Parent::firstOut(edge, node);
deba@1979
   298
      } 
deba@1979
   299
deba@1979
   300
      Parent::firstIn(edge, node);
deba@1979
   301
      while (edge != INVALID ) {
deba@1979
   302
	erase(edge);
deba@1979
   303
	Parent::firstIn(edge, node);
deba@1979
   304
      }
deba@1979
   305
deba@2384
   306
      notifier(Node()).erase(node);
deba@1979
   307
      Parent::erase(node);
deba@1979
   308
    }
deba@1979
   309
    
deba@1979
   310
    void erase(const Edge& edge) {
deba@2384
   311
      notifier(Edge()).erase(edge);
deba@1979
   312
      Parent::erase(edge);
deba@1979
   313
    }
deba@1979
   314
deba@1999
   315
    GraphExtender() {
deba@1999
   316
      node_notifier.setContainer(*this);
deba@1999
   317
      edge_notifier.setContainer(*this);
deba@1999
   318
    } 
deba@1999
   319
    
deba@1979
   320
deba@1979
   321
    ~GraphExtender() {
deba@1999
   322
      edge_notifier.clear();
deba@1999
   323
      node_notifier.clear();
deba@1979
   324
    }
deba@1979
   325
  };
deba@1979
   326
deba@2116
   327
  /// \ingroup graphbits
deba@2116
   328
  ///
deba@2116
   329
  /// \brief Extender for the UGraphs
deba@2116
   330
  template <typename Base> 
deba@2116
   331
  class UGraphExtender : public Base {
deba@2116
   332
  public:
deba@2116
   333
    
deba@2116
   334
    typedef Base Parent;
deba@2116
   335
    typedef UGraphExtender Graph;
deba@2116
   336
deba@2116
   337
    typedef typename Parent::Node Node;
deba@2116
   338
    typedef typename Parent::Edge Edge;
deba@2116
   339
    typedef typename Parent::UEdge UEdge;
deba@2116
   340
deba@2116
   341
    // UGraph extension    
deba@2116
   342
deba@2116
   343
    int maxId(Node) const {
deba@2116
   344
      return Parent::maxNodeId();
deba@2116
   345
    }
deba@2116
   346
deba@2116
   347
    int maxId(Edge) const {
deba@2116
   348
      return Parent::maxEdgeId();
deba@2116
   349
    }
deba@2116
   350
deba@2116
   351
    int maxId(UEdge) const {
deba@2116
   352
      return Parent::maxUEdgeId();
deba@2116
   353
    }
deba@2116
   354
deba@2116
   355
    Node fromId(int id, Node) const {
deba@2116
   356
      return Parent::nodeFromId(id);
deba@2116
   357
    }
deba@2116
   358
deba@2116
   359
    Edge fromId(int id, Edge) const {
deba@2116
   360
      return Parent::edgeFromId(id);
deba@2116
   361
    }
deba@2116
   362
deba@2116
   363
    UEdge fromId(int id, UEdge) const {
deba@2116
   364
      return Parent::uEdgeFromId(id);
deba@2116
   365
    }
deba@2116
   366
deba@2116
   367
    Node oppositeNode(const Node &n, const UEdge &e) const {
deba@2116
   368
      if( n == Parent::source(e))
deba@2116
   369
	return Parent::target(e);
deba@2116
   370
      else if( n == Parent::target(e))
deba@2116
   371
	return Parent::source(e);
deba@2116
   372
      else
deba@2116
   373
	return INVALID;
deba@2116
   374
    }
deba@2116
   375
deba@2116
   376
    Edge oppositeEdge(const Edge &e) const {
deba@2116
   377
      return Parent::direct(e, !Parent::direction(e));
deba@2116
   378
    }
deba@2116
   379
deba@2116
   380
    using Parent::direct;
deba@2116
   381
    Edge direct(const UEdge &ue, const Node &s) const {
deba@2116
   382
      return Parent::direct(ue, Parent::source(ue) == s);
deba@2116
   383
    }
deba@2116
   384
deba@2116
   385
    // Alterable extension
deba@2116
   386
deba@2116
   387
    typedef AlterationNotifier<UGraphExtender, Node> NodeNotifier;
deba@2116
   388
    typedef AlterationNotifier<UGraphExtender, Edge> EdgeNotifier;
deba@2116
   389
    typedef AlterationNotifier<UGraphExtender, UEdge> UEdgeNotifier;
deba@2116
   390
deba@2116
   391
deba@2116
   392
  protected:
deba@2116
   393
deba@2116
   394
    mutable NodeNotifier node_notifier;
deba@2116
   395
    mutable EdgeNotifier edge_notifier;
deba@2116
   396
    mutable UEdgeNotifier uedge_notifier;
deba@2116
   397
deba@2116
   398
  public:
deba@2116
   399
deba@2384
   400
    NodeNotifier& notifier(Node) const {
deba@2116
   401
      return node_notifier;
deba@2116
   402
    }
deba@2116
   403
    
deba@2384
   404
    EdgeNotifier& notifier(Edge) const {
deba@2116
   405
      return edge_notifier;
deba@2116
   406
    }
deba@2116
   407
deba@2384
   408
    UEdgeNotifier& notifier(UEdge) const {
deba@2116
   409
      return uedge_notifier;
deba@2116
   410
    }
deba@2116
   411
deba@2116
   412
deba@2116
   413
deba@2116
   414
    class NodeIt : public Node { 
deba@2116
   415
      const Graph* graph;
deba@2116
   416
    public:
deba@2116
   417
deba@2116
   418
      NodeIt() {}
deba@2116
   419
deba@2116
   420
      NodeIt(Invalid i) : Node(i) { }
deba@2116
   421
deba@2116
   422
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   423
	_graph.first(static_cast<Node&>(*this));
deba@2116
   424
      }
deba@2116
   425
deba@2116
   426
      NodeIt(const Graph& _graph, const Node& node) 
deba@2116
   427
	: Node(node), graph(&_graph) {}
deba@2116
   428
deba@2116
   429
      NodeIt& operator++() { 
deba@2116
   430
	graph->next(*this);
deba@2116
   431
	return *this; 
deba@2116
   432
      }
deba@2116
   433
deba@2116
   434
    };
deba@2116
   435
deba@2116
   436
deba@2116
   437
    class EdgeIt : public Edge { 
deba@2116
   438
      const Graph* graph;
deba@2116
   439
    public:
deba@2116
   440
deba@2116
   441
      EdgeIt() { }
deba@2116
   442
deba@2116
   443
      EdgeIt(Invalid i) : Edge(i) { }
deba@2116
   444
deba@2116
   445
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   446
	_graph.first(static_cast<Edge&>(*this));
deba@2116
   447
      }
deba@2116
   448
deba@2116
   449
      EdgeIt(const Graph& _graph, const Edge& e) : 
deba@2116
   450
	Edge(e), graph(&_graph) { }
deba@2116
   451
deba@2116
   452
      EdgeIt& operator++() { 
deba@2116
   453
	graph->next(*this);
deba@2116
   454
	return *this; 
deba@2116
   455
      }
deba@2116
   456
deba@2116
   457
    };
deba@2116
   458
deba@2116
   459
deba@2116
   460
    class OutEdgeIt : public Edge { 
deba@2116
   461
      const Graph* graph;
deba@2116
   462
    public:
deba@2116
   463
deba@2116
   464
      OutEdgeIt() { }
deba@2116
   465
deba@2116
   466
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@2116
   467
deba@2116
   468
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@2116
   469
	: graph(&_graph) {
deba@2116
   470
	_graph.firstOut(*this, node);
deba@2116
   471
      }
deba@2116
   472
deba@2116
   473
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@2116
   474
	: Edge(edge), graph(&_graph) {}
deba@2116
   475
deba@2116
   476
      OutEdgeIt& operator++() { 
deba@2116
   477
	graph->nextOut(*this);
deba@2116
   478
	return *this; 
deba@2116
   479
      }
deba@2116
   480
deba@2116
   481
    };
deba@2116
   482
deba@2116
   483
deba@2116
   484
    class InEdgeIt : public Edge { 
deba@2116
   485
      const Graph* graph;
deba@2116
   486
    public:
deba@2116
   487
deba@2116
   488
      InEdgeIt() { }
deba@2116
   489
deba@2116
   490
      InEdgeIt(Invalid i) : Edge(i) { }
deba@2116
   491
deba@2116
   492
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@2116
   493
	: graph(&_graph) {
deba@2116
   494
	_graph.firstIn(*this, node);
deba@2116
   495
      }
deba@2116
   496
deba@2116
   497
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@2116
   498
	Edge(edge), graph(&_graph) {}
deba@2116
   499
deba@2116
   500
      InEdgeIt& operator++() { 
deba@2116
   501
	graph->nextIn(*this);
deba@2116
   502
	return *this; 
deba@2116
   503
      }
deba@2116
   504
deba@2116
   505
    };
deba@2116
   506
deba@2116
   507
deba@2116
   508
    class UEdgeIt : public Parent::UEdge { 
deba@2116
   509
      const Graph* graph;
deba@2116
   510
    public:
deba@2116
   511
deba@2116
   512
      UEdgeIt() { }
deba@2116
   513
deba@2116
   514
      UEdgeIt(Invalid i) : UEdge(i) { }
deba@2116
   515
deba@2116
   516
      explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   517
	_graph.first(static_cast<UEdge&>(*this));
deba@2116
   518
      }
deba@2116
   519
deba@2116
   520
      UEdgeIt(const Graph& _graph, const UEdge& e) : 
deba@2116
   521
	UEdge(e), graph(&_graph) { }
deba@2116
   522
deba@2116
   523
      UEdgeIt& operator++() { 
deba@2116
   524
	graph->next(*this);
deba@2116
   525
	return *this; 
deba@2116
   526
      }
deba@2116
   527
deba@2116
   528
    };
deba@2116
   529
deba@2116
   530
    class IncEdgeIt : public Parent::UEdge {
deba@2116
   531
      friend class UGraphExtender;
deba@2116
   532
      const Graph* graph;
deba@2116
   533
      bool direction;
deba@2116
   534
    public:
deba@2116
   535
deba@2116
   536
      IncEdgeIt() { }
deba@2116
   537
deba@2116
   538
      IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
deba@2116
   539
deba@2116
   540
      IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
deba@2116
   541
	_graph.firstInc(*this, direction, n);
deba@2116
   542
      }
deba@2116
   543
deba@2116
   544
      IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
deba@2116
   545
	: graph(&_graph), UEdge(ue) {
deba@2116
   546
	direction = (_graph.source(ue) == n);
deba@2116
   547
      }
deba@2116
   548
deba@2116
   549
      IncEdgeIt& operator++() {
deba@2116
   550
	graph->nextInc(*this, direction);
deba@2116
   551
	return *this; 
deba@2116
   552
      }
deba@2116
   553
    };
deba@2116
   554
deba@2116
   555
    /// \brief Base node of the iterator
deba@2116
   556
    ///
deba@2116
   557
    /// Returns the base node (ie. the source in this case) of the iterator
deba@2116
   558
    Node baseNode(const OutEdgeIt &e) const {
deba@2386
   559
      return Parent::source(static_cast<const Edge&>(e));
deba@2116
   560
    }
deba@2116
   561
    /// \brief Running node of the iterator
deba@2116
   562
    ///
deba@2116
   563
    /// Returns the running node (ie. the target in this case) of the
deba@2116
   564
    /// iterator
deba@2116
   565
    Node runningNode(const OutEdgeIt &e) const {
deba@2386
   566
      return Parent::target(static_cast<const Edge&>(e));
deba@2116
   567
    }
deba@2116
   568
deba@2116
   569
    /// \brief Base node of the iterator
deba@2116
   570
    ///
deba@2116
   571
    /// Returns the base node (ie. the target in this case) of the iterator
deba@2116
   572
    Node baseNode(const InEdgeIt &e) const {
deba@2386
   573
      return Parent::target(static_cast<const Edge&>(e));
deba@2116
   574
    }
deba@2116
   575
    /// \brief Running node of the iterator
deba@2116
   576
    ///
deba@2116
   577
    /// Returns the running node (ie. the source in this case) of the
deba@2116
   578
    /// iterator
deba@2116
   579
    Node runningNode(const InEdgeIt &e) const {
deba@2386
   580
      return Parent::source(static_cast<const Edge&>(e));
deba@2116
   581
    }
deba@2116
   582
deba@2116
   583
    /// Base node of the iterator
deba@2116
   584
    ///
deba@2116
   585
    /// Returns the base node of the iterator
deba@2116
   586
    Node baseNode(const IncEdgeIt &e) const {
deba@2116
   587
      return e.direction ? source(e) : target(e);
deba@2116
   588
    }
deba@2116
   589
    /// Running node of the iterator
deba@2116
   590
    ///
deba@2116
   591
    /// Returns the running node of the iterator
deba@2116
   592
    Node runningNode(const IncEdgeIt &e) const {
deba@2116
   593
      return e.direction ? target(e) : source(e);
deba@2116
   594
    }
deba@2116
   595
deba@2116
   596
    // Mappable extension
deba@2116
   597
deba@2116
   598
    template <typename _Value>
deba@2116
   599
    class NodeMap 
deba@2116
   600
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
deba@2116
   601
    public:
deba@2116
   602
      typedef UGraphExtender Graph;
deba@2116
   603
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
deba@2116
   604
deba@2116
   605
      NodeMap(const Graph& graph) 
deba@2116
   606
	: Parent(graph) {}
deba@2116
   607
      NodeMap(const Graph& graph, const _Value& value) 
deba@2116
   608
	: Parent(graph, value) {}
deba@2116
   609
deba@2116
   610
      NodeMap& operator=(const NodeMap& cmap) {
deba@2116
   611
	return operator=<NodeMap>(cmap);
deba@2116
   612
      }
deba@2116
   613
deba@2116
   614
      template <typename CMap>
deba@2116
   615
      NodeMap& operator=(const CMap& cmap) {
deba@2116
   616
        Parent::operator=(cmap);
deba@2116
   617
	return *this;
deba@2116
   618
      }
deba@2116
   619
deba@2116
   620
    };
deba@2116
   621
deba@2116
   622
    template <typename _Value>
deba@2116
   623
    class EdgeMap 
deba@2116
   624
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
deba@2116
   625
    public:
deba@2116
   626
      typedef UGraphExtender Graph;
deba@2116
   627
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
deba@2116
   628
deba@2116
   629
      EdgeMap(const Graph& graph) 
deba@2116
   630
	: Parent(graph) {}
deba@2116
   631
      EdgeMap(const Graph& graph, const _Value& value) 
deba@2116
   632
	: Parent(graph, value) {}
deba@2116
   633
deba@2116
   634
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2116
   635
	return operator=<EdgeMap>(cmap);
deba@2116
   636
      }
deba@2116
   637
deba@2116
   638
      template <typename CMap>
deba@2116
   639
      EdgeMap& operator=(const CMap& cmap) {
deba@2116
   640
        Parent::operator=(cmap);
deba@2116
   641
	return *this;
deba@2116
   642
      }
deba@2116
   643
    };
deba@2116
   644
deba@2116
   645
deba@2116
   646
    template <typename _Value>
deba@2116
   647
    class UEdgeMap 
deba@2116
   648
      : public MapExtender<DefaultMap<Graph, UEdge, _Value> > {
deba@2116
   649
    public:
deba@2116
   650
      typedef UGraphExtender Graph;
deba@2116
   651
      typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
deba@2116
   652
deba@2116
   653
      UEdgeMap(const Graph& graph) 
deba@2116
   654
	: Parent(graph) {}
deba@2116
   655
deba@2116
   656
      UEdgeMap(const Graph& graph, const _Value& value) 
deba@2116
   657
	: Parent(graph, value) {}
deba@2116
   658
deba@2116
   659
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2116
   660
	return operator=<UEdgeMap>(cmap);
deba@2116
   661
      }
deba@2116
   662
deba@2116
   663
      template <typename CMap>
deba@2116
   664
      UEdgeMap& operator=(const CMap& cmap) {
deba@2116
   665
        Parent::operator=(cmap);
deba@2116
   666
	return *this;
deba@2116
   667
      }
deba@2116
   668
deba@2116
   669
    };
deba@2116
   670
deba@2116
   671
    // Alteration extension
deba@2116
   672
deba@2116
   673
    Node addNode() {
deba@2116
   674
      Node node = Parent::addNode();
deba@2384
   675
      notifier(Node()).add(node);
deba@2116
   676
      return node;
deba@2116
   677
    }
deba@2116
   678
deba@2116
   679
    UEdge addEdge(const Node& from, const Node& to) {
deba@2116
   680
      UEdge uedge = Parent::addEdge(from, to);
deba@2384
   681
      notifier(UEdge()).add(uedge);
deba@2386
   682
      std::vector<Edge> ev;
deba@2386
   683
      ev.push_back(Parent::direct(uedge, true));
deba@2386
   684
      ev.push_back(Parent::direct(uedge, false));      
deba@2386
   685
      notifier(Edge()).add(ev);
deba@2116
   686
      return uedge;
deba@2116
   687
    }
deba@2116
   688
    
deba@2116
   689
    void clear() {
deba@2384
   690
      notifier(Edge()).clear();
deba@2384
   691
      notifier(UEdge()).clear();
deba@2384
   692
      notifier(Node()).clear();
deba@2116
   693
      Parent::clear();
deba@2116
   694
    }
deba@2116
   695
deba@2290
   696
    template <typename Graph, typename NodeRefMap, typename UEdgeRefMap>
deba@2329
   697
    void build(const Graph& graph, NodeRefMap& nodeRef, 
deba@2290
   698
               UEdgeRefMap& uEdgeRef) {
deba@2329
   699
      Parent::build(graph, nodeRef, uEdgeRef);
deba@2384
   700
      notifier(Node()).build();
deba@2384
   701
      notifier(UEdge()).build();
deba@2384
   702
      notifier(Edge()).build();
deba@2290
   703
    }
deba@2290
   704
deba@2116
   705
    void erase(const Node& node) {
deba@2116
   706
      Edge edge;
deba@2116
   707
      Parent::firstOut(edge, node);
deba@2116
   708
      while (edge != INVALID ) {
deba@2116
   709
	erase(edge);
deba@2116
   710
	Parent::firstOut(edge, node);
deba@2116
   711
      } 
deba@2116
   712
deba@2116
   713
      Parent::firstIn(edge, node);
deba@2116
   714
      while (edge != INVALID ) {
deba@2116
   715
	erase(edge);
deba@2116
   716
	Parent::firstIn(edge, node);
deba@2116
   717
      }
deba@2116
   718
deba@2384
   719
      notifier(Node()).erase(node);
deba@2116
   720
      Parent::erase(node);
deba@2116
   721
    }
deba@2116
   722
deba@2116
   723
    void erase(const UEdge& uedge) {
deba@2386
   724
      std::vector<Edge> ev;
deba@2386
   725
      ev.push_back(Parent::direct(uedge, true));
deba@2386
   726
      ev.push_back(Parent::direct(uedge, false));      
deba@2386
   727
      notifier(Edge()).erase(ev);
deba@2384
   728
      notifier(UEdge()).erase(uedge);
deba@2116
   729
      Parent::erase(uedge);
deba@2116
   730
    }
deba@2116
   731
deba@2116
   732
    UGraphExtender() {
deba@2116
   733
      node_notifier.setContainer(*this); 
deba@2116
   734
      edge_notifier.setContainer(*this);
deba@2116
   735
      uedge_notifier.setContainer(*this);
deba@2116
   736
    } 
deba@2116
   737
deba@2116
   738
    ~UGraphExtender() {
deba@2116
   739
      uedge_notifier.clear();
deba@2116
   740
      edge_notifier.clear();
deba@2116
   741
      node_notifier.clear(); 
deba@2116
   742
    } 
deba@2116
   743
deba@2116
   744
  };
deba@2116
   745
deba@2116
   746
  /// \ingroup graphbits
deba@2116
   747
  ///
deba@2116
   748
  /// \brief Extender for the BpUGraphs
deba@2116
   749
  template <typename Base>
deba@2116
   750
  class BpUGraphExtender : public Base {
deba@2116
   751
  public:
deba@2231
   752
deba@2116
   753
    typedef Base Parent;
deba@2116
   754
    typedef BpUGraphExtender Graph;
deba@2116
   755
deba@2116
   756
    typedef typename Parent::Node Node;
deba@2231
   757
    typedef typename Parent::ANode ANode;
deba@2231
   758
    typedef typename Parent::BNode BNode;
deba@2231
   759
    typedef typename Parent::Edge Edge;
deba@2116
   760
    typedef typename Parent::UEdge UEdge;
deba@2116
   761
deba@2116
   762
deba@2231
   763
    Node oppositeNode(const Node& node, const UEdge& edge) const {
deba@2231
   764
      return Parent::aNode(edge) == node ? 
deba@2231
   765
	Parent::bNode(edge) : Parent::aNode(edge);
deba@2116
   766
    }
deba@2116
   767
deba@2231
   768
    using Parent::direct;
deba@2116
   769
    Edge direct(const UEdge& edge, const Node& node) const {
deba@2231
   770
      return Parent::direct(edge, node == Parent::source(edge));
deba@2116
   771
    }
deba@2116
   772
deba@2116
   773
    Edge oppositeEdge(const Edge& edge) const {
deba@2231
   774
      return direct(edge, !Parent::direction(edge));
deba@2116
   775
    }
deba@2231
   776
    
deba@2116
   777
    int maxId(Node) const {
deba@2116
   778
      return Parent::maxNodeId();
deba@2116
   779
    }
deba@2116
   780
    int maxId(BNode) const {
deba@2116
   781
      return Parent::maxBNodeId();
deba@2116
   782
    }
deba@2116
   783
    int maxId(ANode) const {
deba@2116
   784
      return Parent::maxANodeId();
deba@2116
   785
    }
deba@2116
   786
    int maxId(Edge) const {
deba@2231
   787
      return Parent::maxEdgeId();
deba@2116
   788
    }
deba@2116
   789
    int maxId(UEdge) const {
deba@2116
   790
      return Parent::maxUEdgeId();
deba@2116
   791
    }
deba@2116
   792
deba@2116
   793
deba@2116
   794
    Node fromId(int id, Node) const {
deba@2116
   795
      return Parent::nodeFromId(id);
deba@2116
   796
    }
deba@2116
   797
    ANode fromId(int id, ANode) const {
deba@2231
   798
      return Parent::nodeFromANodeId(id);
deba@2116
   799
    }
deba@2116
   800
    BNode fromId(int id, BNode) const {
deba@2231
   801
      return Parent::nodeFromBNodeId(id);
deba@2116
   802
    }
deba@2116
   803
    Edge fromId(int id, Edge) const {
deba@2116
   804
      return Parent::edgeFromId(id);
deba@2116
   805
    }
deba@2116
   806
    UEdge fromId(int id, UEdge) const {
deba@2116
   807
      return Parent::uEdgeFromId(id);
deba@2116
   808
    }  
deba@2116
   809
  
deba@2116
   810
    typedef AlterationNotifier<BpUGraphExtender, ANode> ANodeNotifier;
deba@2116
   811
    typedef AlterationNotifier<BpUGraphExtender, BNode> BNodeNotifier;
deba@2116
   812
    typedef AlterationNotifier<BpUGraphExtender, Node> NodeNotifier;
deba@2116
   813
    typedef AlterationNotifier<BpUGraphExtender, Edge> EdgeNotifier;
deba@2116
   814
    typedef AlterationNotifier<BpUGraphExtender, UEdge> UEdgeNotifier;
deba@2116
   815
deba@2116
   816
  protected:
deba@2116
   817
deba@2116
   818
    mutable ANodeNotifier anode_notifier;
deba@2116
   819
    mutable BNodeNotifier bnode_notifier;
deba@2116
   820
    mutable NodeNotifier node_notifier;
deba@2116
   821
    mutable EdgeNotifier edge_notifier;
deba@2116
   822
    mutable UEdgeNotifier uedge_notifier;
deba@2116
   823
deba@2116
   824
  public:
deba@2116
   825
deba@2384
   826
    NodeNotifier& notifier(Node) const {
deba@2116
   827
      return node_notifier;
deba@2116
   828
    }
deba@2116
   829
deba@2384
   830
    ANodeNotifier& notifier(ANode) const {
deba@2116
   831
      return anode_notifier;
deba@2116
   832
    }
deba@2116
   833
deba@2384
   834
    BNodeNotifier& notifier(BNode) const {
deba@2116
   835
      return bnode_notifier;
deba@2116
   836
    }
deba@2116
   837
deba@2384
   838
    EdgeNotifier& notifier(Edge) const {
deba@2116
   839
      return edge_notifier;
deba@2116
   840
    }
deba@2116
   841
deba@2384
   842
    UEdgeNotifier& notifier(UEdge) const {
deba@2116
   843
      return uedge_notifier;
deba@2116
   844
    }
deba@2116
   845
deba@2116
   846
    class NodeIt : public Node { 
deba@2116
   847
      const Graph* graph;
deba@2116
   848
    public:
deba@2116
   849
    
deba@2116
   850
      NodeIt() { }
deba@2116
   851
    
deba@2116
   852
      NodeIt(Invalid i) : Node(INVALID) { }
deba@2116
   853
    
deba@2116
   854
      explicit NodeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   855
	graph->first(static_cast<Node&>(*this));
deba@2116
   856
      }
deba@2116
   857
deba@2116
   858
      NodeIt(const Graph& _graph, const Node& node) 
deba@2116
   859
	: Node(node), graph(&_graph) { }
deba@2116
   860
    
deba@2116
   861
      NodeIt& operator++() { 
deba@2116
   862
	graph->next(*this);
deba@2116
   863
	return *this; 
deba@2116
   864
      }
deba@2116
   865
deba@2116
   866
    };
deba@2116
   867
deba@2116
   868
    class ANodeIt : public Node { 
deba@2116
   869
      friend class BpUGraphExtender;
deba@2116
   870
      const Graph* graph;
deba@2116
   871
    public:
deba@2116
   872
    
deba@2116
   873
      ANodeIt() { }
deba@2116
   874
    
deba@2116
   875
      ANodeIt(Invalid i) : Node(INVALID) { }
deba@2116
   876
    
deba@2116
   877
      explicit ANodeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   878
	graph->firstANode(static_cast<Node&>(*this));
deba@2116
   879
      }
deba@2116
   880
deba@2116
   881
      ANodeIt(const Graph& _graph, const Node& node) 
deba@2116
   882
	: Node(node), graph(&_graph) {}
deba@2116
   883
    
deba@2116
   884
      ANodeIt& operator++() { 
deba@2116
   885
	graph->nextANode(*this);
deba@2116
   886
	return *this; 
deba@2116
   887
      }
deba@2116
   888
    };
deba@2116
   889
deba@2116
   890
    class BNodeIt : public Node { 
deba@2116
   891
      friend class BpUGraphExtender;
deba@2116
   892
      const Graph* graph;
deba@2116
   893
    public:
deba@2116
   894
    
deba@2116
   895
      BNodeIt() { }
deba@2116
   896
    
deba@2116
   897
      BNodeIt(Invalid i) : Node(INVALID) { }
deba@2116
   898
    
deba@2116
   899
      explicit BNodeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   900
	graph->firstBNode(static_cast<Node&>(*this));
deba@2116
   901
      }
deba@2116
   902
deba@2116
   903
      BNodeIt(const Graph& _graph, const Node& node) 
deba@2116
   904
	: Node(node), graph(&_graph) {}
deba@2116
   905
    
deba@2116
   906
      BNodeIt& operator++() { 
deba@2116
   907
	graph->nextBNode(*this);
deba@2116
   908
	return *this; 
deba@2116
   909
      }
deba@2116
   910
    };
deba@2116
   911
deba@2116
   912
    class EdgeIt : public Edge { 
deba@2116
   913
      friend class BpUGraphExtender;
deba@2116
   914
      const Graph* graph;
deba@2116
   915
    public:
deba@2116
   916
    
deba@2116
   917
      EdgeIt() { }
deba@2116
   918
    
deba@2116
   919
      EdgeIt(Invalid i) : Edge(INVALID) { }
deba@2116
   920
    
deba@2116
   921
      explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   922
	graph->first(static_cast<Edge&>(*this));
deba@2116
   923
      }
deba@2116
   924
deba@2116
   925
      EdgeIt(const Graph& _graph, const Edge& edge) 
deba@2116
   926
	: Edge(edge), graph(&_graph) { }
deba@2116
   927
    
deba@2116
   928
      EdgeIt& operator++() { 
deba@2116
   929
	graph->next(*this);
deba@2116
   930
	return *this; 
deba@2116
   931
      }
deba@2116
   932
deba@2116
   933
    };
deba@2116
   934
deba@2116
   935
    class UEdgeIt : public UEdge { 
deba@2116
   936
      friend class BpUGraphExtender;
deba@2116
   937
      const Graph* graph;
deba@2116
   938
    public:
deba@2116
   939
    
deba@2116
   940
      UEdgeIt() { }
deba@2116
   941
    
deba@2116
   942
      UEdgeIt(Invalid i) : UEdge(INVALID) { }
deba@2116
   943
    
deba@2116
   944
      explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
deba@2116
   945
	graph->first(static_cast<UEdge&>(*this));
deba@2116
   946
      }
deba@2116
   947
deba@2116
   948
      UEdgeIt(const Graph& _graph, const UEdge& edge) 
deba@2116
   949
	: UEdge(edge), graph(&_graph) { }
deba@2116
   950
    
deba@2116
   951
      UEdgeIt& operator++() { 
deba@2116
   952
	graph->next(*this);
deba@2116
   953
	return *this; 
deba@2116
   954
      }
deba@2116
   955
    };
deba@2116
   956
deba@2116
   957
    class OutEdgeIt : public Edge { 
deba@2116
   958
      friend class BpUGraphExtender;
deba@2116
   959
      const Graph* graph;
deba@2116
   960
    public:
deba@2116
   961
    
deba@2116
   962
      OutEdgeIt() { }
deba@2116
   963
    
deba@2116
   964
      OutEdgeIt(Invalid i) : Edge(i) { }
deba@2116
   965
    
deba@2116
   966
      OutEdgeIt(const Graph& _graph, const Node& node) 
deba@2116
   967
	: graph(&_graph) {
deba@2116
   968
	graph->firstOut(*this, node);
deba@2116
   969
      }
deba@2116
   970
    
deba@2116
   971
      OutEdgeIt(const Graph& _graph, const Edge& edge) 
deba@2116
   972
	: Edge(edge), graph(&_graph) {}
deba@2116
   973
    
deba@2116
   974
      OutEdgeIt& operator++() { 
deba@2116
   975
	graph->nextOut(*this);
deba@2116
   976
	return *this; 
deba@2116
   977
      }
deba@2116
   978
deba@2116
   979
    };
deba@2116
   980
deba@2116
   981
deba@2116
   982
    class InEdgeIt : public Edge { 
deba@2116
   983
      friend class BpUGraphExtender;
deba@2116
   984
      const Graph* graph;
deba@2116
   985
    public:
deba@2116
   986
    
deba@2116
   987
      InEdgeIt() { }
deba@2116
   988
    
deba@2116
   989
      InEdgeIt(Invalid i) : Edge(i) { }
deba@2116
   990
    
deba@2116
   991
      InEdgeIt(const Graph& _graph, const Node& node) 
deba@2116
   992
	: graph(&_graph) {
deba@2116
   993
	graph->firstIn(*this, node);
deba@2116
   994
      }
deba@2116
   995
    
deba@2116
   996
      InEdgeIt(const Graph& _graph, const Edge& edge) : 
deba@2116
   997
	Edge(edge), graph(&_graph) {}
deba@2116
   998
    
deba@2116
   999
      InEdgeIt& operator++() { 
deba@2116
  1000
	graph->nextIn(*this);
deba@2116
  1001
	return *this; 
deba@2116
  1002
      }
deba@2116
  1003
deba@2116
  1004
    };
deba@2116
  1005
  
deba@2116
  1006
    /// \brief Base node of the iterator
deba@2116
  1007
    ///
deba@2116
  1008
    /// Returns the base node (ie. the source in this case) of the iterator
deba@2116
  1009
    Node baseNode(const OutEdgeIt &e) const {
deba@2386
  1010
      return Parent::source(static_cast<const Edge&>(e));
deba@2116
  1011
    }
deba@2116
  1012
    /// \brief Running node of the iterator
deba@2116
  1013
    ///
deba@2116
  1014
    /// Returns the running node (ie. the target in this case) of the
deba@2116
  1015
    /// iterator
deba@2116
  1016
    Node runningNode(const OutEdgeIt &e) const {
deba@2386
  1017
      return Parent::target(static_cast<const Edge&>(e));
deba@2116
  1018
    }
deba@2116
  1019
  
deba@2116
  1020
    /// \brief Base node of the iterator
deba@2116
  1021
    ///
deba@2116
  1022
    /// Returns the base node (ie. the target in this case) of the iterator
deba@2116
  1023
    Node baseNode(const InEdgeIt &e) const {
deba@2386
  1024
      return Parent::target(static_cast<const Edge&>(e));
deba@2116
  1025
    }
deba@2116
  1026
    /// \brief Running node of the iterator
deba@2116
  1027
    ///
deba@2116
  1028
    /// Returns the running node (ie. the source in this case) of the
deba@2116
  1029
    /// iterator
deba@2116
  1030
    Node runningNode(const InEdgeIt &e) const {
deba@2386
  1031
      return Parent::source(static_cast<const Edge&>(e));
deba@2116
  1032
    }
deba@2116
  1033
  
deba@2116
  1034
    class IncEdgeIt : public Parent::UEdge { 
deba@2116
  1035
      friend class BpUGraphExtender;
deba@2116
  1036
      const Graph* graph;
deba@2116
  1037
      bool direction;
deba@2116
  1038
    public:
deba@2116
  1039
    
deba@2116
  1040
      IncEdgeIt() { }
deba@2116
  1041
    
deba@2116
  1042
      IncEdgeIt(Invalid i) : UEdge(i), direction(true) { }
deba@2116
  1043
    
deba@2116
  1044
      IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
deba@2116
  1045
	graph->firstInc(*this, direction, n);
deba@2116
  1046
      }
deba@2116
  1047
deba@2116
  1048
      IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
deba@2116
  1049
	: graph(&_graph), UEdge(ue) {
deba@2116
  1050
	direction = (graph->source(ue) == n);
deba@2116
  1051
      }
deba@2116
  1052
deba@2116
  1053
      IncEdgeIt& operator++() {
deba@2116
  1054
	graph->nextInc(*this, direction);
deba@2116
  1055
	return *this; 
deba@2116
  1056
      }
deba@2116
  1057
    };
deba@2116
  1058
  
deba@2116
  1059
deba@2116
  1060
    /// Base node of the iterator
deba@2116
  1061
    ///
deba@2116
  1062
    /// Returns the base node of the iterator
deba@2116
  1063
    Node baseNode(const IncEdgeIt &e) const {
deba@2116
  1064
      return e.direction ? source(e) : target(e);
deba@2116
  1065
    }
deba@2116
  1066
deba@2116
  1067
    /// Running node of the iterator
deba@2116
  1068
    ///
deba@2116
  1069
    /// Returns the running node of the iterator
deba@2116
  1070
    Node runningNode(const IncEdgeIt &e) const {
deba@2116
  1071
      return e.direction ? target(e) : source(e);
deba@2116
  1072
    }
deba@2116
  1073
deba@2116
  1074
    template <typename _Value>
deba@2116
  1075
    class ANodeMap 
deba@2116
  1076
      : public MapExtender<DefaultMap<Graph, ANode, _Value> > {
deba@2116
  1077
    public:
deba@2116
  1078
      typedef BpUGraphExtender Graph;
deba@2116
  1079
      typedef MapExtender<DefaultMap<Graph, ANode, _Value> > Parent;
deba@2116
  1080
    
deba@2116
  1081
      ANodeMap(const Graph& graph) 
deba@2116
  1082
	: Parent(graph) {}
deba@2116
  1083
      ANodeMap(const Graph& graph, const _Value& value) 
deba@2116
  1084
	: Parent(graph, value) {}
deba@2116
  1085
    
deba@2116
  1086
      ANodeMap& operator=(const ANodeMap& cmap) {
deba@2116
  1087
	return operator=<ANodeMap>(cmap);
deba@2116
  1088
      }
deba@2116
  1089
    
deba@2116
  1090
      template <typename CMap>
deba@2116
  1091
      ANodeMap& operator=(const CMap& cmap) {
deba@2116
  1092
        Parent::operator=(cmap);
deba@2116
  1093
	return *this;
deba@2116
  1094
      }
deba@2116
  1095
    
deba@2116
  1096
    };
deba@2116
  1097
deba@2116
  1098
    template <typename _Value>
deba@2116
  1099
    class BNodeMap 
deba@2116
  1100
      : public MapExtender<DefaultMap<Graph, BNode, _Value> > {
deba@2116
  1101
    public:
deba@2116
  1102
      typedef BpUGraphExtender Graph;
deba@2116
  1103
      typedef MapExtender<DefaultMap<Graph, BNode, _Value> > Parent;
deba@2116
  1104
    
deba@2116
  1105
      BNodeMap(const Graph& graph) 
deba@2116
  1106
	: Parent(graph) {}
deba@2116
  1107
      BNodeMap(const Graph& graph, const _Value& value) 
deba@2116
  1108
	: Parent(graph, value) {}
deba@2116
  1109
    
deba@2116
  1110
      BNodeMap& operator=(const BNodeMap& cmap) {
deba@2116
  1111
	return operator=<BNodeMap>(cmap);
deba@2116
  1112
      }
deba@2116
  1113
    
deba@2116
  1114
      template <typename CMap>
deba@2116
  1115
      BNodeMap& operator=(const CMap& cmap) {
deba@2116
  1116
        Parent::operator=(cmap);
deba@2116
  1117
	return *this;
deba@2116
  1118
      }
deba@2116
  1119
    
deba@2116
  1120
    };
deba@2116
  1121
deba@2116
  1122
  public:
deba@2116
  1123
deba@2116
  1124
    template <typename _Value>
deba@2116
  1125
    class NodeMap {
deba@2116
  1126
    public:
deba@2116
  1127
      typedef BpUGraphExtender Graph;
deba@2116
  1128
deba@2116
  1129
      typedef Node Key;
deba@2116
  1130
      typedef _Value Value;
deba@2116
  1131
deba@2116
  1132
      /// The reference type of the map;
deba@2116
  1133
      typedef typename ANodeMap<_Value>::Reference Reference;
deba@2116
  1134
      /// The const reference type of the map;
deba@2116
  1135
      typedef typename ANodeMap<_Value>::ConstReference ConstReference;
deba@2116
  1136
deba@2116
  1137
      typedef True ReferenceMapTag;
deba@2116
  1138
deba@2116
  1139
      NodeMap(const Graph& _graph) 
deba@2116
  1140
	: graph(_graph), aNodeMap(_graph), bNodeMap(_graph) {}
deba@2116
  1141
      NodeMap(const Graph& _graph, const _Value& _value) 
deba@2116
  1142
	: graph(_graph), aNodeMap(_graph, _value), bNodeMap(_graph, _value) {}
deba@2116
  1143
deba@2116
  1144
      NodeMap& operator=(const NodeMap& cmap) {
deba@2116
  1145
	return operator=<NodeMap>(cmap);
deba@2116
  1146
      }
deba@2116
  1147
    
deba@2116
  1148
      template <typename CMap>
deba@2116
  1149
      NodeMap& operator=(const CMap& cmap) {
alpar@2260
  1150
	checkConcept<concepts::ReadMap<Node, _Value>, CMap>();
deba@2231
  1151
        aNodeMap = cmap;
deba@2231
  1152
        bNodeMap = cmap;
deba@2231
  1153
        return *this;
deba@2116
  1154
      }
deba@2116
  1155
deba@2116
  1156
      ConstReference operator[](const Key& node) const {
deba@2116
  1157
	if (Parent::aNode(node)) {
deba@2116
  1158
	  return aNodeMap[node];
deba@2116
  1159
	} else {
deba@2116
  1160
	  return bNodeMap[node];
deba@2116
  1161
	}
deba@2116
  1162
      } 
deba@2116
  1163
deba@2116
  1164
      Reference operator[](const Key& node) {
deba@2116
  1165
	if (Parent::aNode(node)) {
deba@2116
  1166
	  return aNodeMap[node];
deba@2116
  1167
	} else {
deba@2116
  1168
	  return bNodeMap[node];
deba@2116
  1169
	}
deba@2116
  1170
      }
deba@2116
  1171
deba@2116
  1172
      void set(const Key& node, const Value& value) {
deba@2116
  1173
	if (Parent::aNode(node)) {
deba@2116
  1174
	  aNodeMap.set(node, value);
deba@2116
  1175
	} else {
deba@2116
  1176
	  bNodeMap.set(node, value);
deba@2116
  1177
	}
deba@2116
  1178
      }
deba@2116
  1179
deba@2116
  1180
      class MapIt : public NodeIt {
deba@2116
  1181
      public:
deba@2116
  1182
deba@2116
  1183
        typedef NodeIt Parent;
deba@2116
  1184
        
deba@2116
  1185
        explicit MapIt(NodeMap& _map) 
deba@2116
  1186
          : Parent(_map.graph), map(_map) {}
deba@2116
  1187
        
deba@2116
  1188
        typename MapTraits<NodeMap>::ConstReturnValue operator*() const {
deba@2116
  1189
          return map[*this];
deba@2116
  1190
        }
deba@2116
  1191
        
deba@2116
  1192
        typename MapTraits<NodeMap>::ReturnValue operator*() {
deba@2116
  1193
          return map[*this];
deba@2116
  1194
        }
deba@2116
  1195
        
deba@2116
  1196
        void set(const Value& value) {
deba@2116
  1197
          map.set(*this, value);
deba@2116
  1198
        }
deba@2116
  1199
deba@2116
  1200
      private:
deba@2116
  1201
        NodeMap& map;
deba@2116
  1202
      };
deba@2116
  1203
deba@2116
  1204
      class ConstMapIt : public NodeIt {
deba@2116
  1205
      public:
deba@2116
  1206
deba@2116
  1207
        typedef NodeIt Parent;
deba@2116
  1208
        
deba@2116
  1209
        explicit ConstMapIt(const NodeMap& _map) 
deba@2116
  1210
          : Parent(_map.graph), map(_map) {}
deba@2116
  1211
        
deba@2116
  1212
        typename MapTraits<NodeMap>::ConstReturnValue operator*() const {
deba@2116
  1213
          return map[*this];
deba@2116
  1214
        }
deba@2116
  1215
        
deba@2116
  1216
      private:
deba@2116
  1217
        const NodeMap& map;
deba@2116
  1218
      };
deba@2116
  1219
deba@2116
  1220
      class ItemIt : public NodeIt {
deba@2116
  1221
      public:
deba@2116
  1222
deba@2116
  1223
        typedef NodeIt Parent;
deba@2116
  1224
deba@2116
  1225
        explicit ItemIt(const NodeMap& _map)
deba@2116
  1226
          : Parent(_map.graph) {}
deba@2116
  1227
        
deba@2116
  1228
      };
deba@2116
  1229
      
deba@2116
  1230
    private:
deba@2116
  1231
      const Graph& graph;
deba@2116
  1232
      ANodeMap<_Value> aNodeMap;
deba@2116
  1233
      BNodeMap<_Value> bNodeMap;
deba@2116
  1234
    };
deba@2116
  1235
    
deba@2116
  1236
deba@2116
  1237
    template <typename _Value>
deba@2116
  1238
    class EdgeMap 
deba@2116
  1239
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
deba@2116
  1240
    public:
deba@2116
  1241
      typedef BpUGraphExtender Graph;
deba@2116
  1242
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
deba@2116
  1243
    
deba@2116
  1244
      EdgeMap(const Graph& graph) 
deba@2116
  1245
	: Parent(graph) {}
deba@2116
  1246
      EdgeMap(const Graph& graph, const _Value& value) 
deba@2116
  1247
	: Parent(graph, value) {}
deba@2116
  1248
    
deba@2116
  1249
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@2116
  1250
	return operator=<EdgeMap>(cmap);
deba@2116
  1251
      }
deba@2116
  1252
    
deba@2116
  1253
      template <typename CMap>
deba@2116
  1254
      EdgeMap& operator=(const CMap& cmap) {
deba@2116
  1255
        Parent::operator=(cmap);
deba@2116
  1256
	return *this;
deba@2116
  1257
      }
deba@2116
  1258
    };
deba@2116
  1259
deba@2116
  1260
    template <typename _Value>
deba@2116
  1261
    class UEdgeMap 
deba@2116
  1262
      : public MapExtender<DefaultMap<Graph, UEdge, _Value> > {
deba@2116
  1263
    public:
deba@2116
  1264
      typedef BpUGraphExtender Graph;
deba@2116
  1265
      typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;
deba@2116
  1266
    
deba@2116
  1267
      UEdgeMap(const Graph& graph) 
deba@2116
  1268
	: Parent(graph) {}
deba@2116
  1269
      UEdgeMap(const Graph& graph, const _Value& value) 
deba@2116
  1270
	: Parent(graph, value) {}
deba@2116
  1271
    
deba@2116
  1272
      UEdgeMap& operator=(const UEdgeMap& cmap) {
deba@2116
  1273
	return operator=<UEdgeMap>(cmap);
deba@2116
  1274
      }
deba@2116
  1275
    
deba@2116
  1276
      template <typename CMap>
deba@2116
  1277
      UEdgeMap& operator=(const CMap& cmap) {
deba@2116
  1278
        Parent::operator=(cmap);
deba@2116
  1279
	return *this;
deba@2116
  1280
      }
deba@2116
  1281
    };
deba@2116
  1282
deba@2116
  1283
  
deba@2116
  1284
    Node addANode() {
deba@2116
  1285
      Node node = Parent::addANode();
deba@2384
  1286
      notifier(ANode()).add(node);
deba@2384
  1287
      notifier(Node()).add(node);
deba@2116
  1288
      return node;
deba@2116
  1289
    }
deba@2116
  1290
deba@2116
  1291
    Node addBNode() {
deba@2116
  1292
      Node node = Parent::addBNode();
deba@2384
  1293
      notifier(BNode()).add(node);
deba@2384
  1294
      notifier(Node()).add(node);
deba@2116
  1295
      return node;
deba@2116
  1296
    }
deba@2116
  1297
  
deba@2386
  1298
    UEdge addEdge(const Node& s, const Node& t) {
deba@2386
  1299
      UEdge uedge = Parent::addEdge(s, t);
deba@2384
  1300
      notifier(UEdge()).add(uedge);
deba@2116
  1301
    
deba@2386
  1302
      std::vector<Edge> ev;
deba@2386
  1303
      ev.push_back(Parent::direct(uedge, true));
deba@2386
  1304
      ev.push_back(Parent::direct(uedge, false));
deba@2386
  1305
      notifier(Edge()).add(ev);
deba@2116
  1306
    
deba@2116
  1307
      return uedge;
deba@2116
  1308
    }
deba@2116
  1309
deba@2116
  1310
    void clear() {
deba@2384
  1311
      notifier(Edge()).clear();
deba@2384
  1312
      notifier(UEdge()).clear();
deba@2384
  1313
      notifier(Node()).clear();
deba@2384
  1314
      notifier(BNode()).clear();
deba@2384
  1315
      notifier(ANode()).clear();
deba@2116
  1316
      Parent::clear();
deba@2116
  1317
    }
deba@2116
  1318
deba@2290
  1319
    template <typename Graph, typename ANodeRefMap, 
deba@2290
  1320
              typename BNodeRefMap, typename UEdgeRefMap>
deba@2329
  1321
    void build(const Graph& graph, ANodeRefMap& aNodeRef, 
deba@2290
  1322
               BNodeRefMap& bNodeRef, UEdgeRefMap& uEdgeRef) {
deba@2329
  1323
      Parent::build(graph, aNodeRef, bNodeRef, uEdgeRef);
deba@2384
  1324
      notifier(ANode()).build();
deba@2384
  1325
      notifier(BNode()).build();
deba@2384
  1326
      notifier(Node()).build();
deba@2384
  1327
      notifier(UEdge()).build();
deba@2384
  1328
      notifier(Edge()).build();
deba@2290
  1329
    }
deba@2290
  1330
deba@2116
  1331
    void erase(const Node& node) {
deba@2116
  1332
      UEdge uedge;
deba@2116
  1333
      if (Parent::aNode(node)) {
deba@2116
  1334
        Parent::firstFromANode(uedge, node);
deba@2116
  1335
        while (uedge != INVALID) {
deba@2116
  1336
          erase(uedge);
deba@2116
  1337
          Parent::firstFromANode(uedge, node);
deba@2116
  1338
        }
deba@2384
  1339
        notifier(ANode()).erase(node);
deba@2116
  1340
      } else {
deba@2116
  1341
        Parent::firstFromBNode(uedge, node);
deba@2116
  1342
        while (uedge != INVALID) {
deba@2116
  1343
          erase(uedge);
deba@2116
  1344
          Parent::firstFromBNode(uedge, node);
deba@2116
  1345
        }
deba@2384
  1346
        notifier(BNode()).erase(node);
deba@2116
  1347
      }
deba@2116
  1348
deba@2384
  1349
      notifier(Node()).erase(node);
deba@2116
  1350
      Parent::erase(node);
deba@2116
  1351
    }
deba@2116
  1352
    
deba@2116
  1353
    void erase(const UEdge& uedge) {
deba@2386
  1354
      std::vector<Edge> ev;
deba@2386
  1355
      ev.push_back(Parent::direct(uedge, true));
deba@2386
  1356
      ev.push_back(Parent::direct(uedge, false));
deba@2386
  1357
      notifier(Edge()).erase(ev);
deba@2384
  1358
      notifier(UEdge()).erase(uedge);
deba@2116
  1359
      Parent::erase(uedge);
deba@2116
  1360
    }
deba@2116
  1361
deba@2116
  1362
deba@2116
  1363
    BpUGraphExtender() {
deba@2116
  1364
      anode_notifier.setContainer(*this); 
deba@2116
  1365
      bnode_notifier.setContainer(*this); 
deba@2116
  1366
      node_notifier.setContainer(*this); 
deba@2116
  1367
      edge_notifier.setContainer(*this); 
deba@2116
  1368
      uedge_notifier.setContainer(*this);
deba@2116
  1369
    } 
deba@2116
  1370
deba@2116
  1371
    ~BpUGraphExtender() {
deba@2116
  1372
      uedge_notifier.clear();
deba@2116
  1373
      edge_notifier.clear(); 
deba@2116
  1374
      node_notifier.clear(); 
deba@2116
  1375
      anode_notifier.clear(); 
deba@2116
  1376
      bnode_notifier.clear(); 
deba@2116
  1377
    }
deba@2116
  1378
deba@2222
  1379
    Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
deba@2222
  1380
      UEdge uedge = Parent::findUEdge(u, v, prev);
deba@2222
  1381
      if (uedge != INVALID) {
deba@2231
  1382
        return Parent::direct(uedge, Parent::aNode(u));
deba@2222
  1383
      } else {
deba@2222
  1384
        return INVALID;
deba@2222
  1385
      }
deba@2222
  1386
    }
deba@2116
  1387
deba@2116
  1388
  };
deba@2116
  1389
deba@1791
  1390
}
deba@1791
  1391
deba@1996
  1392
#endif