deba@262: // -*- c++ -*- deba@262: #ifndef HUGO_LIST_GRAPH_H deba@262: #define HUGO_LIST_GRAPH_H deba@262: deba@262: #include deba@262: #include deba@262: deba@262: #include deba@262: deba@340: #include "vector_map.h" deba@340: #include "edge_map_registry.h" deba@340: #include "node_map_registry.h" deba@340: #include "edge_map_base.h" deba@340: #include "node_map_base.h" deba@340: deba@262: namespace hugo { deba@262: deba@262: template deba@262: int count(It it) { deba@262: int i=0; deba@262: for( ; it.valid(); ++it) { ++i; } deba@262: return i; deba@262: } deba@262: deba@262: class ListGraph { deba@262: class node_item; deba@262: class edge_item; deba@262: public: deba@262: class Node; deba@262: class NodeIt; deba@262: class Edge; deba@262: class EdgeIt; deba@262: class OutEdgeIt; deba@262: class InEdgeIt; deba@262: class SymEdgeIt; deba@262: deba@340: // template class NodeMap; deba@340: // template class EdgeMap; deba@262: private: deba@340: // template friend class NodeMap; deba@340: // template friend class EdgeMap; deba@262: deba@340: NodeMapRegistry node_maps; deba@262: deba@262: template deba@340: class NodeMap : public VectorMap {}; deba@340: deba@340: EdgeMapRegistry edge_maps; deba@340: deba@340: template deba@340: class EdgeMap : public VectorMap {}; deba@340: deba@262: deba@262: int node_id; deba@262: int edge_id; deba@262: int _node_num; deba@262: int _edge_num; deba@262: deba@262: node_item* _first_node; deba@262: node_item* _last_node; deba@262: deba@262: class node_item { deba@262: friend class ListGraph; deba@262: template friend class NodeMap; deba@262: deba@262: friend class Node; deba@262: friend class NodeIt; deba@262: friend class Edge; deba@262: friend class EdgeIt; deba@262: friend class OutEdgeIt; deba@262: friend class InEdgeIt; deba@262: friend class SymEdgeIt; deba@262: friend std::ostream& operator<<(std::ostream& os, const Node& i); deba@262: friend std::ostream& operator<<(std::ostream& os, const Edge& i); deba@262: //ListGraph* G; deba@262: int id; deba@262: edge_item* _first_out_edge; deba@262: edge_item* _last_out_edge; deba@262: edge_item* _first_in_edge; deba@262: edge_item* _last_in_edge; deba@262: node_item* _next_node; deba@262: node_item* _prev_node; deba@262: public: deba@262: node_item() { } deba@262: }; deba@262: deba@262: class edge_item { deba@262: friend class ListGraph; deba@262: template friend class EdgeMap; deba@262: deba@262: friend class Node; deba@262: friend class NodeIt; deba@262: friend class Edge; deba@262: friend class EdgeIt; deba@262: friend class OutEdgeIt; deba@262: friend class InEdgeIt; deba@262: friend class SymEdgeIt; deba@262: friend std::ostream& operator<<(std::ostream& os, const Edge& i); deba@262: //ListGraph* G; deba@262: int id; deba@262: node_item* _tail; deba@262: node_item* _head; deba@262: edge_item* _next_out; deba@262: edge_item* _prev_out; deba@262: edge_item* _next_in; deba@262: edge_item* _prev_in; deba@262: public: deba@262: edge_item() { } deba@262: }; deba@262: deba@262: node_item* _add_node() { deba@262: node_item* p=new node_item; deba@262: p->id=node_id++; deba@262: p->_first_out_edge=0; deba@262: p->_last_out_edge=0; deba@262: p->_first_in_edge=0; deba@262: p->_last_in_edge=0; deba@262: p->_prev_node=_last_node; deba@262: p->_next_node=0; deba@262: if (_last_node) _last_node->_next_node=p; deba@262: _last_node=p; deba@262: if (!_first_node) _first_node=p; deba@262: deba@262: ++_node_num; deba@262: return p; deba@262: } deba@262: deba@262: edge_item* _add_edge(node_item* _tail, node_item* _head) { deba@262: edge_item* e=new edge_item; deba@262: e->id=edge_id++; deba@262: e->_tail=_tail; deba@262: e->_head=_head; deba@262: deba@262: e->_prev_out=_tail->_last_out_edge; deba@262: if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; deba@262: _tail->_last_out_edge=e; deba@262: if (!_tail->_first_out_edge) _tail->_first_out_edge=e; deba@262: e->_next_out=0; deba@262: deba@262: e->_prev_in=_head->_last_in_edge; deba@262: if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; deba@262: _head->_last_in_edge=e; deba@262: if (!_head->_first_in_edge) { _head->_first_in_edge=e; } deba@262: e->_next_in=0; deba@262: deba@262: ++_edge_num; deba@262: return e; deba@262: } deba@262: deba@262: //deletes a node which has no out edge and no in edge deba@262: void _delete_node(node_item* v) { deba@262: if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else deba@262: _last_node=v->_prev_node; deba@262: if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else deba@262: _first_node=v->_next_node; deba@262: deba@262: delete v; deba@262: --_node_num; deba@262: } deba@262: deba@262: void _delete_edge(edge_item* e) { deba@262: if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else deba@262: (e->_tail)->_last_out_edge=e->_prev_out; deba@262: if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else deba@262: (e->_tail)->_first_out_edge=e->_next_out; deba@262: if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else deba@262: (e->_head)->_last_in_edge=e->_prev_in; deba@262: if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else deba@262: (e->_head)->_first_in_edge=e->_next_in; deba@262: deba@262: delete e; deba@262: --_edge_num; deba@262: } deba@262: deba@262: void _set_tail(edge_item* e, node_item* _tail) { deba@262: if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else deba@262: (e->_tail)->_last_out_edge=e->_prev_out; deba@262: if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else deba@262: (e->_tail)->_first_out_edge=e->_next_out; deba@262: deba@262: e->_tail=_tail; deba@262: deba@262: e->_prev_out=_tail->_last_out_edge; deba@262: if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; deba@262: _tail->_last_out_edge=e; deba@262: if (!_tail->_first_out_edge) _tail->_first_out_edge=e; deba@262: e->_next_out=0; deba@262: } deba@262: deba@262: void _set_head(edge_item* e, node_item* _head) { deba@262: if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else deba@262: (e->_head)->_last_in_edge=e->_prev_in; deba@262: if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else deba@262: (e->_head)->_first_in_edge=e->_next_in; deba@262: deba@262: e->_head=_head; deba@262: deba@262: e->_prev_in=_head->_last_in_edge; deba@262: if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; deba@262: _head->_last_in_edge=e; deba@262: if (!_head->_first_in_edge) { _head->_first_in_edge=e; } deba@262: e->_next_in=0; deba@262: } deba@262: deba@262: public: deba@262: deba@262: /* default constructor */ deba@262: deba@262: ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { } deba@262: deba@262: ~ListGraph() { deba@262: while (first().valid()) erase(first()); deba@262: } deba@262: deba@262: int nodeNum() const { return _node_num; } deba@262: int edgeNum() const { return _edge_num; } deba@262: deba@262: /* functions to construct iterators from the graph, or from each other */ deba@262: deba@262: //NodeIt firstNode() const { return NodeIt(*this); } deba@262: //EdgeIt firstEdge() const { return EdgeIt(*this); } deba@262: deba@262: //OutEdgeIt firstOutEdge(const Node v) const { return OutEdgeIt(v); } deba@262: //InEdgeIt firstInEdge(const Node v) const { return InEdgeIt(v); } deba@262: //SymEdgeIt firstSymEdge(const Node v) const { return SymEdgeIt(v); } deba@262: Node tail(Edge e) const { return e.tailNode(); } deba@262: Node head(Edge e) const { return e.headNode(); } deba@262: deba@262: Node aNode(const OutEdgeIt& e) const { return e.aNode(); } deba@262: Node aNode(const InEdgeIt& e) const { return e.aNode(); } deba@262: Node aNode(const SymEdgeIt& e) const { return e.aNode(); } deba@262: deba@262: Node bNode(const OutEdgeIt& e) const { return e.bNode(); } deba@262: Node bNode(const InEdgeIt& e) const { return e.bNode(); } deba@262: Node bNode(const SymEdgeIt& e) const { return e.bNode(); } deba@262: deba@262: //Node invalid_node() { return Node(); } deba@262: //Edge invalid_edge() { return Edge(); } deba@262: //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); } deba@262: //InEdgeIt invalid_in_edge() { return InEdgeIt(); } deba@262: //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); } deba@262: deba@262: /* same methods in other style */ deba@262: /* for experimental purpose */ deba@262: deba@262: NodeIt& /*getF*/first(NodeIt& v) const { deba@262: v=NodeIt(*this); return v; } deba@262: EdgeIt& /*getF*/first(EdgeIt& e) const { deba@262: e=EdgeIt(*this); return e; } deba@262: OutEdgeIt& /*getF*/first(OutEdgeIt& e, Node v) const { deba@262: e=OutEdgeIt(*this, v); return e; } deba@262: InEdgeIt& /*getF*/first(InEdgeIt& e, Node v) const { deba@262: e=InEdgeIt(*this, v); return e; } deba@262: SymEdgeIt& /*getF*/first(SymEdgeIt& e, Node v) const { deba@262: e=SymEdgeIt(*this, v); return e; } deba@262: //void getTail(Node& n, const Edge& e) const { n=tail(e); } deba@262: //void getHead(Node& n, const Edge& e) const { n=head(e); } deba@262: deba@262: //void getANode(Node& n, const OutEdgeIt& e) const { n=e.aNode(); } deba@262: //void getANode(Node& n, const InEdgeIt& e) const { n=e.aNode(); } deba@262: //void getANode(Node& n, const SymEdgeIt& e) const { n=e.aNode(); } deba@262: //void getBNode(Node& n, const OutEdgeIt& e) const { n=e.bNode(); } deba@262: //void getBNode(Node& n, const InEdgeIt& e) const { n=e.bNode(); } deba@262: //void getBNode(Node& n, const SymEdgeIt& e) const { n=e.bNode(); } deba@262: //void get_invalid(Node& n) { n=Node(); } deba@262: //void get_invalid(Edge& e) { e=Edge(); } deba@262: //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); } deba@262: //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); } deba@262: //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); } deba@262: deba@262: template< typename It > deba@262: It first() const { deba@262: It e; deba@262: /*getF*/first(e); deba@262: return e; deba@262: } deba@262: deba@262: template< typename It > deba@262: It first(Node v) const { deba@262: It e; deba@262: /*getF*/first(e, v); deba@262: return e; deba@262: } deba@262: deba@262: bool valid(Node n) const { return n.valid(); } deba@262: bool valid(Edge e) const { return e.valid(); } deba@262: deba@262: template It getNext(It it) const { deba@262: It tmp(it); return next(tmp); } deba@262: template It& next(It& it) const { return ++it; } deba@262: deba@262: deba@262: /* for getting id's of graph objects */ deba@262: /* these are important for the implementation of property vectors */ deba@262: deba@262: int id(Node v) const { return v.node->id; } deba@262: int id(Edge e) const { return e.edge->id; } deba@262: deba@262: /* adding nodes and edges */ deba@262: deba@340: Node addNode() { deba@340: Node n = _add_node(); deba@340: node_maps.add(n); deba@340: return n; deba@340: } deba@262: Edge addEdge(Node u, Node v) { deba@340: Edge e = _add_edge(u.node, v.node); deba@340: edge_maps.add(e); deba@340: return e; deba@262: } deba@262: deba@262: void erase(Node i) { deba@340: node_map.erase(i); deba@262: while (first(i).valid()) erase(first(i)); deba@262: while (first(i).valid()) erase(first(i)); deba@262: _delete_node(i.node); deba@262: } deba@262: deba@340: void erase(Edge e) { deba@340: edge_maps.erase(e); deba@340: _delete_edge(e.edge); deba@340: } deba@262: deba@262: void clear() { deba@262: while (first().valid()) erase(first()); deba@262: } deba@262: deba@262: void setTail(Edge e, Node tail) { deba@262: _set_tail(e.edge, tail.node); deba@262: } deba@262: deba@262: void setHead(Edge e, Node head) { deba@262: _set_head(e.edge, head.node); deba@262: } deba@262: deba@262: /* stream operations, for testing purpose */ deba@262: deba@262: friend std::ostream& operator<<(std::ostream& os, const Node& i) { deba@262: os << i.node->id; return os; deba@262: } deba@262: friend std::ostream& operator<<(std::ostream& os, const Edge& i) { deba@262: os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; deba@262: return os; deba@262: } deba@262: deba@262: class Node { deba@262: friend class ListGraph; deba@262: template friend class NodeMap; deba@262: deba@262: friend class Edge; deba@262: friend class OutEdgeIt; deba@262: friend class InEdgeIt; deba@262: friend class SymEdgeIt; deba@262: //public: //FIXME: It is required by op= of NodeIt deba@262: protected: deba@262: node_item* node; deba@262: protected: deba@262: friend int ListGraph::id(Node v) const; deba@262: public: deba@262: Node() /*: node(0)*/ { } deba@262: Node(const Invalid&) : node(0) { } deba@262: protected: deba@262: Node(node_item* _node) : node(_node) { } deba@262: bool valid() const { return (node); } deba@262: public: deba@262: //void makeInvalid() { node=0; } deba@262: friend bool operator==(Node u, Node v) { return v.node==u.node; } deba@262: friend bool operator!=(Node u, Node v) { return v.node!=u.node; } deba@262: friend std::ostream& operator<<(std::ostream& os, const Node& i); deba@262: }; deba@262: deba@262: class NodeIt : public Node { deba@262: friend class ListGraph; deba@262: //protected: deba@262: public: //for everybody but marci deba@262: NodeIt(const ListGraph& G) : Node(G._first_node) { } deba@262: public: deba@262: NodeIt() : Node() { } deba@262: NodeIt(const Invalid& i) : Node(i) { } deba@262: protected: deba@262: NodeIt(node_item* v) : Node(v) { } deba@262: NodeIt& operator++() { node=node->_next_node; return *this; } deba@262: //FIXME:: deba@262: // NodeIt& operator=(const Node& e) deba@262: // { node=e.node; return *this; } deba@262: }; deba@262: deba@262: class Edge { deba@262: friend class ListGraph; deba@262: template friend class EdgeMap; deba@262: deba@262: friend class Node; deba@262: friend class NodeIt; deba@262: protected: deba@262: edge_item* edge; deba@262: friend int ListGraph::id(Edge e) const; deba@262: public: deba@262: Edge() /*: edge(0)*/ { } deba@262: Edge(const Invalid&) : edge(0) { } deba@262: //Edge() { } deba@262: protected: deba@262: Edge(edge_item* _edge) : edge(_edge) { } deba@262: bool valid() const { return (edge); } deba@262: public: deba@262: //void makeInvalid() { edge=0; } deba@262: friend bool operator==(Edge u, Edge v) { return v.edge==u.edge; } deba@262: friend bool operator!=(Edge u, Edge v) { return v.edge!=u.edge; } deba@262: protected: deba@262: Node tailNode() const { return Node(edge->_tail); } deba@262: Node headNode() const { return Node(edge->_head); } deba@262: public: deba@262: friend std::ostream& operator<<(std::ostream& os, const Edge& i); deba@262: }; deba@262: deba@262: class EdgeIt : public Edge { deba@262: friend class ListGraph; deba@262: //protected: deba@262: public: //for alpar deba@262: EdgeIt(const ListGraph& G) { deba@262: node_item* v=G._first_node; deba@262: if (v) edge=v->_first_out_edge; else edge=0; deba@262: while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } deba@262: } deba@262: public: deba@262: EdgeIt() : Edge() { } deba@262: EdgeIt(const Invalid& i) : Edge(i) { } deba@262: protected: deba@262: EdgeIt(edge_item* _e) : Edge(_e) { } deba@262: EdgeIt& operator++() { deba@262: node_item* v=edge->_tail; deba@262: edge=edge->_next_out; deba@262: while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } deba@262: return *this; deba@262: } deba@262: }; deba@262: deba@262: class OutEdgeIt : public Edge { deba@262: friend class ListGraph; deba@262: //node_item* v; deba@262: //protected: deba@262: protected: //for alpar deba@262: OutEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } deba@262: public: deba@262: OutEdgeIt() : Edge()/*, v(0)*/ { } deba@262: OutEdgeIt(const Invalid& i) : Edge(i) { } deba@262: OutEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } deba@262: protected: deba@262: OutEdgeIt& operator++() { edge=edge->_next_out; return *this; } deba@262: protected: deba@262: Node aNode() const { return Node(edge->_tail); } deba@262: Node bNode() const { return Node(edge->_head); } deba@262: }; deba@262: deba@262: class InEdgeIt : public Edge { deba@262: friend class ListGraph; deba@262: //node_item* v; deba@262: //protected: deba@262: protected: //for alpar deba@262: InEdgeIt(const Node& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } deba@262: public: deba@262: InEdgeIt() : Edge()/*, v(0)*/ { } deba@262: InEdgeIt(const Invalid& i) : Edge(i) { } deba@262: InEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } deba@262: protected: deba@262: InEdgeIt& operator++() { edge=edge->_next_in; return *this; } deba@262: protected: deba@262: Node aNode() const { return Node(edge->_head); } deba@262: Node bNode() const { return Node(edge->_tail); } deba@262: }; deba@262: deba@262: class SymEdgeIt : public Edge { deba@262: friend class ListGraph; deba@262: bool out_or_in; //1 iff out, 0 iff in deba@262: //node_item* v; deba@262: //protected: deba@262: public: //for alpar deba@262: SymEdgeIt(const Node& _v) /*: v(_v.node)*/ { deba@262: out_or_in=1; deba@262: edge=_v.node->_first_out_edge; deba@262: if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } deba@262: } deba@262: public: deba@262: SymEdgeIt() : Edge() /*, v(0)*/ { } deba@262: SymEdgeIt(const Invalid& i) : Edge(i) { } deba@262: SymEdgeIt(const ListGraph&, Node _v) /*: v(_v.node)*/ { deba@262: out_or_in=1; deba@262: edge=_v.node->_first_out_edge; deba@262: if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } deba@262: } deba@262: protected: deba@262: SymEdgeIt& operator++() { deba@262: if (out_or_in) { deba@262: node_item* v=edge->_tail; deba@262: edge=edge->_next_out; deba@262: if (!edge) { out_or_in=0; edge=v->_first_in_edge; } deba@262: } else { deba@262: edge=edge->_next_in; deba@262: } deba@262: return *this; deba@262: } deba@262: protected: deba@262: Node aNode() const { deba@262: return (out_or_in) ? Node(edge->_tail) : Node(edge->_head); } deba@262: Node bNode() const { deba@262: return (out_or_in) ? Node(edge->_head) : Node(edge->_tail); } deba@262: }; deba@262: deba@262: }; deba@262: deba@262: // template< typename T > deba@262: // T ListGraph::first() const { deba@262: // std::cerr << "Invalid use of template T ListGraph::first();" << std::endl; deba@262: // return T(); deba@262: // } deba@262: deba@262: // template<> deba@262: // ListGraph::NodeIt ListGraph::first() const { deba@262: // return firstNode(); deba@262: // } deba@262: deba@262: // template<> deba@262: // ListGraph::EdgeIt ListGraph::first() const { deba@262: // return firstEdge(); deba@262: // } deba@262: deba@262: // template< typename T > deba@262: // T ListGraph::first(ListGraph::Node v) const { deba@262: // std::cerr << "Invalid use of template T ListGraph::first(ListGRaph::Node);" << std::endl; deba@262: // return T(); deba@262: // } deba@262: deba@262: // template<> deba@262: // ListGraph::OutEdgeIt ListGraph::first(const ListGraph::Node v) const { deba@262: // return firstOutEdge(v); deba@262: // } deba@262: deba@262: // template<> deba@262: // ListGraph::InEdgeIt ListGraph::first(const ListGraph::Node v) const { deba@262: // return firstInEdge(v); deba@262: // } deba@262: deba@262: // template<> deba@262: // ListGraph::SymEdgeIt ListGraph::first(const ListGraph::Node v) const { deba@262: // return firstSymEdge(v); deba@262: // } deba@262: deba@262: deba@262: } //namespace hugo deba@262: deba@262: #endif //HUGO_LIST_GRAPH_H