0
2
0
| ... | ... |
@@ -91,192 +91,193 @@ |
| 91 | 91 |
Node u(Edge edge) const {
|
| 92 | 92 |
if (edge._id < _edge_limit) {
|
| 93 | 93 |
return edge._id; |
| 94 | 94 |
} else {
|
| 95 | 95 |
return (edge._id - _edge_limit) % (_width - 1) + |
| 96 | 96 |
(edge._id - _edge_limit) / (_width - 1) * _width; |
| 97 | 97 |
} |
| 98 | 98 |
} |
| 99 | 99 |
|
| 100 | 100 |
Node v(Edge edge) const {
|
| 101 | 101 |
if (edge._id < _edge_limit) {
|
| 102 | 102 |
return edge._id + _width; |
| 103 | 103 |
} else {
|
| 104 | 104 |
return (edge._id - _edge_limit) % (_width - 1) + |
| 105 | 105 |
(edge._id - _edge_limit) / (_width - 1) * _width + 1; |
| 106 | 106 |
} |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
Node source(Arc arc) const {
|
| 110 | 110 |
return (arc._id & 1) == 1 ? u(arc) : v(arc); |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
Node target(Arc arc) const {
|
| 114 | 114 |
return (arc._id & 1) == 1 ? v(arc) : u(arc); |
| 115 | 115 |
} |
| 116 | 116 |
|
| 117 | 117 |
static int id(Node node) { return node._id; }
|
| 118 | 118 |
static int id(Edge edge) { return edge._id; }
|
| 119 | 119 |
static int id(Arc arc) { return arc._id; }
|
| 120 | 120 |
|
| 121 | 121 |
int maxNodeId() const { return _node_num - 1; }
|
| 122 | 122 |
int maxEdgeId() const { return _edge_num - 1; }
|
| 123 | 123 |
int maxArcId() const { return 2 * _edge_num - 1; }
|
| 124 | 124 |
|
| 125 | 125 |
static Node nodeFromId(int id) { return Node(id);}
|
| 126 | 126 |
static Edge edgeFromId(int id) { return Edge(id);}
|
| 127 | 127 |
static Arc arcFromId(int id) { return Arc(id);}
|
| 128 | 128 |
|
| 129 | 129 |
typedef True FindEdgeTag; |
| 130 | 130 |
|
| 131 | 131 |
Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
|
| 132 | 132 |
if (prev != INVALID) return INVALID; |
| 133 | 133 |
if (v._id > u._id) {
|
| 134 | 134 |
if (v._id - u._id == _width) |
| 135 | 135 |
return Edge(u._id); |
| 136 | 136 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) {
|
| 137 | 137 |
return Edge(u._id / _width * (_width - 1) + |
| 138 | 138 |
u._id % _width + _edge_limit); |
| 139 | 139 |
} |
| 140 | 140 |
} else {
|
| 141 | 141 |
if (u._id - v._id == _width) |
| 142 | 142 |
return Edge(v._id); |
| 143 | 143 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) {
|
| 144 | 144 |
return Edge(v._id / _width * (_width - 1) + |
| 145 | 145 |
v._id % _width + _edge_limit); |
| 146 | 146 |
} |
| 147 | 147 |
} |
| 148 | 148 |
return INVALID; |
| 149 | 149 |
} |
| 150 | 150 |
|
| 151 | 151 |
Arc findArc(Node u, Node v, Arc prev = INVALID) const {
|
| 152 | 152 |
if (prev != INVALID) return INVALID; |
| 153 | 153 |
if (v._id > u._id) {
|
| 154 | 154 |
if (v._id - u._id == _width) |
| 155 | 155 |
return Arc((u._id << 1) | 1); |
| 156 | 156 |
if (v._id - u._id == 1 && u._id % _width < _width - 1) {
|
| 157 | 157 |
return Arc(((u._id / _width * (_width - 1) + |
| 158 | 158 |
u._id % _width + _edge_limit) << 1) | 1); |
| 159 | 159 |
} |
| 160 | 160 |
} else {
|
| 161 | 161 |
if (u._id - v._id == _width) |
| 162 | 162 |
return Arc(v._id << 1); |
| 163 | 163 |
if (u._id - v._id == 1 && v._id % _width < _width - 1) {
|
| 164 | 164 |
return Arc((v._id / _width * (_width - 1) + |
| 165 | 165 |
v._id % _width + _edge_limit) << 1); |
| 166 | 166 |
} |
| 167 | 167 |
} |
| 168 | 168 |
return INVALID; |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
class Node {
|
| 172 | 172 |
friend class GridGraphBase; |
| 173 | 173 |
|
| 174 | 174 |
protected: |
| 175 | 175 |
int _id; |
| 176 | 176 |
Node(int id) : _id(id) {}
|
| 177 | 177 |
public: |
| 178 | 178 |
Node() {}
|
| 179 | 179 |
Node (Invalid) : _id(-1) {}
|
| 180 | 180 |
bool operator==(const Node node) const {return _id == node._id;}
|
| 181 | 181 |
bool operator!=(const Node node) const {return _id != node._id;}
|
| 182 | 182 |
bool operator<(const Node node) const {return _id < node._id;}
|
| 183 | 183 |
}; |
| 184 | 184 |
|
| 185 | 185 |
class Edge {
|
| 186 | 186 |
friend class GridGraphBase; |
| 187 |
friend class Arc; |
|
| 187 | 188 |
|
| 188 | 189 |
protected: |
| 189 | 190 |
int _id; |
| 190 | 191 |
|
| 191 | 192 |
Edge(int id) : _id(id) {}
|
| 192 | 193 |
|
| 193 | 194 |
public: |
| 194 | 195 |
Edge() {}
|
| 195 | 196 |
Edge (Invalid) : _id(-1) {}
|
| 196 | 197 |
bool operator==(const Edge edge) const {return _id == edge._id;}
|
| 197 | 198 |
bool operator!=(const Edge edge) const {return _id != edge._id;}
|
| 198 | 199 |
bool operator<(const Edge edge) const {return _id < edge._id;}
|
| 199 | 200 |
}; |
| 200 | 201 |
|
| 201 | 202 |
class Arc {
|
| 202 | 203 |
friend class GridGraphBase; |
| 203 | 204 |
|
| 204 | 205 |
protected: |
| 205 | 206 |
int _id; |
| 206 | 207 |
|
| 207 | 208 |
Arc(int id) : _id(id) {}
|
| 208 | 209 |
|
| 209 | 210 |
public: |
| 210 | 211 |
Arc() {}
|
| 211 | 212 |
Arc (Invalid) : _id(-1) {}
|
| 212 | 213 |
operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
|
| 213 | 214 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
| 214 | 215 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
| 215 | 216 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
| 216 | 217 |
}; |
| 217 | 218 |
|
| 218 | 219 |
static bool direction(Arc arc) {
|
| 219 | 220 |
return (arc._id & 1) == 1; |
| 220 | 221 |
} |
| 221 | 222 |
|
| 222 | 223 |
static Arc direct(Edge edge, bool dir) {
|
| 223 | 224 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
| 224 | 225 |
} |
| 225 | 226 |
|
| 226 | 227 |
void first(Node& node) const {
|
| 227 | 228 |
node._id = _node_num - 1; |
| 228 | 229 |
} |
| 229 | 230 |
|
| 230 | 231 |
static void next(Node& node) {
|
| 231 | 232 |
--node._id; |
| 232 | 233 |
} |
| 233 | 234 |
|
| 234 | 235 |
void first(Edge& edge) const {
|
| 235 | 236 |
edge._id = _edge_num - 1; |
| 236 | 237 |
} |
| 237 | 238 |
|
| 238 | 239 |
static void next(Edge& edge) {
|
| 239 | 240 |
--edge._id; |
| 240 | 241 |
} |
| 241 | 242 |
|
| 242 | 243 |
void first(Arc& arc) const {
|
| 243 | 244 |
arc._id = 2 * _edge_num - 1; |
| 244 | 245 |
} |
| 245 | 246 |
|
| 246 | 247 |
static void next(Arc& arc) {
|
| 247 | 248 |
--arc._id; |
| 248 | 249 |
} |
| 249 | 250 |
|
| 250 | 251 |
void firstOut(Arc& arc, const Node& node) const {
|
| 251 | 252 |
if (node._id % _width < _width - 1) {
|
| 252 | 253 |
arc._id = (_edge_limit + node._id % _width + |
| 253 | 254 |
(node._id / _width) * (_width - 1)) << 1 | 1; |
| 254 | 255 |
return; |
| 255 | 256 |
} |
| 256 | 257 |
if (node._id < _node_num - _width) {
|
| 257 | 258 |
arc._id = node._id << 1 | 1; |
| 258 | 259 |
return; |
| 259 | 260 |
} |
| 260 | 261 |
if (node._id % _width > 0) {
|
| 261 | 262 |
arc._id = (_edge_limit + node._id % _width + |
| 262 | 263 |
(node._id / _width) * (_width - 1) - 1) << 1; |
| 263 | 264 |
return; |
| 264 | 265 |
} |
| 265 | 266 |
if (node._id >= _width) {
|
| 266 | 267 |
arc._id = (node._id - _width) << 1; |
| 267 | 268 |
return; |
| 268 | 269 |
} |
| 269 | 270 |
arc._id = -1; |
| 270 | 271 |
} |
| 271 | 272 |
|
| 272 | 273 |
void nextOut(Arc& arc) const {
|
| 273 | 274 |
int nid = arc._id >> 1; |
| 274 | 275 |
if ((arc._id & 1) == 1) {
|
| 275 | 276 |
if (nid >= _edge_limit) {
|
| 276 | 277 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 277 | 278 |
(nid - _edge_limit) / (_width - 1) * _width; |
| 278 | 279 |
if (nid < _node_num - _width) {
|
| 279 | 280 |
arc._id = nid << 1 | 1; |
| 280 | 281 |
return; |
| 281 | 282 |
} |
| 282 | 283 |
} |
| ... | ... |
@@ -379,217 +380,217 @@ |
| 379 | 380 |
edge._id = node._id - _width; |
| 380 | 381 |
dir = false; |
| 381 | 382 |
return; |
| 382 | 383 |
} |
| 383 | 384 |
edge._id = -1; |
| 384 | 385 |
dir = true; |
| 385 | 386 |
} |
| 386 | 387 |
|
| 387 | 388 |
void nextInc(Edge& edge, bool& dir) const {
|
| 388 | 389 |
int nid = edge._id; |
| 389 | 390 |
if (dir) {
|
| 390 | 391 |
if (nid >= _edge_limit) {
|
| 391 | 392 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 392 | 393 |
(nid - _edge_limit) / (_width - 1) * _width; |
| 393 | 394 |
if (nid < _node_num - _width) {
|
| 394 | 395 |
edge._id = nid; |
| 395 | 396 |
return; |
| 396 | 397 |
} |
| 397 | 398 |
} |
| 398 | 399 |
if (nid % _width > 0) {
|
| 399 | 400 |
edge._id = _edge_limit + nid % _width + |
| 400 | 401 |
(nid / _width) * (_width - 1) - 1; |
| 401 | 402 |
dir = false; |
| 402 | 403 |
return; |
| 403 | 404 |
} |
| 404 | 405 |
if (nid >= _width) {
|
| 405 | 406 |
edge._id = nid - _width; |
| 406 | 407 |
dir = false; |
| 407 | 408 |
return; |
| 408 | 409 |
} |
| 409 | 410 |
} else {
|
| 410 | 411 |
if (nid >= _edge_limit) {
|
| 411 | 412 |
nid = (nid - _edge_limit) % (_width - 1) + |
| 412 | 413 |
(nid - _edge_limit) / (_width - 1) * _width + 1; |
| 413 | 414 |
if (nid >= _width) {
|
| 414 | 415 |
edge._id = nid - _width; |
| 415 | 416 |
return; |
| 416 | 417 |
} |
| 417 | 418 |
} |
| 418 | 419 |
} |
| 419 | 420 |
edge._id = -1; |
| 420 | 421 |
dir = true; |
| 421 | 422 |
} |
| 422 | 423 |
|
| 423 | 424 |
Arc right(Node n) const {
|
| 424 | 425 |
if (n._id % _width < _width - 1) {
|
| 425 | 426 |
return Arc(((_edge_limit + n._id % _width + |
| 426 | 427 |
(n._id / _width) * (_width - 1)) << 1) | 1); |
| 427 | 428 |
} else {
|
| 428 | 429 |
return INVALID; |
| 429 | 430 |
} |
| 430 | 431 |
} |
| 431 | 432 |
|
| 432 | 433 |
Arc left(Node n) const {
|
| 433 | 434 |
if (n._id % _width > 0) {
|
| 434 | 435 |
return Arc((_edge_limit + n._id % _width + |
| 435 | 436 |
(n._id / _width) * (_width - 1) - 1) << 1); |
| 436 | 437 |
} else {
|
| 437 | 438 |
return INVALID; |
| 438 | 439 |
} |
| 439 | 440 |
} |
| 440 | 441 |
|
| 441 | 442 |
Arc up(Node n) const {
|
| 442 | 443 |
if (n._id < _edge_limit) {
|
| 443 | 444 |
return Arc((n._id << 1) | 1); |
| 444 | 445 |
} else {
|
| 445 | 446 |
return INVALID; |
| 446 | 447 |
} |
| 447 | 448 |
} |
| 448 | 449 |
|
| 449 | 450 |
Arc down(Node n) const {
|
| 450 | 451 |
if (n._id >= _width) {
|
| 451 | 452 |
return Arc((n._id - _width) << 1); |
| 452 | 453 |
} else {
|
| 453 | 454 |
return INVALID; |
| 454 | 455 |
} |
| 455 | 456 |
} |
| 456 | 457 |
|
| 457 | 458 |
private: |
| 458 | 459 |
int _width, _height; |
| 459 | 460 |
int _node_num, _edge_num; |
| 460 | 461 |
int _edge_limit; |
| 461 | 462 |
}; |
| 462 | 463 |
|
| 463 | 464 |
|
| 464 | 465 |
typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase; |
| 465 | 466 |
|
| 466 | 467 |
/// \ingroup graphs |
| 467 | 468 |
/// |
| 468 | 469 |
/// \brief Grid graph class |
| 469 | 470 |
/// |
| 470 | 471 |
/// This class implements a special graph type. The nodes of the |
| 471 | 472 |
/// graph can be indexed by two integer \c (i,j) value where \c i is |
| 472 | 473 |
/// in the \c [0..width()-1] range and j is in the \c |
| 473 | 474 |
/// [0..height()-1] range. Two nodes are connected in the graph if |
| 474 | 475 |
/// the indexes differ exactly on one position and exactly one is |
| 475 |
/// the difference. The nodes of the graph be indexed by position |
|
| 476 |
/// with \c operator()() function. The positions of the nodes can be |
|
| 476 |
/// the difference. The nodes of the graph can be indexed by position |
|
| 477 |
/// with the \c operator()() function. The positions of the nodes can be |
|
| 477 | 478 |
/// get with \c pos(), \c col() and \c row() members. The outgoing |
| 478 | 479 |
/// arcs can be retrieved with the \c right(), \c up(), \c left() |
| 479 | 480 |
/// and \c down() functions, where the bottom-left corner is the |
| 480 | 481 |
/// origin. |
| 481 | 482 |
/// |
| 482 | 483 |
/// \image html grid_graph.png |
| 483 |
/// \image latex grid_graph.eps "Grid |
|
| 484 |
/// \image latex grid_graph.eps "Grid graph" row_num=\textrow_num |
|
| 484 | 485 |
/// |
| 485 | 486 |
/// A short example about the basic usage: |
| 486 | 487 |
///\code |
| 487 | 488 |
/// GridGraph graph(rows, cols); |
| 488 | 489 |
/// GridGraph::NodeMap<int> val(graph); |
| 489 | 490 |
/// for (int i = 0; i < graph.width(); ++i) {
|
| 490 | 491 |
/// for (int j = 0; j < graph.height(); ++j) {
|
| 491 | 492 |
/// val[graph(i, j)] = i + j; |
| 492 | 493 |
/// } |
| 493 | 494 |
/// } |
| 494 | 495 |
///\endcode |
| 495 | 496 |
/// |
| 496 |
/// |
|
| 497 |
/// This graph type is fully conform to the \ref concepts::Graph |
|
| 497 | 498 |
/// "Graph" concept, and it also has an important extra feature |
| 498 |
/// that its maps are real \ref concepts::ReferenceMap "reference |
|
| 499 |
/// map"s. |
|
| 499 |
/// that its maps are real \ref concepts::ReferenceMap |
|
| 500 |
/// "reference map"s. |
|
| 500 | 501 |
class GridGraph : public ExtendedGridGraphBase {
|
| 501 | 502 |
public: |
| 502 | 503 |
|
| 503 | 504 |
typedef ExtendedGridGraphBase Parent; |
| 504 | 505 |
|
| 505 | 506 |
/// \brief Map to get the indices of the nodes as dim2::Point<int>. |
| 506 | 507 |
/// |
| 507 | 508 |
/// Map to get the indices of the nodes as dim2::Point<int>. |
| 508 | 509 |
class IndexMap {
|
| 509 | 510 |
public: |
| 510 | 511 |
/// \brief The key type of the map |
| 511 | 512 |
typedef GridGraph::Node Key; |
| 512 | 513 |
/// \brief The value type of the map |
| 513 | 514 |
typedef dim2::Point<int> Value; |
| 514 | 515 |
|
| 515 | 516 |
/// \brief Constructor |
| 516 | 517 |
/// |
| 517 | 518 |
/// Constructor |
| 518 | 519 |
IndexMap(const GridGraph& graph) : _graph(graph) {}
|
| 519 | 520 |
|
| 520 | 521 |
/// \brief The subscript operator |
| 521 | 522 |
/// |
| 522 | 523 |
/// The subscript operator. |
| 523 | 524 |
Value operator[](Key key) const {
|
| 524 | 525 |
return _graph.pos(key); |
| 525 | 526 |
} |
| 526 | 527 |
|
| 527 | 528 |
private: |
| 528 | 529 |
const GridGraph& _graph; |
| 529 | 530 |
}; |
| 530 | 531 |
|
| 531 | 532 |
/// \brief Map to get the column of the nodes. |
| 532 | 533 |
/// |
| 533 | 534 |
/// Map to get the column of the nodes. |
| 534 | 535 |
class ColMap {
|
| 535 | 536 |
public: |
| 536 | 537 |
/// \brief The key type of the map |
| 537 | 538 |
typedef GridGraph::Node Key; |
| 538 | 539 |
/// \brief The value type of the map |
| 539 | 540 |
typedef int Value; |
| 540 | 541 |
|
| 541 | 542 |
/// \brief Constructor |
| 542 | 543 |
/// |
| 543 | 544 |
/// Constructor |
| 544 | 545 |
ColMap(const GridGraph& graph) : _graph(graph) {}
|
| 545 | 546 |
|
| 546 | 547 |
/// \brief The subscript operator |
| 547 | 548 |
/// |
| 548 | 549 |
/// The subscript operator. |
| 549 | 550 |
Value operator[](Key key) const {
|
| 550 | 551 |
return _graph.col(key); |
| 551 | 552 |
} |
| 552 | 553 |
|
| 553 | 554 |
private: |
| 554 | 555 |
const GridGraph& _graph; |
| 555 | 556 |
}; |
| 556 | 557 |
|
| 557 | 558 |
/// \brief Map to get the row of the nodes. |
| 558 | 559 |
/// |
| 559 | 560 |
/// Map to get the row of the nodes. |
| 560 | 561 |
class RowMap {
|
| 561 | 562 |
public: |
| 562 | 563 |
/// \brief The key type of the map |
| 563 | 564 |
typedef GridGraph::Node Key; |
| 564 | 565 |
/// \brief The value type of the map |
| 565 | 566 |
typedef int Value; |
| 566 | 567 |
|
| 567 | 568 |
/// \brief Constructor |
| 568 | 569 |
/// |
| 569 | 570 |
/// Constructor |
| 570 | 571 |
RowMap(const GridGraph& graph) : _graph(graph) {}
|
| 571 | 572 |
|
| 572 | 573 |
/// \brief The subscript operator |
| 573 | 574 |
/// |
| 574 | 575 |
/// The subscript operator. |
| 575 | 576 |
Value operator[](Key key) const {
|
| 576 | 577 |
return _graph.row(key); |
| 577 | 578 |
} |
| 578 | 579 |
|
| 579 | 580 |
private: |
| 580 | 581 |
const GridGraph& _graph; |
| 581 | 582 |
}; |
| 582 | 583 |
|
| 583 | 584 |
/// \brief Constructor |
| 584 | 585 |
/// |
| 585 | 586 |
/// Construct a grid graph with given size. |
| 586 | 587 |
GridGraph(int width, int height) { construct(width, height); }
|
| 587 | 588 |
|
| 588 | 589 |
/// \brief Resize the graph |
| 589 | 590 |
/// |
| 590 | 591 |
/// Resize the graph. The function will fully destroy and rebuild |
| 591 | 592 |
/// the graph. This cause that the maps of the graph will |
| 592 | 593 |
/// reallocated automatically and the previous values will be |
| 593 | 594 |
/// lost. |
| 594 | 595 |
void resize(int width, int height) {
|
| 595 | 596 |
Parent::notifier(Arc()).clear(); |
| ... | ... |
@@ -99,194 +99,194 @@ |
| 99 | 99 |
{ // Checking graph components
|
| 100 | 100 |
checkConcept<BaseGraphComponent, BaseGraphComponent >(); |
| 101 | 101 |
|
| 102 | 102 |
checkConcept<IDableGraphComponent<>, |
| 103 | 103 |
IDableGraphComponent<> >(); |
| 104 | 104 |
|
| 105 | 105 |
checkConcept<IterableGraphComponent<>, |
| 106 | 106 |
IterableGraphComponent<> >(); |
| 107 | 107 |
|
| 108 | 108 |
checkConcept<MappableGraphComponent<>, |
| 109 | 109 |
MappableGraphComponent<> >(); |
| 110 | 110 |
} |
| 111 | 111 |
{ // Checking skeleton graph
|
| 112 | 112 |
checkConcept<Graph, Graph>(); |
| 113 | 113 |
} |
| 114 | 114 |
{ // Checking ListGraph
|
| 115 | 115 |
checkConcept<Graph, ListGraph>(); |
| 116 | 116 |
checkConcept<AlterableGraphComponent<>, ListGraph>(); |
| 117 | 117 |
checkConcept<ExtendableGraphComponent<>, ListGraph>(); |
| 118 | 118 |
checkConcept<ClearableGraphComponent<>, ListGraph>(); |
| 119 | 119 |
checkConcept<ErasableGraphComponent<>, ListGraph>(); |
| 120 | 120 |
} |
| 121 | 121 |
{ // Checking SmartGraph
|
| 122 | 122 |
checkConcept<Graph, SmartGraph>(); |
| 123 | 123 |
checkConcept<AlterableGraphComponent<>, SmartGraph>(); |
| 124 | 124 |
checkConcept<ExtendableGraphComponent<>, SmartGraph>(); |
| 125 | 125 |
checkConcept<ClearableGraphComponent<>, SmartGraph>(); |
| 126 | 126 |
} |
| 127 | 127 |
// { // Checking FullGraph
|
| 128 | 128 |
// checkConcept<Graph, FullGraph>(); |
| 129 | 129 |
// checkGraphIterators<FullGraph>(); |
| 130 | 130 |
// } |
| 131 | 131 |
{ // Checking GridGraph
|
| 132 | 132 |
checkConcept<Graph, GridGraph>(); |
| 133 | 133 |
} |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
template <typename Graph> |
| 137 | 137 |
void checkGraphValidity() {
|
| 138 | 138 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 139 | 139 |
Graph g; |
| 140 | 140 |
|
| 141 | 141 |
Node |
| 142 | 142 |
n1 = g.addNode(), |
| 143 | 143 |
n2 = g.addNode(), |
| 144 | 144 |
n3 = g.addNode(); |
| 145 | 145 |
|
| 146 | 146 |
Edge |
| 147 | 147 |
e1 = g.addEdge(n1, n2), |
| 148 | 148 |
e2 = g.addEdge(n2, n3); |
| 149 | 149 |
|
| 150 | 150 |
check(g.valid(n1), "Wrong validity check"); |
| 151 | 151 |
check(g.valid(e1), "Wrong validity check"); |
| 152 | 152 |
check(g.valid(g.direct(e1, true)), "Wrong validity check"); |
| 153 | 153 |
|
| 154 | 154 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
| 155 | 155 |
check(!g.valid(g.edgeFromId(-1)), "Wrong validity check"); |
| 156 | 156 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
| 157 | 157 |
} |
| 158 | 158 |
|
| 159 | 159 |
template <typename Graph> |
| 160 | 160 |
void checkGraphValidityErase() {
|
| 161 | 161 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 162 | 162 |
Graph g; |
| 163 | 163 |
|
| 164 | 164 |
Node |
| 165 | 165 |
n1 = g.addNode(), |
| 166 | 166 |
n2 = g.addNode(), |
| 167 | 167 |
n3 = g.addNode(); |
| 168 | 168 |
|
| 169 | 169 |
Edge |
| 170 | 170 |
e1 = g.addEdge(n1, n2), |
| 171 | 171 |
e2 = g.addEdge(n2, n3); |
| 172 | 172 |
|
| 173 | 173 |
check(g.valid(n1), "Wrong validity check"); |
| 174 | 174 |
check(g.valid(e1), "Wrong validity check"); |
| 175 | 175 |
check(g.valid(g.direct(e1, true)), "Wrong validity check"); |
| 176 | 176 |
|
| 177 | 177 |
g.erase(n1); |
| 178 | 178 |
|
| 179 | 179 |
check(!g.valid(n1), "Wrong validity check"); |
| 180 | 180 |
check(g.valid(n2), "Wrong validity check"); |
| 181 | 181 |
check(g.valid(n3), "Wrong validity check"); |
| 182 | 182 |
check(!g.valid(e1), "Wrong validity check"); |
| 183 | 183 |
check(g.valid(e2), "Wrong validity check"); |
| 184 | 184 |
|
| 185 | 185 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
| 186 | 186 |
check(!g.valid(g.edgeFromId(-1)), "Wrong validity check"); |
| 187 | 187 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
| 188 | 188 |
} |
| 189 | 189 |
|
| 190 | 190 |
void checkGridGraph(int width, int height) {
|
| 191 | 191 |
typedef GridGraph Graph; |
| 192 | 192 |
GRAPH_TYPEDEFS(Graph); |
| 193 | 193 |
Graph G(width, height); |
| 194 | 194 |
|
| 195 |
check(G.width() == width, "Wrong row number"); |
|
| 196 |
check(G.height() == height, "Wrong column number"); |
|
| 195 |
check(G.width() == width, "Wrong column number"); |
|
| 196 |
check(G.height() == height, "Wrong row number"); |
|
| 197 | 197 |
|
| 198 | 198 |
for (int i = 0; i < width; ++i) {
|
| 199 | 199 |
for (int j = 0; j < height; ++j) {
|
| 200 | 200 |
check(G.col(G(i, j)) == i, "Wrong column"); |
| 201 | 201 |
check(G.row(G(i, j)) == j, "Wrong row"); |
| 202 | 202 |
check(G.pos(G(i, j)).x == i, "Wrong column"); |
| 203 | 203 |
check(G.pos(G(i, j)).y == j, "Wrong row"); |
| 204 | 204 |
} |
| 205 | 205 |
} |
| 206 | 206 |
|
| 207 | 207 |
for (int j = 0; j < height; ++j) {
|
| 208 | 208 |
for (int i = 0; i < width - 1; ++i) {
|
| 209 | 209 |
check(G.source(G.right(G(i, j))) == G(i, j), "Wrong right"); |
| 210 | 210 |
check(G.target(G.right(G(i, j))) == G(i + 1, j), "Wrong right"); |
| 211 | 211 |
} |
| 212 | 212 |
check(G.right(G(width - 1, j)) == INVALID, "Wrong right"); |
| 213 | 213 |
} |
| 214 | 214 |
|
| 215 | 215 |
for (int j = 0; j < height; ++j) {
|
| 216 | 216 |
for (int i = 1; i < width; ++i) {
|
| 217 | 217 |
check(G.source(G.left(G(i, j))) == G(i, j), "Wrong left"); |
| 218 | 218 |
check(G.target(G.left(G(i, j))) == G(i - 1, j), "Wrong left"); |
| 219 | 219 |
} |
| 220 | 220 |
check(G.left(G(0, j)) == INVALID, "Wrong left"); |
| 221 | 221 |
} |
| 222 | 222 |
|
| 223 | 223 |
for (int i = 0; i < width; ++i) {
|
| 224 | 224 |
for (int j = 0; j < height - 1; ++j) {
|
| 225 | 225 |
check(G.source(G.up(G(i, j))) == G(i, j), "Wrong up"); |
| 226 | 226 |
check(G.target(G.up(G(i, j))) == G(i, j + 1), "Wrong up"); |
| 227 | 227 |
} |
| 228 | 228 |
check(G.up(G(i, height - 1)) == INVALID, "Wrong up"); |
| 229 | 229 |
} |
| 230 | 230 |
|
| 231 | 231 |
for (int i = 0; i < width; ++i) {
|
| 232 | 232 |
for (int j = 1; j < height; ++j) {
|
| 233 | 233 |
check(G.source(G.down(G(i, j))) == G(i, j), "Wrong down"); |
| 234 | 234 |
check(G.target(G.down(G(i, j))) == G(i, j - 1), "Wrong down"); |
| 235 | 235 |
} |
| 236 | 236 |
check(G.down(G(i, 0)) == INVALID, "Wrong down"); |
| 237 | 237 |
} |
| 238 | 238 |
|
| 239 | 239 |
checkGraphNodeList(G, width * height); |
| 240 | 240 |
checkGraphEdgeList(G, width * (height - 1) + (width - 1) * height); |
| 241 | 241 |
checkGraphArcList(G, 2 * (width * (height - 1) + (width - 1) * height)); |
| 242 | 242 |
|
| 243 | 243 |
for (NodeIt n(G); n != INVALID; ++n) {
|
| 244 | 244 |
int nb = 4; |
| 245 | 245 |
if (G.col(n) == 0) --nb; |
| 246 | 246 |
if (G.col(n) == width - 1) --nb; |
| 247 | 247 |
if (G.row(n) == 0) --nb; |
| 248 | 248 |
if (G.row(n) == height - 1) --nb; |
| 249 | 249 |
|
| 250 | 250 |
checkGraphOutArcList(G, n, nb); |
| 251 | 251 |
checkGraphInArcList(G, n, nb); |
| 252 | 252 |
checkGraphIncEdgeList(G, n, nb); |
| 253 | 253 |
} |
| 254 | 254 |
|
| 255 | 255 |
checkArcDirections(G); |
| 256 | 256 |
|
| 257 | 257 |
checkGraphConArcList(G, 2 * (width * (height - 1) + (width - 1) * height)); |
| 258 | 258 |
checkGraphConEdgeList(G, width * (height - 1) + (width - 1) * height); |
| 259 | 259 |
|
| 260 | 260 |
checkNodeIds(G); |
| 261 | 261 |
checkArcIds(G); |
| 262 | 262 |
checkEdgeIds(G); |
| 263 | 263 |
checkGraphNodeMap(G); |
| 264 | 264 |
checkGraphArcMap(G); |
| 265 | 265 |
checkGraphEdgeMap(G); |
| 266 | 266 |
|
| 267 | 267 |
} |
| 268 | 268 |
|
| 269 | 269 |
void checkGraphs() {
|
| 270 | 270 |
{ // Checking ListGraph
|
| 271 | 271 |
checkGraph<ListGraph>(); |
| 272 | 272 |
checkGraphValidityErase<ListGraph>(); |
| 273 | 273 |
} |
| 274 | 274 |
{ // Checking SmartGraph
|
| 275 | 275 |
checkGraph<SmartGraph>(); |
| 276 | 276 |
checkGraphValidity<SmartGraph>(); |
| 277 | 277 |
} |
| 278 | 278 |
// { // Checking FullGraph
|
| 279 | 279 |
// FullGraph g(5); |
| 280 | 280 |
// checkGraphNodeList(g, 5); |
| 281 | 281 |
// checkGraphEdgeList(g, 10); |
| 282 | 282 |
// } |
| 283 | 283 |
{ // Checking GridGraph
|
| 284 | 284 |
checkGridGraph(5, 8); |
| 285 | 285 |
checkGridGraph(8, 5); |
| 286 | 286 |
checkGridGraph(5, 5); |
| 287 | 287 |
checkGridGraph(0, 0); |
| 288 | 288 |
checkGridGraph(1, 1); |
| 289 | 289 |
} |
| 290 | 290 |
} |
| 291 | 291 |
|
| 292 | 292 |
int main() {
|
0 comments (0 inline)