3  * This file is a part of LEMON, a generic C++ optimization library
 
     5  * Copyright (C) 2003-2006
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     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.
 
    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
 
    19 #ifndef LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
 
    20 #define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H
 
    22 #include <lemon/bits/invalid.h>
 
    23 #include <lemon/error.h>
 
    25 #include <lemon/bits/default_map.h>
 
    30 ///\brief Extenders for the graph adaptor types
 
    33   /// \ingroup graphbits
 
    35   /// \brief Extender for the GraphAdaptors
 
    36   template <typename Base>
 
    37   class GraphAdaptorExtender : public Base {
 
    41     typedef GraphAdaptorExtender Graph;
 
    45     typedef typename Parent::Node Node;
 
    46     typedef typename Parent::Edge Edge;
 
    48     int maxId(Node) const {
 
    49       return Parent::maxNodeId();
 
    52     int maxId(Edge) const {
 
    53       return Parent::maxEdgeId();
 
    56     Node fromId(int id, Node) const {
 
    57       return Parent::nodeFromId(id);
 
    60     Edge fromId(int id, Edge) const {
 
    61       return Parent::edgeFromId(id);
 
    64     Node oppositeNode(const Node &n, const Edge &e) const {
 
    65       if (n == Parent::source(e))
 
    66 	return Parent::target(e);
 
    67       else if(n==Parent::target(e))
 
    68 	return Parent::source(e);
 
    73     class NodeIt : public Node { 
 
    79       NodeIt(Invalid i) : Node(i) { }
 
    81       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
 
    82 	_graph.first(*static_cast<Node*>(this));
 
    85       NodeIt(const Graph& _graph, const Node& node) 
 
    86 	: Node(node), graph(&_graph) {}
 
    88       NodeIt& operator++() { 
 
    96     class EdgeIt : public Edge { 
 
   102       EdgeIt(Invalid i) : Edge(i) { }
 
   104       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
 
   105 	_graph.first(*static_cast<Edge*>(this));
 
   108       EdgeIt(const Graph& _graph, const Edge& e) : 
 
   109 	Edge(e), graph(&_graph) { }
 
   111       EdgeIt& operator++() { 
 
   119     class OutEdgeIt : public Edge { 
 
   125       OutEdgeIt(Invalid i) : Edge(i) { }
 
   127       OutEdgeIt(const Graph& _graph, const Node& node) 
 
   129 	_graph.firstOut(*this, node);
 
   132       OutEdgeIt(const Graph& _graph, const Edge& edge) 
 
   133 	: Edge(edge), graph(&_graph) {}
 
   135       OutEdgeIt& operator++() { 
 
   136 	graph->nextOut(*this);
 
   143     class InEdgeIt : public Edge { 
 
   149       InEdgeIt(Invalid i) : Edge(i) { }
 
   151       InEdgeIt(const Graph& _graph, const Node& node) 
 
   153 	_graph.firstIn(*this, node);
 
   156       InEdgeIt(const Graph& _graph, const Edge& edge) : 
 
   157 	Edge(edge), graph(&_graph) {}
 
   159       InEdgeIt& operator++() { 
 
   160 	graph->nextIn(*this);
 
   166     /// \brief Base node of the iterator
 
   168     /// Returns the base node (ie. the source in this case) of the iterator
 
   169     Node baseNode(const OutEdgeIt &e) const {
 
   170       return Parent::source(e);
 
   172     /// \brief Running node of the iterator
 
   174     /// Returns the running node (ie. the target in this case) of the
 
   176     Node runningNode(const OutEdgeIt &e) const {
 
   177       return Parent::target(e);
 
   180     /// \brief Base node of the iterator
 
   182     /// Returns the base node (ie. the target in this case) of the iterator
 
   183     Node baseNode(const InEdgeIt &e) const {
 
   184       return Parent::target(e);
 
   186     /// \brief Running node of the iterator
 
   188     /// Returns the running node (ie. the source in this case) of the
 
   190     Node runningNode(const InEdgeIt &e) const {
 
   191       return Parent::source(e);
 
   197   /// \ingroup graphbits
 
   199   /// \brief Extender for the UGraphAdaptors
 
   200   template <typename Base> 
 
   201   class UGraphAdaptorExtender : public Base {
 
   205     typedef UGraphAdaptorExtender Graph;
 
   207     typedef typename Parent::Node Node;
 
   208     typedef typename Parent::Edge Edge;
 
   209     typedef typename Parent::UEdge UEdge;
 
   213     int maxId(Node) const {
 
   214       return Parent::maxNodeId();
 
   217     int maxId(Edge) const {
 
   218       return Parent::maxEdgeId();
 
   221     int maxId(UEdge) const {
 
   222       return Parent::maxUEdgeId();
 
   225     Node fromId(int id, Node) const {
 
   226       return Parent::nodeFromId(id);
 
   229     Edge fromId(int id, Edge) const {
 
   230       return Parent::edgeFromId(id);
 
   233     UEdge fromId(int id, UEdge) const {
 
   234       return Parent::uEdgeFromId(id);
 
   237     Node oppositeNode(const Node &n, const UEdge &e) const {
 
   238       if( n == Parent::source(e))
 
   239 	return Parent::target(e);
 
   240       else if( n == Parent::target(e))
 
   241 	return Parent::source(e);
 
   246     Edge oppositeEdge(const Edge &e) const {
 
   247       return Parent::direct(e, !Parent::direction(e));
 
   250     using Parent::direct;
 
   251     Edge direct(const UEdge &ue, const Node &s) const {
 
   252       return Parent::direct(ue, Parent::source(ue) == s);
 
   256     class NodeIt : public Node { 
 
   262       NodeIt(Invalid i) : Node(i) { }
 
   264       explicit NodeIt(const Graph& _graph) : graph(&_graph) {
 
   265 	_graph.first(*static_cast<Node*>(this));
 
   268       NodeIt(const Graph& _graph, const Node& node) 
 
   269 	: Node(node), graph(&_graph) {}
 
   271       NodeIt& operator++() { 
 
   279     class EdgeIt : public Edge { 
 
   285       EdgeIt(Invalid i) : Edge(i) { }
 
   287       explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
 
   288 	_graph.first(*static_cast<Edge*>(this));
 
   291       EdgeIt(const Graph& _graph, const Edge& e) : 
 
   292 	Edge(e), graph(&_graph) { }
 
   294       EdgeIt& operator++() { 
 
   302     class OutEdgeIt : public Edge { 
 
   308       OutEdgeIt(Invalid i) : Edge(i) { }
 
   310       OutEdgeIt(const Graph& _graph, const Node& node) 
 
   312 	_graph.firstOut(*this, node);
 
   315       OutEdgeIt(const Graph& _graph, const Edge& edge) 
 
   316 	: Edge(edge), graph(&_graph) {}
 
   318       OutEdgeIt& operator++() { 
 
   319 	graph->nextOut(*this);
 
   326     class InEdgeIt : public Edge { 
 
   332       InEdgeIt(Invalid i) : Edge(i) { }
 
   334       InEdgeIt(const Graph& _graph, const Node& node) 
 
   336 	_graph.firstIn(*this, node);
 
   339       InEdgeIt(const Graph& _graph, const Edge& edge) : 
 
   340 	Edge(edge), graph(&_graph) {}
 
   342       InEdgeIt& operator++() { 
 
   343 	graph->nextIn(*this);
 
   349     class UEdgeIt : public Parent::UEdge { 
 
   355       UEdgeIt(Invalid i) : UEdge(i) { }
 
   357       explicit UEdgeIt(const Graph& _graph) : graph(&_graph) {
 
   358 	_graph.first(*static_cast<UEdge*>(this));
 
   361       UEdgeIt(const Graph& _graph, const UEdge& e) : 
 
   362 	UEdge(e), graph(&_graph) { }
 
   364       UEdgeIt& operator++() { 
 
   371     class IncEdgeIt : public Parent::UEdge { 
 
   372       friend class UGraphAdaptorExtender;
 
   379       IncEdgeIt(Invalid i) : UEdge(i), direction(false) { }
 
   381       IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
 
   382 	_graph.firstInc(static_cast<UEdge&>(*this), direction, n);
 
   385       IncEdgeIt(const Graph& _graph, const UEdge &ue, const Node &n)
 
   386 	: graph(&_graph), UEdge(ue) {
 
   387 	direction = (_graph.source(ue) == n);
 
   390       IncEdgeIt& operator++() {
 
   391 	graph->nextInc(*this, direction);
 
   396     /// \brief Base node of the iterator
 
   398     /// Returns the base node (ie. the source in this case) of the iterator
 
   399     Node baseNode(const OutEdgeIt &e) const {
 
   400       return Parent::source((Edge)e);
 
   402     /// \brief Running node of the iterator
 
   404     /// Returns the running node (ie. the target in this case) of the
 
   406     Node runningNode(const OutEdgeIt &e) const {
 
   407       return Parent::target((Edge)e);
 
   410     /// \brief Base node of the iterator
 
   412     /// Returns the base node (ie. the target in this case) of the iterator
 
   413     Node baseNode(const InEdgeIt &e) const {
 
   414       return Parent::target((Edge)e);
 
   416     /// \brief Running node of the iterator
 
   418     /// Returns the running node (ie. the source in this case) of the
 
   420     Node runningNode(const InEdgeIt &e) const {
 
   421       return Parent::source((Edge)e);
 
   424     /// Base node of the iterator
 
   426     /// Returns the base node of the iterator
 
   427     Node baseNode(const IncEdgeIt &e) const {
 
   428       return e.direction ? source(e) : target(e);
 
   430     /// Running node of the iterator
 
   432     /// Returns the running node of the iterator
 
   433     Node runningNode(const IncEdgeIt &e) const {
 
   434       return e.direction ? target(e) : source(e);