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