lemon/bits/graph_extender.h
author Peter Kovacs <kpeter@inf.elte.hu>
Sun, 26 Apr 2009 16:44:53 +0200
changeset 625 029a48052c67
parent 440 88ed40ad0d4f
child 685 a27356ceb5bd
permissions -rw-r--r--
Modify the interface of MinCostArborescence + improvements (#267)

- Rename arborescenceValue() to arborescenceCost().
- Rename DefXyz template named paramaters to SetXyz.
- Rearrange public functions (for better doc).
- Doc improvements.
- Extend the test file with interface checking.
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@57
     2
 *
alpar@209
     3
 * This file is a part of LEMON, a generic C++ optimization library.
deba@57
     4
 *
alpar@440
     5
 * Copyright (C) 2003-2009
deba@57
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57
     8
 *
deba@57
     9
 * Permission to use, modify and distribute this software is granted
deba@57
    10
 * provided that this copyright notice appears in all copies. For
deba@57
    11
 * precise terms see the accompanying LICENSE file.
deba@57
    12
 *
deba@57
    13
 * This software is provided "AS IS" with no warranty of any kind,
deba@57
    14
 * express or implied, and with no claim as to its suitability for any
deba@57
    15
 * purpose.
deba@57
    16
 *
deba@57
    17
 */
deba@57
    18
deba@57
    19
#ifndef LEMON_BITS_GRAPH_EXTENDER_H
deba@57
    20
#define LEMON_BITS_GRAPH_EXTENDER_H
deba@57
    21
deba@220
    22
#include <lemon/core.h>
deba@57
    23
deba@57
    24
#include <lemon/bits/map_extender.h>
deba@57
    25
#include <lemon/bits/default_map.h>
deba@57
    26
deba@57
    27
#include <lemon/concept_check.h>
deba@57
    28
#include <lemon/concepts/maps.h>
deba@57
    29
kpeter@314
    30
//\ingroup graphbits
kpeter@314
    31
//\file
kpeter@361
    32
//\brief Extenders for the graph types
deba@57
    33
namespace lemon {
deba@57
    34
kpeter@314
    35
  // \ingroup graphbits
kpeter@314
    36
  //
kpeter@361
    37
  // \brief Extender for the digraph implementations
deba@57
    38
  template <typename Base>
deba@57
    39
  class DigraphExtender : public Base {
kpeter@617
    40
    typedef Base Parent;
kpeter@617
    41
deba@57
    42
  public:
deba@57
    43
deba@57
    44
    typedef DigraphExtender Digraph;
deba@57
    45
deba@57
    46
    // Base extensions
deba@57
    47
deba@57
    48
    typedef typename Parent::Node Node;
deba@57
    49
    typedef typename Parent::Arc Arc;
deba@57
    50
deba@57
    51
    int maxId(Node) const {
deba@57
    52
      return Parent::maxNodeId();
deba@57
    53
    }
deba@57
    54
deba@57
    55
    int maxId(Arc) const {
deba@57
    56
      return Parent::maxArcId();
deba@57
    57
    }
deba@57
    58
deba@57
    59
    Node fromId(int id, Node) const {
deba@57
    60
      return Parent::nodeFromId(id);
deba@57
    61
    }
deba@57
    62
deba@57
    63
    Arc fromId(int id, Arc) const {
deba@57
    64
      return Parent::arcFromId(id);
deba@57
    65
    }
deba@57
    66
deba@78
    67
    Node oppositeNode(const Node &node, const Arc &arc) const {
deba@78
    68
      if (node == Parent::source(arc))
alpar@209
    69
        return Parent::target(arc);
deba@78
    70
      else if(node == Parent::target(arc))
alpar@209
    71
        return Parent::source(arc);
deba@57
    72
      else
alpar@209
    73
        return INVALID;
deba@57
    74
    }
deba@57
    75
deba@57
    76
    // Alterable extension
deba@57
    77
deba@57
    78
    typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
deba@57
    79
    typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
deba@57
    80
deba@57
    81
deba@57
    82
  protected:
deba@57
    83
deba@57
    84
    mutable NodeNotifier node_notifier;
deba@57
    85
    mutable ArcNotifier arc_notifier;
deba@57
    86
deba@57
    87
  public:
deba@57
    88
deba@57
    89
    NodeNotifier& notifier(Node) const {
deba@57
    90
      return node_notifier;
deba@57
    91
    }
alpar@209
    92
deba@57
    93
    ArcNotifier& notifier(Arc) const {
deba@57
    94
      return arc_notifier;
deba@57
    95
    }
deba@57
    96
alpar@209
    97
    class NodeIt : public Node {
deba@78
    98
      const Digraph* _digraph;
deba@57
    99
    public:
deba@57
   100
deba@57
   101
      NodeIt() {}
deba@57
   102
deba@57
   103
      NodeIt(Invalid i) : Node(i) { }
deba@57
   104
deba@78
   105
      explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
alpar@209
   106
        _digraph->first(static_cast<Node&>(*this));
deba@57
   107
      }
deba@57
   108
alpar@209
   109
      NodeIt(const Digraph& digraph, const Node& node)
alpar@209
   110
        : Node(node), _digraph(&digraph) {}
deba@57
   111
alpar@209
   112
      NodeIt& operator++() {
alpar@209
   113
        _digraph->next(*this);
alpar@209
   114
        return *this;
deba@57
   115
      }
deba@57
   116
deba@57
   117
    };
deba@57
   118
deba@57
   119
alpar@209
   120
    class ArcIt : public Arc {
deba@78
   121
      const Digraph* _digraph;
deba@57
   122
    public:
deba@57
   123
deba@57
   124
      ArcIt() { }
deba@57
   125
deba@57
   126
      ArcIt(Invalid i) : Arc(i) { }
deba@57
   127
deba@78
   128
      explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
alpar@209
   129
        _digraph->first(static_cast<Arc&>(*this));
deba@57
   130
      }
deba@57
   131
alpar@209
   132
      ArcIt(const Digraph& digraph, const Arc& arc) :
alpar@209
   133
        Arc(arc), _digraph(&digraph) { }
deba@57
   134
alpar@209
   135
      ArcIt& operator++() {
alpar@209
   136
        _digraph->next(*this);
alpar@209
   137
        return *this;
deba@57
   138
      }
deba@57
   139
deba@57
   140
    };
deba@57
   141
deba@57
   142
alpar@209
   143
    class OutArcIt : public Arc {
deba@78
   144
      const Digraph* _digraph;
deba@57
   145
    public:
deba@57
   146
deba@57
   147
      OutArcIt() { }
deba@57
   148
deba@57
   149
      OutArcIt(Invalid i) : Arc(i) { }
deba@57
   150
alpar@209
   151
      OutArcIt(const Digraph& digraph, const Node& node)
alpar@209
   152
        : _digraph(&digraph) {
alpar@209
   153
        _digraph->firstOut(*this, node);
deba@57
   154
      }
deba@57
   155
alpar@209
   156
      OutArcIt(const Digraph& digraph, const Arc& arc)
alpar@209
   157
        : Arc(arc), _digraph(&digraph) {}
deba@57
   158
alpar@209
   159
      OutArcIt& operator++() {
alpar@209
   160
        _digraph->nextOut(*this);
alpar@209
   161
        return *this;
deba@57
   162
      }
deba@57
   163
deba@57
   164
    };
deba@57
   165
deba@57
   166
alpar@209
   167
    class InArcIt : public Arc {
deba@78
   168
      const Digraph* _digraph;
deba@57
   169
    public:
deba@57
   170
deba@57
   171
      InArcIt() { }
deba@57
   172
deba@57
   173
      InArcIt(Invalid i) : Arc(i) { }
deba@57
   174
alpar@209
   175
      InArcIt(const Digraph& digraph, const Node& node)
alpar@209
   176
        : _digraph(&digraph) {
alpar@209
   177
        _digraph->firstIn(*this, node);
deba@57
   178
      }
deba@57
   179
alpar@209
   180
      InArcIt(const Digraph& digraph, const Arc& arc) :
alpar@209
   181
        Arc(arc), _digraph(&digraph) {}
deba@57
   182
alpar@209
   183
      InArcIt& operator++() {
alpar@209
   184
        _digraph->nextIn(*this);
alpar@209
   185
        return *this;
deba@57
   186
      }
deba@57
   187
deba@57
   188
    };
deba@57
   189
kpeter@314
   190
    // \brief Base node of the iterator
kpeter@314
   191
    //
kpeter@314
   192
    // Returns the base node (i.e. the source in this case) of the iterator
deba@78
   193
    Node baseNode(const OutArcIt &arc) const {
deba@78
   194
      return Parent::source(arc);
deba@57
   195
    }
kpeter@314
   196
    // \brief Running node of the iterator
kpeter@314
   197
    //
kpeter@314
   198
    // Returns the running node (i.e. the target in this case) of the
kpeter@314
   199
    // iterator
deba@78
   200
    Node runningNode(const OutArcIt &arc) const {
deba@78
   201
      return Parent::target(arc);
deba@57
   202
    }
deba@57
   203
kpeter@314
   204
    // \brief Base node of the iterator
kpeter@314
   205
    //
kpeter@314
   206
    // Returns the base node (i.e. the target in this case) of the iterator
deba@78
   207
    Node baseNode(const InArcIt &arc) const {
deba@78
   208
      return Parent::target(arc);
deba@57
   209
    }
kpeter@314
   210
    // \brief Running node of the iterator
kpeter@314
   211
    //
kpeter@314
   212
    // Returns the running node (i.e. the source in this case) of the
kpeter@314
   213
    // iterator
deba@78
   214
    Node runningNode(const InArcIt &arc) const {
deba@78
   215
      return Parent::source(arc);
deba@57
   216
    }
deba@57
   217
alpar@209
   218
deba@57
   219
    template <typename _Value>
alpar@209
   220
    class NodeMap
deba@57
   221
      : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
deba@57
   222
      typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
deba@57
   223
kpeter@617
   224
    public:
alpar@209
   225
      explicit NodeMap(const Digraph& digraph)
alpar@209
   226
        : Parent(digraph) {}
alpar@209
   227
      NodeMap(const Digraph& digraph, const _Value& value)
alpar@209
   228
        : Parent(digraph, value) {}
deba@57
   229
kpeter@263
   230
    private:
deba@57
   231
      NodeMap& operator=(const NodeMap& cmap) {
alpar@209
   232
        return operator=<NodeMap>(cmap);
deba@57
   233
      }
deba@57
   234
deba@57
   235
      template <typename CMap>
deba@57
   236
      NodeMap& operator=(const CMap& cmap) {
deba@57
   237
        Parent::operator=(cmap);
alpar@209
   238
        return *this;
deba@57
   239
      }
deba@57
   240
deba@57
   241
    };
