lemon/bits/graph_extender.h
author Balazs Dezso <deba@google.com>
Fri, 22 Jan 2021 10:55:32 +0100
changeset 1208 c6aa2cc1af04
parent 1092 dceba191c00d
permissions -rw-r--r--
Factor out recursion from weighted matching algorithms (#638)
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@1092
     5
 * Copyright (C) 2003-2013
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
ggab90@1130
    30
#include <lemon/bits/stl_iterators.h>
ggab90@1130
    31
kpeter@314
    32
//\ingroup graphbits
kpeter@314
    33
//\file
kpeter@361
    34
//\brief Extenders for the graph types
deba@57
    35
namespace lemon {
deba@57
    36
kpeter@314
    37
  // \ingroup graphbits
kpeter@314
    38
  //
kpeter@361
    39
  // \brief Extender for the digraph implementations
deba@57
    40
  template <typename Base>
deba@57
    41
  class DigraphExtender : public Base {
kpeter@617
    42
    typedef Base Parent;
kpeter@617
    43
deba@57
    44
  public:
deba@57
    45
deba@57
    46
    typedef DigraphExtender Digraph;
deba@57
    47
deba@57
    48
    // Base extensions
deba@57
    49
deba@57
    50
    typedef typename Parent::Node Node;
deba@57
    51
    typedef typename Parent::Arc Arc;
deba@57
    52
deba@57
    53
    int maxId(Node) const {
deba@57
    54
      return Parent::maxNodeId();
deba@57
    55
    }
deba@57
    56
deba@57
    57
    int maxId(Arc) const {
deba@57
    58
      return Parent::maxArcId();
deba@57
    59
    }
deba@57
    60
kpeter@778
    61
    static Node fromId(int id, Node) {
deba@57
    62
      return Parent::nodeFromId(id);
deba@57
    63
    }
deba@57
    64
kpeter@778
    65
    static Arc fromId(int id, Arc) {
deba@57
    66
      return Parent::arcFromId(id);
deba@57
    67
    }
deba@57
    68
deba@78
    69
    Node oppositeNode(const Node &node, const Arc &arc) const {
deba@78
    70
      if (node == Parent::source(arc))
alpar@209
    71
        return Parent::target(arc);
deba@78
    72
      else if(node == Parent::target(arc))
alpar@209
    73
        return Parent::source(arc);
deba@57
    74
      else
alpar@209
    75
        return INVALID;
deba@57
    76
    }
deba@57
    77
deba@57
    78
    // Alterable extension
deba@57
    79
deba@57
    80
    typedef AlterationNotifier<DigraphExtender, Node> NodeNotifier;
deba@57
    81
    typedef AlterationNotifier<DigraphExtender, Arc> ArcNotifier;
deba@57
    82
deba@57
    83
deba@57
    84
  protected:
deba@57
    85
deba@57
    86
    mutable NodeNotifier node_notifier;
deba@57
    87
    mutable ArcNotifier arc_notifier;
deba@57
    88
deba@57
    89
  public:
deba@57
    90
deba@57
    91
    NodeNotifier& notifier(Node) const {
deba@57
    92
      return node_notifier;
deba@57
    93
    }
alpar@209
    94
deba@57
    95
    ArcNotifier& notifier(Arc) const {
deba@57
    96
      return arc_notifier;
deba@57
    97
    }
deba@57
    98
alpar@209
    99
    class NodeIt : public Node {
deba@78
   100
      const Digraph* _digraph;
deba@57
   101
    public:
deba@57
   102
deba@57
   103
      NodeIt() {}
deba@57
   104
deba@57
   105
      NodeIt(Invalid i) : Node(i) { }
deba@57
   106
deba@78
   107
      explicit NodeIt(const Digraph& digraph) : _digraph(&digraph) {
alpar@209
   108
        _digraph->first(static_cast<Node&>(*this));
deba@57
   109
      }
deba@57
   110
alpar@209
   111
      NodeIt(const Digraph& digraph, const Node& node)
alpar@209
   112
        : Node(node), _digraph(&digraph) {}
deba@57
   113
alpar@209
   114
      NodeIt& operator++() {
alpar@209
   115
        _digraph->next(*this);
alpar@209
   116
        return *this;
deba@57
   117
      }
deba@57
   118
deba@57
   119
    };
deba@57
   120
ggab90@1130
   121
    LemonRangeWrapper1<NodeIt, Digraph> nodes() const {
ggab90@1130
   122
      return LemonRangeWrapper1<NodeIt, Digraph>(*this);
ggab90@1130
   123
    }
ggab90@1130
   124
deba@57
   125
alpar@209
   126
    class ArcIt : public Arc {
deba@78
   127
      const Digraph* _digraph;
deba@57
   128
    public:
deba@57
   129
deba@57
   130
      ArcIt() { }
deba@57
   131
deba@57
   132
      ArcIt(Invalid i) : Arc(i) { }
deba@57
   133
deba@78
   134
      explicit ArcIt(const Digraph& digraph) : _digraph(&digraph) {
alpar@209
   135
        _digraph->first(static_cast<Arc&>(*this));
deba@57
   136
      }
deba@57
   137
alpar@209
   138
      ArcIt(const Digraph& digraph, const Arc& arc) :
alpar@209
   139
        Arc(arc), _digraph(&digraph) { }
deba@57
   140
alpar@209
   141
      ArcIt& operator++() {
alpar@209
   142
        _digraph->next(*this);
alpar@209
   143
        return *this;
deba@57
   144
      }
deba@57
   145
deba@57
   146
    };
deba@57
   147
ggab90@1130
   148
    LemonRangeWrapper1<ArcIt, Digraph> arcs() const {
ggab90@1130
   149
      return LemonRangeWrapper1<ArcIt, Digraph>(*this);
ggab90@1130
   150
    }
ggab90@1130
   151
deba@57
   152
alpar@209
   153
    class OutArcIt : public Arc {
deba@78
   154
      const Digraph* _digraph;
deba@57
   155
    public:
deba@57
   156
deba@57
   157
      OutArcIt() { }
deba@57
   158
deba@57
   159
      OutArcIt(Invalid i) : Arc(i) { }
deba@57
   160
alpar@209
   161
      OutArcIt(const Digraph& digraph, const Node& node)
alpar@209
   162
        : _digraph(&digraph) {
alpar@209
   163
        _digraph->firstOut(*this, node);
deba@57
   164
      }
deba@57
   165
alpar@209
   166
      OutArcIt(const Digraph& digraph, const Arc& arc)
alpar@209
   167
        : Arc(arc), _digraph(&digraph) {}
deba@57
   168
alpar@209
   169
      OutArcIt& operator++() {
alpar@209
   170
        _digraph->nextOut(*this);
alpar@209
   171
        return *this;
deba@57
   172
      }
deba@57
   173
deba@57
   174
    };
deba@57
   175
ggab90@1130
   176
    LemonRangeWrapper2<OutArcIt, Digraph, Node> outArcs(const Node& u) const {
ggab90@1130
   177
      return LemonRangeWrapper2<OutArcIt, Digraph, Node>(*this, u);
ggab90@1130
   178
    }
ggab90@1130
   179
deba@57
   180
alpar@209
   181
    class InArcIt : public Arc {
deba@78
   182
      const Digraph* _digraph;
deba@57
   183
    public:
deba@57
   184
deba@57
   185
      InArcIt() { }
deba@57
   186
deba@57
   187
      InArcIt(Invalid i) : Arc(i) { }
deba@57
   188
alpar@209
   189
      InArcIt(const Digraph& digraph, const Node& node)
alpar@209
   190
        : _digraph(&digraph) {
alpar@209
   191
        _digraph->firstIn(*this, node);
deba@57
   192
      }
deba@57
   193
alpar@209
   194
      InArcIt(const Digraph& digraph, const Arc& arc) :
alpar@209
   195
        Arc(arc), _digraph(&digraph) {}
deba@57
   196
alpar@209
   197
      InArcIt& operator++() {
alpar@209
   198
        _digraph->nextIn(*this);
alpar@209
   199
        return *this;
deba@57
   200
      }
deba@57
   201
deba@57
   202
    };
deba@57
   203
ggab90@1130
   204
    LemonRangeWrapper2<InArcIt, Digraph, Node> inArcs(const Node& u) const {
ggab90@1130
   205
      return LemonRangeWrapper2<InArcIt, Digraph, Node>(*this, u);
ggab90@1130
   206
    }
ggab90@1130
   207
kpeter@314
   208
    // \brief Base node of the iterator
kpeter@314
   209
    //
kpeter@314
   210
    // Returns the base node (i.e. the source in this case) of the iterator
deba@78
   211
    Node baseNode(const OutArcIt &arc) const {
deba@78
   212
      return Parent::source(arc);
deba@57
   213
    }
kpeter@314
   214
    // \brief Running node of the iterator
kpeter@314
   215
    //
kpeter@314
   216
    // Returns the running node (i.e. the target in this case) of the
kpeter@314
   217
    // iterator
deba@78
   218
    Node runningNode(const OutArcIt &arc) const {
deba@78
   219
      return Parent::target(arc);
deba@57
   220
    }
deba@57
   221
kpeter@314
   222
    // \brief Base node of the iterator
kpeter@314
   223
    //
kpeter@314
   224
    // Returns the base node (i.e. the target in this case) of the iterator
deba@78
   225
    Node baseNode(const InArcIt &arc) const {
deba@78
   226
      return Parent::target(arc);
deba@57
   227
    }
kpeter@314
   228
    // \brief Running node of the iterator
kpeter@314
   229
    //
kpeter@314
   230
    // Returns the running node (i.e. the source in this case) of the
kpeter@314
   231
    // iterator
deba@78
   232
    Node runningNode(const InArcIt &arc) const {
deba@78
   233
      return Parent::source(arc);
deba@57
   234
    }
deba@57
   235
alpar@209
   236
deba@57
   237
    template <typename _Value>
alpar@209
   238
    class NodeMap
deba@57
   239
      : public MapExtender<DefaultMap<Digraph, Node, _Value> > {
deba@57
   240
      typedef MapExtender<DefaultMap<Digraph, Node, _Value> > Parent;
deba@57
   241
kpeter@617
   242
    public:
alpar@209
   243
      explicit NodeMap(const Digraph& digraph)
alpar@209
   244
        : Parent(digraph) {}
alpar@209
   245
      NodeMap(const Digraph& digraph, const _Value& value)
alpar@209
   246
        : Parent(digraph, value) {}
deba@57
   247
kpeter@263
   248
    private:
deba@57
   249
      NodeMap& operator=(const NodeMap& cmap) {
alpar@209
   250
        return operator=<NodeMap>(cmap);
deba@57
   251
      }
deba@57
   252
deba@57
   253
      template <typename CMap>
deba@57
   254
      NodeMap& operator=(const CMap& cmap) {
deba@57
   255
        Parent::operator=(cmap);
alpar@209
   256
        return *this;
deba@57
   257
      }
deba@57
   258
deba@57
   259
    };
deba@57
   260
deba@57
   261
    template <typename _Value>
alpar@209
   262
    class ArcMap
deba@57
   263
      : public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
deba@57
   264
      typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
deba@57
   265
kpeter@617
   266
    public:
alpar@209
   267
      explicit ArcMap(const Digraph& digraph)
alpar@209
   268
        : Parent(digraph) {}
alpar@209
   269
      ArcMap(const Digraph& digraph, const _Value& value)
alpar@209
   270
        : Parent(digraph, value) {}
deba@57
   271
kpeter@263
   272
    private:
deba@57
   273
      ArcMap& operator=(const ArcMap& cmap) {
alpar@209
   274
        return operator=<ArcMap>(cmap);
deba@57
   275
      }
deba@57
   276
deba@57
   277
      template <typename CMap>
deba@57
   278
      ArcMap& operator=(const CMap& cmap) {
deba@57
   279
        Parent::operator=(cmap);
alpar@209
   280
        return *this;
deba@57
   281
      }
deba@57
   282
    };
deba@57
   283
deba@57
   284
deba@57
   285
    Node addNode() {
deba@57
   286
      Node node = Parent::addNode();
deba@57
   287
      notifier(Node()).add(node);
deba@57
   288
      return node;
deba@57
   289
    }
alpar@209
   290
deba@57
   291
    Arc addArc(const Node& from, const Node& to) {
deba@57
   292
      Arc arc = Parent::addArc(from, to);
deba@57
   293
      notifier(Arc()).add(arc);
deba@57
   294
      return arc;
deba@57
   295
    }
deba@57
   296
deba@57
   297
    void clear() {
deba@57
   298
      notifier(Arc()).clear();
deba@57
   299
      notifier(Node()).clear();
deba@57
   300
      Parent::clear();
deba@57
   301
    }
deba@57
   302
deba@57
   303
    template <typename Digraph, typename NodeRefMap, typename ArcRefMap>
deba@57
   304
    void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
deba@57
   305
      Parent::build(digraph, nodeRef, arcRef);
deba@57
   306
      notifier(Node()).build();
deba@57
   307
      notifier(Arc()).build();
deba@57
   308
    }
deba@57
   309
deba@57
   310
    void erase(const Node& node) {
deba@57
   311
      Arc arc;
deba@57
   312
      Parent::firstOut(arc, node);
deba@57
   313
      while (arc != INVALID ) {
alpar@209
   314
        erase(arc);
alpar@209
   315
        Parent::firstOut(arc, node);
alpar@209
   316
      }
deba@57
   317
deba@57
   318
      Parent::firstIn(arc, node);
deba@57
   319
      while (arc != INVALID ) {
alpar@209
   320
        erase(arc);
alpar@209
   321
        Parent::firstIn(arc, node);
deba@57
   322
      }
deba@57
   323
deba@57
   324
      notifier(Node()).erase(node);
deba@57
   325
      Parent::erase(node);
deba@57
   326
    }
alpar@209
   327
deba@57
   328
    void erase(const Arc& arc) {
deba@57
   329
      notifier(Arc()).erase(arc);
deba@57
   330
      Parent::erase(arc);
deba@57
   331
    }
deba@57
   332
deba@57
   333
    DigraphExtender() {
deba@57
   334
      node_notifier.setContainer(*this);
deba@57
   335
      arc_notifier.setContainer(*this);
alpar@209
   336
    }
alpar@209
   337
deba@57
   338
deba@57
   339
    ~DigraphExtender() {
deba@57
   340
      arc_notifier.clear();
deba@57
   341
      node_notifier.clear();
deba@57
   342
    }
deba@57
   343
  };
