COIN-OR::LEMON - Graph Library

source: lemon/lemon/bits/edge_set_extender.h @ 1081:f1398882a928

1.1 r1.1.4
Last change on this file since 1081:f1398882a928 was 1081:f1398882a928, checked in by Alpar Juttner <alpar@…>, 13 years ago

Unify sources

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