deba@57
   242
deba@57
   243
    template <typename _Value>
alpar@209
   244
    class ArcMap
deba@57
   245
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
deba@57
   246
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
deba@57
   247
kpeter@617
   248
    public:
alpar@209
   249
      explicit ArcMap(const Digraph& digraph)
alpar@209
   250
        : Parent(digraph) {}
alpar@209
   251
      ArcMap(const Digraph& digraph, const _Value& value)
alpar@209
   252
        : Parent(digraph, value) {}
deba@57
   253
kpeter@263
   254
    private:
deba@57
   255
      ArcMap& operator=(const ArcMap& cmap) {
alpar@209
   256
        return operator=<ArcMap>(cmap);
deba@57
   257
      }
deba@57
   258
deba@57
   259
      template <typename CMap>
deba@57
   260
      ArcMap& operator=(const CMap& cmap) {
deba@57
   261
        Parent::operator=(cmap);
alpar@209
   262
        return *this;
deba@57
   263
      }
deba@57
   264
    };
deba@57
   265
deba@57
   266
deba@57
   267
    Node addNode() {
deba@57
   268
      Node node = Parent::addNode();
deba@57
   269
      notifier(Node()).add(node);
deba@57
   270
      return node;
deba@57
   271
    }
alpar@209
   272