deba@57
   344
kpeter@314
   345
  // \ingroup _graphbits
kpeter@314
   346
  //
kpeter@314
   347
  // \brief Extender for the Graphs
alpar@209
   348
  template <typename Base>
deba@57
   349
  class GraphExtender : public Base {
kpeter@617
   350
    typedef Base Parent;
kpeter@617
   351
deba@57
   352
  public:
alpar@209
   353
deba@78
   354
    typedef GraphExtender Graph;
deba@57
   355
deba@77
   356
    typedef True UndirectedTag;
deba@77
   357
deba@57
   358
    typedef typename Parent::Node Node;
deba@57
   359
    typedef typename Parent::Arc Arc;
deba@57
   360
    typedef typename Parent::Edge Edge;
deba@57
   361
alpar@209
   362
    // Graph extension
deba@57
   363
deba@57
   364
    int maxId(Node) const {
deba@57
   365
      return Parent::maxNodeId();
deba@57
   366
    }
deba@57
   367
deba@57
   368
    int maxId(Arc) const {
deba@57
   369
      return Parent::maxArcId();
deba@57
   370
    }
deba@57
   371
deba@57
   372
    int maxId(Edge) const {
deba@57
   373
      return Parent::maxEdgeId();
deba@57
   374
    }
deba@57
   375
kpeter@778
   376
    static Node fromId(int id, Node) {
deba@57
   377
      return Parent::nodeFromId(id);
deba@57
   378
    }
deba@57
   379
kpeter@778
   380
    static Arc fromId(int id, Arc) {
deba@57
   381
      return Parent::arcFromId(id);
deba@57
   382
    }
deba@57
   383
kpeter@778
   384
    static Edge fromId(int id, Edge) {
deba@57
   385
      return Parent::edgeFromId(id);
deba@57
   386
    }
deba@57
   387
deba@57
   388
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@125
   389
      if( n == Parent::u(e))
alpar@209
   390
        return Parent::v(e);
deba@125
   391
      else if( n == Parent::v(e))
alpar@209
   392
        return Parent::u(e);
deba@57
   393
      else
alpar@209
   394
        return INVALID;
deba@57
   395
    }
deba@57
   396
