marci@642: // -*- c++ -*-
alpar@921: #ifndef LEMON_SAGE_GRAPH_H
alpar@921: #define LEMON_SAGE_GRAPH_H
marci@642: 
marci@642: #include <iostream>
marci@642: #include <vector>
marci@642: 
alpar@921: #include <lemon/invalid.h>
marci@642: 
alpar@921: namespace lemon {
marci@642: 
alpar@774: //   template <typename It>
alpar@774: //   int count(It it) { 
alpar@774: //     int i=0;
alpar@774: //     for( ; it.valid(); ++it) { ++i; } 
alpar@774: //     return i;
alpar@774: //   }
marci@642: 
marci@642:   class SageGraph {
marci@642:     struct node_item;
marci@642:     struct edge_item;
marci@642:   public:
marci@642:     class Node;
marci@642:     class NodeIt;
marci@642:     class Edge;
marci@642:     class EdgeIt;
marci@642:     class OutEdgeIt;
marci@642:     class InEdgeIt;
marci@642:     class SymEdgeIt;
marci@642:     template <typename T> class NodeMap;
marci@642:     template <typename T> class EdgeMap;
marci@642: //  private:
marci@642:     template <typename T> friend class NodeMap;
marci@642:     template <typename T> friend class EdgeMap;
marci@642:  
marci@642:     template <typename T>
marci@642:     class NodeMap {
marci@642:       const SageGraph& G; 
marci@642:       std::vector<T> container;
marci@642:     public:
alpar@987:       typedef T Value;
alpar@987:       typedef Node Key;
marci@642:       NodeMap(const SageGraph& _G) : G(_G), container(G.node_id) { }
marci@642:       NodeMap(const SageGraph& _G, T a) : 
marci@642: 	G(_G), container(G.node_id, a) { }
marci@642:       void set(Node n, T a) { container[/*G.id(n)*/n.node->id]=a; }
marci@642: //      T get(Node n) const { return container[/*G.id(n)*/n.node->id]; }
marci@642:       typename std::vector<T>::reference operator[](Node n) { 
marci@642: 	return container[/*G.id(n)*/n.node->id]; }
marci@642:       typename std::vector<T>::const_reference operator[](Node n) const { 
marci@642: 	return container[/*G.id(n)*/n.node->id]; 
marci@642:       }
marci@642:       void update() { container.resize(G.node_id); }
marci@642:       void update(T a) { container.resize(G.node_id, a); }
marci@642:     };
marci@642: 
marci@642:     template <typename T>
marci@642:     class EdgeMap {
marci@642:       const SageGraph& G; 
marci@642:       std::vector<T> container;
marci@642:     public:
alpar@987:       typedef T Value;
alpar@987:       typedef Edge Key;
marci@642:       EdgeMap(const SageGraph& _G) : G(_G), container(G.edge_id) { }
marci@642:       EdgeMap(const SageGraph& _G, T a) : 
marci@642: 	G(_G), container(G.edge_id, a) { }
marci@642:       void set(Edge e, T a) { container[/*G.id(e)*/e.edge->id]=a; }
marci@642: //      T get(Edge e) const { return container[/*G.id(e)*/e.edge->id]; }
marci@642:       typename std::vector<T>::reference operator[](Edge e) { 
marci@642: 	return container[/*G.id(e)*/e.edge->id]; } 
marci@642:       typename std::vector<T>::const_reference operator[](Edge e) const { 
marci@642: 	return container[/*G.id(e)*/e.edge->id]; 
marci@642:       } 
marci@642:       void update() { container.resize(G.edge_id); }
marci@642:       void update(T a) { container.resize(G.edge_id, a); }
marci@642:     };
marci@642: 
marci@642:   private:
marci@642:     int node_id;
marci@642:     int edge_id;
marci@642:     int _node_num;
marci@642:     int _edge_num;
marci@642: 
marci@642:     node_item* _first_node;
marci@642:     node_item* _last_node;
marci@642: 
marci@642:     struct node_item {
marci@642:       int id;
marci@642:       edge_item* _first_out_edge;
marci@642:       edge_item* _last_out_edge;
marci@642:       edge_item* _first_in_edge;
marci@642:       edge_item* _last_in_edge;
marci@642:       node_item* _next_node;
marci@642:       node_item* _prev_node;
marci@642:     };
marci@642: 
marci@642:     struct edge_item {
marci@642:       int id;
alpar@986:       node_item* _source;
alpar@986:       node_item* _target;
marci@642:       edge_item* _next_out;
marci@642:       edge_item* _prev_out;
marci@642:       edge_item* _next_in;
marci@642:       edge_item* _prev_in;
marci@642:     };
marci@642: 
marci@642:     node_item* _add_node() { 
marci@642:       node_item* p=new node_item;
marci@642:       p->id=node_id++;
marci@642:       p->_first_out_edge=0;
marci@642:       p->_last_out_edge=0;
marci@642:       p->_first_in_edge=0;
marci@642:       p->_last_in_edge=0;
marci@642:       p->_prev_node=_last_node;
marci@642:       p->_next_node=0;
marci@642:       if (_last_node) _last_node->_next_node=p;
marci@642:       _last_node=p;
marci@642:       if (!_first_node) _first_node=p;
marci@642: 
marci@642:       ++_node_num;
marci@642:       return p;
marci@642:     }
marci@642: 
alpar@986:     edge_item* _add_edge(node_item* _source, node_item* _target) {
marci@642:       edge_item* e=new edge_item;
marci@642:       e->id=edge_id++;
alpar@986:       e->_source=_source;
alpar@986:       e->_target=_target;
marci@642:       
alpar@986:       e->_prev_out=_source->_last_out_edge;
alpar@986:       if (_source->_last_out_edge) (_source->_last_out_edge)->_next_out=e;
alpar@986:       _source->_last_out_edge=e;
alpar@986:       if (!_source->_first_out_edge) _source->_first_out_edge=e; 
marci@642:       e->_next_out=0;
marci@642:  
alpar@986:       e->_prev_in=_target->_last_in_edge;
alpar@986:       if (_target->_last_in_edge) (_target->_last_in_edge)->_next_in=e;
alpar@986:       _target->_last_in_edge=e;
alpar@986:       if (!_target->_first_in_edge) { _target->_first_in_edge=e; } 
marci@642:       e->_next_in=0;
marci@642: 
marci@642:       ++_edge_num;
marci@642:       return e;
marci@642:     }
marci@642: 
marci@642:     //deletes a node which has no out edge and no in edge
marci@642:     void _delete_node(node_item* v) {
marci@642:       if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else 
marci@642: 	_last_node=v->_prev_node;
marci@642:       if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else 
marci@642: 	_first_node=v->_next_node;
marci@642: 
marci@642:       delete v;
marci@642:       --_node_num;
marci@642:     }
marci@642: 
marci@642:     void _delete_edge(edge_item* e) {
marci@642:       if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else 
alpar@986: 	(e->_source)->_last_out_edge=e->_prev_out;
marci@642:       if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else 
alpar@986: 	(e->_source)->_first_out_edge=e->_next_out;
marci@642:       if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else 
alpar@986: 	(e->_target)->_last_in_edge=e->_prev_in;
marci@642:       if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else 
alpar@986: 	(e->_target)->_first_in_edge=e->_next_in;
marci@642: 
marci@642:       delete e;
marci@642:       --_edge_num;
marci@642:     }
marci@642: 
alpar@986:     void _set_source(edge_item* e, node_item* _source) {
marci@642:       if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else 
alpar@986: 	(e->_source)->_last_out_edge=e->_prev_out;
marci@642:       if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else 
alpar@986: 	(e->_source)->_first_out_edge=e->_next_out;
marci@642:       
alpar@986:       e->_source=_source;
marci@642:       
alpar@986:       e->_prev_out=_source->_last_out_edge;
alpar@986:       if (_source->_last_out_edge) (_source->_last_out_edge)->_next_out=e;
alpar@986:       _source->_last_out_edge=e;
alpar@986:       if (!_source->_first_out_edge) _source->_first_out_edge=e; 
marci@642:       e->_next_out=0;
marci@642:     }
marci@642: 
alpar@986:     void _set_target(edge_item* e, node_item* _target) {
marci@642:       if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else 
alpar@986: 	(e->_target)->_last_in_edge=e->_prev_in;
marci@642:       if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else 
alpar@986: 	(e->_target)->_first_in_edge=e->_next_in;
marci@642:       
alpar@986:       e->_target=_target;
marci@642:       
alpar@986:       e->_prev_in=_target->_last_in_edge;
alpar@986:       if (_target->_last_in_edge) (_target->_last_in_edge)->_next_in=e;
alpar@986:       _target->_last_in_edge=e;
alpar@986:       if (!_target->_first_in_edge) { _target->_first_in_edge=e; } 
marci@642:       e->_next_in=0;
marci@642:     }
marci@642: 
marci@642:   public:
marci@642: 
marci@642:     /* default constructor */
marci@642: 
marci@642:     SageGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { }
marci@642:     
marci@642:     ~SageGraph() { 
marci@642:       NodeIt n;
marci@642:       while (this->valid(first(n))) erase(n);
marci@642:       //while (first<NodeIt>().valid()) erase(first<NodeIt>());
marci@642:     }
marci@642: 
marci@642:     int nodeNum() const { return _node_num; }
marci@642:     int edgeNum() const { return _edge_num; }
marci@642: 
marci@642:     /* functions to construct iterators from the graph, or from each other */
marci@642: 
marci@642:     //NodeIt firstNode() const { return NodeIt(*this); }
marci@642:     //EdgeIt firstEdge() const { return EdgeIt(*this); }
marci@642:     
marci@642:     //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); }
marci@642:     //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); }
marci@642:     //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); }
alpar@986:     Node source(Edge e) const { return e.sourceNode(); }
alpar@986:     Node target(Edge e) const { return e.targetNode(); }
marci@642: 
marci@642:     Node aNode(const OutEdgeIt& e) const { return e.aNode(); }
marci@642:     Node aNode(const InEdgeIt& e) const { return e.aNode(); }
marci@642:     Node aNode(const SymEdgeIt& e) const { return e.aNode(); }
marci@642: 
marci@642:     Node bNode(const OutEdgeIt& e) const { return e.bNode(); }
marci@642:     Node bNode(const InEdgeIt& e) const { return e.bNode(); }
marci@642:     Node bNode(const SymEdgeIt& e) const { return e.bNode(); }
marci@642: 
marci@642:     //Node invalid_node() { return Node(); }
marci@642:     //Edge invalid_edge() { return Edge(); }
marci@642:     //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); }
marci@642:     //InEdgeIt invalid_in_edge() { return InEdgeIt(); }
marci@642:     //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); }
marci@642: 
marci@642:     /* same methods in other style */
marci@642:     /* for experimental purpose */
marci@642: 
marci@642:     NodeIt& first(NodeIt& v) const { 
marci@642:       v=NodeIt(*this); return v; }
marci@642:     EdgeIt& first(EdgeIt& e) const { 
marci@642:       e=EdgeIt(*this); return e; }
marci@642:     OutEdgeIt& first(OutEdgeIt& e, Node v) const { 
marci@642:       e=OutEdgeIt(*this, v); return e; }
marci@642:     InEdgeIt& first(InEdgeIt& e, Node v) const { 
marci@642:       e=InEdgeIt(*this, v); return e; }
marci@642:     SymEdgeIt& first(SymEdgeIt& e, Node v) const { 
marci@642:       e=SymEdgeIt(*this, v); return e; }
alpar@986:     //void getSource(Node& n, const Edge& e) const { n=source(e); }
alpar@986:     //void getTarget(Node& n, const Edge& e) const { n=target(e); }
marci@642: 
marci@642:     //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); }
marci@642:     //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); }
marci@642:     //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); }
marci@642:     //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); }
marci@642:     //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); }
marci@642:     //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); }
marci@642:     //void get_invalid(Node& n) { n=Node(); }
marci@642:     //void get_invalid(Edge& e) { e=Edge(); }
marci@642:     //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); }
marci@642:     //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); }
marci@642:     //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); }
marci@642: 
marci@642: //     template< typename It >
marci@642: //     It first() const { 
marci@642: //       It e;
marci@642: //       first(e);
marci@642: //       return e; 
marci@642: //     }
marci@642: 
marci@642: //     template< typename It >
marci@642: //     It first(Node v) const { 
marci@642: //       It e;
marci@642: //       first(e, v);
marci@642: //       return e; 
marci@642: //     }
marci@642: 
marci@642:     bool valid(Node n) const { return n.valid(); }
marci@642:     bool valid(Edge e) const { return e.valid(); }
marci@642:     
marci@642: //    template <typename It> It getNext(It it) const { 
marci@642: //      It tmp(it); next(tmp); return tmp; }
marci@642: //     NodeIt& next(NodeIt& it) const { return ++it; }
marci@642: //     EdgeIt& next(EdgeIt& it) const { return ++it; }
marci@642: //     OutEdgeIt& next(OutEdgeIt& it) const { return ++it; }
marci@642: //     InEdgeIt& next(InEdgeIt& it) const { return ++it; }
marci@642: //     SymEdgeIt& next(SymEdgeIt& it) const { return ++it; }
marci@642: //    template <typename It> It& next(It& it) const { return ++it; }
marci@642:     template <typename It> It& next(It& it) const { ++it; return it; }
marci@642:    
marci@642: 
marci@642:     /* for getting id's of graph objects */
marci@642:     /* these are important for the implementation of property vectors */
marci@642: 
marci@642:     int id(Node v) const { return v.node->id; }
marci@642:     int id(Edge e) const { return e.edge->id; }
marci@642: 
marci@642:     /* adding nodes and edges */
marci@642: 
marci@642:     Node addNode() { return Node(_add_node()); }
marci@642:     Edge addEdge(Node u, Node v) {
marci@642:       return Edge(_add_edge(u.node, v.node)); 
marci@642:     }
marci@642: 
marci@642:     void erase(Node i) { 
marci@642:       { 
marci@642: 	OutEdgeIt e;
marci@642: 	while (this->valid(first(e, i))) erase(e);
marci@642:       }
marci@642:       {
marci@642: 	InEdgeIt e;
marci@642: 	while (this->valid(first(e, i))) erase(e);
marci@642:       }
marci@642:       //while (first<OutEdgeIt>(i).valid()) erase(first<OutEdgeIt>(i));
marci@642:       //while (first<InEdgeIt>(i).valid()) erase(first<InEdgeIt>(i));
marci@642:       _delete_node(i.node); 
marci@642:     }
marci@642:   
marci@642:     void erase(Edge e) { _delete_edge(e.edge); }
marci@642: 
marci@642:     void clear() { 
marci@642:       NodeIt e;
marci@642:       while (this->valid(first(e))) erase(e); 
marci@642:       //while (first<NodeIt>().valid()) erase(first<NodeIt>());
marci@642:     }
marci@642: 
alpar@986:     void setSource(Edge e, Node source) {
alpar@986:       _set_source(e.edge, source.node); 
marci@642:     }
marci@642: 
alpar@986:     void setTarget(Edge e, Node target) {
alpar@986:       _set_target(e.edge, target.node); 
marci@642:     }
marci@642: 
marci@642:     /* stream operations, for testing purpose */
marci@642: 
marci@642: //     friend std::ostream& operator<<(std::ostream& os, const Node& i) { 
marci@642: //       if (i.valid())
marci@642: // 	os << i.node->id; 
marci@642: //       else
marci@642: // 	os << "invalid";
marci@642: //       return os; 
marci@642: //     }
marci@642: //     friend std::ostream& operator<<(std::ostream& os, const Edge& i) { 
marci@642: //       if (i.valid()) 
alpar@986: // 	os << "(" << i.edge->_source->id << "--" << i.edge->id << "->" << i.edge->_target->id << ")"; 
marci@642: //       else 
marci@642: // 	os << "invalid";
marci@642: //       return os; 
marci@642: //     }
marci@642: 
marci@642:     class Node {
marci@642:       friend class SageGraph;
marci@642:       template <typename T> friend class NodeMap;
marci@642: 
marci@642:       friend class Edge;
marci@642:       friend class OutEdgeIt;
marci@642:       friend class InEdgeIt;
marci@642:       friend class SymEdgeIt;
marci@642:       //public:  //FIXME: It is required by op= of NodeIt
marci@642:     protected:
marci@642:       node_item* node;
marci@642:     protected:
marci@642:       friend int SageGraph::id(Node v) const; 
marci@642:     public:
marci@642:       Node() /*: node(0)*/ { }
marci@642:       Node(const Invalid&) : node(0) { }
marci@642:     protected:
marci@642:       Node(node_item* _node) : node(_node) { }
marci@642:       bool valid() const { return (node); }
marci@642:     public:
marci@642:       //void makeInvalid() { node=0; }
marci@642:       friend bool operator==(Node u, Node v) { return v.node==u.node; } 
marci@642:       friend bool operator!=(Node u, Node v) { return v.node!=u.node; } 
marci@642:       friend std::ostream& operator<<(std::ostream& os, const Node& i);
marci@642:     };
marci@642:     
marci@642:     class NodeIt : public Node {
marci@642:       friend class SageGraph;
marci@642:       //protected:
marci@642:     public: //for everybody but marci
marci@642:       NodeIt(const SageGraph& G) : Node(G._first_node) { }
alpar@774:       NodeIt(const SageGraph& G, const Node& n) : Node(n) { } 
marci@642:     public:
marci@642:       NodeIt() : Node() { }
marci@642:       NodeIt(const Invalid& i) : Node(i) { }
marci@642:     protected:
marci@642:       NodeIt(node_item* v) : Node(v) { }
alpar@774:     public:
marci@642:       NodeIt& operator++() { node=node->_next_node; return *this; }
marci@642:       //FIXME::
marci@642:       //      NodeIt& operator=(const Node& e)
marci@642:       //      { node=e.node; return *this; }
marci@642:     };
marci@642: 
marci@642:     class Edge {
marci@642:       friend class SageGraph;
marci@642:       template <typename T> friend class EdgeMap;
marci@642:       
marci@642:       friend class Node;
marci@642:       friend class NodeIt;
marci@642:     protected:
marci@642:       edge_item* edge;
marci@642:       friend int SageGraph::id(Edge e) const;
marci@642:     public:
marci@642:       Edge() /*: edge(0)*/ { }
marci@642:       Edge(const Invalid&) : edge(0) { }
marci@642:       //Edge() { }
marci@642:     protected:
marci@642:       Edge(edge_item* _edge) : edge(_edge) { }
marci@642:       bool valid() const { return (edge); }
marci@642:     public:
marci@642:       //void makeInvalid() { edge=0; }
marci@642:       friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; } 
marci@642:       friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; } 
marci@642:     protected:
alpar@986:       Node sourceNode() const { return Node(edge->_source); }
alpar@986:       Node targetNode() const { return Node(edge->_target); }
marci@642:     public:
marci@642:       friend std::ostream& operator<<(std::ostream& os, const Edge& i);
marci@642:     };
marci@642:     
marci@642:     class EdgeIt : public Edge {
marci@642:       friend class SageGraph;
alpar@774:     public:
alpar@774:       EdgeIt() : Edge() { }
alpar@774:       EdgeIt(const Invalid& i) : Edge(i) { }
marci@642:       EdgeIt(const SageGraph& G) {
marci@642: 	node_item* v=G._first_node;
marci@642: 	if (v) edge=v->_first_out_edge; else edge=0;
marci@642: 	while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
marci@642:       }
alpar@774:       EdgeIt(const SageGraph& G, const Edge& e) : Edge(e) { }
alpar@774: //     protected:
alpar@774: //       EdgeIt(edge_item* _e) : Edge(_e) { }
marci@642:     public:
marci@642:       EdgeIt& operator++() { 
alpar@986: 	node_item* v=edge->_source;
marci@642: 	edge=edge->_next_out; 
marci@642: 	while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; }
marci@642: 	return *this;
marci@642:       }
marci@642:     };
marci@642:     
marci@642:     class OutEdgeIt : public Edge {
marci@642:       friend class SageGraph;
marci@642:     public:
alpar@774:       OutEdgeIt() : Edge() { }
marci@642:       OutEdgeIt(const Invalid& i) : Edge(i) { }
alpar@774:       OutEdgeIt(const SageGraph&, Node _v) : Edge(_v.node->_first_out_edge) { }
alpar@774:       OutEdgeIt(const SageGraph&, const Edge& e) : Edge(e) { }
marci@642:       OutEdgeIt& operator++() { edge=edge->_next_out; return *this; }
marci@642:     protected:
alpar@986:       Node aNode() const { return Node(edge->_source); }
alpar@986:       Node bNode() const { return Node(edge->_target); }
marci@642:     };
marci@642:     
marci@642:     class InEdgeIt : public Edge {
marci@642:       friend class SageGraph;
marci@642:     public:
alpar@774:       InEdgeIt() : Edge() { }
alpar@774:       InEdgeIt(Invalid i) : Edge(i) { }
alpar@774:       InEdgeIt(const SageGraph&, Node _v) : Edge(_v.node->_first_in_edge) { }
alpar@774:       InEdgeIt(const SageGraph&, const Edge& e) : Edge(e) { }
marci@642:       InEdgeIt& operator++() { edge=edge->_next_in; return *this; }
marci@642:     protected:
alpar@986:       Node aNode() const { return Node(edge->_target); }
alpar@986:       Node bNode() const { return Node(edge->_source); }
marci@642:     };
marci@642: 
marci@642:     class SymEdgeIt : public Edge {
marci@642:       friend class SageGraph;
marci@642:       bool out_or_in; //1 iff out, 0 iff in
marci@642:       //node_item* v;
marci@642:       //protected:
marci@642:     protected: //for alpar
marci@642:       SymEdgeIt(const Node& _v) /*: v(_v.node)*/ { 
marci@642: 	out_or_in=1;
marci@642: 	edge=_v.node->_first_out_edge; 
marci@642: 	if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
marci@642:       }
marci@642:     public:
marci@642:       SymEdgeIt() : Edge() /*, v(0)*/ { }
marci@642:       SymEdgeIt(const Invalid& i) : Edge(i) { }
marci@642:       SymEdgeIt(const SageGraph&, Node _v) /*: v(_v.node)*/ { 
marci@642: 	out_or_in=1;
marci@642: 	edge=_v.node->_first_out_edge; 
marci@642: 	if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; }
marci@642:       }
marci@642:     protected:
marci@642:       SymEdgeIt& operator++() { 
marci@642: 	if (out_or_in) { 
alpar@986: 	  node_item* v=edge->_source;
marci@642: 	  edge=edge->_next_out; 
marci@642: 	  if (!edge) { out_or_in=0; edge=v->_first_in_edge; }
marci@642: 	} else {
marci@642: 	  edge=edge->_next_in; 
marci@642: 	}
marci@642: 	return *this;
marci@642:       }
marci@642:     protected:
marci@642:       Node aNode() const { 
alpar@986: 	return (out_or_in) ? Node(edge->_source) : Node(edge->_target); }
marci@642:       Node bNode() const { 
alpar@986: 	return (out_or_in) ? Node(edge->_target) : Node(edge->_source); }
marci@642:     };
marci@642:   };
marci@642: 
marci@642:   inline
marci@642:   std::ostream& operator<<(std::ostream& os, const SageGraph::Node& i) { 
marci@642:     if (i.valid())
marci@642:       os << i.node->id;
marci@642:     else
marci@642:       os << "invalid";
marci@642:     return os; 
marci@642:   }
marci@642: 
marci@642:   inline
marci@642:   std::ostream& operator<<(std::ostream& os, const SageGraph::Edge& i) { 
marci@642:     if (i.valid()) 
alpar@986:       os << "(" << i.sourceNode() << "--" << i.edge->id << "->" 
alpar@986: 	 << i.targetNode() << ")"; 
marci@642:     else 
marci@642:       os << "invalid";
marci@642:     return os; 
marci@642:   }
marci@642: 
marci@642:   class UndirSageGraph : public SageGraph {
marci@642:   public:
marci@642:     typedef SymEdgeIt OutEdgeIt;
marci@642:     typedef SymEdgeIt InEdgeIt;
marci@642:   };
marci@642: 
alpar@921: } //namespace lemon
marci@642: 
alpar@921: #endif //LEMON_SAGE_GRAPH_H