src/work/alpar/smart_graph.h
author alpar
Fri, 20 Feb 2004 21:59:34 +0000
changeset 107 8d62f0072ff0
parent 104 7a2d991e9852
child 108 0351b00fd283
permissions -rw-r--r--
marci -> hugo
resize -> update
     1 // -*- mode:C++ -*-
     2 
     3 #ifndef SMART_GRAPH_H
     4 #define SMART_GRAPH_H
     5 
     6 #include <iostream>
     7 #include <vector>
     8 
     9 namespace hugo {
    10 
    11   class SmartGraph {
    12 
    13     static const int INVALID=-1;
    14 
    15     struct NodeT 
    16     {
    17       int first_in,first_out;      
    18       NodeT() : first_in(INVALID), first_out(INVALID) {}
    19     };
    20     struct EdgeT 
    21     {
    22       int head, tail, next_in, next_out;      
    23       //FIXME: is this necessary?
    24       EdgeT() : next_in(INVALID), next_out(INVALID) {}  
    25     };
    26 
    27     std::vector<NodeT> nodes;
    28     std::vector<EdgeT> edges;
    29     
    30 
    31   public:
    32 
    33     class NodeIt;
    34     class EachNodeIt;
    35     class EdgeIt;
    36     class EachEdgeIt;
    37     class OutEdgeIt;
    38     class InEdgeIt;
    39     
    40     //      class NodeIt { int n; };
    41     //     class EachNodeIt : public NodeIt { };
    42     //     class EdgeIt { int n; };
    43     //     class EachEdgeIt : public EdgeIt {};
    44     //     class OutEdgeIt : public EdgeIt {};
    45     //     class InEdgeIt : public EdgeIt {};
    46     //    class SymEdgeIt;
    47     
    48     template <typename T> class NodeMap;
    49     template <typename T> class EdgeMap;
    50     
    51   public:
    52 
    53     /* default constructor */
    54 
    55     SmartGraph() : nodes(), edges() { }
    56     
    57     ~SmartGraph() {}
    58 
    59     int nodeNum() const { return nodes.size(); }  //FIXME: What is this?
    60     int edgeNum() const { return edges.size(); }  //FIXME: What is this?
    61 
    62     NodeIt tail(EdgeIt e) const { return edges[e.n].tail; }
    63     NodeIt head(EdgeIt e) const { return edges[e.n].head; }
    64 
    65     NodeIt aNode(const OutEdgeIt& e) const { return tail(e); }
    66     NodeIt aNode(const InEdgeIt& e) const { return head(e); }
    67     //NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); }
    68 
    69     NodeIt bNode(const OutEdgeIt& e) const { return head(e); }
    70     NodeIt bNode(const InEdgeIt& e) const { return tail(e); }
    71     //NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); }
    72 
    73     EachNodeIt& getFirst(EachNodeIt& v) const { 
    74       v=EachNodeIt(*this); return v; }
    75     EachEdgeIt& getFirst(EachEdgeIt& e) const { 
    76       e=EachEdgeIt(*this); return e; }
    77     OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt v) const { 
    78       e=OutEdgeIt(*this,v); return e; }
    79     InEdgeIt& getFirst(InEdgeIt& e, const NodeIt v) const { 
    80       e=InEdgeIt(*this,v); return e; }
    81 
    82     template< typename It >
    83     It first() const { 
    84       It e;
    85       getFirst(e);
    86       return e; 
    87     }
    88 
    89     template< typename It >
    90     It first(NodeIt v) const { 
    91       It e;
    92       getFirst(e, v);
    93       return e; 
    94     }
    95 
    96     bool valid(EdgeIt e) const { return e.n!=INVALID; }
    97     bool valid(EachEdgeIt e) const { return e.n<int(edges.size()); }
    98     bool valid(NodeIt n) const { return n.n<int(nodes.size()); }
    99     
   100     template <typename It> It next(It it) const
   101       //    { It tmp(it); return goNext(tmp); }
   102     { It tmp; tmp.n=it.n+1; return tmp; }
   103 
   104     NodeIt& goNext(NodeIt& it) const { ++it.n; return it; }
   105     OutEdgeIt& goNext(OutEdgeIt& it) const
   106     { it.n=edges[it.n].next_out; return it; }
   107     InEdgeIt& goNext(InEdgeIt& it) const
   108     { it.n=edges[it.n].next_in; return it; }
   109     EachEdgeIt& goNext(EachEdgeIt& it) const { ++it.n; return it; }
   110 
   111     int id(NodeIt v) const { return v.n; }
   112     int id(EdgeIt e) const { return e.n; }
   113 
   114     NodeIt addNode() {
   115       NodeIt n; n.n=nodes.size();
   116       nodes.push_back(NodeT()); //FIXME: Hmmm...
   117       return n;
   118     }
   119     EdgeIt addEdge(NodeIt u, NodeIt v) {
   120       EdgeIt e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
   121       edges[e.n].tail=u.n; edges[e.n].head=v.n;
   122       edges[e.n].next_out=nodes[u.n].first_out;
   123       edges[e.n].next_in=nodes[v.n].first_in;
   124       nodes[u.n].first_out=nodes[v.n].first_in=e.n;
   125       return e;
   126     }
   127 
   128     void clear() {nodes.clear();edges.clear();}
   129 
   130     class NodeIt {
   131       friend class SmartGraph;
   132       template <typename T> friend class NodeMap;
   133       
   134       friend class EdgeIt;
   135       friend class OutEdgeIt;
   136       friend class InEdgeIt;
   137       friend class SymEdgeIt;
   138 
   139     protected:
   140       int n;
   141       friend int SmartGraph::id(NodeIt v) const; 
   142     public:
   143       NodeIt() {}
   144       NodeIt(int nn) {n=nn;}
   145       bool operator==(const NodeIt i) const {return n==i.n;}
   146       bool operator!=(const NodeIt i) const {return n!=i.n;}
   147     };
   148     
   149     class EachNodeIt : public NodeIt {
   150       friend class SmartGraph;
   151     public:
   152       EachNodeIt(const SmartGraph& G) : NodeIt(0) { }
   153       EachNodeIt() : NodeIt() { }
   154     };
   155 
   156     class EdgeIt {
   157       friend class SmartGraph;
   158       template <typename T> friend class EdgeMap;
   159       
   160       friend class NodeIt;
   161       friend class EachNodeIt;
   162     protected:
   163       int n;
   164       friend int SmartGraph::id(EdgeIt e) const;
   165     public:
   166       EdgeIt() { }
   167       EdgeIt(int nn) {n=nn;}
   168       bool operator==(const EdgeIt i) const {return n==i.n;}
   169       bool operator!=(const EdgeIt i) const {return n!=i.n;}
   170     };
   171     
   172     class EachEdgeIt : public EdgeIt {
   173       friend class SmartGraph;
   174     public:
   175       EachEdgeIt(const SmartGraph& G) : EdgeIt(0) { }
   176       EachEdgeIt() : EdgeIt() { }
   177     };
   178     
   179     class OutEdgeIt : public EdgeIt {
   180       friend class SmartGraph;
   181     public: 
   182       OutEdgeIt() : EdgeIt() { }
   183       OutEdgeIt(const SmartGraph& G,const NodeIt v)
   184 	: EdgeIt(G.nodes[v.n].first_out) {}
   185     };
   186     
   187     class InEdgeIt : public EdgeIt {
   188       friend class SmartGraph;
   189     public: 
   190       InEdgeIt() : EdgeIt() { }
   191       InEdgeIt(const SmartGraph& G,NodeIt v) :EdgeIt(G.nodes[v.n].first_in){}
   192     };
   193 
   194     // Map types
   195 
   196     template <typename T>
   197     class NodeMap {
   198       const SmartGraph& G; 
   199       std::vector<T> container;
   200     public:
   201       typedef T ValueType;
   202       typedef NodeIt KeyType;
   203       NodeMap(const SmartGraph& _G) : G(_G), container(G.nodeNum()) { }
   204       NodeMap(const SmartGraph& _G, T a) : 
   205 	G(_G), container(G.nodeNum(), a) { }
   206       void set(NodeIt n, T a) { container[n.n]=a; }
   207       T get(NodeIt n) const { return container[n.n]; }
   208       T& operator[](NodeIt n) { return container[n.n]; }
   209       const T& operator[](NodeIt n) const { return container[n.n]; }
   210       void update() { container.resize(G.nodeNum()); }
   211       void update(T a) { container.resize(G.nodeNum(), a); }
   212     };
   213 
   214     template <typename T>
   215     class EdgeMap {
   216       const SmartGraph& G; 
   217       std::vector<T> container;
   218     public:
   219       typedef T ValueType;
   220       typedef EdgeIt KeyType;
   221       EdgeMap(const SmartGraph& _G) : G(_G), container(G.edgeNum()) { }
   222       EdgeMap(const SmartGraph& _G, T a) : 
   223 	G(_G), container(G.edgeNum(), a) { }
   224       void set(EdgeIt e, T a) { container[e.n]=a; }
   225       T get(EdgeIt e) const { return container[e.n]; }
   226       T& operator[](EdgeIt e) { return container[e.n]; } 
   227       const T& operator[](EdgeIt e) const { return container[e.n]; } 
   228       void update() { container.resize(G.edgeNum()); }
   229       void update(T a) { container.resize(G.edgeNum(), a); }
   230     };
   231 
   232 
   233 
   234 
   235   };
   236 } //namespace hugo
   237 
   238 #endif //SMART_GRAPH_H