deba@78
   397
    Arc oppositeArc(const Arc &arc) const {
deba@78
   398
      return Parent::direct(arc, !Parent::direction(arc));
deba@57
   399
    }
deba@57
   400
deba@57
   401
    using Parent::direct;
deba@78
   402
    Arc direct(const Edge &edge, const Node &node) const {
deba@125
   403
      return Parent::direct(edge, Parent::u(edge) == node);
deba@57
   404
    }
deba@57
   405
deba@57
   406
    // Alterable extension
deba@57
   407
deba@57
   408
    typedef AlterationNotifier<GraphExtender, Node> NodeNotifier;
deba@57
   409
    typedef AlterationNotifier<GraphExtender, Arc> ArcNotifier;
deba@57
   410
    typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier;
deba@57
   411
deba@57
   412
deba@57
   413
  protected:
deba@57
   414
deba@57
   415
    mutable NodeNotifier node_notifier;
deba@57
   416
    mutable ArcNotifier arc_notifier;
deba@57
   417
    mutable EdgeNotifier edge_notifier;
deba@57
   418
deba@57
   419
  public:
deba@57
   420
deba@57
   421
    NodeNotifier& notifier(Node) const {
deba@57
   422
      return node_notifier;
deba@57
   423
    }
alpar@209
   424
deba@57
   425
    ArcNotifier& notifier(Arc) const {
deba@57
   426
      return arc_notifier;
deba@57
   427
    }
deba@57
   428
deba@57
   429
    EdgeNotifier& notifier(Edge) const {
deba@57
   430
      return edge_notifier;
deba@57
   431
    }
deba@57
   432
deba@57
   433
deba@57
   434
alpar@209
   435
    class NodeIt : public Node {
deba@78
   436
      const Graph* _graph;
deba@57
   437
    public:
deba@57
   438
deba@57
   439
      NodeIt() {}
deba@57
   440
deba@57
   441
      NodeIt(Invalid i) : Node(i) { }
deba@57
   442
deba@78
   443
      explicit NodeIt(const Graph& graph) : _graph(&graph) {
alpar@209
   444
        _graph->first(static_cast<Node&>(*this));
deba@57
   445
      }
deba@57
   446
alpar@209
   447
      NodeIt(const Graph& graph, const Node& node)
alpar@209
   448
        : Node(node), _graph(&graph) {}
deba@57
   449
alpar@209
   450
      NodeIt& operator++() {
alpar@209
   451
        _graph->next(*this);
alpar@209
   452
        return *this;
deba@57
   453
      }
deba@57
   454
deba@57
   455
    };
deba@57
   456
ggab90@1130
   457
    LemonRangeWrapper1<NodeIt, Graph> nodes() const {
ggab90@1130
   458
      return LemonRangeWrapper1<NodeIt, Graph>(*this);
ggab90@1130
   459
    }
ggab90@1130
   460
deba@57
   461
alpar@209
   462
    class ArcIt : public Arc {
deba@78
   463
      const Graph* _graph;
deba@57
   464
    public:
deba@57
   465
deba@57
   466
      ArcIt() { }
deba@57
   467
deba@57
   468
      ArcIt(Invalid i) : Arc(i) { }
deba@57
   469
deba@78
   470
      explicit ArcIt(const Graph& graph) : _graph(&graph) {
alpar@209
   471
        _graph->first(static_cast<Arc&>(*this));
deba@57
   472
      }
deba@57
   473
alpar@209
   474
      ArcIt(const Graph& graph, const Arc& arc) :
alpar@209
   475
        Arc(arc), _graph(&graph) { }
deba@57
   476
alpar@209
   477
      ArcIt& operator++() {
alpar@209
   478
        _graph->next(*this);
alpar@209
   479
        return *this;
deba@57
   480
      }
deba@57
   481
deba@57
   482
    };
deba@57
   483
ggab90@1130
   484
    LemonRangeWrapper1<ArcIt, Graph> arcs() const {
ggab90@1130
   485
      return LemonRangeWrapper1<ArcIt, Graph>(*this);
ggab90@1130
   486
    }
ggab90@1130
   487
deba@57
   488
alpar@209
   489
    class OutArcIt : public Arc {
deba@78
   490
      const Graph* _graph;
deba@57
   491
    public:
deba@57
   492
deba@57
   493
      OutArcIt() { }
deba@57
   494
deba@57
   495
      OutArcIt(Invalid i) : Arc(i) { }
deba@57
   496
alpar@209
   497
      OutArcIt(const Graph& graph, const Node& node)
alpar@209
   498
        : _graph(&graph) {
alpar@209
   499
        _graph->firstOut(*this, node);
deba@57
   500
      }
deba@57
   501
alpar@209
   502
      OutArcIt(const Graph& graph, const Arc& arc)
alpar@209
   503
        : Arc(arc), _graph(&graph) {}
deba@57
   504
alpar@209
   505
      OutArcIt& operator++() {
alpar@209
   506
        _graph->nextOut(*this);
alpar@209
   507
        return *this;
deba@57
   508
      }
deba@57
   509
deba@57
   510
    };
deba@57
   511
ggab90@1130
   512
    LemonRangeWrapper2<OutArcIt, Graph, Node> outArcs(const Node& u) const {
ggab90@1130
   513
      return LemonRangeWrapper2<OutArcIt, Graph, Node>(*this, u);
ggab90@1130
   514
    }
ggab90@1130
   515
deba@57
   516
alpar@209
   517
    class InArcIt : public Arc {
deba@78
   518
      const Graph* _graph;
deba@57
   519
    public:
deba@57
   520
deba@57
   521
      InArcIt() { }
deba@57
   522
deba@57
   523
      InArcIt(Invalid i) : Arc(i) { }
deba@57
   524
alpar@209
   525
      InArcIt(const Graph& graph, const Node& node)
alpar@209
   526
        : _graph(&graph) {
alpar@209
   527
        _graph->firstIn(*this, node);
deba@57
   528
      }
deba@57
   529
alpar@209
   530
      InArcIt(const Graph& graph, const Arc& arc) :
alpar@209
   531
        Arc(arc), _graph(&graph) {}
deba@57
   532
alpar@209
   533
      InArcIt& operator++() {
alpar@209
   534
        _graph->nextIn(*this);
alpar@209
   535
        return *this;
deba@57
   536
      }
deba@57
   537
deba@57
   538
    };
deba@57
   539
ggab90@1130
   540
    LemonRangeWrapper2<InArcIt, Graph, Node> inArcs(const Node& u) const {
ggab90@1130
   541
      return LemonRangeWrapper2<InArcIt, Graph, Node>(*this, u);
ggab90@1130
   542
    }
ggab90@1130
   543
deba@57
   544
alpar@209
   545
    class EdgeIt : public Parent::Edge {
deba@78
   546
      const Graph* _graph;
deba@57
   547
    public:
deba@57
   548
deba@57
   549
      EdgeIt() { }
deba@57
   550
deba@57
   551
      EdgeIt(Invalid i) : Edge(i) { }
deba@57
   552
deba@78
   553
      explicit EdgeIt(const Graph& graph) : _graph(&graph) {
alpar@209
   554
        _graph->first(static_cast<Edge&>(*this));
deba@57
   555
      }
deba@57
   556
alpar@209
   557
      EdgeIt(const Graph& graph, const Edge& edge) :
alpar@209
   558
        Edge(edge), _graph(&graph) { }
deba@57
   559
alpar@209
   560
      EdgeIt& operator++() {
alpar@209
   561
        _graph->next(*this);
alpar@209
   562
        return *this;
deba@57
   563
      }
deba@57
   564
deba@57
   565
    };
deba@57
   566
ggab90@1130
   567
    LemonRangeWrapper1<EdgeIt, Graph> edges() const {
ggab90@1130
   568
      return LemonRangeWrapper1<EdgeIt, Graph>(*this);
ggab90@1130
   569
    }
ggab90@1130
   570
ggab90@1130
   571
