13 static const int INVALID=-1;
17 int first_in,first_out;
18 NodeT() : first_in(INVALID), first_out(INVALID) {}
22 int head, tail, next_in, next_out;
23 //FIXME: is this necessary?
24 EdgeT() : next_in(INVALID), next_out(INVALID) {}
27 std::vector<NodeT> nodes;
28 std::vector<EdgeT> edges;
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 {};
48 template <typename T> class NodeMap;
49 template <typename T> class EdgeMap;
53 /* default constructor */
55 SmartGraph() : nodes(), edges() { }
59 int nodeNum() const { return nodes.size(); } //FIXME: What is this?
60 int edgeNum() const { return edges.size(); } //FIXME: What is this?
62 NodeIt tail(EdgeIt e) const { return edges[e.n].tail; }
63 NodeIt head(EdgeIt e) const { return edges[e.n].head; }
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(); }
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(); }
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; }
82 template< typename It >
89 template< typename It >
90 It first(NodeIt v) const {
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()); }
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; }
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; }
111 int id(NodeIt v) const { return v.n; }
112 int id(EdgeIt e) const { return e.n; }
115 NodeIt n; n.n=nodes.size();
116 nodes.push_back(NodeT()); //FIXME: Hmmm...
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;
128 void clear() {nodes.clear();edges.clear();}
131 friend class SmartGraph;
132 template <typename T> friend class NodeMap;
135 friend class OutEdgeIt;
136 friend class InEdgeIt;
137 friend class SymEdgeIt;
141 friend int SmartGraph::id(NodeIt v) const;
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;}
149 class EachNodeIt : public NodeIt {
150 friend class SmartGraph;
152 EachNodeIt(const SmartGraph& G) : NodeIt(0) { }
153 EachNodeIt() : NodeIt() { }
157 friend class SmartGraph;
158 template <typename T> friend class EdgeMap;
161 friend class EachNodeIt;
164 friend int SmartGraph::id(EdgeIt e) const;
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;}
172 class EachEdgeIt : public EdgeIt {
173 friend class SmartGraph;
175 EachEdgeIt(const SmartGraph& G) : EdgeIt(0) { }
176 EachEdgeIt() : EdgeIt() { }
179 class OutEdgeIt : public EdgeIt {
180 friend class SmartGraph;
182 OutEdgeIt() : EdgeIt() { }
183 OutEdgeIt(const SmartGraph& G,const NodeIt v)
184 : EdgeIt(G.nodes[v.n].first_out) {}
187 class InEdgeIt : public EdgeIt {
188 friend class SmartGraph;
190 InEdgeIt() : EdgeIt() { }
191 InEdgeIt(const SmartGraph& G,NodeIt v) :EdgeIt(G.nodes[v.n].first_in){}
196 template <typename T>
199 std::vector<T> container;
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); }
214 template <typename T>
217 std::vector<T> container;
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); }
238 #endif //SMART_GRAPH_H