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