deba@57
   273
    Arc addArc(const Node& from, const Node& to) {
deba@57
   274
      Arc arc = Parent::addArc(from, to);
deba@57
   275
      notifier(Arc()).add(arc);
deba@57
   276
      return arc;
deba@57
   277
    }
deba@57
   278
deba@57
   279
    void clear() {
deba@57
   280
      notifier(Arc()).clear();
deba@57
   281
      notifier(Node()).clear();
deba@57
   282
      Parent::clear();
deba@57
   283
    }
deba@57
   284
deba@57
   285
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
deba@57
   286
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
deba@57
   287
      Parent::build(digraph, nodeRef, arcRef);
deba@57
   288
      notifier(Node()).build();
deba@57
   289
      notifier(Arc()).build();
deba@57
   290
    }
deba@57
   291
deba@57
   292
    void erase(const Node& node) {
deba@57
   293
      Arc arc;
deba@57
   294
      Parent::firstOut(arc, node);
deba@57
   295
      while (arc != INVALID ) {
alpar@209
   296
        erase(arc);
alpar@209
   297
        Parent::firstOut(arc, node);
alpar@209
   298
      }
deba@57
   299
deba@57
   300
      Parent::firstIn(arc, node);
deba@57
   301
      while (arc != INVALID ) {
alpar@209
   302
        erase(arc);
alpar@209
   303
        Parent::firstIn(arc, node);
deba@57
   304
      }
deba@57
   305
deba@57
   306
      notifier(Node()).erase(node);
deba@57
   307
      Parent::erase(node);
deba@57
   308
    }
