| ... | ... |
@@ -116,385 +116,387 @@ |
| 116 | 116 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
class Node {
|
| 120 | 120 |
friend class SmartDigraphBase; |
| 121 | 121 |
friend class SmartDigraph; |
| 122 | 122 |
|
| 123 | 123 |
protected: |
| 124 | 124 |
int _id; |
| 125 | 125 |
explicit Node(int id) : _id(id) {}
|
| 126 | 126 |
public: |
| 127 | 127 |
Node() {}
|
| 128 | 128 |
Node (Invalid) : _id(-1) {}
|
| 129 | 129 |
bool operator==(const Node i) const {return _id == i._id;}
|
| 130 | 130 |
bool operator!=(const Node i) const {return _id != i._id;}
|
| 131 | 131 |
bool operator<(const Node i) const {return _id < i._id;}
|
| 132 | 132 |
}; |
| 133 | 133 |
|
| 134 | 134 |
|
| 135 | 135 |
class Arc {
|
| 136 | 136 |
friend class SmartDigraphBase; |
| 137 | 137 |
friend class SmartDigraph; |
| 138 | 138 |
|
| 139 | 139 |
protected: |
| 140 | 140 |
int _id; |
| 141 | 141 |
explicit Arc(int id) : _id(id) {}
|
| 142 | 142 |
public: |
| 143 | 143 |
Arc() { }
|
| 144 | 144 |
Arc (Invalid) : _id(-1) {}
|
| 145 | 145 |
bool operator==(const Arc i) const {return _id == i._id;}
|
| 146 | 146 |
bool operator!=(const Arc i) const {return _id != i._id;}
|
| 147 | 147 |
bool operator<(const Arc i) const {return _id < i._id;}
|
| 148 | 148 |
}; |
| 149 | 149 |
|
| 150 | 150 |
void first(Node& node) const {
|
| 151 | 151 |
node._id = nodes.size() - 1; |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
static void next(Node& node) {
|
| 155 | 155 |
--node._id; |
| 156 | 156 |
} |
| 157 | 157 |
|
| 158 | 158 |
void first(Arc& arc) const {
|
| 159 | 159 |
arc._id = arcs.size() - 1; |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
static void next(Arc& arc) {
|
| 163 | 163 |
--arc._id; |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
void firstOut(Arc& arc, const Node& node) const {
|
| 167 | 167 |
arc._id = nodes[node._id].first_out; |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 | 170 |
void nextOut(Arc& arc) const {
|
| 171 | 171 |
arc._id = arcs[arc._id].next_out; |
| 172 | 172 |
} |
| 173 | 173 |
|
| 174 | 174 |
void firstIn(Arc& arc, const Node& node) const {
|
| 175 | 175 |
arc._id = nodes[node._id].first_in; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
void nextIn(Arc& arc) const {
|
| 179 | 179 |
arc._id = arcs[arc._id].next_in; |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
}; |
| 183 | 183 |
|
| 184 | 184 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
| 185 | 185 |
|
| 186 | 186 |
///\ingroup graphs |
| 187 | 187 |
/// |
| 188 | 188 |
///\brief A smart directed graph class. |
| 189 | 189 |
/// |
| 190 | 190 |
///This is a simple and fast digraph implementation. |
| 191 | 191 |
///It is also quite memory efficient, but at the price |
| 192 | 192 |
///that <b> it does support only limited (only stack-like) |
| 193 | 193 |
///node and arc deletions</b>. |
| 194 | 194 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
| 195 | 195 |
///an important extra feature that its maps are real \ref |
| 196 | 196 |
///concepts::ReferenceMap "reference map"s. |
| 197 | 197 |
/// |
| 198 | 198 |
///\sa concepts::Digraph. |
| 199 | 199 |
class SmartDigraph : public ExtendedSmartDigraphBase {
|
| 200 | 200 |
public: |
| 201 | 201 |
|
| 202 | 202 |
typedef ExtendedSmartDigraphBase Parent; |
| 203 | 203 |
|
| 204 | 204 |
private: |
| 205 | 205 |
|
| 206 | 206 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
| 207 | 207 |
|
| 208 | 208 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
| 209 | 209 |
/// |
| 210 | 210 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
|
| 211 | 211 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
| 212 | 212 |
///Use DigraphCopy() instead. |
| 213 | 213 |
|
| 214 | 214 |
///Assignment of SmartDigraph to another one is \e not allowed. |
| 215 | 215 |
///Use DigraphCopy() instead. |
| 216 | 216 |
void operator=(const SmartDigraph &) {}
|
| 217 | 217 |
|
| 218 | 218 |
public: |
| 219 | 219 |
|
| 220 | 220 |
/// Constructor |
| 221 | 221 |
|
| 222 | 222 |
/// Constructor. |
| 223 | 223 |
/// |
| 224 | 224 |
SmartDigraph() {};
|
| 225 | 225 |
|
| 226 | 226 |
///Add a new node to the digraph. |
| 227 | 227 |
|
| 228 | 228 |
/// \return the new node. |
| 229 | 229 |
/// |
| 230 | 230 |
Node addNode() { return Parent::addNode(); }
|
| 231 | 231 |
|
| 232 | 232 |
///Add a new arc to the digraph. |
| 233 | 233 |
|
| 234 | 234 |
///Add a new arc to the digraph with source node \c s |
| 235 | 235 |
///and target node \c t. |
| 236 | 236 |
///\return the new arc. |
| 237 | 237 |
Arc addArc(const Node& s, const Node& t) {
|
| 238 | 238 |
return Parent::addArc(s, t); |
| 239 | 239 |
} |
| 240 | 240 |
|
| 241 | 241 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 242 | 242 |
/// allocation. |
| 243 | 243 |
|
| 244 | 244 |
/// Using this it is possible to avoid the superfluous memory |
| 245 | 245 |
/// allocation: if you know that the digraph you want to build will |
| 246 | 246 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 247 | 247 |
/// then it is worth reserving space for this amount before starting |
| 248 | 248 |
/// to build the digraph. |
| 249 | 249 |
/// \sa reserveArc |
| 250 | 250 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 251 | 251 |
|
| 252 | 252 |
/// \brief Using this it is possible to avoid the superfluous memory |
| 253 | 253 |
/// allocation. |
| 254 | 254 |
|
| 255 | 255 |
/// Using this it is possible to avoid the superfluous memory |
| 256 | 256 |
/// allocation: if you know that the digraph you want to build will |
| 257 | 257 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
| 258 | 258 |
/// then it is worth reserving space for this amount before starting |
| 259 | 259 |
/// to build the digraph. |
| 260 | 260 |
/// \sa reserveNode |
| 261 | 261 |
void reserveArc(int m) { arcs.reserve(m); };
|
| 262 | 262 |
|
| 263 | 263 |
/// \brief Node validity check |
| 264 | 264 |
/// |
| 265 | 265 |
/// This function gives back true if the given node is valid, |
| 266 | 266 |
/// ie. it is a real node of the graph. |
| 267 | 267 |
/// |
| 268 | 268 |
/// \warning A removed node (using Snapshot) could become valid again |
| 269 | 269 |
/// when new nodes are added to the graph. |
| 270 | 270 |
bool valid(Node n) const { return Parent::valid(n); }
|
| 271 | 271 |
|
| 272 | 272 |
/// \brief Arc validity check |
| 273 | 273 |
/// |
| 274 | 274 |
/// This function gives back true if the given arc is valid, |
| 275 | 275 |
/// ie. it is a real arc of the graph. |
| 276 | 276 |
/// |
| 277 | 277 |
/// \warning A removed arc (using Snapshot) could become valid again |
| 278 | 278 |
/// when new arcs are added to the graph. |
| 279 | 279 |
bool valid(Arc a) const { return Parent::valid(a); }
|
| 280 | 280 |
|
| 281 | 281 |
///Clear the digraph. |
| 282 | 282 |
|
| 283 | 283 |
///Erase all the nodes and arcs from the digraph. |
| 284 | 284 |
/// |
| 285 | 285 |
void clear() {
|
| 286 | 286 |
Parent::clear(); |
| 287 | 287 |
} |
| 288 | 288 |
|
| 289 | 289 |
///Split a node. |
| 290 | 290 |
|
| 291 | 291 |
///This function splits a node. First a new node is added to the digraph, |
| 292 | 292 |
///then the source of each outgoing arc of \c n is moved to this new node. |
| 293 | 293 |
///If \c connect is \c true (this is the default value), then a new arc |
| 294 | 294 |
///from \c n to the newly created node is also added. |
| 295 | 295 |
///\return The newly created node. |
| 296 | 296 |
/// |
| 297 | 297 |
///\note The <tt>Arc</tt>s |
| 298 | 298 |
///referencing a moved arc remain |
| 299 | 299 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
| 300 | 300 |
///may be invalidated. |
| 301 | 301 |
///\warning This functionality cannot be used together with the Snapshot |
| 302 | 302 |
///feature. |
| 303 | 303 |
Node split(Node n, bool connect = true) |
| 304 | 304 |
{
|
| 305 | 305 |
Node b = addNode(); |
| 306 | 306 |
nodes[b._id].first_out=nodes[n._id].first_out; |
| 307 | 307 |
nodes[n._id].first_out=-1; |
| 308 |
for(int i=nodes[b._id].first_out;i!=-1; |
|
| 308 |
for(int i=nodes[b._id].first_out; i!=-1; i=arcs[i].next_out) {
|
|
| 309 |
arcs[i].source=b._id; |
|
| 310 |
} |
|
| 309 | 311 |
if(connect) addArc(n,b); |
| 310 | 312 |
return b; |
| 311 | 313 |
} |
| 312 | 314 |
|
| 313 | 315 |
public: |
| 314 | 316 |
|
| 315 | 317 |
class Snapshot; |
| 316 | 318 |
|
| 317 | 319 |
protected: |
| 318 | 320 |
|
| 319 | 321 |
void restoreSnapshot(const Snapshot &s) |
| 320 | 322 |
{
|
| 321 | 323 |
while(s.arc_num<arcs.size()) {
|
| 322 | 324 |
Arc arc = arcFromId(arcs.size()-1); |
| 323 | 325 |
Parent::notifier(Arc()).erase(arc); |
| 324 | 326 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
| 325 | 327 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
| 326 | 328 |
arcs.pop_back(); |
| 327 | 329 |
} |
| 328 | 330 |
while(s.node_num<nodes.size()) {
|
| 329 | 331 |
Node node = nodeFromId(nodes.size()-1); |
| 330 | 332 |
Parent::notifier(Node()).erase(node); |
| 331 | 333 |
nodes.pop_back(); |
| 332 | 334 |
} |
| 333 | 335 |
} |
| 334 | 336 |
|
| 335 | 337 |
public: |
| 336 | 338 |
|
| 337 | 339 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 338 | 340 |
|
| 339 | 341 |
///Class to make a snapshot of the digraph and to restrore to it later. |
| 340 | 342 |
/// |
| 341 | 343 |
///The newly added nodes and arcs can be removed using the |
| 342 | 344 |
///restore() function. |
| 343 | 345 |
///\note After you restore a state, you cannot restore |
| 344 | 346 |
///a later state, in other word you cannot add again the arcs deleted |
| 345 | 347 |
///by restore() using another one Snapshot instance. |
| 346 | 348 |
/// |
| 347 | 349 |
///\warning If you do not use correctly the snapshot that can cause |
| 348 | 350 |
///either broken program, invalid state of the digraph, valid but |
| 349 | 351 |
///not the restored digraph or no change. Because the runtime performance |
| 350 | 352 |
///the validity of the snapshot is not stored. |
| 351 | 353 |
class Snapshot |
| 352 | 354 |
{
|
| 353 | 355 |
SmartDigraph *_graph; |
| 354 | 356 |
protected: |
| 355 | 357 |
friend class SmartDigraph; |
| 356 | 358 |
unsigned int node_num; |
| 357 | 359 |
unsigned int arc_num; |
| 358 | 360 |
public: |
| 359 | 361 |
///Default constructor. |
| 360 | 362 |
|
| 361 | 363 |
///Default constructor. |
| 362 | 364 |
///To actually make a snapshot you must call save(). |
| 363 | 365 |
/// |
| 364 | 366 |
Snapshot() : _graph(0) {}
|
| 365 | 367 |
///Constructor that immediately makes a snapshot |
| 366 | 368 |
|
| 367 | 369 |
///This constructor immediately makes a snapshot of the digraph. |
| 368 | 370 |
///\param graph The digraph we make a snapshot of. |
| 369 | 371 |
Snapshot(SmartDigraph &graph) : _graph(&graph) {
|
| 370 | 372 |
node_num=_graph->nodes.size(); |
| 371 | 373 |
arc_num=_graph->arcs.size(); |
| 372 | 374 |
} |
| 373 | 375 |
|
| 374 | 376 |
///Make a snapshot. |
| 375 | 377 |
|
| 376 | 378 |
///Make a snapshot of the digraph. |
| 377 | 379 |
/// |
| 378 | 380 |
///This function can be called more than once. In case of a repeated |
| 379 | 381 |
///call, the previous snapshot gets lost. |
| 380 | 382 |
///\param graph The digraph we make the snapshot of. |
| 381 | 383 |
void save(SmartDigraph &graph) |
| 382 | 384 |
{
|
| 383 | 385 |
_graph=&graph; |
| 384 | 386 |
node_num=_graph->nodes.size(); |
| 385 | 387 |
arc_num=_graph->arcs.size(); |
| 386 | 388 |
} |
| 387 | 389 |
|
| 388 | 390 |
///Undo the changes until a snapshot. |
| 389 | 391 |
|
| 390 | 392 |
///Undo the changes until a snapshot created by save(). |
| 391 | 393 |
/// |
| 392 | 394 |
///\note After you restored a state, you cannot restore |
| 393 | 395 |
///a later state, in other word you cannot add again the arcs deleted |
| 394 | 396 |
///by restore(). |
| 395 | 397 |
void restore() |
| 396 | 398 |
{
|
| 397 | 399 |
_graph->restoreSnapshot(*this); |
| 398 | 400 |
} |
| 399 | 401 |
}; |
| 400 | 402 |
}; |
| 401 | 403 |
|
| 402 | 404 |
|
| 403 | 405 |
class SmartGraphBase {
|
| 404 | 406 |
|
| 405 | 407 |
protected: |
| 406 | 408 |
|
| 407 | 409 |
struct NodeT {
|
| 408 | 410 |
int first_out; |
| 409 | 411 |
}; |
| 410 | 412 |
|
| 411 | 413 |
struct ArcT {
|
| 412 | 414 |
int target; |
| 413 | 415 |
int next_out; |
| 414 | 416 |
}; |
| 415 | 417 |
|
| 416 | 418 |
std::vector<NodeT> nodes; |
| 417 | 419 |
std::vector<ArcT> arcs; |
| 418 | 420 |
|
| 419 | 421 |
int first_free_arc; |
| 420 | 422 |
|
| 421 | 423 |
public: |
| 422 | 424 |
|
| 423 | 425 |
typedef SmartGraphBase Digraph; |
| 424 | 426 |
|
| 425 | 427 |
class Node; |
| 426 | 428 |
class Arc; |
| 427 | 429 |
class Edge; |
| 428 | 430 |
|
| 429 | 431 |
class Node {
|
| 430 | 432 |
friend class SmartGraphBase; |
| 431 | 433 |
protected: |
| 432 | 434 |
|
| 433 | 435 |
int _id; |
| 434 | 436 |
explicit Node(int id) { _id = id;}
|
| 435 | 437 |
|
| 436 | 438 |
public: |
| 437 | 439 |
Node() {}
|
| 438 | 440 |
Node (Invalid) { _id = -1; }
|
| 439 | 441 |
bool operator==(const Node& node) const {return _id == node._id;}
|
| 440 | 442 |
bool operator!=(const Node& node) const {return _id != node._id;}
|
| 441 | 443 |
bool operator<(const Node& node) const {return _id < node._id;}
|
| 442 | 444 |
}; |
| 443 | 445 |
|
| 444 | 446 |
class Edge {
|
| 445 | 447 |
friend class SmartGraphBase; |
| 446 | 448 |
protected: |
| 447 | 449 |
|
| 448 | 450 |
int _id; |
| 449 | 451 |
explicit Edge(int id) { _id = id;}
|
| 450 | 452 |
|
| 451 | 453 |
public: |
| 452 | 454 |
Edge() {}
|
| 453 | 455 |
Edge (Invalid) { _id = -1; }
|
| 454 | 456 |
bool operator==(const Edge& arc) const {return _id == arc._id;}
|
| 455 | 457 |
bool operator!=(const Edge& arc) const {return _id != arc._id;}
|
| 456 | 458 |
bool operator<(const Edge& arc) const {return _id < arc._id;}
|
| 457 | 459 |
}; |
| 458 | 460 |
|
| 459 | 461 |
class Arc {
|
| 460 | 462 |
friend class SmartGraphBase; |
| 461 | 463 |
protected: |
| 462 | 464 |
|
| 463 | 465 |
int _id; |
| 464 | 466 |
explicit Arc(int id) { _id = id;}
|
| 465 | 467 |
|
| 466 | 468 |
public: |
| 467 | 469 |
operator Edge() const {
|
| 468 | 470 |
return _id != -1 ? edgeFromId(_id / 2) : INVALID; |
| 469 | 471 |
} |
| 470 | 472 |
|
| 471 | 473 |
Arc() {}
|
| 472 | 474 |
Arc (Invalid) { _id = -1; }
|
| 473 | 475 |
bool operator==(const Arc& arc) const {return _id == arc._id;}
|
| 474 | 476 |
bool operator!=(const Arc& arc) const {return _id != arc._id;}
|
| 475 | 477 |
bool operator<(const Arc& arc) const {return _id < arc._id;}
|
| 476 | 478 |
}; |
| 477 | 479 |
|
| 478 | 480 |
|
| 479 | 481 |
|
| 480 | 482 |
SmartGraphBase() |
| 481 | 483 |
: nodes(), arcs() {}
|
| 482 | 484 |
|
| 483 | 485 |
typedef True NodeNumTag; |
| 484 | 486 |
typedef True EdgeNumTag; |
| 485 | 487 |
typedef True ArcNumTag; |
| 486 | 488 |
|
| 487 | 489 |
int nodeNum() const { return nodes.size(); }
|
| 488 | 490 |
int edgeNum() const { return arcs.size() / 2; }
|
| 489 | 491 |
int arcNum() const { return arcs.size(); }
|
| 490 | 492 |
|
| 491 | 493 |
int maxNodeId() const { return nodes.size()-1; }
|
| 492 | 494 |
int maxEdgeId() const { return arcs.size() / 2 - 1; }
|
| 493 | 495 |
int maxArcId() const { return arcs.size()-1; }
|
| 494 | 496 |
|
| 495 | 497 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); }
|
| 496 | 498 |
Node target(Arc e) const { return Node(arcs[e._id].target); }
|
| 497 | 499 |
|
| 498 | 500 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); }
|
| 499 | 501 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
|
| 500 | 502 |
|
0 comments (0 inline)