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;
30 template <typename Key> class DynMapBase
35 virtual void add(const Key k) = NULL;
36 virtual void erase(const Key k) = NULL;
37 DynMapBase(SmartGraph &_G) : G(&_G) {}
38 virtual ~DynMapBase() {}
39 friend class SmartGraph;
43 template <typename T> class DynEdgeMap;
44 template <typename T> class DynEdgeMap;
50 std::vector<DynMapBase<NodeIt> * > dyn_node_maps;
51 std::vector<DynMapBase<EdgeIt> * > dyn_edge_maps;
60 // class NodeIt { int n; };
61 // class EachNodeIt : public NodeIt { };
62 // class EdgeIt { int n; };
63 // class EachEdgeIt : public EdgeIt {};
64 // class OutEdgeIt : public EdgeIt {};
65 // class InEdgeIt : public EdgeIt {};
68 template <typename T> class NodeMap;
69 template <typename T> class EdgeMap;
73 /* default constructor */
75 SmartGraph() : nodes(), edges() { }
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;
85 int nodeNum() const { return nodes.size(); } //FIXME: What is this?
86 int edgeNum() const { return edges.size(); } //FIXME: What is this?
88 int maxNodeId() const { return nodes.size(); } //FIXME: What is this?
89 int maxEdgeId() const { return edges.size(); } //FIXME: What is this?
92 NodeIt tail(EdgeIt e) const { return edges[e.n].tail; }
93 NodeIt head(EdgeIt e) const { return edges[e.n].head; }
95 NodeIt aNode(const OutEdgeIt& e) const { return tail(e); }
96 NodeIt aNode(const InEdgeIt& e) const { return head(e); }
97 //NodeIt aNode(const SymEdgeIt& e) const { return e.aNode(); }
99 NodeIt bNode(const OutEdgeIt& e) const { return head(e); }
100 NodeIt bNode(const InEdgeIt& e) const { return tail(e); }
101 //NodeIt bNode(const SymEdgeIt& e) const { return e.bNode(); }
103 EachNodeIt& getFirst(EachNodeIt& v) const {
104 v=EachNodeIt(*this); return v; }
105 EachEdgeIt& getFirst(EachEdgeIt& e) const {
106 e=EachEdgeIt(*this); return e; }
107 OutEdgeIt& getFirst(OutEdgeIt& e, const NodeIt v) const {
108 e=OutEdgeIt(*this,v); return e; }
109 InEdgeIt& getFirst(InEdgeIt& e, const NodeIt v) const {
110 e=InEdgeIt(*this,v); return e; }
112 template< typename It >
119 template< typename It >
120 It first(NodeIt v) const {
126 bool valid(EdgeIt e) const { return e.n!=INVALID; }
127 bool valid(EachEdgeIt e) const { return e.n<int(edges.size()); }
128 bool valid(NodeIt n) const { return n.n<int(nodes.size()); }
130 template <typename It> It next(It it) const
131 // { It tmp(it); return goNext(tmp); }
132 { It tmp; tmp.n=it.n+1; return tmp; }
134 NodeIt& goNext(NodeIt& it) const { ++it.n; return it; }
135 OutEdgeIt& goNext(OutEdgeIt& it) const
136 { it.n=edges[it.n].next_out; return it; }
137 InEdgeIt& goNext(InEdgeIt& it) const
138 { it.n=edges[it.n].next_in; return it; }
139 EachEdgeIt& goNext(EachEdgeIt& it) const { ++it.n; return it; }
141 int id(NodeIt v) const { return v.n; }
142 int id(EdgeIt e) const { return e.n; }
145 NodeIt n; n.n=nodes.size();
146 nodes.push_back(NodeT()); //FIXME: Hmmm...
148 for(std::vector<DynMapBase<NodeIt> * >::iterator i=dyn_node_maps.begin();
149 i!=dyn_node_maps.end(); ++i) (**i).add(n.n);
154 EdgeIt addEdge(NodeIt u, NodeIt v) {
155 EdgeIt e; e.n=edges.size(); edges.push_back(EdgeT()); //FIXME: Hmmm...
156 edges[e.n].tail=u.n; edges[e.n].head=v.n;
157 edges[e.n].next_out=nodes[u.n].first_out;
158 edges[e.n].next_in=nodes[v.n].first_in;
159 nodes[u.n].first_out=nodes[v.n].first_in=e.n;
161 for(std::vector<DynMapBase<EdgeIt> * >::iterator i=dyn_edge_maps.begin();
162 i!=dyn_edge_maps.end(); ++i) (**i).add(e.n);
167 void clear() {nodes.clear();edges.clear();}
170 friend class SmartGraph;
171 template <typename T> friend class NodeMap;
172 template <typename T> friend class DynNodeMap;
175 friend class OutEdgeIt;
176 friend class InEdgeIt;
177 friend class SymEdgeIt;
181 friend int SmartGraph::id(NodeIt v) const;
184 NodeIt(int nn) {n=nn;}
185 bool operator==(const NodeIt i) const {return n==i.n;}
186 bool operator!=(const NodeIt i) const {return n!=i.n;}
189 class EachNodeIt : public NodeIt {
190 friend class SmartGraph;
192 EachNodeIt(const SmartGraph& G) : NodeIt(0) { }
193 EachNodeIt() : NodeIt() { }
197 friend class SmartGraph;
198 template <typename T> friend class EdgeMap;
199 template <typename T> friend class DynEdgeMap;
202 friend class EachNodeIt;
205 friend int SmartGraph::id(EdgeIt e) const;
208 EdgeIt(int nn) {n=nn;}
209 bool operator==(const EdgeIt i) const {return n==i.n;}
210 bool operator!=(const EdgeIt i) const {return n!=i.n;}
213 class EachEdgeIt : public EdgeIt {
214 friend class SmartGraph;
216 EachEdgeIt(const SmartGraph& G) : EdgeIt(0) { }
217 EachEdgeIt() : EdgeIt() { }
220 class OutEdgeIt : public EdgeIt {
221 friend class SmartGraph;
223 OutEdgeIt() : EdgeIt() { }
224 OutEdgeIt(const SmartGraph& G,const NodeIt v)
225 : EdgeIt(G.nodes[v.n].first_out) {}
228 class InEdgeIt : public EdgeIt {
229 friend class SmartGraph;
231 InEdgeIt() : EdgeIt() { }
232 InEdgeIt(const SmartGraph& G,NodeIt v) :EdgeIt(G.nodes[v.n].first_in){}
237 template <typename T>
240 std::vector<T> container;
243 typedef NodeIt KeyType;
244 NodeMap(const SmartGraph& _G) : G(_G), container(G.maxNodeId()) { }
245 NodeMap(const SmartGraph& _G, T a) :
246 G(_G), container(G.maxNodeId(), a) { }
247 void set(NodeIt n, T a) { container[n.n]=a; }
248 T get(NodeIt n) const { return container[n.n]; }
249 T& operator[](NodeIt n) { return container[n.n]; }
250 const T& operator[](NodeIt n) const { return container[n.n]; }
251 void update() { container.resize(G.maxNodeId()); }
252 void update(T a) { container.resize(G.maxNodeId(), a); }
255 template <typename T>
258 std::vector<T> container;
261 typedef EdgeIt KeyType;
262 EdgeMap(const SmartGraph& _G) : G(_G), container(G.maxEdgeId()) { }
263 EdgeMap(const SmartGraph& _G, T a) :
264 G(_G), container(G.maxEdgeId(), a) { }
265 void set(EdgeIt e, T a) { container[e.n]=a; }
266 T get(EdgeIt e) const { return container[e.n]; }
267 T& operator[](EdgeIt e) { return container[e.n]; }
268 const T& operator[](EdgeIt e) const { return container[e.n]; }
269 void update() { container.resize(G.maxEdgeId()); }
270 void update(T a) { container.resize(G.maxEdgeId(), a); }
273 template <typename T> class DynNodeMap : public DynMapBase<NodeIt>
275 std::vector<T> container;
279 typedef NodeIt KeyType;
281 DynNodeMap(SmartGraph &_G) :
282 DynMapBase<NodeIt>(_G), container(_G.maxNodeId())
284 //FIXME: What if there are empty Id's?
285 G->dyn_node_maps.push_back(this);
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...
297 void add(const NodeIt k)
299 if(k.n>=container.size()) container.resize(k.n+1);
301 void erase(const NodeIt k)
303 //FIXME: Please implement me.
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]; }
311 void update() {} //Useless for DynMaps
312 void update(T a) {} //Useless for DynMaps
315 template <typename T> class DynEdgeMap : public DynMapBase<EdgeIt>
317 std::vector<T> container;
321 typedef EdgeIt KeyType;
323 DynEdgeMap(SmartGraph &_G) :
324 DynMapBase<EdgeIt>(_G), container(_G.maxEdgeId())
326 //FIXME: What if there are empty Id's?
327 //FIXME: Can I do that? :
328 G->dyn_edge_maps.push_back(this);
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...
340 void add(const EdgeIt k)
342 if(k.n>=int(container.size())) container.resize(k.n+1);
344 void erase(const EdgeIt k)
346 //FIXME: Please implement me.
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]; }
354 void update() {} //Useless for DynMaps
355 void update(T a) {} //Useless for DynMaps
361 #endif //SMART_GRAPH_H