alpar@209
   309
deba@57
   310
    void erase(const Arc& arc) {
deba@57
   311
      notifier(Arc()).erase(arc);
deba@57
   312
      Parent::erase(arc);
deba@57
   313
    }
deba@57
   314
deba@57
   315
    DigraphExtender() {
deba@57
   316
      node_notifier.setContainer(*this);
deba@57
   317
      arc_notifier.setContainer(*this);
alpar@209
   318
    }
alpar@209
   319
deba@57
   320
deba@57
   321
    ~DigraphExtender() {
deba@57
   322
      arc_notifier.clear();
deba@57
   323
      node_notifier.clear();
deba@57
   324
    }
deba@57
   325
  };
deba@57
   326
kpeter@314
   327
  // \ingroup _graphbits
kpeter@314
   328
  //
kpeter@314
   329
  // \brief Extender for the Graphs
alpar@209
   330
  template <typename Base>
deba@57
   331
  class GraphExtender : public Base {
kpeter@617
   332
    typedef Base Parent;
kpeter@617
   333
deba@57
   334
  public:
alpar@209
   335
deba@78
   336
    typedef GraphExtender Graph;
deba@57
   337
deba@77
   338
    typedef True UndirectedTag;
deba@77
   339
deba@57
   340
    typedef typename Parent::Node Node;
deba@57
   341
    typedef typename Parent::Arc Arc;
deba@57
   342
    typedef typename Parent::Edge Edge;
deba@57
   343
alpar@209
   344
    // Graph extension
deba@57
   345
deba@57
   346
    int maxId(Node) const {
deba@57
   347
      return Parent::maxNodeId();
deba@57
   348
    }
deba@57
   349
deba@57
   350
    int maxId(Arc) const {
deba@57
   351
      return Parent::maxArcId();
deba@57
   352
    }
deba@57
   353
deba@57
   354
    int maxId(Edge) const {
deba@57
   355
      return Parent::maxEdgeId();
deba@57
   356
    }
deba@57
   357
deba@57
   358
    Node fromId(int id, Node) const {
deba@57
   359
      return Parent::nodeFromId(id);
deba@57
   360
    }
deba@57
   361
deba@57
   362
    Arc fromId(int id, Arc) const {
deba@57
   363
      return Parent::arcFromId(id);
deba@57
   364
    }
deba@57
   365
deba@57
   366
    Edge fromId(int id, Edge) const {
deba@57
   367
      return Parent::edgeFromId(id);
deba@57
   368
    }
deba@57
   369
deba@57
   370
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@125
   371
      if( n == Parent::u(e))
alpar@209
   372
        return Parent::v(e);
deba@125
   373
      else if( n == Parent::v(e))
alpar@209
   374
        return Parent::u(e);
deba@57
   375
      else
alpar@209
   376
        return INVALID;
deba@57
   377
    }
