15 // static const int INVALID=-1;
16 // static const int INVALID_NODE=INT_MAX;
20 int first_in,first_out;
21 NodeT() : first_in(-1), first_out(-1) {}
25 int head, tail, next_in, next_out;
26 //FIXME: is this necessary?
27 EdgeT() : next_in(-1), next_out(-1) {}
30 std::vector<NodeT> nodes;
32 std::vector<EdgeT> edges;
34 template <typename Key> class DynMapBase
39 virtual void add(const Key k) = NULL;
40 virtual void erase(const Key k) = NULL;
41 DynMapBase(const SmartGraph &_G) : G(&_G) {}
42 virtual ~DynMapBase() {}
43 friend class SmartGraph;
46 template <typename T> class DynEdgeMap;
47 template <typename T> class DynEdgeMap;
53 mutable std::vector<DynMapBase<Node> * > dyn_node_maps;
54 mutable std::vector<DynMapBase<Edge> * > dyn_edge_maps;
63 // class Node { int n; };
64 // class NodeIt : public Node { };
65 // class Edge { int n; };
66 // class EdgeIt : public Edge {};
67 // class OutEdgeIt : public Edge {};
68 // class InEdgeIt : public Edge {};
71 template <typename T> class NodeMap;
72 template <typename T> class EdgeMap;
76 /* default constructor */
78 SmartGraph() : nodes(), edges() { }
79 SmartGraph(const SmartGraph &_g) : nodes(_g.nodes), edges(_g.edges) { }
83 for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
84 i!=dyn_node_maps.end(); ++i) (**i).G=NULL;
85 for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
86 i!=dyn_edge_maps.end(); ++i) (**i).G=NULL;
89 int nodeNum() const { return nodes.size(); } //FIXME: What is this?
90 int edgeNum() const { return edges.size(); } //FIXME: What is this?
92 int maxNodeId() const { return nodes.size(); } //FIXME: What is this?
93 int maxEdgeId() const { return edges.size(); } //FIXME: What is this?
96 Node tail(Edge e) const { return edges[e.n].tail; }
97 Node head(Edge e) const { return edges[e.n].head; }
99 // Node aNode(const OutEdgeIt& e) const { return tail(e); }
100 // Node aNode(const InEdgeIt& e) const { return head(e); }
101 // //Node aNode(const SymEdge& e) const { return e.aNode(); }
103 // Node bNode(const OutEdgeIt& e) const { return head(e); }
104 // Node bNode(const InEdgeIt& e) const { return tail(e); }
105 // //Node bNode(const SymEdge& e) const { return e.bNode(); }
107 NodeIt& first(NodeIt& v) const {
108 v=NodeIt(*this); return v; }
109 EdgeIt& first(EdgeIt& e) const {
110 e=EdgeIt(*this); return e; }
111 OutEdgeIt& first(OutEdgeIt& e, const Node v) const {
112 e=OutEdgeIt(*this,v); return e; }
113 InEdgeIt& first(InEdgeIt& e, const Node v) const {
114 e=InEdgeIt(*this,v); return e; }
116 template< typename It >
123 template< typename It >
124 It first(Node v) const {
130 bool valid(Edge e) const { return e.n!=-1; }
131 // bool valid(EdgeIt e) const { return e.n<int(edges.size()); }
132 bool valid(Node n) const { return n.n!=-1; }
134 void setInvalid(Edge &e) { e.n=-1; }
135 void setInvalid(Node &n) { n.n=-1; }
137 template <typename It> It getNext(It it) const
138 { It tmp(it); return next(tmp); }
139 //{ It tmp; tmp.n=it.n+1; return tmp; }
141 Node& next(Node& it) const { it.n=(it.n+2)%nodes.size()-1; return it; }
142 OutEdgeIt& next(OutEdgeIt& it) const
143 { it.n=edges[it.n].next_out; return it; }
144 InEdgeIt& next(InEdgeIt& it) const
145 { it.n=edges[it.n].next_in; return it; }
146 EdgeIt& next(EdgeIt& it) const { --it.n; return it; }
148 int id(Node v) const { return v.n; }
149 int id(Edge e) const { return e.n; }
152 Node n; n.n=nodes.size();
153 nodes.push_back(NodeT()); //FIXME: Hmmm...
155 for(std::vector<DynMapBase<Node> * >::iterator i=dyn_node_maps.begin();
156 i!=dyn_node_maps.end(); ++i) (**i).add(n.n);
161 Edge addEdge(Node u, Node v) {
162 Edge e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
163 edges[e.n].tail=u.n; edges[e.n].head=v.n;
164 edges[e.n].next_out=nodes[u.n].first_out;
165 edges[e.n].next_in=nodes[v.n].first_in;
166 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
168 for(std::vector<DynMapBase<Edge> * >::iterator i=dyn_edge_maps.begin();
169 i!=dyn_edge_maps.end(); ++i) (**i).add(e);
174 void clear() {nodes.clear();edges.clear();}
177 friend class SmartGraph;
178 template <typename T> friend class NodeMap;
179 template <typename T> friend class DynNodeMap;
182 friend class OutEdgeIt;
183 friend class InEdgeIt;
184 friend class SymEdge;
188 friend int SmartGraph::id(Node v) const;
192 Node (Invalid i) { n=-1; }
193 bool operator==(const Node i) const {return n==i.n;}
194 bool operator!=(const Node i) const {return n!=i.n;}
195 bool operator<(const Node i) const {return n<i.n;}
198 class NodeIt : public Node {
199 friend class SmartGraph;
201 NodeIt(const SmartGraph& G) : Node(G.nodes.size()?0:-1) { }
202 NodeIt() : Node() { }
206 friend class SmartGraph;
207 template <typename T> friend class EdgeMap;
208 template <typename T> friend class DynEdgeMap;
214 friend int SmartGraph::id(Edge e) const;
219 Edge (Invalid i) { n=-1; }
220 bool operator==(const Edge i) const {return n==i.n;}
221 bool operator!=(const Edge i) const {return n!=i.n;}
222 bool operator<(const Edge i) const {return n<i.n;}
225 class EdgeIt : public Edge {
226 friend class SmartGraph;
228 EdgeIt(const SmartGraph& G) : Edge(G.edges.size()-1) { }
229 EdgeIt (Invalid i) : Edge(i) { }
230 EdgeIt() : Edge() { }
233 class OutEdgeIt : public Edge {
234 friend class SmartGraph;
236 OutEdgeIt() : Edge() { }
237 OutEdgeIt (Invalid i) : Edge(i) { }
239 OutEdgeIt(const SmartGraph& G,const Node v)
240 : Edge(G.nodes[v.n].first_out) {}
243 class InEdgeIt : public Edge {
244 friend class SmartGraph;
246 InEdgeIt() : Edge() { }
247 InEdgeIt (Invalid i) : Edge(i) { }
248 InEdgeIt(const SmartGraph& G,Node v) :Edge(G.nodes[v.n].first_in){}
253 template <typename T>
256 std::vector<T> container;
259 typedef Node KeyType;
260 NodeMap(const SmartGraph& _G) : G(_G), container(G.maxNodeId()) { }
261 NodeMap(const SmartGraph& _G, T a) :
262 G(_G), container(G.maxNodeId(), a) { }
263 void set(Node n, T a) { container[n.n]=a; }
264 T get(Node n) const { return container[n.n]; }
265 T& operator[](Node n) { return container[n.n]; }
266 const T& operator[](Node n) const { return container[n.n]; }
267 void update() { container.resize(G.maxNodeId()); }
268 void update(T a) { container.resize(G.maxNodeId(), a); }
271 template <typename T>
274 std::vector<T> container;
277 typedef Edge KeyType;
278 EdgeMap(const SmartGraph& _G) : G(_G), container(G.maxEdgeId()) { }
279 EdgeMap(const SmartGraph& _G, T a) :
280 G(_G), container(G.maxEdgeId(), a) { }
281 void set(Edge e, T a) { container[e.n]=a; }
282 T get(Edge e) const { return container[e.n]; }
283 T& operator[](Edge e) { return container[e.n]; }
284 const T& operator[](Edge e) const { return container[e.n]; }
285 void update() { container.resize(G.maxEdgeId()); }
286 void update(T a) { container.resize(G.maxEdgeId(), a); }
289 template <typename T> class DynNodeMap : public DynMapBase<Node>
291 std::vector<T> container;
295 typedef Node KeyType;
297 DynNodeMap(const SmartGraph &_G) :
298 DynMapBase<Node>(_G), container(_G.maxNodeId())
300 //FIXME: What if there are empty Id's?
301 //FIXME: Can I use 'this' in a constructor?
302 G->dyn_node_maps.push_back(this);
307 std::vector<DynMapBase<Node>* >::iterator i;
308 for(i=G->dyn_node_maps.begin();
309 i!=G->dyn_node_maps.end() && *i!=this; ++i) ;
310 //if(*i==this) G->dyn_node_maps.erase(i); //FIXME: Way too slow...
311 //A better way to do that: (Is this really important?)
313 *i=G->dyn_node_maps.back();
314 G->dyn_node_maps.pop_back();
319 void add(const Node k)
321 if(k.n>=container.size()) container.resize(k.n+1);
323 // void erase(const Node k)
325 // //FIXME: Please implement me.
327 // void erase(const Edge k)
329 // //FIXME: Please implement me.
332 void set(Node n, T a) { container[n.n]=a; }
333 T get(Node n) const { return container[n.n]; }
334 T& operator[](Node n) { return container[n.n]; }
335 const T& operator[](Node n) const { return container[n.n]; }
337 void update() {} //Useless for DynMaps
338 void update(T a) {} //Useless for DynMaps
341 template <typename T> class DynEdgeMap : public DynMapBase<Edge>
343 std::vector<T> container;
347 typedef Edge KeyType;
349 DynEdgeMap(const SmartGraph &_G) :
350 DynMapBase<Edge>(_G), container(_G.maxEdgeId())
352 //FIXME: What if there are empty Id's?
353 //FIXME: Can I use 'this' in a constructor?
354 G->dyn_edge_maps.push_back(this);
359 std::vector<DynMapBase<Edge>* >::iterator i;
360 for(i=G->dyn_edge_maps.begin();
361 i!=G->dyn_edge_maps.end() && *i!=this; ++i) ;
362 //if(*i==this) G->dyn_edge_maps.erase(i); //Way too slow...
363 //A better way to do that: (Is this really important?)
365 *i=G->dyn_edge_maps.back();
366 G->dyn_edge_maps.pop_back();
371 void add(const Edge k)
373 if(k.n>=int(container.size())) container.resize(k.n+1);
375 void erase(const Edge k)
377 //FIXME: Please implement me.
380 void set(Edge n, T a) { container[n.n]=a; }
381 T get(Edge n) const { return container[n.n]; }
382 T& operator[](Edge n) { return container[n.n]; }
383 const T& operator[](Edge n) const { return container[n.n]; }
385 void update() {} //Useless for DynMaps
386 void update(T a) {} //Useless for DynMaps
395 #endif //SMART_GRAPH_H