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