deba@57
   378
deba@78
   379
    Arc oppositeArc(const Arc &arc) const {
deba@78
   380
      return Parent::direct(arc, !Parent::direction(arc));
deba@57
   381
    }
deba@57
   382
deba@57
   383
    using Parent::direct;
deba@78
   384
    Arc direct(const Edge &edge, const Node &node) const {
deba@125
   385
      return Parent::direct(edge, Parent::u(edge) == node);
deba@57
   386
    }
deba@57
   387
deba@57
   388
    // Alterable extension
deba@57
   389
deba@57
   390
    typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
deba@57
   391
    typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
deba@57
   392
    typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
deba@57
   393
deba@57
   394
deba@57
   395
  protected:
deba@57
   396
deba@57
   397
    mutable NodeNotifier node_notifier;
deba@57
   398
    mutable ArcNotifier arc_notifier;
deba@57
   399
    mutable EdgeNotifier edge_notifier;
deba@57
   400
deba@57
   401
  public:
deba@57
   402
deba@57
   403
    NodeNotifier& notifier(Node) const {
deba@57
   404
      return node_notifier;
deba@57
   405
    }
alpar@209
   406
deba@57
   407
    ArcNotifier& notifier(Arc) const {
deba@57
   408
      return arc_notifier;
deba@57
   409
    }
deba@57
   410
deba@57
   411
    EdgeNotifier& notifier(Edge) const {
deba@57
   412
      return edge_notifier;
deba@57
   413
    }
deba@57
   414
deba@57
   415
deba@57
   416
alpar@209
   417
    class NodeIt : public Node {
deba@78
   418
      const Graph* _graph;
deba@57
   419
    public:
deba@57
   420
deba@57
   421
      NodeIt() {}
deba@57
   422
deba@57
   423
      NodeIt(Invalid i) : Node(i) { }
deba@57
   424
deba@78
   425
      explicit NodeIt(const Graph& graph) : _graph(&graph) {
alpar@209
   426
        _graph->first(static_cast<Node&>(*this));
deba@57
   427
      }
deba@57
   428
alpar@209
   429
      NodeIt(const Graph& graph, const Node& node)
alpar@209
   430
        : Node(node), _graph(&graph) {}
deba@57
   431
alpar@209
   432
      NodeIt& operator++() {
alpar@209
   433
        _graph->next(*this);
alpar@209
   434
        return *this;
deba@57
   435
      }
deba@57
   436
deba@57
   437
    };
deba@57
   438
deba@57
   439
alpar@209
   440
    class ArcIt : public Arc {
deba@78
   441
      const Graph* _graph;
deba@57
   442
    public:
deba@57
   443
deba@57
   444
      ArcIt() { }
deba@57
   445
deba@57
   446
      ArcIt(Invalid i) : Arc(i) { }
deba@57
   447
deba@78
   448
      explicit ArcIt(const Graph& graph) : _graph(&graph) {
alpar@209
   449
        _graph->first(static_cast<Arc&>(*this));
deba@57
   450
      }
deba@57
   451
alpar@209
   452
      ArcIt(const Graph& graph, const Arc& arc) :
alpar@209
   453
        Arc(arc), _graph(&graph) { }
deba@57
   454
alpar@209
   455
      ArcIt& operator++() {
alpar@209
   456
        _graph->next(*this);
alpar@209
   457
        return *this;
deba@57
   458
      }
deba@57
   459
deba@57
   460
    };
deba@57
   461
deba@57
   462
alpar@209
   463
    class OutArcIt : public Arc {
deba@78
   464
      const Graph* _graph;
deba@57
   465
    public:
deba@57
   466
deba@57
   467
      OutArcIt() { }
deba@57
   468
deba@57
   469
      OutArcIt(Invalid i) : Arc(i) { }
deba@57
   470
alpar@209
   471
      OutArcIt(const Graph& graph, const Node& node)
alpar@209
   472
        : _graph(&graph) {
alpar@209
   473
        _graph->firstOut(*this, node);
deba@57
   474
      }
deba@57
   475
alpar@209
   476
      OutArcIt(const Graph& graph, const Arc& arc)
alpar@209
   477
        : Arc(arc), _graph(&graph) {}
deba@57
   478
alpar@209
   479
      OutArcIt& operator++() {
alpar@209
   480
        _graph->nextOut(*this);
alpar@209
   481
        return *this;
deba@57
   482
      }
deba@57
   483
deba@57
   484
    };
