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