gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fixes and improvements related to GridGraph
0 2 0
default
2 files changed with 9 insertions and 8 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -139,96 +139,97 @@
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 {
... ...
@@ -427,121 +428,121 @@
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 digraph" row_num=\textrow_num
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
  /// The graph type is fully conform to the \ref concepts::Graph
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
      ///
Ignore white space 96 line context
... ...
@@ -147,98 +147,98 @@
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;
0 comments (0 inline)