deba@57
   485
deba@57
   486
alpar@209
   487
    class InArcIt : public Arc {
deba@78
   488
      const Graph* _graph;
deba@57
   489
    public:
deba@57
   490
deba@57
   491
      InArcIt() { }
deba@57
   492
deba@57
   493
      InArcIt(Invalid i) : Arc(i) { }
deba@57
   494
alpar@209
   495
      InArcIt(const Graph& graph, const Node& node)
alpar@209
   496
        : _graph(&graph) {
alpar@209
   497
        _graph->firstIn(*this, node);
deba@57
   498
      }
deba@57
   499
alpar@209
   500
      InArcIt(const Graph& graph, const Arc& arc) :
alpar@209
   501
        Arc(arc), _graph(&graph) {}
deba@57
   502
alpar@209
   503
      InArcIt& operator++() {
alpar@209
   504
        _graph->nextIn(*this);
alpar@209
   505
        return *this;
deba@57
   506
      }
deba@57
   507
deba@57
   508
    };
deba@57
   509
deba@57
   510
alpar@209
   511
    class EdgeIt : public Parent::Edge {
deba@78
   512
      const Graph* _graph;
deba@57
   513
    public:
deba@57
   514
deba@57
   515
      EdgeIt() { }
deba@57
   516
deba@57
   517
      EdgeIt(Invalid i) : Edge(i) { }
deba@57
   518
deba@78
   519
      explicit EdgeIt(const Graph& graph) : _graph(&graph) {
alpar@209
   520
        _graph->first(static_cast<Edge&>(*this));
deba@57
   521
      }
deba@57
   522
alpar@209
   523
      EdgeIt(const Graph& graph, const Edge& edge) :
alpar@209
   524
        Edge(edge), _graph(&graph) { }
deba@57
   525
alpar@209
   526
      EdgeIt& operator++() {
alpar@209
   527
        _graph->next(*this);
alpar@209
   528
        return *this;
deba@57
   529
      }
deba@57
   530
deba@57
   531
    };
deba@57
   532
deba@78
   533
    class IncEdgeIt : public Parent::Edge {
deba@57
   534
      friend class GraphExtender;
deba@78
   535
      const Graph* _graph;
deba@78
   536
      bool _direction;
deba@57
   537
    public:
deba@57
   538
deba@78
   539
      IncEdgeIt() { }
deba@57
   540
deba@78
   541
      IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
deba@57
   542
deba@78
   543
      IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
alpar@209
   544
        _graph->firstInc(*this, _direction, node);
deba@57
   545
      }
deba@57
   546
deba@78
   547
      IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
alpar@209
   548
        : _graph(&graph), Edge(edge) {
alpar@209
   549
        _direction = (_graph->source(edge) == node);
deba@57
   550
      }
deba@57
   551
deba@78
   552
      IncEdgeIt& operator++() {
alpar@209
   553
        _graph->nextInc(*this, _direction);
alpar@209
   554
        return *this;
deba@57
   555
      }
deba@57
   556
    };
deba@57
   557
kpeter@314
   558
    // \brief Base node of the iterator
kpeter@314
   559
    //
kpeter@314
   560
    // Returns the base node (ie. the source in this case) of the iterator
deba@78
   561
    Node baseNode(const OutArcIt &arc) const {
deba@78
   562
      return Parent::source(static_cast<const Arc&>(arc));
deba@57
   563
    }
kpeter@314
   564
    // \brief Running node of the iterator
kpeter@314
   565
    //
kpeter@314
   566
    // Returns the running node (ie. the target in this case) of the
kpeter@314
   567
    // iterator