deba@78
   572
    class IncEdgeIt : public Parent::Edge {
deba@57
   573
      friend class GraphExtender;
deba@78
   574
      const Graph* _graph;
deba@78
   575
      bool _direction;
deba@57
   576
    public:
deba@57
   577
deba@78
   578
      IncEdgeIt() { }
deba@57
   579
deba@78
   580
      IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
deba@57
   581
deba@78
   582
      IncEdgeIt(const Graph& graph, const Node &node) : _graph(&graph) {
alpar@209
   583
        _graph->firstInc(*this, _direction, node);
deba@57
   584
      }
deba@57
   585
deba@78
   586
      IncEdgeIt(const Graph& graph, const Edge &edge, const Node &node)
alpar@209
   587
        : _graph(&graph), Edge(edge) {
alpar@209
   588
        _direction = (_graph->source(edge) == node);
deba@57
   589
      }
deba@57
   590
deba@78
   591
      IncEdgeIt& operator++() {
alpar@209
   592
        _graph->nextInc(*this, _direction);
alpar@209
   593
        return *this;
deba@57
   594
      }
deba@57
   595
    };
deba@57
   596
ggab90@1130
   597
    LemonRangeWrapper2<IncEdgeIt, Graph, Node> incEdges(const Node& u) const {
ggab90@1130
   598
      return LemonRangeWrapper2<IncEdgeIt, Graph, Node>(*this, u);
ggab90@1130
   599
    }
ggab90@1130
   600
ggab90@1130
   601
kpeter@314
   602
    // \brief Base node of the iterator
kpeter@314
   603
    //
kpeter@314
   604
    // Returns the base node (ie. the source in this case) of the iterator
deba@78
   605
    Node baseNode(const OutArcIt &arc) const {
deba@78
   606
      return Parent::source(static_cast<const Arc&>(arc));
deba@57
   607
    }
kpeter@314
   608
    // \brief Running node of the iterator
kpeter@314
   609
    //
kpeter@314
   610
    // Returns the running node (ie. the target in this case) of the
kpeter@314
   611
    // iterator
deba@78
   612
    Node runningNode(const OutArcIt &arc) const {
deba@78
   613
      return Parent::target(static_cast<const Arc&>(arc));
deba@57
   614
    }
deba@57
   615
kpeter@314
   616
    // \brief Base node of the iterator
kpeter@314
   617
    //
kpeter@314
   618
    // Returns the base node (ie. the target in this case) of the iterator
deba@78
   619
    Node baseNode(const InArcIt &arc) const {
deba@78
   620
      return Parent::target(static_cast<const Arc&>(arc));
deba@57
   621
    }
kpeter@314
   622
    // \brief Running node of the iterator
kpeter@314
   623
    //
kpeter@314
   624
    // Returns the running node (ie. the source in this case) of the
kpeter@314
   625
    // iterator
deba@78
   626
    Node runningNode(const InArcIt &arc) const {
deba@78
   627
      return Parent::source(static_cast<const Arc&>(arc));
deba@57
   628
    }
deba@57
   629
kpeter@314
   630
    // Base node of the iterator
kpeter@314
   631
    //
kpeter@314
   632
    // Returns the base node of the iterator
deba@78
   633
    Node baseNode(const IncEdgeIt &edge) const {
alpar@997
   634
      return edge._direction ? this->u(edge) : this->v(edge);
deba@57
   635
    }
kpeter@314
   636
    // Running node of the iterator
kpeter@314
   637
    //
kpeter@314
   638
    // Returns the running node of the iterator
deba@78
   639
    Node runningNode(const IncEdgeIt &edge) const {
alpar@997
   640
      return edge._direction ? this->v(edge) : this->u(edge);
deba@57
   641
    }
deba@57
   642
deba@57
   643
    // Mappable extension
deba@57
   644
deba@57
   645
    template <typename _Value>
alpar@209
   646
    class NodeMap
deba@78
   647
      : public MapExtender<DefaultMap<Graph, Node, _Value> > {
deba@78
   648
      typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent;
deba@57
   649
kpeter@617
   650
    public:
kpeter@685
   651
      explicit NodeMap(const Graph& graph)
alpar@209
   652
        : Parent(graph) {}
alpar@209
   653
      NodeMap(const Graph& graph, const _Value& value)
alpar@209
   654
        : Parent(graph, value) {}
deba@57
   655
kpeter@263
   656
    private:
deba@57
   657
      NodeMap& operator=(const NodeMap& cmap) {
alpar@209
   658
        return operator=<NodeMap>(cmap);
deba@57
   659
      }
deba@57
   660
deba@57
   661
      template <typename CMap>
deba@57
   662
      NodeMap& operator=(const CMap& cmap) {
deba@57
   663
        Parent::operator=(cmap);
alpar@209
   664
        return *this;
deba@57
   665
      }
deba@57
   666
deba@57
   667
    };
deba@57
   668
deba@57
   669
    template <typename _Value>
alpar@209
   670
    class ArcMap
deba@78
   671
      : public MapExtender<DefaultMap<Graph, Arc, _Value> > {
deba@78
   672
      typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent;
deba@57
   673
kpeter@617
   674
    public:
kpeter@685
   675
      explicit ArcMap(const Graph& graph)
alpar@209
   676
        : Parent(graph) {}
alpar@209
   677
      ArcMap(const Graph& graph, const _Value& value)
alpar@209
   678
        : Parent(graph, value) {}
deba@57
   679
kpeter@263
   680
    private:
deba@57
   681
      ArcMap& operator=(const ArcMap& cmap) {
alpar@209
   682
        return operator=<ArcMap>(cmap);
deba@57
   683
      }
deba@57
   684
deba@57
   685
      template <typename CMap>
deba@57
   686
      ArcMap& operator=(const CMap& cmap) {
deba@57
   687
        Parent::operator=(cmap);
alpar@209
   688
        return *this;
deba@57
   689
      }
deba@57
   690
    };
deba@57
   691
deba@57
   692
deba@57
   693
    template <typename _Value>
alpar@209
   694
    class EdgeMap
deba@78
   695
      : public MapExtender<DefaultMap<Graph, Edge, _Value> > {
deba@78
   696
      typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
deba@57
   697
kpeter@617
   698
    public:
kpeter@685
   699
      explicit EdgeMap(const Graph& graph)
alpar@209
   700
        : Parent(graph) {}
deba@57
   701
alpar@209
   702
      EdgeMap(const Graph& graph, const _Value& value)
alpar@209
   703
        : Parent(graph, value) {}
deba@57
   704
kpeter@263
   705
    private:
deba@57
   706
      EdgeMap& operator=(const EdgeMap& cmap) {
alpar@209
   707
        return operator=<EdgeMap>(cmap);
deba@57
   708
      }
deba@57
   709
deba@57
   710
      template <typename CMap>
deba@57
   711
      EdgeMap& operator=(const CMap& cmap) {
deba@57
   712
        Parent::operator=(cmap);
alpar@209
   713
        return *this;
deba@57
   714
      }
deba@57
   715
deba@57
   716
    };
deba@57
   717
deba@57
   718
    // Alteration extension
deba@57
   719
deba@57
   720
    Node addNode() {
deba@57
   721
      Node node = Parent::addNode();
deba@57
   722
      notifier(Node()).add(node);
deba@57
   723
      return node;
deba@57
   724
    }
deba@57
   725
deba@57
   726
    Edge addEdge(const Node& from, const Node& to) {
deba@57
   727
      Edge edge = Parent::addEdge(from, to);
deba@57
   728
      notifier(Edge()).add(edge);
deba@57
   729
      std::vector<Arc> ev;
deba@57
   730
      ev.push_back(Parent::direct(edge, true));
alpar@209
   731
      ev.push_back(Parent::direct(edge, false));
deba@57
   732
      notifier(Arc()).add(ev);
deba@57
   733
      return edge;
deba@57
   734
    }
alpar@209
   735
deba@57
   736
    void clear() {
deba@57
   737
      notifier(Arc()).clear();
deba@57
   738
      notifier(Edge()).clear();
deba@57
   739
      notifier(Node()).clear();
deba@57
   740
      Parent::clear();
deba@57
   741
    }
deba@57
   742
deba@78
   743
    template <typename Graph, typename NodeRefMap, typename EdgeRefMap>
alpar@209
   744
    void build(const Graph& graph, NodeRefMap& nodeRef,
deba@57
   745
               EdgeRefMap& edgeRef) {
deba@78
   746
      Parent::build(graph, nodeRef, edgeRef);
deba@57
   747
      notifier(Node()).build();
deba@57
   748
      notifier(Edge()).build();
deba@57
   749
      notifier(Arc()).build();
deba@57
   750
    }
deba@57
   751
