marci@59: #ifndef LIST_GRAPH_HH marci@59: #define LIST_GRAPH_HH marci@46: marci@46: #include alpar@81: #include marci@46: alpar@107: namespace hugo { 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@69: template class NodeMap; marci@69: template class EdgeMap; marci@59: marci@59: private: marci@59: marci@69: template friend class NodeMap; marci@69: template friend class EdgeMap; marci@46: marci@69: template marci@46: class NodeMap { marci@46: const ListGraph& G; marci@69: std::vector container; marci@46: public: marci@69: typedef T ValueType; marci@69: typedef NodeIt KeyType; marci@69: NodeMap(const ListGraph& _G) : G(_G), container(G.node_id) { } marci@69: NodeMap(const ListGraph& _G, T a) : marci@69: G(_G), container(G.node_id, a) { } marci@75: void set(NodeIt n, T a) { container[/*G.id(n)*/n.node->id]=a; } marci@75: T get(NodeIt n) const { return container[/*G.id(n)*/n.node->id]; } marci@75: T& operator[](NodeIt n) { return container[/*G.id(n)*/n.node->id]; } marci@75: const T& operator[](NodeIt n) const { return container[/*G.id(n)*/n.node->id]; } alpar@107: void update() { container.resize(G.node_id); } alpar@107: void update(T a) { container.resize(G.node_id, a); } marci@46: }; marci@46: marci@69: template marci@46: class EdgeMap { marci@46: const ListGraph& G; marci@69: std::vector container; marci@46: public: marci@69: typedef T ValueType; marci@69: typedef EdgeIt KeyType; marci@69: EdgeMap(const ListGraph& _G) : G(_G), container(G.edge_id) { } marci@69: EdgeMap(const ListGraph& _G, T a) : marci@69: G(_G), container(G.edge_id, a) { } marci@75: void set(EdgeIt e, T a) { container[/*G.id(e)*/e.edge->id]=a; } marci@75: T get(EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; } marci@75: T& operator[](EdgeIt e) { return container[/*G.id(e)*/e.edge->id]; } marci@75: const T& operator[](EdgeIt e) const { return container[/*G.id(e)*/e.edge->id]; } alpar@107: void update() { container.resize(G.edge_id); } alpar@107: void update(T a) { container.resize(G.edge_id, a); } 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@75: template friend class NodeMap; marci@75: 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@75: template friend class EdgeMap; marci@75: 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@67: while (first().valid()) erase(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@64: NodeIt tail(EdgeIt e) const { return e.tailNode(); } marci@64: NodeIt head(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@75: EachNodeIt& getFirst(EachNodeIt& v) const { marci@75: v=EachNodeIt(*this); return v; } marci@75: EachEdgeIt& getFirst(EachEdgeIt& e) const { marci@75: e=EachEdgeIt(*this); return e; } marci@75: OutEdgeIt& getFirst(OutEdgeIt& e, NodeIt v) const { marci@75: e=OutEdgeIt(v); return e; } marci@75: InEdgeIt& getFirst(InEdgeIt& e, NodeIt v) const { marci@75: e=InEdgeIt(v); return e; } marci@75: SymEdgeIt& getFirst(SymEdgeIt& e, NodeIt v) const { marci@75: e=SymEdgeIt(v); return e; } marci@64: //void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); } marci@64: //void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); } marci@46: marci@64: //void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); } marci@64: //void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); } marci@64: //void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); } marci@64: //void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); } marci@64: //void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); } marci@64: //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@64: It first(NodeIt v) const { marci@46: It e; marci@46: getFirst(e, v); marci@46: return e; marci@46: } marci@46: marci@148: bool valid(NodeIt n) const { return n.valid(); } marci@86: bool valid(EdgeIt e) const { return e.valid(); } marci@86: marci@139: template It getNext(It it) const { marci@139: It tmp(it); return next(tmp); } marci@139: template It& next(It& it) const { return ++it; } marci@86: marci@86: marci@46: /* for getting id's of graph objects */ marci@46: /* these are important for the implementation of property vectors */ marci@46: marci@64: int id(NodeIt v) const { return v.node->id; } marci@64: int id(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@64: EdgeIt addEdge(NodeIt u, NodeIt v) { marci@46: return EdgeIt(_add_edge(u.node, v.node)); marci@46: } marci@46: marci@67: void erase(NodeIt i) { marci@67: while (first(i).valid()) erase(first(i)); marci@67: while (first(i).valid()) erase(first(i)); marci@46: _delete_node(i.node); marci@46: } marci@46: marci@67: void erase(EdgeIt e) { _delete_edge(e.edge); } marci@46: marci@69: void clear() { marci@69: while (first().valid()) erase(first()); marci@69: } marci@69: marci@64: void setTail(EdgeIt e, NodeIt tail) { marci@46: _set_tail(e.edge, tail.node); marci@46: } marci@46: marci@64: void setHead(EdgeIt e, 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@75: template friend class NodeMap; marci@46: marci@46: friend class EdgeIt; marci@46: friend class OutEdgeIt; marci@46: friend class InEdgeIt; marci@46: friend class SymEdgeIt; marci@134: //public: //FIXME: It is required by op= of EachNodeIt marci@134: protected: alpar@92: node_item* node; marci@46: protected: marci@64: friend int ListGraph::id(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@90: //protected: alpar@92: public: //for everybody but marci 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; } alpar@92: //FIXME:: alpar@94: // EachNodeIt& operator=(const NodeIt& e) alpar@94: // { node=e.node; return *this; } marci@46: }; marci@46: marci@46: class EdgeIt { marci@46: friend class ListGraph; marci@75: template friend class EdgeMap; marci@46: marci@46: friend class NodeIt; marci@46: friend class EachNodeIt; marci@46: protected: marci@46: edge_item* edge; marci@64: friend int ListGraph::id(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@90: //protected: marci@90: public: //for alpar 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@75: //node_item* v; marci@90: //protected: marci@90: public: //for alpar marci@75: OutEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } marci@46: public: marci@75: OutEdgeIt() : EdgeIt()/*, v(0)*/ { } marci@75: OutEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_out_edge; } marci@46: OutEdgeIt& operator++() { edge=edge->_next_out; return *this; } marci@46: protected: marci@75: NodeIt aNode() const { return NodeIt(edge->_tail); } marci@75: NodeIt bNode() const { return NodeIt(edge->_head); } marci@46: }; marci@46: marci@46: class InEdgeIt : public EdgeIt { marci@46: friend class ListGraph; marci@75: //node_item* v; marci@90: //protected: marci@90: public: //for alpar marci@75: InEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } marci@46: public: marci@75: InEdgeIt() : EdgeIt()/*, v(0)*/ { } marci@75: InEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { edge=_v.node->_first_in_edge; } marci@46: InEdgeIt& operator++() { edge=edge->_next_in; return *this; } marci@46: protected: marci@75: NodeIt aNode() const { return NodeIt(edge->_head); } marci@75: NodeIt bNode() const { return 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@75: //node_item* v; marci@90: //protected: marci@90: public: //for alpar marci@75: SymEdgeIt(const NodeIt& _v) /*: v(_v.node)*/ { marci@46: out_or_in=1; marci@75: edge=_v.node->_first_out_edge; marci@75: if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } marci@46: } marci@46: public: marci@75: SymEdgeIt() : EdgeIt() /*, v(0)*/ { } marci@75: SymEdgeIt(const ListGraph& G, NodeIt _v) /*: v(_v.node)*/ { marci@46: out_or_in=1; marci@75: edge=_v.node->_first_out_edge; marci@75: if (!edge) { edge=_v.node->_first_in_edge; out_or_in=0; } marci@46: } marci@46: SymEdgeIt& operator++() { marci@46: if (out_or_in) { marci@75: node_item* v=edge->_tail; 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@75: NodeIt aNode() const { marci@75: return (out_or_in) ? NodeIt(edge->_tail) : NodeIt(edge->_head); } marci@46: NodeIt bNode() const { marci@75: return (out_or_in) ? NodeIt(edge->_head) : NodeIt(edge->_tail); } marci@46: }; marci@46: marci@46: }; marci@46: alpar@107: // template< typename T > alpar@107: // T ListGraph::first() const { alpar@107: // std::cerr << "Invalid use of template T ListGraph::first();" << std::endl; alpar@107: // return T(); alpar@107: // } marci@46: alpar@107: // template<> alpar@107: // ListGraph::EachNodeIt ListGraph::first() const { alpar@107: // return firstNode(); alpar@107: // } marci@46: alpar@107: // template<> alpar@107: // ListGraph::EachEdgeIt ListGraph::first() const { alpar@107: // return firstEdge(); alpar@107: // } marci@46: alpar@107: // template< typename T > alpar@107: // T ListGraph::first(ListGraph::NodeIt v) const { alpar@107: // std::cerr << "Invalid use of template T ListGraph::first(ListGRaph::NodeIt);" << std::endl; alpar@107: // return T(); alpar@107: // } marci@46: alpar@107: // template<> alpar@107: // ListGraph::OutEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { alpar@107: // return firstOutEdge(v); alpar@107: // } marci@46: alpar@107: // template<> alpar@107: // ListGraph::InEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { alpar@107: // return firstInEdge(v); alpar@107: // } marci@46: alpar@107: // template<> alpar@107: // ListGraph::SymEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { alpar@107: // return firstSymEdge(v); alpar@107: // } marci@46: marci@46: alpar@107: } //namespace hugo marci@46: marci@59: #endif //LIST_GRAPH_HH