deba@78
   568
    Node runningNode(const OutArcIt &arc) const {
deba@78
   569
      return Parent::target(static_cast<const Arc&>(arc));
deba@57
   570
    }
deba@57
   571
kpeter@314
   572
    // \brief Base node of the iterator
kpeter@314
   573
    //
kpeter@314
   574
    // Returns the base node (ie. the target in this case) of the iterator
deba@78
   575
    Node baseNode(const InArcIt &arc) const {
deba@78
   576
      return Parent::target(static_cast<const Arc&>(arc));
deba@57
   577
    }
kpeter@314
   578
    // \brief Running node of the iterator
kpeter@314
   579
    //
kpeter@314
   580
    // Returns the running node (ie. the source in this case) of the
kpeter@314
   581
    // iterator
deba@78
   582
    Node runningNode(const InArcIt &arc) const {
deba@78
   583
      return Parent::source(static_cast<const Arc&>(arc));
deba@57
   584
    }
deba@57
   585
kpeter@314
   586
    // Base node of the iterator
kpeter@314
   587
    //
kpeter@314
   588
    // Returns the base node of the iterator
deba@78
   589
    Node baseNode(const IncEdgeIt &edge) const {
deba@125
   590
      return edge._direction ? u(edge) : v(edge);
deba@57
   591
    }
kpeter@314
   592
    // Running node of the iterator
kpeter@314
   593
    //
kpeter@314
   594
    // Returns the running node of the iterator
deba@78
   595
    Node runningNode(const IncEdgeIt &edge) const {
deba@125
   596
      return edge._direction ? v(edge) : u(edge);
deba@57
   597
    }
deba@57
   598
deba@57
   599
    // Mappable extension
deba@57
   600
deba@57
   601
    template <typename _Value>
alpar@209
   602
    class NodeMap
deba@78
   603
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
deba@78
   604
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
deba@57
   605
kpeter@617
   606
    public:
alpar@209
   607
      NodeMap(const Graph& graph)
alpar@209
   608
        : Parent(graph) {}
alpar@209
   609
      NodeMap(const Graph& graph, const _Value& value)
alpar@209
   610
        : Parent(graph, value) {}
deba@57
   611
kpeter@263
   612
    private:
deba@57
   613
      NodeMap& operator=(const NodeMap& cmap) {
alpar@209
   614
        return operator=<NodeMap>(cmap);
deba@57
   615
      }
deba@57
   616
deba@57
   617
      template <typename CMap>
deba@57
   618
      NodeMap& operator=(const CMap& cmap) {
deba@57
   619
        Parent::operator=(cmap);
alpar@209
   620
        return *this;
deba@57
   621
      }
deba@57
   622
deba@57
   623
    };
deba@57
   624
deba@57
   625
    template <typename _Value>
alpar@209
   626
    class ArcMap
deba@78
   627
      : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
deba@78
   628
      typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
deba@57
   629
kpeter@617
   630
    public:
alpar@209
   631
      ArcMap(const Graph& graph)
alpar@209
   632
        : Parent(graph) {}
alpar@209
   633
      ArcMap(const Graph& graph, const _Value& value)
alpar@209
   634
        : Parent(graph, value) {}
deba@57
   635
kpeter@263
   636
    private:
deba@57
   637
      ArcMap& operator=(const ArcMap& cmap) {
alpar@209
   638
        return operator=<ArcMap>(cmap);
deba@57
   639
      }
deba@57
   640
deba@57
   641
      template <typename CMap>
deba@57
   642
      ArcMap& operator=(const CMap& cmap) {
deba@57
   643
        Parent::operator=(cmap);
alpar@209
   644
        return *this;
deba@57
   645
      }
deba@57
   646
    };
deba@57
   647
deba@57
   648
deba@57
   649
    template <typename _Value>
alpar@209
   650
    class EdgeMap
deba@78
   651
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
deba@78
   652
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
deba@57
   653
kpeter@617
   654
    public:
alpar@209
   655
      EdgeMap(const Graph& graph)
alpar@209
   656
        : Parent(graph) {}
deba@57
   657
alpar@209
   658
      EdgeMap(const Graph& graph, const _Value& value)
alpar@209
   659
        : Parent(graph, value) {}