deba@57
   752
    void erase(const Node& node) {
deba@57
   753
      Arc arc;
deba@57
   754
      Parent::firstOut(arc, node);
deba@57
   755
      while (arc != INVALID ) {
alpar@209
   756
        erase(arc);
alpar@209
   757
        Parent::firstOut(arc, node);
alpar@209
   758
      }
deba@57
   759
deba@57
   760
      Parent::firstIn(arc, node);
deba@57
   761
      while (arc != INVALID ) {
alpar@209
   762
        erase(arc);
alpar@209
   763
        Parent::firstIn(arc, node);
deba@57
   764
      }
deba@57
   765
deba@57
   766
      notifier(Node()).erase(node);
deba@57
   767
      Parent::erase(node);
deba@57
   768
    }
deba@57
   769
deba@57
   770
    void erase(const Edge& edge) {
deba@78
   771
      std::vector<Arc> av;
deba@78
   772
      av.push_back(Parent::direct(edge, true));
alpar@209
   773
      av.push_back(Parent::direct(edge, false));
deba@78
   774
      notifier(Arc()).erase(av);
deba@57
   775
      notifier(Edge()).erase(edge);
deba@57
   776
      Parent::erase(edge);
deba@57
   777
    }
deba@57
   778
deba@57
   779
    GraphExtender() {
alpar@209
   780
      node_notifier.setContainer(*this);
deba@57
   781
      arc_notifier.setContainer(*this);
deba@57
   782
      edge_notifier.setContainer(*this);
alpar@209
   783
    }
deba@57
   784
deba@57
   785
    ~GraphExtender() {
deba@57
   786
      edge_notifier.clear();
deba@57
   787
      arc_notifier.clear();
alpar@209
   788
      node_notifier.clear();
alpar@209
   789
    }
deba@57
   790
deba@57
   791
  };
deba@57
   792
deba@1019
   793
  // \ingroup _graphbits
deba@1019
   794
  //
deba@1019
   795
  // \brief Extender for the BpGraphs
deba@1019
   796
  template <typename Base>
