[105] | 1 | // -*- mode:C++ -*- |
---|
| 2 | |
---|
[104] | 3 | #ifndef SMART_GRAPH_H |
---|
| 4 | #define SMART_GRAPH_H |
---|
| 5 | |
---|
| 6 | #include <iostream> |
---|
| 7 | #include <vector> |
---|
| 8 | |
---|
[105] | 9 | namespace hugo { |
---|
[104] | 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 | |
---|
[108] | 30 | template <typename Key> class DynMapBase |
---|
| 31 | { |
---|
| 32 | protected: |
---|
| 33 | SmartGraph* G; |
---|
| 34 | public: |
---|
| 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; |
---|
| 40 | }; |
---|
[104] | 41 | |
---|
| 42 | public: |
---|
[108] | 43 | template <typename T> class DynEdgeMap; |
---|
| 44 | template <typename T> class DynEdgeMap; |
---|
[104] | 45 | |
---|
| 46 | class NodeIt; |
---|
[108] | 47 | class EdgeIt; |
---|
| 48 | |
---|
| 49 | protected: |
---|
| 50 | std::vector<DynMapBase<NodeIt> * > dyn_node_maps; |
---|
| 51 | std::vector<DynMapBase<EdgeIt> * > dyn_edge_maps; |
---|
| 52 | |
---|
| 53 | public: |
---|
| 54 | |
---|
[104] | 55 | class EachNodeIt; |
---|
| 56 | class EachEdgeIt; |
---|
| 57 | class OutEdgeIt; |
---|
| 58 | class InEdgeIt; |
---|
| 59 | |
---|
[105] | 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 {}; |
---|
[104] | 66 | // class SymEdgeIt; |
---|
[105] | 67 | |
---|
| 68 | template <typename T> class NodeMap; |
---|
[104] | 69 | template <typename T> class EdgeMap; |
---|
| 70 | |
---|
| 71 | public: |
---|
| 72 | |
---|
| 73 | /* default constructor */ |
---|
| 74 | |
---|
| 75 | SmartGraph() : nodes(), edges() { } |
---|
| 76 | |
---|
[108] | 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 | } |
---|
[104] | 84 | |
---|
| 85 | int nodeNum() const { return nodes.size(); } //FIXME: What is this? |
---|
| 86 | int edgeNum() const { return edges.size(); } //FIXME: What is this? |
---|
| 87 | |
---|
[108] | 88 | int maxNodeId() const { return nodes.size(); } //FIXME: What is this? |
---|
| 89 | int maxEdgeId() const { return edges.size(); } //FIXME: What is this? |
---|
| 90 | |
---|
| 91 | |
---|
[104] | 92 | NodeIt tail(EdgeIt e) const { return edges[e.n].tail; } |
---|
| 93 | NodeIt head(EdgeIt e) const { return edges[e.n].head; } |
---|
| 94 | |
---|
| 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(); } |
---|
| 98 | |
---|
| 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(); } |
---|
| 102 | |
---|
| 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; } |
---|
| 111 | |
---|
| 112 | template< typename It > |
---|
| 113 | It first() const { |
---|
| 114 | It e; |
---|
| 115 | getFirst(e); |
---|
| 116 | return e; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | template< typename It > |
---|
| 120 | It first(NodeIt v) const { |
---|
| 121 | It e; |
---|
| 122 | getFirst(e, v); |
---|
| 123 | return e; |
---|
| 124 | } |
---|
| 125 | |
---|
| 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()); } |
---|
| 129 | |
---|
[105] | 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; } |
---|
[104] | 133 | |
---|
| 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; } |
---|
| 140 | |
---|
| 141 | int id(NodeIt v) const { return v.n; } |
---|
| 142 | int id(EdgeIt e) const { return e.n; } |
---|
| 143 | |
---|
| 144 | NodeIt addNode() { |
---|
| 145 | NodeIt n; n.n=nodes.size(); |
---|
| 146 | nodes.push_back(NodeT()); //FIXME: Hmmm... |
---|
[108] | 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 | |
---|
[104] | 151 | return n; |
---|
| 152 | } |
---|
[108] | 153 | |
---|
[104] | 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; |
---|
[108] | 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 | |
---|
[104] | 164 | return e; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | void clear() {nodes.clear();edges.clear();} |
---|
| 168 | |
---|
| 169 | class NodeIt { |
---|
| 170 | friend class SmartGraph; |
---|
| 171 | template <typename T> friend class NodeMap; |
---|
[108] | 172 | template <typename T> friend class DynNodeMap; |
---|
[104] | 173 | |
---|
| 174 | friend class EdgeIt; |
---|
| 175 | friend class OutEdgeIt; |
---|
| 176 | friend class InEdgeIt; |
---|
| 177 | friend class SymEdgeIt; |
---|
| 178 | |
---|
| 179 | protected: |
---|
| 180 | int n; |
---|
| 181 | friend int SmartGraph::id(NodeIt v) const; |
---|
| 182 | public: |
---|
| 183 | NodeIt() {} |
---|
| 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;} |
---|
| 187 | }; |
---|
| 188 | |
---|
| 189 | class EachNodeIt : public NodeIt { |
---|
| 190 | friend class SmartGraph; |
---|
| 191 | public: |
---|
| 192 | EachNodeIt(const SmartGraph& G) : NodeIt(0) { } |
---|
| 193 | EachNodeIt() : NodeIt() { } |
---|
| 194 | }; |
---|
| 195 | |
---|
| 196 | class EdgeIt { |
---|
| 197 | friend class SmartGraph; |
---|
| 198 | template <typename T> friend class EdgeMap; |
---|
[108] | 199 | template <typename T> friend class DynEdgeMap; |
---|
[104] | 200 | |
---|
| 201 | friend class NodeIt; |
---|
| 202 | friend class EachNodeIt; |
---|
| 203 | protected: |
---|
| 204 | int n; |
---|
| 205 | friend int SmartGraph::id(EdgeIt e) const; |
---|
| 206 | public: |
---|
| 207 | EdgeIt() { } |
---|
| 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;} |
---|
| 211 | }; |
---|
| 212 | |
---|
| 213 | class EachEdgeIt : public EdgeIt { |
---|
| 214 | friend class SmartGraph; |
---|
| 215 | public: |
---|
| 216 | EachEdgeIt(const SmartGraph& G) : EdgeIt(0) { } |
---|
| 217 | EachEdgeIt() : EdgeIt() { } |
---|
| 218 | }; |
---|
| 219 | |
---|
| 220 | class OutEdgeIt : public EdgeIt { |
---|
| 221 | friend class SmartGraph; |
---|
| 222 | public: |
---|
| 223 | OutEdgeIt() : EdgeIt() { } |
---|
| 224 | OutEdgeIt(const SmartGraph& G,const NodeIt v) |
---|
| 225 | : EdgeIt(G.nodes[v.n].first_out) {} |
---|
| 226 | }; |
---|
| 227 | |
---|
| 228 | class InEdgeIt : public EdgeIt { |
---|
| 229 | friend class SmartGraph; |
---|
| 230 | public: |
---|
| 231 | InEdgeIt() : EdgeIt() { } |
---|
| 232 | InEdgeIt(const SmartGraph& G,NodeIt v) :EdgeIt(G.nodes[v.n].first_in){} |
---|
| 233 | }; |
---|
[105] | 234 | |
---|
| 235 | // Map types |
---|
| 236 | |
---|
| 237 | template <typename T> |
---|
| 238 | class NodeMap { |
---|
| 239 | const SmartGraph& G; |
---|
| 240 | std::vector<T> container; |
---|
| 241 | public: |
---|
| 242 | typedef T ValueType; |
---|
| 243 | typedef NodeIt KeyType; |
---|
[108] | 244 | NodeMap(const SmartGraph& _G) : G(_G), container(G.maxNodeId()) { } |
---|
[105] | 245 | NodeMap(const SmartGraph& _G, T a) : |
---|
[108] | 246 | G(_G), container(G.maxNodeId(), a) { } |
---|
[105] | 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]; } |
---|
[108] | 251 | void update() { container.resize(G.maxNodeId()); } |
---|
| 252 | void update(T a) { container.resize(G.maxNodeId(), a); } |
---|
[105] | 253 | }; |
---|
| 254 | |
---|
| 255 | template <typename T> |
---|
| 256 | class EdgeMap { |
---|
| 257 | const SmartGraph& G; |
---|
| 258 | std::vector<T> container; |
---|
| 259 | public: |
---|
| 260 | typedef T ValueType; |
---|
| 261 | typedef EdgeIt KeyType; |
---|
[108] | 262 | EdgeMap(const SmartGraph& _G) : G(_G), container(G.maxEdgeId()) { } |
---|
[105] | 263 | EdgeMap(const SmartGraph& _G, T a) : |
---|
[108] | 264 | G(_G), container(G.maxEdgeId(), a) { } |
---|
[105] | 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]; } |
---|
[108] | 269 | void update() { container.resize(G.maxEdgeId()); } |
---|
| 270 | void update(T a) { container.resize(G.maxEdgeId(), a); } |
---|
[105] | 271 | }; |
---|
| 272 | |
---|
[108] | 273 | template <typename T> class DynNodeMap : public DynMapBase<NodeIt> |
---|
| 274 | { |
---|
| 275 | std::vector<T> container; |
---|
[105] | 276 | |
---|
[108] | 277 | public: |
---|
| 278 | typedef T ValueType; |
---|
| 279 | typedef NodeIt KeyType; |
---|
[105] | 280 | |
---|
[108] | 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 | } |
---|
[105] | 296 | |
---|
[108] | 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 | |
---|
[104] | 358 | }; |
---|
[105] | 359 | } //namespace hugo |
---|
[104] | 360 | |
---|
| 361 | #endif //SMART_GRAPH_H |
---|