* This file is a part of LEMON, a generic C++ optimization library
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
#ifndef LEMON_BITS_EDGE_SET_EXTENDER_H
#define LEMON_BITS_EDGE_SET_EXTENDER_H
#include <lemon/bits/default_map.h>
///\brief Extenders for the arc set types
/// \brief Extender for the ArcSets
class ArcSetExtender : public Base {
typedef ArcSetExtender Digraph;
typedef typename Parent::Node Node;
typedef typename Parent::Arc Arc;
return Parent::maxNodeId();
return Parent::maxArcId();
Node fromId(int id, Node) const {
return Parent::nodeFromId(id);
Arc fromId(int id, Arc) const {
return Parent::arcFromId(id);
Node oppositeNode(const Node &n, const Arc &e) const {
if (n == Parent::source(e))
return Parent::target(e);
else if(n==Parent::target(e))
return Parent::source(e);
// Alteration notifier extensions
/// The arc observer registry.
typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier;
mutable ArcNotifier arc_notifier;
/// \brief Gives back the arc alteration notifier.
/// Gives back the arc alteration notifier.
ArcNotifier& notifier(Arc) const {
class NodeIt : public Node {
NodeIt(Invalid i) : Node(i) { }
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
_graph.first(static_cast<Node&>(*this));
NodeIt(const Digraph& _graph, const Node& node)
: Node(node), digraph(&_graph) {}
class ArcIt : public Arc {
ArcIt(Invalid i) : Arc(i) { }
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
_graph.first(static_cast<Arc&>(*this));
ArcIt(const Digraph& _graph, const Arc& e) :
Arc(e), digraph(&_graph) { }
class OutArcIt : public Arc {
OutArcIt(Invalid i) : Arc(i) { }
OutArcIt(const Digraph& _graph, const Node& node)
_graph.firstOut(*this, node);
OutArcIt(const Digraph& _graph, const Arc& arc)
: Arc(arc), digraph(&_graph) {}
class InArcIt : public Arc {
InArcIt(Invalid i) : Arc(i) { }
InArcIt(const Digraph& _graph, const Node& node)
_graph.firstIn(*this, node);
InArcIt(const Digraph& _graph, const Arc& arc) :
Arc(arc), digraph(&_graph) {}
/// \brief Base node of the iterator
/// Returns the base node (ie. the source in this case) of the iterator
Node baseNode(const OutArcIt &e) const {
return Parent::source(static_cast<const Arc&>(e));
/// \brief Running node of the iterator
/// Returns the running node (ie. the target in this case) of the
Node runningNode(const OutArcIt &e) const {
return Parent::target(static_cast<const Arc&>(e));
/// \brief Base node of the iterator
/// Returns the base node (ie. the target in this case) of the iterator
Node baseNode(const InArcIt &e) const {
return Parent::target(static_cast<const Arc&>(e));
/// \brief Running node of the iterator
/// Returns the running node (ie. the source in this case) of the
Node runningNode(const InArcIt &e) const {
return Parent::source(static_cast<const Arc&>(e));
template <typename _Value>
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
typedef ArcSetExtender Digraph;
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
explicit ArcMap(const Digraph& _g)
ArcMap(const Digraph& _g, const _Value& _v)
ArcMap& operator=(const ArcMap& cmap) {
return operator=<ArcMap>(cmap);
ArcMap& operator=(const CMap& cmap) {
Arc addArc(const Node& from, const Node& to) {
Arc arc = Parent::addArc(from, to);
notifier(Arc()).add(arc);
void erase(const Arc& arc) {
notifier(Arc()).erase(arc);
arc_notifier.setContainer(*this);
/// \brief Extender for the EdgeSets
class EdgeSetExtender : public Base {
typedef EdgeSetExtender Digraph;
typedef typename Parent::Node Node;
typedef typename Parent::Arc Arc;
typedef typename Parent::Edge Edge;
return Parent::maxNodeId();
return Parent::maxArcId();
return Parent::maxEdgeId();
Node fromId(int id, Node) const {
return Parent::nodeFromId(id);
Arc fromId(int id, Arc) const {
return Parent::arcFromId(id);
Edge fromId(int id, Edge) const {
return Parent::edgeFromId(id);
Node oppositeNode(const Node &n, const Edge &e) const {
else if( n == Parent::v(e))
Arc oppositeArc(const Arc &e) const {
return Parent::direct(e, !Parent::direction(e));
Arc direct(const Edge &e, const Node &s) const {
return Parent::direct(e, Parent::u(e) == s);
typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier;
typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier;
mutable ArcNotifier arc_notifier;
mutable EdgeNotifier edge_notifier;
ArcNotifier& notifier(Arc) const {
EdgeNotifier& notifier(Edge) const {
class NodeIt : public Node {
NodeIt(Invalid i) : Node(i) { }
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
_graph.first(static_cast<Node&>(*this));
NodeIt(const Digraph& _graph, const Node& node)
: Node(node), digraph(&_graph) {}
class ArcIt : public Arc {
ArcIt(Invalid i) : Arc(i) { }
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
_graph.first(static_cast<Arc&>(*this));
ArcIt(const Digraph& _graph, const Arc& e) :
Arc(e), digraph(&_graph) { }
class OutArcIt : public Arc {
OutArcIt(Invalid i) : Arc(i) { }
OutArcIt(const Digraph& _graph, const Node& node)
_graph.firstOut(*this, node);
OutArcIt(const Digraph& _graph, const Arc& arc)
: Arc(arc), digraph(&_graph) {}
class InArcIt : public Arc {
InArcIt(Invalid i) : Arc(i) { }
InArcIt(const Digraph& _graph, const Node& node)
_graph.firstIn(*this, node);
InArcIt(const Digraph& _graph, const Arc& arc) :
Arc(arc), digraph(&_graph) {}
class EdgeIt : public Parent::Edge {
EdgeIt(Invalid i) : Edge(i) { }
explicit EdgeIt(const Digraph& _graph) : digraph(&_graph) {
_graph.first(static_cast<Edge&>(*this));
EdgeIt(const Digraph& _graph, const Edge& e) :
Edge(e), digraph(&_graph) { }
class IncEdgeIt : public Parent::Edge {
friend class EdgeSetExtender;
IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
IncEdgeIt(const Digraph& _graph, const Node &n) : digraph(&_graph) {
_graph.firstInc(*this, direction, n);
IncEdgeIt(const Digraph& _graph, const Edge &ue, const Node &n)
: digraph(&_graph), Edge(ue) {
direction = (_graph.source(ue) == n);
IncEdgeIt& operator++() {
digraph->nextInc(*this, direction);
/// \brief Base node of the iterator
/// Returns the base node (ie. the source in this case) of the iterator
Node baseNode(const OutArcIt &e) const {
return Parent::source(static_cast<const Arc&>(e));
/// \brief Running node of the iterator
/// Returns the running node (ie. the target in this case) of the
Node runningNode(const OutArcIt &e) const {
return Parent::target(static_cast<const Arc&>(e));
/// \brief Base node of the iterator
/// Returns the base node (ie. the target in this case) of the iterator
Node baseNode(const InArcIt &e) const {
return Parent::target(static_cast<const Arc&>(e));
/// \brief Running node of the iterator
/// Returns the running node (ie. the source in this case) of the
Node runningNode(const InArcIt &e) const {
return Parent::source(static_cast<const Arc&>(e));
/// Base node of the iterator
/// Returns the base node of the iterator
Node baseNode(const IncEdgeIt &e) const {
return e.direction ? u(e) : v(e);
/// Running node of the iterator
/// Returns the running node of the iterator
Node runningNode(const IncEdgeIt &e) const {
return e.direction ? v(e) : u(e);
template <typename _Value>
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
typedef EdgeSetExtender Digraph;
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent;
ArcMap(const Digraph& _g)
ArcMap(const Digraph& _g, const _Value& _v)
ArcMap& operator=(const ArcMap& cmap) {
return operator=<ArcMap>(cmap);
ArcMap& operator=(const CMap& cmap) {
template <typename _Value>
: public MapExtender<DefaultMap<Digraph, Edge, _Value> > {
typedef EdgeSetExtender Digraph;
typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent;
EdgeMap(const Digraph& _g)
EdgeMap(const Digraph& _g, const _Value& _v)
EdgeMap& operator=(const EdgeMap& cmap) {
return operator=<EdgeMap>(cmap);
EdgeMap& operator=(const CMap& cmap) {
Edge addEdge(const Node& from, const Node& to) {
Edge edge = Parent::addEdge(from, to);
notifier(Edge()).add(edge);
arcs.push_back(Parent::direct(edge, true));
arcs.push_back(Parent::direct(edge, false));
notifier(Arc()).add(arcs);
notifier(Edge()).clear();
void erase(const Edge& edge) {
arcs.push_back(Parent::direct(edge, true));
arcs.push_back(Parent::direct(edge, false));
notifier(Arc()).erase(arcs);
notifier(Edge()).erase(edge);
arc_notifier.setContainer(*this);
edge_notifier.setContainer(*this);