# HG changeset patch # User marci # Date 1075474324 0 # Node ID 0f1aacd7867bbb4f33e938a4eb5f66b4cffe135f # Parent 8fe92d6829e8ba611fc8d1e50cebce0763f7d215 marci_list_graph.hh in the new upper-cased concept diff -r 8fe92d6829e8 -r 0f1aacd7867b src/work/list_graph.hh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/list_graph.hh Fri Jan 30 14:52:04 2004 +0000 @@ -0,0 +1,552 @@ +#ifndef MARCI_LIST_GRAPH_HH +#define MARCI_LIST_GRAPH_HH + +#include + +namespace marci { + + template + int number_of(It _it) { + int i=0; + for( ; _it.valid(); ++_it) { ++i; } + return i; + } + + template + int count(It it) { + int i=0; + for( ; it.valid(); ++it) { ++i; } + return i; + } + + class ListGraph { + class node_item; + class edge_item; + public: + class NodeIt; + class EachNodeIt; + class EdgeIt; + class EachEdgeIt; + class OutEdgeIt; + class InEdgeIt; + class SymEdgeIt; + + template + class NodeMap { + //typedef typename Graph::NodeIt NodeIt; + //typedef typename Graph::EachNodeIt EachNodeIt; + const ListGraph& G; + std::vector container; + public: + NodeMap(const ListGraph& _G) : G(_G) { + int i=0; + for(EachNodeIt it=G.template first(); it.valid(); ++it) ++i; + container.resize(i); + } + NodeMap(const ListGraph& _G, const T a) : G(_G) { + for(EachNodeIt it=G.template first(); it.valid(); ++it) { + container.push_back(a); + } + } + void set(const NodeIt nit, const T a) { container[G.id(nit)]=a; } + T get(const NodeIt nit) const { return container[G.id(nit)]; } + }; + + template + class EdgeMap { + //typedef typename Graph::EdgeIt EdgeIt; + //typedef typename Graph::EachEdgeIt EachEdgeIt; + const ListGraph& G; + std::vector container; + public: + EdgeMap(const ListGraph& _G) : G(_G) { + int i=0; + for(EachEdgeIt it=G.template first(); it.valid(); ++it) ++i; + container.resize(i); + } + EdgeMap(const ListGraph& _G, const T a) : G(_G) { + for(EachEdgeIt it=G.template first(); it.valid(); ++it) { + container.push_back(a); + } + } + void set(const EdgeIt eit, const T a) { container[G.id(eit)]=a; } + T get(const EdgeIt eit) const { return container[G.id(eit)]; } + }; + + //typedef template NodePropertyVector NodeMap; + //template + //typedef NodePropertyVector NodeMap; + //template + //typedef typename NodePropertyVector NodeMap; + //template + //typedef NodePropertyVector NodeMap; + //template + //typedef typename NodePropertyVector NodeMap; + //template + //typedef template NodePropertyVector NodeMap; + //template + //typedef template typename NodePropertyVector NodeMap; + //template + //typedef template NodePropertyVector NodeMap; + //template + //typedef template typename NodePropertyVector NodeMap; + //template + //typedef EdgePropertyVector EdgeMap; + + private: + int node_id; + int edge_id; + int _node_num; + int _edge_num; + + node_item* _first_node; + node_item* _last_node; + + class node_item { + friend class ListGraph; + friend class NodeIt; + friend class EachNodeIt; + friend class EdgeIt; + friend class EachEdgeIt; + friend class OutEdgeIt; + friend class InEdgeIt; + friend class SymEdgeIt; + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i); + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); + ListGraph* G; + int id; + edge_item* _first_out_edge; + edge_item* _last_out_edge; + edge_item* _first_in_edge; + edge_item* _last_in_edge; + node_item* _next_node; + node_item* _prev_node; + public: + node_item() { } + }; + + class edge_item { + friend class ListGraph; + friend class NodeIt; + friend class EachNodeIt; + friend class EdgeIt; + friend class EachEdgeIt; + friend class OutEdgeIt; + friend class InEdgeIt; + friend class SymEdgeIt; + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); + ListGraph* G; + int id; + node_item* _tail; + node_item* _head; + edge_item* _next_out; + edge_item* _prev_out; + edge_item* _next_in; + edge_item* _prev_in; + public: + edge_item() { } + }; + + node_item* _add_node() { + node_item* p=new node_item; + p->id=node_id++; + p->_first_out_edge=0; + p->_last_out_edge=0; + p->_first_in_edge=0; + p->_last_in_edge=0; + p->_prev_node=_last_node; + p->_next_node=0; + if (_last_node) _last_node->_next_node=p; + _last_node=p; + if (!_first_node) _first_node=p; + + ++_node_num; + return p; + } + + edge_item* _add_edge(node_item* _tail, node_item* _head) { + edge_item* e=new edge_item; + e->id=edge_id++; + e->_tail=_tail; + e->_head=_head; + + e->_prev_out=_tail->_last_out_edge; + if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; + _tail->_last_out_edge=e; + if (!_tail->_first_out_edge) _tail->_first_out_edge=e; + e->_next_out=0; + + e->_prev_in=_head->_last_in_edge; + if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; + _head->_last_in_edge=e; + if (!_head->_first_in_edge) { _head->_first_in_edge=e; } + e->_next_in=0; + + ++_edge_num; + return e; + } + + //deletes a node which has no out edge and no in edge + void _delete_node(node_item* v) { + if (v->_next_node) (v->_next_node)->_prev_node=v->_prev_node; else + _last_node=v->_prev_node; + if (v->_prev_node) (v->_prev_node)->_next_node=v->_next_node; else + _first_node=v->_next_node; + + delete v; + --_node_num; + } + + void _delete_edge(edge_item* e) { + if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else + (e->_tail)->_last_out_edge=e->_prev_out; + if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else + (e->_tail)->_first_out_edge=e->_next_out; + if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else + (e->_head)->_last_in_edge=e->_prev_in; + if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else + (e->_head)->_first_in_edge=e->_next_in; + + delete e; + --_edge_num; + } + + void _set_tail(edge_item* e, node_item* _tail) { + if (e->_next_out) (e->_next_out)->_prev_out=e->_prev_out; else + (e->_tail)->_last_out_edge=e->_prev_out; + if (e->_prev_out) (e->_prev_out)->_next_out=e->_next_out; else + (e->_tail)->_first_out_edge=e->_next_out; + + e->_tail=_tail; + + e->_prev_out=_tail->_last_out_edge; + if (_tail->_last_out_edge) (_tail->_last_out_edge)->_next_out=e; + _tail->_last_out_edge=e; + if (!_tail->_first_out_edge) _tail->_first_out_edge=e; + e->_next_out=0; + } + + void _set_head(edge_item* e, node_item* _head) { + if (e->_next_in) (e->_next_in)->_prev_in=e->_prev_in; else + (e->_head)->_last_in_edge=e->_prev_in; + if (e->_prev_in) (e->_prev_in)->_next_in=e->_next_in; else + (e->_head)->_first_in_edge=e->_next_in; + + e->_head=_head; + + e->_prev_in=_head->_last_in_edge; + if (_head->_last_in_edge) (_head->_last_in_edge)->_next_in=e; + _head->_last_in_edge=e; + if (!_head->_first_in_edge) { _head->_first_in_edge=e; } + e->_next_in=0; + } + + public: + + /* default constructor */ + + ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0) { } + + ~ListGraph() { + while (first().valid()) deleteNode(first()); + } + + int nodeNum() const { return _node_num; } + int edgeNum() const { return _edge_num; } + + /* functions to construct iterators from the graph, or from each other */ + + EachNodeIt firstNode() const { return EachNodeIt(_first_node); } + EachEdgeIt firstEdge() const { + node_item* v=_first_node; + edge_item* edge=v->_first_out_edge; + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } + return EachEdgeIt(v, edge); + } + + //OutEdgeIt firstOutEdge(const NodeIt v) const { return OutEdgeIt(v); } + InEdgeIt firstInEdge(const NodeIt v) const { return InEdgeIt(v); } + SymEdgeIt firstSymEdge(const NodeIt v) const { return SymEdgeIt(v); } + NodeIt tail(const EdgeIt e) const { return e.tailNode(); } + NodeIt head(const EdgeIt e) const { return e.headNode(); } + + NodeIt aNode(const OutEdgeIt& e) const { return e.aNode(); } + NodeIt aNode(const InEdgeIt& e) const { return e.aNode(); } + NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); } + + NodeIt bNode(const OutEdgeIt& e) const { return e.bNode(); } + NodeIt bNode(const InEdgeIt& e) const { return e.bNode(); } + NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); } + + //NodeIt invalid_node() { return NodeIt(); } + //EdgeIt invalid_edge() { return EdgeIt(); } + //OutEdgeIt invalid_out_edge() { return OutEdgeIt(); } + //InEdgeIt invalid_in_edge() { return InEdgeIt(); } + //SymEdgeIt invalid_sym_edge() { return SymEdgeIt(); } + + /* same methods in other style */ + /* for experimental purpose */ + + void getFirst(EachNodeIt& v) const { v=EachNodeIt(_first_node); } + void getFirst(EachEdgeIt& e) const { e=firstEdge(); } + void getFirst(OutEdgeIt& e, const NodeIt& v) const { e=OutEdgeIt(v); } + void getFirst(InEdgeIt& e, const NodeIt& v) const { e=InEdgeIt(v); } + void getFirst(SymEdgeIt& e, const NodeIt& v) const { e=SymEdgeIt(v); } + void getTail(NodeIt& n, const EdgeIt& e) const { n=tail(e); } + void getHead(NodeIt& n, const EdgeIt& e) const { n=head(e); } + + void getANode(NodeIt& n, const OutEdgeIt& e) const { n=e.aNode(); } + void getANode(NodeIt& n, const InEdgeIt& e) const { n=e.aNode(); } + void getANode(NodeIt& n, const SymEdgeIt& e) const { n=e.aNode(); } + void getBNode(NodeIt& n, const OutEdgeIt& e) const { n=e.bNode(); } + void getBNode(NodeIt& n, const InEdgeIt& e) const { n=e.bNode(); } + void getBNode(NodeIt& n, const SymEdgeIt& e) const { n=e.bNode(); } + //void get_invalid(NodeIt& n) { n=NodeIt(); } + //void get_invalid(EdgeIt& e) { e=EdgeIt(); } + //void get_invalid(OutEdgeIt& e) { e=OutEdgeIt(); } + //void get_invalid(InEdgeIt& e) { e=InEdgeIt(); } + //void get_invalid(SymEdgeIt& e) { e=SymEdgeIt(); } + + template< typename It > + It first() const { + It e; + getFirst(e); + return e; + } + + template< typename It > + It first(const NodeIt v) const { + It e; + getFirst(e, v); + return e; + } + + /* for getting id's of graph objects */ + /* these are important for the implementation of property vectors */ + + int id(const NodeIt v) const { return v.node->id; } + int id(const EdgeIt e) const { return e.edge->id; } + + /* adding nodes and edges */ + + NodeIt addNode() { return NodeIt(_add_node()); } + EdgeIt addEdge(const NodeIt u, const NodeIt v) { + return EdgeIt(_add_edge(u.node, v.node)); + } + + void deleteNode(const NodeIt i) { + while (first(i).valid()) deleteEdge(first(i)); + while (first(i).valid()) deleteEdge(first(i)); + _delete_node(i.node); + } + + void deleteEdge(const EdgeIt e) { _delete_edge(e.edge); } + + void setTail(const EdgeIt e, const NodeIt tail) { + _set_tail(e.edge, tail.node); + } + + void setHead(const EdgeIt e, const NodeIt head) { + _set_head(e.edge, head.node); + } + + /* stream operations, for testing purpose */ + + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i) { + os << i.node->id; return os; + } + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i) { + os << "(" << i.edge->_tail->id << "--" << i.edge->id << "->" << i.edge->_head->id << ")"; + return os; + } + + class NodeIt { + friend class ListGraph; + + friend class EdgeIt; + friend class OutEdgeIt; + friend class InEdgeIt; + friend class SymEdgeIt; + protected: + node_item* node; + friend int ListGraph::id(const NodeIt v) const; + public: + NodeIt() : node(0) { } + NodeIt(node_item* _node) : node(_node) { } + bool valid() const { return (node!=0); } + //void makeInvalid() { node=0; } + friend bool operator==(const NodeIt& u, const NodeIt& v) { + return v.node==u.node; + } + friend bool operator!=(const NodeIt& u, const NodeIt& v) { + return v.node!=u.node; + } + friend std::ostream& operator<<(std::ostream& os, const NodeIt& i); + }; + + class EachNodeIt : public NodeIt { + friend class ListGraph; + public: + EachNodeIt() : NodeIt() { } + EachNodeIt(node_item* v) : NodeIt(v) { } + EachNodeIt(const ListGraph& G) : NodeIt(G._first_node) { } + EachNodeIt& operator++() { node=node->_next_node; return *this; } + }; + + class EdgeIt { + friend class ListGraph; + + friend class NodeIt; + friend class EachNodeIt; + protected: + edge_item* edge; + friend int ListGraph::id(const EdgeIt e) const; + public: + EdgeIt() : edge(0) { } + //EdgeIt() { } + EdgeIt(edge_item* _edge) : edge(_edge) { } + bool valid() const { return (edge!=0); } + //void makeInvalid() { edge=0; } + friend bool operator==(const EdgeIt& u, const EdgeIt& v) { + return v.edge==u.edge; + } + friend bool operator!=(const EdgeIt& u, const EdgeIt& v) { + return v.edge!=u.edge; + } + protected: + NodeIt tailNode() const { return NodeIt(edge->_tail); } + NodeIt headNode() const { return NodeIt(edge->_head); } + public: + friend std::ostream& operator<<(std::ostream& os, const EdgeIt& i); + }; + + class EachEdgeIt : public EdgeIt { + friend class ListGraph; + node_item* v; + public: + EachEdgeIt() : EdgeIt(), v(0) { } + EachEdgeIt(node_item* _v, edge_item* _e) : EdgeIt(_e), v(_v) { } + EachEdgeIt(const ListGraph& G) { + v=G._first_node; + edge=v->_first_out_edge; + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } + } + EachEdgeIt& operator++() { + edge=edge->_next_out; + while (v && !edge) { v=v->_next_node; if (v) edge=v->_first_out_edge; } + return *this; + } + }; + + class OutEdgeIt : public EdgeIt { + friend class ListGraph; + node_item* v; + public: + OutEdgeIt() : EdgeIt(), v(0) { } + protected: + OutEdgeIt(const NodeIt& _v) : v(_v.node) { edge=v->_first_out_edge; } + public: + OutEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) { edge=v->_first_out_edge; } + OutEdgeIt& operator++() { edge=edge->_next_out; return *this; } + protected: + NodeIt aNode() const { return NodeIt(v); } + NodeIt bNode() const { + return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); } + }; + + class InEdgeIt : public EdgeIt { + friend class ListGraph; + node_item* v; + public: + InEdgeIt() : EdgeIt(), v(0) { } + protected: + InEdgeIt(const NodeIt& _v) : v(_v.node) { + edge=v->_first_in_edge; + } + public: + InEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) { edge=v->_first_in_edge; } + InEdgeIt& operator++() { edge=edge->_next_in; return *this; } + protected: + NodeIt aNode() const { return NodeIt(v); } + NodeIt bNode() const { + return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); } + }; + + class SymEdgeIt : public EdgeIt { + friend class ListGraph; + bool out_or_in; //1 iff out, 0 iff in + node_item* v; + public: + SymEdgeIt() : EdgeIt(), v(0) { } + protected: + SymEdgeIt(const NodeIt& _v) : v(_v.node) { + out_or_in=1; + edge=v->_first_out_edge; + if (!edge) { edge=v->_first_in_edge; out_or_in=0; } + } + public: + SymEdgeIt(const ListGraph& G, const NodeIt _v) : v(_v.node) { + out_or_in=1; + edge=v->_first_out_edge; + if (!edge) { edge=v->_first_in_edge; out_or_in=0; } + } + SymEdgeIt& operator++() { + if (out_or_in) { + edge=edge->_next_out; + if (!edge) { out_or_in=0; edge=v->_first_in_edge; } + } else { + edge=edge->_next_in; + } + return *this; + } + protected: + NodeIt aNode() const { return NodeIt(v); } + NodeIt bNode() const { + return (edge->_tail==v) ? NodeIt(edge->_head) : NodeIt(edge->_tail); } + }; + + }; + + /* + template< typename T > + T ListGraph::first() const { + std::cerr << "Invalid use of template T ListGraph::first();" << std::endl; + return T(); + } + + template<> + ListGraph::EachNodeIt ListGraph::first() const { + return firstNode(); + } + + template<> + ListGraph::EachEdgeIt ListGraph::first() const { + return firstEdge(); + } + + template< typename T > + T ListGraph::first(ListGraph::NodeIt v) const { + std::cerr << "Invalid use of template T ListGraph::first(ListGRaph::NodeIt);" << std::endl; + return T(); + } + + template<> + ListGraph::OutEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { + return firstOutEdge(v); + } + + template<> + ListGraph::InEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { + return firstInEdge(v); + } + + template<> + ListGraph::SymEdgeIt ListGraph::first(const ListGraph::NodeIt v) const { + return firstSymEdge(v); + } + */ + + +} //namespace marci + +#endif //MARCI_LIST_GRAPH_HH