52 |
72 |
53 /* default constructor */ |
73 /* default constructor */ |
54 |
74 |
55 SmartGraph() : nodes(), edges() { } |
75 SmartGraph() : nodes(), edges() { } |
56 |
76 |
57 ~SmartGraph() {} |
77 ~SmartGraph() |
|
78 { |
|
79 for(std::vector<DynMapBase<NodeIt> * >::iterator i=dyn_node_maps.begin(); |
|
80 i!=dyn_node_maps.end(); ++i) (**i).G=NULL; |
|
81 for(std::vector<DynMapBase<EdgeIt> * >::iterator i=dyn_edge_maps.begin(); |
|
82 i!=dyn_edge_maps.end(); ++i) (**i).G=NULL; |
|
83 } |
58 |
84 |
59 int nodeNum() const { return nodes.size(); } //FIXME: What is this? |
85 int nodeNum() const { return nodes.size(); } //FIXME: What is this? |
60 int edgeNum() const { return edges.size(); } //FIXME: What is this? |
86 int edgeNum() const { return edges.size(); } //FIXME: What is this? |
61 |
87 |
|
88 int maxNodeId() const { return nodes.size(); } //FIXME: What is this? |
|
89 int maxEdgeId() const { return edges.size(); } //FIXME: What is this? |
|
90 |
|
91 |
62 NodeIt tail(EdgeIt e) const { return edges[e.n].tail; } |
92 NodeIt tail(EdgeIt e) const { return edges[e.n].tail; } |
63 NodeIt head(EdgeIt e) const { return edges[e.n].head; } |
93 NodeIt head(EdgeIt e) const { return edges[e.n].head; } |
64 |
94 |
65 NodeIt aNode(const OutEdgeIt& e) const { return tail(e); } |
95 NodeIt aNode(const OutEdgeIt& e) const { return tail(e); } |
66 NodeIt aNode(const InEdgeIt& e) const { return head(e); } |
96 NodeIt aNode(const InEdgeIt& e) const { return head(e); } |
112 int id(EdgeIt e) const { return e.n; } |
142 int id(EdgeIt e) const { return e.n; } |
113 |
143 |
114 NodeIt addNode() { |
144 NodeIt addNode() { |
115 NodeIt n; n.n=nodes.size(); |
145 NodeIt n; n.n=nodes.size(); |
116 nodes.push_back(NodeT()); //FIXME: Hmmm... |
146 nodes.push_back(NodeT()); //FIXME: Hmmm... |
|
147 |
|
148 for(std::vector<DynMapBase<NodeIt> * >::iterator i=dyn_node_maps.begin(); |
|
149 i!=dyn_node_maps.end(); ++i) (**i).add(n.n); |
|
150 |
117 return n; |
151 return n; |
118 } |
152 } |
|
153 |
119 EdgeIt addEdge(NodeIt u, NodeIt v) { |
154 EdgeIt addEdge(NodeIt u, NodeIt v) { |
120 EdgeIt e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm... |
155 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; |
156 edges[e.n].tail=u.n; edges[e.n].head=v.n; |
122 edges[e.n].next_out=nodes[u.n].first_out; |
157 edges[e.n].next_out=nodes[u.n].first_out; |
123 edges[e.n].next_in=nodes[v.n].first_in; |
158 edges[e.n].next_in=nodes[v.n].first_in; |
124 nodes[u.n].first_out=nodes[v.n].first_in=e.n; |
159 nodes[u.n].first_out=nodes[v.n].first_in=e.n; |
|
160 |
|
161 for(std::vector<DynMapBase<EdgeIt> * >::iterator i=dyn_edge_maps.begin(); |
|
162 i!=dyn_edge_maps.end(); ++i) (**i).add(e.n); |
|
163 |
125 return e; |
164 return e; |
126 } |
165 } |
127 |
166 |
128 void clear() {nodes.clear();edges.clear();} |
167 void clear() {nodes.clear();edges.clear();} |
129 |
168 |
130 class NodeIt { |
169 class NodeIt { |
131 friend class SmartGraph; |
170 friend class SmartGraph; |
132 template <typename T> friend class NodeMap; |
171 template <typename T> friend class NodeMap; |
|
172 template <typename T> friend class DynNodeMap; |
133 |
173 |
134 friend class EdgeIt; |
174 friend class EdgeIt; |
135 friend class OutEdgeIt; |
175 friend class OutEdgeIt; |
136 friend class InEdgeIt; |
176 friend class InEdgeIt; |
137 friend class SymEdgeIt; |
177 friend class SymEdgeIt; |
198 const SmartGraph& G; |
239 const SmartGraph& G; |
199 std::vector<T> container; |
240 std::vector<T> container; |
200 public: |
241 public: |
201 typedef T ValueType; |
242 typedef T ValueType; |
202 typedef NodeIt KeyType; |
243 typedef NodeIt KeyType; |
203 NodeMap(const SmartGraph& _G) : G(_G), container(G.nodeNum()) { } |
244 NodeMap(const SmartGraph& _G) : G(_G), container(G.maxNodeId()) { } |
204 NodeMap(const SmartGraph& _G, T a) : |
245 NodeMap(const SmartGraph& _G, T a) : |
205 G(_G), container(G.nodeNum(), a) { } |
246 G(_G), container(G.maxNodeId(), a) { } |
206 void set(NodeIt n, T a) { container[n.n]=a; } |
247 void set(NodeIt n, T a) { container[n.n]=a; } |
207 T get(NodeIt n) const { return container[n.n]; } |
248 T get(NodeIt n) const { return container[n.n]; } |
208 T& operator[](NodeIt n) { return container[n.n]; } |
249 T& operator[](NodeIt n) { return container[n.n]; } |
209 const T& operator[](NodeIt n) const { return container[n.n]; } |
250 const T& operator[](NodeIt n) const { return container[n.n]; } |
210 void update() { container.resize(G.nodeNum()); } |
251 void update() { container.resize(G.maxNodeId()); } |
211 void update(T a) { container.resize(G.nodeNum(), a); } |
252 void update(T a) { container.resize(G.maxNodeId(), a); } |
212 }; |
253 }; |
213 |
254 |
214 template <typename T> |
255 template <typename T> |
215 class EdgeMap { |
256 class EdgeMap { |
216 const SmartGraph& G; |
257 const SmartGraph& G; |
217 std::vector<T> container; |
258 std::vector<T> container; |
218 public: |
259 public: |
219 typedef T ValueType; |
260 typedef T ValueType; |
220 typedef EdgeIt KeyType; |
261 typedef EdgeIt KeyType; |
221 EdgeMap(const SmartGraph& _G) : G(_G), container(G.edgeNum()) { } |
262 EdgeMap(const SmartGraph& _G) : G(_G), container(G.maxEdgeId()) { } |
222 EdgeMap(const SmartGraph& _G, T a) : |
263 EdgeMap(const SmartGraph& _G, T a) : |
223 G(_G), container(G.edgeNum(), a) { } |
264 G(_G), container(G.maxEdgeId(), a) { } |
224 void set(EdgeIt e, T a) { container[e.n]=a; } |
265 void set(EdgeIt e, T a) { container[e.n]=a; } |
225 T get(EdgeIt e) const { return container[e.n]; } |
266 T get(EdgeIt e) const { return container[e.n]; } |
226 T& operator[](EdgeIt e) { return container[e.n]; } |
267 T& operator[](EdgeIt e) { return container[e.n]; } |
227 const T& operator[](EdgeIt e) const { return container[e.n]; } |
268 const T& operator[](EdgeIt e) const { return container[e.n]; } |
228 void update() { container.resize(G.edgeNum()); } |
269 void update() { container.resize(G.maxEdgeId()); } |
229 void update(T a) { container.resize(G.edgeNum(), a); } |
270 void update(T a) { container.resize(G.maxEdgeId(), a); } |
230 }; |
271 }; |
231 |
272 |
232 |
273 template <typename T> class DynNodeMap : public DynMapBase<NodeIt> |
233 |
274 { |
234 |
275 std::vector<T> container; |
|
276 |
|
277 public: |
|
278 typedef T ValueType; |
|
279 typedef NodeIt KeyType; |
|
280 |
|
281 DynNodeMap(SmartGraph &_G) : |
|
282 DynMapBase<NodeIt>(_G), container(_G.maxNodeId()) |
|
283 { |
|
284 //FIXME: What if there are empty Id's? |
|
285 G->dyn_node_maps.push_back(this); |
|
286 } |
|
287 ~DynNodeMap() |
|
288 { |
|
289 if(G) { |
|
290 std::vector<DynMapBase<NodeIt>* >::iterator i; |
|
291 for(i=G->dyn_node_maps.begin(); |
|
292 i!=G->dyn_node_maps.end() && *i!=this; ++i) ; |
|
293 if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow... |
|
294 } |
|
295 } |
|
296 |
|
297 void add(const NodeIt k) |
|
298 { |
|
299 if(k.n>=container.size()) container.resize(k.n+1); |
|
300 } |
|
301 void erase(const NodeIt k) |
|
302 { |
|
303 //FIXME: Please implement me. |
|
304 } |
|
305 |
|
306 void set(NodeIt n, T a) { container[n.n]=a; } |
|
307 T get(NodeIt n) const { return container[n.n]; } |
|
308 T& operator[](NodeIt n) { return container[n.n]; } |
|
309 const T& operator[](NodeIt n) const { return container[n.n]; } |
|
310 |
|
311 void update() {} //Useless for DynMaps |
|
312 void update(T a) {} //Useless for DynMaps |
|
313 }; |
|
314 |
|
315 template <typename T> class DynEdgeMap : public DynMapBase<EdgeIt> |
|
316 { |
|
317 std::vector<T> container; |
|
318 |
|
319 public: |
|
320 typedef T ValueType; |
|
321 typedef EdgeIt KeyType; |
|
322 |
|
323 DynEdgeMap(SmartGraph &_G) : |
|
324 DynMapBase<EdgeIt>(_G), container(_G.maxEdgeId()) |
|
325 { |
|
326 //FIXME: What if there are empty Id's? |
|
327 //FIXME: Can I do that? : |
|
328 G->dyn_edge_maps.push_back(this); |
|
329 } |
|
330 ~DynEdgeMap() |
|
331 { |
|
332 if(G) { |
|
333 std::vector<DynMapBase<EdgeIt>* >::iterator i; |
|
334 for(i=G->dyn_edge_maps.begin(); |
|
335 i!=G->dyn_edge_maps.end() && *i!=this; ++i) ; |
|
336 if(*i==this) G->dyn_edge_maps.erase(i); //FIXME: Way too slow... |
|
337 } |
|
338 } |
|
339 |
|
340 void add(const EdgeIt k) |
|
341 { |
|
342 if(k.n>=int(container.size())) container.resize(k.n+1); |
|
343 } |
|
344 void erase(const EdgeIt k) |
|
345 { |
|
346 //FIXME: Please implement me. |
|
347 } |
|
348 |
|
349 void set(EdgeIt n, T a) { container[n.n]=a; } |
|
350 T get(EdgeIt n) const { return container[n.n]; } |
|
351 T& operator[](EdgeIt n) { return container[n.n]; } |
|
352 const T& operator[](EdgeIt n) const { return container[n.n]; } |
|
353 |
|
354 void update() {} //Useless for DynMaps |
|
355 void update(T a) {} //Useless for DynMaps |
|
356 }; |
|
357 |
235 }; |
358 }; |
236 } //namespace hugo |
359 } //namespace hugo |
237 |
360 |
238 #endif //SMART_GRAPH_H |
361 #endif //SMART_GRAPH_H |