deba@57
   660
kpeter@263
   661
    private:
deba@57
   662
      EdgeMap& operator=(const EdgeMap& cmap) {
alpar@209
   663
        return operator=<EdgeMap>(cmap);
deba@57
   664
      }
deba@57
   665
deba@57
   666
      template <typename CMap>
deba@57
   667
      EdgeMap& operator=(const CMap& cmap) {
deba@57
   668
        Parent::operator=(cmap);
alpar@209
   669
        return *this;
deba@57
   670
      }
deba@57
   671
deba@57
   672
    };
deba@57
   673
deba@57
   674
    // Alteration extension
deba@57
   675
deba@57
   676
    Node addNode() {
deba@57
   677
      Node node = Parent::addNode();
deba@57
   678
      notifier(Node()).add(node);
deba@57
   679
      return node;
deba@57
   680
    }
deba@57
   681
deba@57
   682
    Edge addEdge(const Node& from, const Node& to) {
deba@57
   683
      Edge edge = Parent::addEdge(from, to);
deba@57
   684
      notifier(Edge()).add(edge);
deba@57
   685
      std::vector<Arc> ev;
deba@57
   686
      ev.push_back(Parent::direct(edge, true));
alpar@209
   687
      ev.push_back(Parent::direct(edge, false));
deba@57
   688
      notifier(Arc()).add(ev);
deba@57
   689
      return edge;
deba@57
   690
    }
alpar@209
   691
deba@57
   692
    void clear() {
deba@57
   693
      notifier(Arc()).clear();
deba@57
   694
      notifier(Edge()).clear();
deba@57
   695
      notifier(Node()).clear();
deba@57
   696
      Parent::clear();
deba@57
   697
    }
deba@57
   698
deba@78
   699
    template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
alpar@209
   700
    void build(const Graph& graph, NodeRefMap& nodeRef,
deba@57
   701
               EdgeRefMap& edgeRef) {
deba@78
   702
      Parent::build(graph, nodeRef, edgeRef);
deba@57
   703
      notifier(Node()).build();
deba@57
   704
      notifier(Edge()).build();
deba@57
   705
      notifier(Arc()).build();
deba@57
   706
    }
deba@57
   707
deba@57
   708
    void erase(const Node& node) {
deba@57
   709
      Arc arc;
deba@57
   710
      Parent::firstOut(arc, node);
deba@57
   711
      while (arc != INVALID ) {
alpar@209
   712
        erase(arc);
alpar@209
   713
        Parent::firstOut(arc, node);
alpar@209
   714
      }
deba@57
   715
deba@57
   716
      Parent::firstIn(arc, node);
deba@57
   717
      while (arc != INVALID ) {
alpar@209
   718
        erase(arc);
alpar@209
   719
        Parent::firstIn(arc, node);
deba@57
   720
      }
deba@57
   721
deba@57
   722
      notifier(Node()).erase(node);
deba@57
   723
      Parent::erase(node);
deba@57
   724
    }
deba@57
   725
deba@57
   726
    void erase(const Edge& edge) {
deba@78
   727
      std::vector<Arc> av;
deba@78
   728
      av.push_back(Parent::direct(edge, true));
alpar@209
   729
      av.push_back(Parent::direct(edge, false));
deba@78
   730
      notifier(Arc()).erase(av);
deba@57
   731
      notifier(Edge()).erase(edge);
deba@57
   732
      Parent::erase(edge);
deba@57
   733
    }
deba@57
   734
deba@57
   735
    GraphExtender() {
alpar@209
   736
      node_notifier.setContainer(*this);
deba@57
   737
      arc_notifier.setContainer(*this);
deba@57
   738
      edge_notifier.setContainer(*this);
alpar@209
   739
    }
deba@57
   740
deba@57
   741
    ~GraphExtender() {
deba@57
   742
      edge_notifier.clear();
deba@57
   743
      arc_notifier.clear();
alpar@209
   744
      node_notifier.clear();
alpar@209
   745
    }
deba@57
   746
deba@57
   747
  };
deba@57
   748
deba@57
   749
}
deba@57
   750
deba@57
   751
#endif