deba@1019
   797
  class BpGraphExtender : public Base {
deba@1019
   798
    typedef Base Parent;
deba@1019
   799
deba@1019
   800
  public:
deba@1019
   801
deba@1019
   802
    typedef BpGraphExtender BpGraph;
deba@1019
   803
deba@1019
   804
    typedef True UndirectedTag;
deba@1019
   805
deba@1019
   806
    typedef typename Parent::Node Node;
deba@1025
   807
    typedef typename Parent::RedNode RedNode;
deba@1025
   808
    typedef typename Parent::BlueNode BlueNode;
deba@1019
   809
    typedef typename Parent::Arc Arc;
deba@1019
   810
    typedef typename Parent::Edge Edge;
deba@1019
   811
deba@1019
   812
    // BpGraph extension
deba@1019
   813
deba@1019
   814
    using Parent::first;
deba@1019
   815
    using Parent::next;
deba@1019
   816
    using Parent::id;
deba@1019
   817
deba@1019
   818
    int maxId(Node) const {
deba@1019
   819
      return Parent::maxNodeId();
deba@1019
   820
    }
deba@1019
   821
deba@1019
   822
    int maxId(RedNode) const {
deba@1019
   823
      return Parent::maxRedId();
deba@1019
   824
    }
deba@1019
   825
deba@1019
   826
    int maxId(BlueNode) const {
deba@1019
   827
      return Parent::maxBlueId();
deba@1019
   828
    }
deba@1019
   829
deba@1019
   830
    int maxId(Arc) const {
deba@1019
   831
      return Parent::maxArcId();
deba@1019
   832
    }
deba@1019
   833
deba@1019
   834
    int maxId(Edge) const {
deba@1019
   835
      return Parent::maxEdgeId();
deba@1019
   836
    }
deba@1019
   837
deba@1019
   838
    static Node fromId(int id, Node) {
deba@1019
   839
      return Parent::nodeFromId(id);
deba@1019
   840
    }
deba@1019
   841
deba@1019
   842
    static Arc fromId(int id, Arc) {
deba@1019
   843
      return Parent::arcFromId(id);
deba@1019
   844
    }
deba@1019
   845
deba@1019
   846
    static Edge fromId(int id, Edge) {
deba@1019
   847
      return Parent::edgeFromId(id);
deba@1019
   848
    }
deba@1019
   849
deba@1020
   850
    Node u(Edge e) const { return this->redNode(e); }
deba@1020
   851
    Node v(Edge e) const { return this->blueNode(e); }
deba@1020
   852
deba@1019
   853
    Node oppositeNode(const Node &n, const Edge &e) const {
deba@1020
   854
      if( n == u(e))
deba@1020
   855
        return v(e);
deba@1020
   856
      else if( n == v(e))
deba@1020
   857
        return u(e);
deba@1019
   858
      else
deba@1019
   859
        return INVALID;
deba@1019
   860
    }
deba@1019
   861
deba@1019
   862
    Arc oppositeArc(const Arc &arc) const {
deba@1019
   863
      return Parent::direct(arc, !Parent::direction(arc));
deba@1019
   864
    }
deba@1019
   865
deba@1019
   866
    using Parent::direct;
deba@1019
   867
    Arc direct(const Edge &edge, const Node &node) const {
deba@1020
   868
      return Parent::direct(edge, Parent::redNode(edge) == node);
deba@1019
   869
    }
deba@1019
   870
deba@1025
   871
    RedNode asRedNode(const Node& node) const {
deba@1025
   872
      if (node == INVALID || Parent::blue(node)) {
deba@1025
   873
        return INVALID;
deba@1025
   874
      } else {
deba@1025
   875
        return Parent::asRedNodeUnsafe(node);
deba@1025
   876
      }
deba@1025
   877
    }
deba@1025
   878
deba@1025
   879
    BlueNode asBlueNode(const Node& node) const {
deba@1025
   880
      if (node == INVALID || Parent::red(node)) {
deba@1025
   881
        return INVALID;
deba@1025
   882
      } else {
deba@1025
   883
        return Parent::asBlueNodeUnsafe(node);
deba@1025
   884
      }
deba@1025
   885
    }
deba@1025
   886
deba@1019
   887
    // Alterable extension
deba@1019
   888
deba@1019
   889
    typedef AlterationNotifier<BpGraphExtender, Node> NodeNotifier;
alpar@1092
   890
    typedef AlterationNotifier<BpGraphExtender, RedNode> RedNodeNotifier;
deba@1019
   891
    typedef AlterationNotifier<BpGraphExtender, BlueNode> BlueNodeNotifier;
deba@1019
   892
    typedef AlterationNotifier<BpGraphExtender, Arc> ArcNotifier;
deba@1019
   893
    typedef AlterationNotifier<BpGraphExtender, Edge> EdgeNotifier;
deba@1019
   894
deba@1019
   895
deba@1019
   896
  protected:
deba@1019
   897
deba@1019
   898
    mutable NodeNotifier node_notifier;
deba@1019
   899
    mutable RedNodeNotifier red_node_notifier;
deba@1019
   900
    mutable BlueNodeNotifier blue_node_notifier;
deba@1019
   901
    mutable ArcNotifier arc_notifier;
deba@1019
   902
    mutable EdgeNotifier edge_notifier;
deba@1019
   903
deba@1019
   904
  public:
deba@1019
   905
deba@1019
   906
    NodeNotifier& notifier(Node) const {
deba@1019
   907
      return node_notifier;
deba@1019
   908
    }
deba@1019
   909
deba@1019
   910
    RedNodeNotifier& notifier(RedNode) const {
deba@1019
   911
      return red_node_notifier;
deba@1019
   912
    }
deba@1019
   913
deba@1019
   914
    BlueNodeNotifier& notifier(BlueNode) const {
deba@1019
   915
      return blue_node_notifier;
deba@1019
   916
    }
deba@1019
   917
deba@1019
   918
    ArcNotifier& notifier(Arc) const {
deba@1019
   919
      return arc_notifier;
deba@1019
   920
    }
deba@1019
   921
deba@1019
   922
    EdgeNotifier& notifier(Edge) const {
deba@1019
   923
      return edge_notifier;
deba@1019
   924
    }
deba@1019
   925
deba@1019
   926
deba@1019
   927
deba@1019
   928
    class NodeIt : public Node {
deba@1019
   929
      const BpGraph* _graph;
deba@1019
   930
    public:
deba@1019
   931
deba@1019
   932
      NodeIt() {}
deba@1019
   933
deba@1019
   934
      NodeIt(Invalid i) : Node(i) { }
deba@1019
   935
deba@1019
   936
      explicit NodeIt(const BpGraph& graph) : _graph(&graph) {
deba@1019
   937
        _graph->first(static_cast<Node&>(*this));
deba@1019
   938
      }
deba@1019
   939
deba@1019
   940
      NodeIt(const BpGraph& graph, const Node& node)
deba@1019
   941
        : Node(node), _graph(&graph) {}
deba@1019
   942
deba@1019
   943
      NodeIt& operator++() {
deba@1019
   944
        _graph->next(*this);
deba@1019
   945
        return *this;
deba@1019
   946
      }
deba@1019
   947
deba@1019
   948
    };
deba@1019
   949
ggab90@1130
   950
    LemonRangeWrapper1<NodeIt, BpGraph> nodes() const {
ggab90@1130
   951
      return LemonRangeWrapper1<NodeIt, BpGraph>(*this);
ggab90@1130
   952
    }
ggab90@1130
   953
ggab90@1130
   954
deba@1026
   955
    class RedNodeIt : public RedNode {
deba@1019
   956
      const BpGraph* _graph;
deba@1019
   957
    public:
deba@1019
   958
deba@1026
   959
      RedNodeIt() {}
deba@1019
   960
deba@1026
   961
      RedNodeIt(Invalid i) : RedNode(i) { }
deba@1019
   962
deba@1026
   963
      explicit RedNodeIt(const BpGraph& graph) : _graph(&graph) {
deba@1025
   964
        _graph->first(static_cast<RedNode&>(*this));
deba@1019
   965
      }
deba@1019
   966
deba@1026
   967
      RedNodeIt(const BpGraph& graph, const RedNode& node)
deba@1025
   968
        : RedNode(node), _graph(&graph) {}
deba@1019
   969
deba@1026
   970
      RedNodeIt& operator++() {
deba@1025
   971
        _graph->next(static_cast<RedNode&>(*this));
deba@1019
   972
        return *this;
deba@1019
   973
      }
deba@1019
   974
deba@1019
   975
    };
deba@1019
   976
ggab90@1130
   977
    LemonRangeWrapper1<RedNodeIt, BpGraph> redNodes() const {
ggab90@1130
   978
      return LemonRangeWrapper1<RedNodeIt, BpGraph>(*this);
ggab90@1130
   979
    }
ggab90@1130
   980
ggab90@1130
   981
deba@1026
   982
    class BlueNodeIt : public BlueNode {
deba@1019
   983
      const BpGraph* _graph;
deba@1019
   984
    public:
deba@1019
   985
deba@1026
   986
      BlueNodeIt() {}
deba@1019
   987
deba@1026
   988
      BlueNodeIt(Invalid i) : BlueNode(i) { }
deba@1019
   989
deba@1026
   990
      explicit BlueNodeIt(const BpGraph& graph) : _graph(&graph) {
deba@1025
   991
        _graph->first(static_cast<BlueNode&>(*this));
deba@1019
   992
      }
deba@1019
   993
deba@1026
   994
      BlueNodeIt(const BpGraph& graph, const BlueNode& node)
deba@1025
   995
        : BlueNode(node), _graph(&graph) {}
deba@1019
   996
deba@1026
   997
      BlueNodeIt& operator++() {
deba@1025
   998
        _graph->next(static_cast<BlueNode&>(*this));
deba@1019
   999
        return *this;
deba@1019
  1000
      }
deba@1019
  1001
deba@1019
  1002
    };
deba@1019
  1003
ggab90@1130
  1004
    LemonRangeWrapper1<BlueNodeIt, BpGraph> blueNodes() const {
ggab90@1130
  1005
      return LemonRangeWrapper1<BlueNodeIt, BpGraph>(*this);
ggab90@1130
  1006
    }
ggab90@1130
  1007
ggab90@1130
  1008
deba@1019
  1009
deba@1019
  1010
    class ArcIt : public Arc {
deba@1019
  1011
      const BpGraph* _graph;
deba@1019
  1012
    public:
deba@1019
  1013
deba@1019
  1014
      ArcIt() { }
deba@1019
  1015
deba@1019
  1016
      ArcIt(Invalid i) : Arc(i) { }
deba@1019
  1017
deba@1019
  1018
      explicit ArcIt(const BpGraph& graph) : _graph(&graph) {
deba@1019
  1019
        _graph->first(static_cast<Arc&>(*this));
deba@1019
  1020
      }
deba@1019
  1021
deba@1019
  1022
      ArcIt(const BpGraph& graph, const Arc& arc) :
deba@1019
  1023
        Arc(arc), _graph(&graph) { }
deba@1019
  1024
deba@1019
  1025
      ArcIt& operator++() {
deba@1019
  1026
        _graph->next(*this);
deba@1019
  1027
        return *this;
deba@1019
  1028
      }
deba@1019
  1029
deba@1019
  1030
    };
deba@1019
  1031
ggab90@1130
  1032
    LemonRangeWrapper1<ArcIt, BpGraph> arcs() const {
ggab90@1130
  1033
      return LemonRangeWrapper1<ArcIt, BpGraph>(*this);
ggab90@1130
  1034
    }
ggab90@1130
  1035
deba@1019
  1036
deba@1019
  1037
    class OutArcIt : public Arc {
deba@1019
  1038
      const BpGraph* _graph;
deba@1019
  1039
    public:
deba@1019
  1040
deba@1019
  1041
      OutArcIt() { }
deba@1019
  1042
deba@1019
  1043
      OutArcIt(Invalid i) : Arc(i) { }
deba@1019
  1044
deba@1019
  1045
      OutArcIt(const BpGraph& graph, const Node& node)
deba@1019
  1046
        : _graph(&graph) {
deba@1019
  1047
        _graph->firstOut(*this, node);
deba@1019
  1048
      }
deba@1019
  1049
deba@1019
  1050
      OutArcIt(const BpGraph& graph, const Arc& arc)
deba@1019
  1051
        : Arc(arc), _graph(&graph) {}
deba@1019
  1052
deba@1019
  1053
      OutArcIt& operator++() {
deba@1019
  1054
        _graph->nextOut(*this);
deba@1019
  1055
        return *this;
deba@1019
  1056
      }
deba@1019
  1057
deba@1019
  1058
    };
deba@1019
  1059
ggab90@1130
  1060
    LemonRangeWrapper2<OutArcIt, BpGraph, Node> outArcs(const Node& u) const {
ggab90@1130
  1061
      return LemonRangeWrapper2<OutArcIt, BpGraph, Node>(*this, u);
ggab90@1130
  1062
    }
ggab90@1130
  1063
deba@1019
  1064
deba@1019
  1065
    class InArcIt : public Arc {
deba@1019
  1066
      const BpGraph* _graph;
deba@1019
  1067
    public:
deba@1019
  1068
deba@1019
  1069
      InArcIt() { }
deba@1019
  1070
deba@1019
  1071
      InArcIt(Invalid i) : Arc(i) { }
deba@1019
  1072
deba@1019
  1073
      InArcIt(const BpGraph& graph, const Node& node)
deba@1019
  1074
        : _graph(&graph) {
deba@1019
  1075
        _graph->firstIn(*this, node);
deba@1019
  1076
      }
deba@1019
  1077
deba@1019
  1078
      InArcIt(const BpGraph& graph, const Arc& arc) :
deba@1019
  1079
        Arc(arc), _graph(&graph) {}
deba@1019
  1080
deba@1019
  1081
      InArcIt& operator++() {
deba@1019
  1082
        _graph->nextIn(*this);
deba@1019
  1083
        return *this;
deba@1019
  1084
      }
deba@1019
  1085
deba@1019
  1086
    };
deba@1019
  1087
ggab90@1130
  1088
    LemonRangeWrapper2<InArcIt, BpGraph, Node> inArcs(const Node& u) const {
ggab90@1130
  1089
      return LemonRangeWrapper2<InArcIt, BpGraph, Node>(*this, u);
ggab90@1130
  1090
    }
ggab90@1130
  1091
deba@1019
  1092
deba@1019
  1093
    class EdgeIt : public Parent::Edge {
deba@1019
  1094
      const BpGraph* _graph;
deba@1019
  1095
    public:
deba@1019
  1096
deba@1019
  1097
      EdgeIt() { }
deba@1019
  1098
deba@1019
  1099
      EdgeIt(Invalid i) : Edge(i) { }
deba@1019
  1100
deba@1019
  1101
      explicit EdgeIt(const BpGraph& graph) : _graph(&graph) {
deba@1019
  1102
        _graph->first(static_cast<Edge&>(*this));
deba@1019
  1103
      }
deba@1019
  1104
deba@1019
  1105
      EdgeIt(const BpGraph& graph, const Edge& edge) :
deba@1019
  1106
        Edge(edge), _graph(&graph) { }
deba@1019
  1107
deba@1019
  1108
      EdgeIt& operator++() {
deba@1019
  1109
        _graph->next(*this);
deba@1019
  1110
        return *this;
deba@1019
  1111
      }
deba@1019
  1112
deba@1019
  1113
    };
deba@1019
  1114
ggab90@1130
  1115
    LemonRangeWrapper1<EdgeIt, BpGraph> edges() const {
ggab90@1130
  1116
      return LemonRangeWrapper1<EdgeIt, BpGraph>(*this);
ggab90@1130
  1117
    }
ggab90@1130
  1118
ggab90@1130
  1119
deba@1019
  1120
    class IncEdgeIt : public Parent::Edge {
deba@1019
  1121
      friend class BpGraphExtender;
deba@1019
  1122
      const BpGraph* _graph;
deba@1019
  1123
      bool _direction;
deba@1019
  1124
    public:
deba@1019
  1125
deba@1019
  1126
      IncEdgeIt() { }
deba@1019
  1127
deba@1019
  1128
      IncEdgeIt(Invalid i) : Edge(i), _direction(false) { }
deba@1019
  1129
deba@1019
  1130
      IncEdgeIt(const BpGraph& graph, const Node &node) : _graph(&graph) {
deba@1019
  1131
        _graph->firstInc(*this, _direction, node);
deba@1019
  1132
      }
deba@1019
  1133
deba@1019
  1134
      IncEdgeIt(const BpGraph& graph, const Edge &edge, const Node &node)
deba@1019
  1135
        : _graph(&graph), Edge(edge) {
deba@1019
  1136
        _direction = (_graph->source(edge) == node);
deba@1019
  1137
      }
deba@1019
  1138
deba@1019
  1139
      IncEdgeIt& operator++() {
deba@1019
  1140
        _graph->nextInc(*this, _direction);
deba@1019
  1141
        return *this;
deba@1019
  1142
      }
deba@1019
  1143
    };
deba@1019
  1144
ggab90@1130
  1145
    LemonRangeWrapper2<IncEdgeIt, BpGraph, Node> incEdges(const Node& u) const {
ggab90@1130
  1146
      return LemonRangeWrapper2<IncEdgeIt, BpGraph, Node>(*this, u);
ggab90@1130
  1147
    }
ggab90@1130
  1148
ggab90@1130
  1149
deba@1019
  1150
    // \brief Base node of the iterator
deba@1019
  1151
    //
deba@1019
  1152
    // Returns the base node (ie. the source in this case) of the iterator
deba@1019
  1153
    Node baseNode(const OutArcIt &arc) const {
deba@1019
  1154
      return Parent::source(static_cast<const Arc&>(arc));
deba@1019
  1155
    }
deba@1019
  1156
    // \brief Running node of the iterator
deba@1019
  1157
    //
deba@1019
  1158
    // Returns the running node (ie. the target in this case) of the
deba@1019
  1159
    // iterator
deba@1019
  1160
    Node runningNode(const OutArcIt &arc) const {
deba@1019
  1161
      return Parent::target(static_cast<const Arc&>(arc));
deba@1019
  1162
    }
deba@1019
  1163
deba@1019
  1164
    // \brief Base node of the iterator
deba@1019
  1165
    //
deba@1019
  1166
    // Returns the base node (ie. the target in this case) of the iterator
deba@1019
  1167
    Node baseNode(const InArcIt &arc) const {
deba@1019
  1168
      return Parent::target(static_cast<const Arc&>(arc));
deba@1019
  1169
    }
deba@1019
  1170
    // \brief Running node of the iterator
deba@1019
  1171
    //
deba@1019
  1172
    // Returns the running node (ie. the source in this case) of the
deba@1019
  1173
    // iterator
deba@1019
  1174
    Node runningNode(const InArcIt &arc) const {
deba@1019
  1175
      return Parent::source(static_cast<const Arc&>(arc));
deba@1019
  1176
    }
deba@1019
  1177
deba@1019
  1178
    // Base node of the iterator
deba@1019
  1179
    //
deba@1019
  1180
    // Returns the base node of the iterator
deba@1019
  1181
    Node baseNode(const IncEdgeIt &edge) const {
deba@1019
  1182
      return edge._direction ? this->u(edge) : this->v(edge);
deba@1019
  1183
    }
deba@1019
  1184
    // Running node of the iterator
deba@1019
  1185
    //
deba@1019
  1186
    // Returns the running node of the iterator
deba@1019
  1187
    Node runningNode(const IncEdgeIt &edge) const {
deba@1019
  1188
      return edge._direction ? this->v(edge) : this->u(edge);
deba@1019
  1189
    }
deba@1019
  1190
deba@1019
  1191
    // Mappable extension
deba@1019
  1192
deba@1019
  1193
    template <typename _Value>
deba@1019
  1194
    class NodeMap
deba@1019
  1195
      : public MapExtender<DefaultMap<BpGraph, Node, _Value> > {
deba@1019
  1196
      typedef MapExtender<DefaultMap<BpGraph, Node, _Value> > Parent;
deba@1019
  1197
deba@1019
  1198
    public:
deba@1019
  1199
      explicit NodeMap(const BpGraph& bpgraph)
deba@1019
  1200
        : Parent(bpgraph) {}
deba@1019
  1201
      NodeMap(const BpGraph& bpgraph, const _Value& value)
deba@1019
  1202
        : Parent(bpgraph, value) {}
deba@1019
  1203
deba@1019
  1204
    private:
deba@1019
  1205
      NodeMap& operator=(const NodeMap& cmap) {
deba@1019
  1206
        return operator=<NodeMap>(cmap);
deba@1019
  1207
      }
deba@1019
  1208
deba@1019
  1209
      template <typename CMap>
deba@1019
  1210
      NodeMap& operator=(const CMap& cmap) {
deba@1019
  1211
        Parent::operator=(cmap);
deba@1019
  1212
        return *this;
deba@1019
  1213
      }
deba@1019
  1214
deba@1019
  1215
    };
deba@1019
  1216
deba@1019
  1217
    template <typename _Value>
deba@1026
  1218
    class RedNodeMap
deba@1019
  1219
      : public MapExtender<DefaultMap<BpGraph, RedNode, _Value> > {
deba@1019
  1220
      typedef MapExtender<DefaultMap<BpGraph, RedNode, _Value> > Parent;
deba@1019
  1221
deba@1019
  1222
    public:
deba@1026
  1223
      explicit RedNodeMap(const BpGraph& bpgraph)
deba@1019
  1224
        : Parent(bpgraph) {}
deba@1026
  1225
      RedNodeMap(const BpGraph& bpgraph, const _Value& value)
deba@1019
  1226
        : Parent(bpgraph, value) {}
deba@1019
  1227
deba@1019
  1228
    private:
deba@1026
  1229
      RedNodeMap& operator=(const RedNodeMap& cmap) {
deba@1026
  1230
        return operator=<RedNodeMap>(cmap);
deba@1019
  1231
      }
deba@1019
  1232
deba@1019
  1233
      template <typename CMap>
deba@1026
  1234
      RedNodeMap& operator=(const CMap& cmap) {
deba@1019
  1235
        Parent::operator=(cmap);
deba@1019
  1236
        return *this;
deba@1019
  1237
      }
deba@1019
  1238
deba@1019
  1239
    };
deba@1019
  1240
deba@1019
  1241
    template <typename _Value>
deba@1026
  1242
    class BlueNodeMap
deba@1019
  1243
      : public MapExtender<DefaultMap<BpGraph, BlueNode, _Value> > {
deba@1019
  1244
      typedef MapExtender<DefaultMap<BpGraph, BlueNode, _Value> > Parent;
deba@1019
  1245
deba@1019
  1246
    public:
deba@1026
  1247
      explicit BlueNodeMap(const BpGraph& bpgraph)
deba@1019
  1248
        : Parent(bpgraph) {}
deba@1026
  1249
      BlueNodeMap(const BpGraph& bpgraph, const _Value& value)
deba@1019
  1250
        : Parent(bpgraph, value) {}
deba@1019
  1251
deba@1019
  1252
    private:
deba@1026
  1253
      BlueNodeMap& operator=(const BlueNodeMap& cmap) {
deba@1026
  1254
        return operator=<BlueNodeMap>(cmap);
deba@1019
  1255
      }
deba@1019
  1256
deba@1019
  1257
      template <typename CMap>
deba@1026
  1258
      BlueNodeMap& operator=(const CMap& cmap) {
deba@1019
  1259
        Parent::operator=(cmap);
deba@1019
  1260
        return *this;
deba@1019
  1261
      }
deba@1019
  1262
deba@1019
  1263
    };
deba@1019
  1264
deba@1019
  1265
    template <typename _Value>
deba@1019
  1266
    class ArcMap
deba@1019
  1267
      : public MapExtender<DefaultMap<BpGraph, Arc, _Value> > {
deba@1019
  1268
      typedef MapExtender<DefaultMap<BpGraph, Arc, _Value> > Parent;
deba@1019
  1269
deba@1019
  1270
    public:
deba@1019
  1271
      explicit ArcMap(const BpGraph& graph)
deba@1019
  1272
        : Parent(graph) {}
deba@1019
  1273
      ArcMap(const BpGraph& graph, const _Value& value)
deba@1019
  1274
        : Parent(graph, value) {}
deba@1019
  1275
deba@1019
  1276
    private:
deba@1019
  1277
      ArcMap& operator=(const ArcMap& cmap) {
deba@1019
  1278
        return operator=<ArcMap>(cmap);
deba@1019
  1279
      }
deba@1019
  1280
deba@1019
  1281
      template <typename CMap>
deba@1019
  1282
      ArcMap& operator=(const CMap& cmap) {
deba@1019
  1283
        Parent::operator=(cmap);
deba@1019
  1284
        return *this;
deba@1019
  1285
      }
deba@1019
  1286
    };
deba@1019
  1287
deba@1019
  1288
deba@1019
  1289
    template <typename _Value>
deba@1019
  1290
    class EdgeMap
deba@1019
  1291
      : public MapExtender<DefaultMap<BpGraph, Edge, _Value> > {
deba@1019
  1292
      typedef MapExtender<DefaultMap<BpGraph, Edge, _Value> > Parent;
deba@1019
  1293
deba@1019
  1294
    public:
deba@1019
  1295
      explicit EdgeMap(const BpGraph& graph)
deba@1019
  1296
        : Parent(graph) {}
deba@1019
  1297
deba@1019
  1298
      EdgeMap(const BpGraph& graph, const _Value& value)
deba@1019
  1299
        : Parent(graph, value) {}
deba@1019
  1300
deba@1019
  1301
    private:
deba@1019
  1302
      EdgeMap& operator=(const EdgeMap& cmap) {
deba@1019
  1303
        return operator=<EdgeMap>(cmap);
deba@1019
  1304
      }
deba@1019
  1305
deba@1019
  1306
      template <typename CMap>
deba@1019
  1307
      EdgeMap& operator=(const CMap& cmap) {
deba@1019
  1308
        Parent::operator=(cmap);
deba@1019
  1309
        return *this;
deba@1019
  1310
      }
deba@1019
  1311
deba@1019
  1312
    };
deba@1019
  1313
deba@1019
  1314
    // Alteration extension
deba@1019
  1315
deba@1025
  1316
    RedNode addRedNode() {
deba@1025
  1317
      RedNode node = Parent::addRedNode();
deba@1019
  1318
      notifier(RedNode()).add(node);
deba@1019
  1319
      notifier(Node()).add(node);
deba@1019
  1320
      return node;
deba@1019
  1321
    }
deba@1019
  1322
deba@1025
  1323
    BlueNode addBlueNode() {
deba@1025
  1324
      BlueNode node = Parent::addBlueNode();
deba@1019
  1325
      notifier(BlueNode()).add(node);
deba@1019
  1326
      notifier(Node()).add(node);
deba@1019
  1327
      return node;
deba@1019
  1328
    }
deba@1019
  1329
deba@1025
  1330
    Edge addEdge(const RedNode& from, const BlueNode& to) {
deba@1019
  1331
      Edge edge = Parent::addEdge(from, to);
deba@1019
  1332
      notifier(Edge()).add(edge);
deba@1019
  1333
      std::vector<Arc> av;
deba@1019
  1334
      av.push_back(Parent::direct(edge, true));
deba@1019
  1335
      av.push_back(Parent::direct(edge, false));
deba@1019
  1336
      notifier(Arc()).add(av);
deba@1019
  1337
      return edge;
deba@1019
  1338
    }
deba@1019
  1339
deba@1019
  1340
    void clear() {
deba@1019
  1341
      notifier(Arc()).clear();
deba@1019
  1342
      notifier(Edge()).clear();
deba@1019
  1343
      notifier(Node()).clear();
deba@1019
  1344
      notifier(BlueNode()).clear();
deba@1019
  1345
      notifier(RedNode()).clear();
deba@1019
  1346
      Parent::clear();
deba@1019
  1347
    }
deba@1019
  1348
deba@1019
  1349
    template <typename BpGraph, typename NodeRefMap, typename EdgeRefMap>
deba@1019
  1350
    void build(const BpGraph& graph, NodeRefMap& nodeRef,
deba@1019
  1351
               EdgeRefMap& edgeRef) {
deba@1019
  1352
      Parent::build(graph, nodeRef, edgeRef);
deba@1019
  1353
      notifier(RedNode()).build();
deba@1019
  1354
      notifier(BlueNode()).build();
deba@1019
  1355
      notifier(Node()).build();
deba@1019
  1356
      notifier(Edge()).build();
deba@1019
  1357
      notifier(Arc()).build();
deba@1019
  1358
    }
deba@1019
  1359
deba@1019
  1360
    void erase(const Node& node) {
deba@1019
  1361
      Arc arc;
deba@1019
  1362
      Parent::firstOut(arc, node);
deba@1019
  1363
      while (arc != INVALID ) {
deba@1019
  1364
        erase(arc);
deba@1019
  1365
        Parent::firstOut(arc, node);
deba@1019
  1366
      }
deba@1019
  1367
deba@1019
  1368
      Parent::firstIn(arc, node);
deba@1019
  1369
      while (arc != INVALID ) {
deba@1019
  1370
        erase(arc);
deba@1019
  1371
        Parent::firstIn(arc, node);
deba@1019
  1372
      }
deba@1019
  1373
deba@1019
  1374
      if (Parent::red(node)) {
deba@1025
  1375
        notifier(RedNode()).erase(this->asRedNodeUnsafe(node));
deba@1019
  1376
      } else {
deba@1025
  1377
        notifier(BlueNode()).erase(this->asBlueNodeUnsafe(node));
deba@1019
  1378
      }
deba@1019
  1379
deba@1019
  1380
      notifier(Node()).erase(node);
deba@1019
  1381
      Parent::erase(node);
deba@1019
  1382
    }
deba@1019
  1383
deba@1019
  1384
    void erase(const Edge& edge) {
deba@1019
  1385
      std::vector<Arc> av;
deba@1019
  1386
      av.push_back(Parent::direct(edge, true));
deba@1019
  1387
      av.push_back(Parent::direct(edge, false));
deba@1019
  1388
      notifier(Arc()).erase(av);
deba@1019
  1389
      notifier(Edge()).erase(edge);
deba@1019
  1390
      Parent::erase(edge);
deba@1019
  1391
    }
deba@1019
  1392
deba@1019
  1393
    BpGraphExtender() {
deba@1019
  1394
      red_node_notifier.setContainer(*this);
deba@1019
  1395
      blue_node_notifier.setContainer(*this);
deba@1019
  1396
      node_notifier.setContainer(*this);
deba@1019
  1397
      arc_notifier.setContainer(*this);
deba@1019
  1398
      edge_notifier.setContainer(*this);
deba@1019
  1399
    }
deba@1019
  1400
deba@1019
  1401
    ~BpGraphExtender() {
deba@1019
  1402
      edge_notifier.clear();
deba@1019
  1403
      arc_notifier.clear();
deba@1019
  1404
      node_notifier.clear();
deba@1019
  1405
      blue_node_notifier.clear();
deba@1019
  1406
      red_node_notifier.clear();
deba@1019
  1407
    }
deba@1019
  1408
deba@1019
  1409
  };
deba@1019
  1410
deba@57
  1411
}
deba@57
  1412
deba@57
  1413
#endif