gravatar
alpar (Alpar Juttner)
alpar@cs.elte.hu
Doc improvements
0 2 0
default
2 files changed with 73 insertions and 72 deletions:
↑ Collapse diff ↑
Ignore white space 192 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup paths
20 20
///\file
21 21
///\brief Classes for representing paths in digraphs.
22 22
///
23 23

	
24 24
#ifndef LEMON_PATH_H
25 25
#define LEMON_PATH_H
26 26

	
27 27
#include <vector>
28 28
#include <algorithm>
29 29

	
30 30
#include <lemon/path_utils.h>
31 31
#include <lemon/error.h>
32 32
#include <lemon/bits/invalid.h>
33 33

	
34 34
namespace lemon {
35 35

	
36 36
  /// \addtogroup paths
37 37
  /// @{
38 38

	
39 39

	
40 40
  /// \brief A structure for representing directed paths in a digraph.
41 41
  ///
42 42
  /// A structure for representing directed path in a digraph.
43 43
  /// \param Digraph The digraph type in which the path is.
44 44
  ///
45 45
  /// In a sense, the path can be treated as a list of arcs. The
46
  /// lemon path type stores just this list. As a consequence it
47
  /// cannot enumerate the nodes in the path and the zero length paths
48
  /// cannot store the source.
46
  /// lemon path type stores just this list. As a consequence, it
47
  /// cannot enumerate the nodes of the path and the source node of
48
  /// a zero length path is undefined.
49 49
  ///
50 50
  /// This implementation is a back and front insertable and erasable
51 51
  /// path type. It can be indexed in O(1) time. The front and back
52
  /// insertion and erasure is amortized O(1) time. The
53
  /// impelementation is based on two opposite organized vectors.
52
  /// insertion and erase is done in O(1) (amortized) time. The
53
  /// implementation uses two vectors for storing the front and back
54
  /// insertions.
54 55
  template <typename _Digraph>
55 56
  class Path {
56 57
  public:
57 58

	
58 59
    typedef _Digraph Digraph;
59 60
    typedef typename Digraph::Arc Arc;
60 61

	
61 62
    /// \brief Default constructor
62 63
    ///
63 64
    /// Default constructor
64 65
    Path() {}
65 66

	
66 67
    /// \brief Template copy constructor
67 68
    ///
68
    /// This path can be initialized with any other path type. It just
69
    /// makes a copy of the given path.
69
    /// This constuctor initializes the path from any other path type.
70
    /// It simply makes a copy of the given path.
70 71
    template <typename CPath>
71 72
    Path(const CPath& cpath) {
72 73
      copyPath(*this, cpath);
73 74
    }
74 75

	
75 76
    /// \brief Template copy assignment
76 77
    ///
77
    /// This path can be initialized with any other path type. It just
78
    /// makes a copy of the given path.
78
    /// This operator makes a copy of a path of any other type.
79 79
    template <typename CPath>
80 80
    Path& operator=(const CPath& cpath) {
81 81
      copyPath(*this, cpath);
82 82
      return *this;
83 83
    }
84 84

	
85 85
    /// \brief Lemon style iterator for path arcs
86 86
    ///
87 87
    /// This class is used to iterate on the arcs of the paths.
88 88
    class ArcIt {
89 89
      friend class Path;
90 90
    public:
91 91
      /// \brief Default constructor
92 92
      ArcIt() {}
93 93
      /// \brief Invalid constructor
94 94
      ArcIt(Invalid) : path(0), idx(-1) {}
95
      /// \brief Initializate the constructor to the first arc of path
95
      /// \brief Initializate the iterator to the first arc of path
96 96
      ArcIt(const Path &_path) 
97 97
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
98 98

	
99 99
    private:
100 100

	
101 101
      ArcIt(const Path &_path, int _idx) 
102 102
        : path(&_path), idx(_idx) {}
103 103

	
104 104
    public:
105 105

	
106 106
      /// \brief Conversion to Arc
107 107
      operator const Arc&() const {
108 108
        return path->nth(idx);
109 109
      }
110 110

	
111 111
      /// \brief Next arc
112 112
      ArcIt& operator++() { 
113 113
        ++idx;
114 114
        if (idx >= path->length()) idx = -1; 
115 115
        return *this; 
116 116
      }
117 117

	
118 118
      /// \brief Comparison operator
119 119
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
120 120
      /// \brief Comparison operator
121 121
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
122 122
      /// \brief Comparison operator
123 123
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
124 124

	
125 125
    private:
126 126
      const Path *path;
127 127
      int idx;
128 128
    };
129 129

	
130 130
    /// \brief Length of the path.
131 131
    int length() const { return head.size() + tail.size(); }
132
    /// \brief Returns whether the path is empty.
132
    /// \brief Return whether the path is empty.
133 133
    bool empty() const { return head.empty() && tail.empty(); }
134 134

	
135
    /// \brief Resets the path to an empty path.
135
    /// \brief Reset the path to an empty one.
136 136
    void clear() { head.clear(); tail.clear(); }
137 137

	
138
    /// \brief Gives back the nth arc.
138
    /// \brief The nth arc.
139 139
    ///
140 140
    /// \pre n is in the [0..length() - 1] range
141 141
    const Arc& nth(int n) const {
142 142
      return n < int(head.size()) ? *(head.rbegin() + n) :
143 143
        *(tail.begin() + (n - head.size()));
144 144
    }
145 145

	
146
    /// \brief Initializes arc iterator to point to the nth arc
146
    /// \brief Initialize arc iterator to point to the nth arc
147 147
    ///
148 148
    /// \pre n is in the [0..length() - 1] range
149 149
    ArcIt nthIt(int n) const {
150 150
      return ArcIt(*this, n);
151 151
    }
152 152

	
153
    /// \brief Gives back the first arc of the path
153
    /// \brief The first arc of the path
154 154
    const Arc& front() const {
155 155
      return head.empty() ? tail.front() : head.back();
156 156
    }
157 157

	
158 158
    /// \brief Add a new arc before the current path
159 159
    void addFront(const Arc& arc) {
160 160
      head.push_back(arc);
161 161
    }
162 162

	
163 163
    /// \brief Erase the first arc of the path
164 164
    void eraseFront() {
165 165
      if (!head.empty()) {
166 166
        head.pop_back();
167 167
      } else {
168 168
        head.clear();
169 169
        int halfsize = tail.size() / 2;
170 170
        head.resize(halfsize);
171 171
        std::copy(tail.begin() + 1, tail.begin() + halfsize + 1,
172 172
                  head.rbegin());
173 173
        std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin());
174 174
        tail.resize(tail.size() - halfsize - 1);
175 175
      }
176 176
    }
177 177

	
178
    /// \brief Gives back the last arc of the path
178
    /// \brief The last arc of the path
179 179
    const Arc& back() const {
180 180
      return tail.empty() ? head.front() : tail.back();
181 181
    }
182 182

	
183 183
    /// \brief Add a new arc behind the current path
184 184
    void addBack(const Arc& arc) {
185 185
      tail.push_back(arc);
186 186
    }
187 187

	
188 188
    /// \brief Erase the last arc of the path
189 189
    void eraseBack() {
190 190
      if (!tail.empty()) {
191 191
        tail.pop_back();
192 192
      } else {
193 193
        int halfsize = head.size() / 2;
194 194
        tail.resize(halfsize);
195 195
        std::copy(head.begin() + 1, head.begin() + halfsize + 1,
196 196
                  tail.rbegin());
197 197
        std::copy(head.begin() + halfsize + 1, head.end(), head.begin());
198 198
        head.resize(head.size() - halfsize - 1);
199 199
      }
200 200
    }
201 201

	
202

	
203

	
204 202
    typedef True BuildTag;
205 203

	
206 204
    template <typename CPath>
207 205
    void build(const CPath& path) {
208 206
      int len = path.length();
209 207
      tail.reserve(len);
210 208
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
211 209
        tail.push_back(it);
212 210
      }
213 211
    }
214 212

	
215 213
    template <typename CPath>
216 214
    void buildRev(const CPath& path) {
217 215
      int len = path.length();
218 216
      head.reserve(len);
219 217
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
220 218
        head.push_back(it);
221 219
      }
222 220
    }
223 221

	
224 222
  protected:
225 223
    typedef std::vector<Arc> Container;
226 224
    Container head, tail;
227 225

	
228 226
  };
229 227

	
230 228
  /// \brief A structure for representing directed paths in a digraph.
231 229
  ///
232 230
  /// A structure for representing directed path in a digraph.
233 231
  /// \param Digraph The digraph type in which the path is.
234 232
  ///
235 233
  /// In a sense, the path can be treated as a list of arcs. The
236 234
  /// lemon path type stores just this list. As a consequence it
237 235
  /// cannot enumerate the nodes in the path and the zero length paths
238 236
  /// cannot store the source.
239 237
  ///
240 238
  /// This implementation is a just back insertable and erasable path
241 239
  /// type. It can be indexed in O(1) time. The back insertion and
242 240
  /// erasure is amortized O(1) time. This implementation is faster
243 241
  /// then the \c Path type because it use just one vector for the
244 242
  /// arcs.
245 243
  template <typename _Digraph>
246 244
  class SimplePath {
247 245
  public:
248 246

	
249 247
    typedef _Digraph Digraph;
250 248
    typedef typename Digraph::Arc Arc;
251 249

	
252 250
    /// \brief Default constructor
253 251
    ///
254 252
    /// Default constructor
255 253
    SimplePath() {}
256 254

	
257 255
    /// \brief Template copy constructor
258 256
    ///
259 257
    /// This path can be initialized with any other path type. It just
260 258
    /// makes a copy of the given path.
261 259
    template <typename CPath>
262 260
    SimplePath(const CPath& cpath) {
263 261
      copyPath(*this, cpath);
264 262
    }
265 263

	
266 264
    /// \brief Template copy assignment
267 265
    ///
268 266
    /// This path can be initialized with any other path type. It just
269 267
    /// makes a copy of the given path.
270 268
    template <typename CPath>
271 269
    SimplePath& operator=(const CPath& cpath) {
272 270
      copyPath(*this, cpath);
273 271
      return *this;
274 272
    }
275 273

	
276 274
    /// \brief Iterator class to iterate on the arcs of the paths
277 275
    ///
278 276
    /// This class is used to iterate on the arcs of the paths
279 277
    ///
280 278
    /// Of course it converts to Digraph::Arc
281 279
    class ArcIt {
282 280
      friend class SimplePath;
283 281
    public:
284 282
      /// Default constructor
285 283
      ArcIt() {}
286 284
      /// Invalid constructor
287 285
      ArcIt(Invalid) : path(0), idx(-1) {}
288 286
      /// \brief Initializate the constructor to the first arc of path
289 287
      ArcIt(const SimplePath &_path) 
290 288
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
291 289

	
292 290
    private:
293 291

	
294 292
      /// Constructor with starting point
295 293
      ArcIt(const SimplePath &_path, int _idx) 
296 294
        : idx(_idx), path(&_path) {}
297 295

	
298 296
    public:
299 297

	
300 298
      ///Conversion to Digraph::Arc
301 299
      operator const Arc&() const {
302 300
        return path->nth(idx);
303 301
      }
304 302

	
305 303
      /// Next arc
306 304
      ArcIt& operator++() { 
307 305
        ++idx;
308 306
        if (idx >= path->length()) idx = -1; 
309 307
        return *this; 
310 308
      }
311 309

	
312 310
      /// Comparison operator
313 311
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
314 312
      /// Comparison operator
315 313
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
316 314
      /// Comparison operator
317 315
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
318 316

	
319 317
    private:
320 318
      const SimplePath *path;
321 319
      int idx;
322 320
    };
323 321

	
324 322
    /// \brief Length of the path.
325 323
    int length() const { return data.size(); }
326
    /// \brief Returns whether the path is empty.
324
    /// \brief Return true if the path is empty.
327 325
    bool empty() const { return data.empty(); }
328 326

	
329
    /// \brief Resets the path to an empty path.
327
    /// \brief Reset the path to an empty one.
330 328
    void clear() { data.clear(); }
331 329

	
332
    /// \brief Gives back the nth arc.
330
    /// \brief The nth arc.
333 331
    ///
334 332
    /// \pre n is in the [0..length() - 1] range
335 333
    const Arc& nth(int n) const {
336 334
      return data[n];
337 335
    }
338 336

	
339 337
    /// \brief  Initializes arc iterator to point to the nth arc.
340 338
    ArcIt nthIt(int n) const {
341 339
      return ArcIt(*this, n);
342 340
    }
343 341

	
344
    /// \brief Gives back the first arc of the path.
342
    /// \brief The first arc of the path.
345 343
    const Arc& front() const {
346 344
      return data.front();
347 345
    }
348 346

	
349
    /// \brief Gives back the last arc of the path.
347
    /// \brief The last arc of the path.
350 348
    const Arc& back() const {
351 349
      return data.back();
352 350
    }
353 351

	
354 352
    /// \brief Add a new arc behind the current path.
355 353
    void addBack(const Arc& arc) {
356 354
      data.push_back(arc);
357 355
    }
358 356

	
359 357
    /// \brief Erase the last arc of the path
360 358
    void eraseBack() {
361 359
      data.pop_back();
362 360
    }
363 361

	
364 362
    typedef True BuildTag;
365 363

	
366 364
    template <typename CPath>
367 365
    void build(const CPath& path) {
368 366
      int len = path.length();
369 367
      data.resize(len);
370 368
      int index = 0;
371 369
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
372 370
        data[index] = it;;
373 371
        ++index;
374 372
      }
375 373
    }
376 374

	
377 375
    template <typename CPath>
378 376
    void buildRev(const CPath& path) {
379 377
      int len = path.length();
380 378
      data.resize(len);
381 379
      int index = len;
382 380
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
383 381
        --index;
384 382
        data[index] = it;;
385 383
      }
386 384
    }
387 385

	
388 386
  protected:
389 387
    typedef std::vector<Arc> Container;
390 388
    Container data;
391 389

	
392 390
  };
393 391

	
394 392
  /// \brief A structure for representing directed paths in a digraph.
395 393
  ///
396 394
  /// A structure for representing directed path in a digraph.
397 395
  /// \param Digraph The digraph type in which the path is.
398 396
  ///
399 397
  /// In a sense, the path can be treated as a list of arcs. The
400 398
  /// lemon path type stores just this list. As a consequence it
401 399
  /// cannot enumerate the nodes in the path and the zero length paths
402 400
  /// cannot store the source.
403 401
  ///
404 402
  /// This implementation is a back and front insertable and erasable
405 403
  /// path type. It can be indexed in O(k) time, where k is the rank
406 404
  /// of the arc in the path. The length can be computed in O(n)
407 405
  /// time. The front and back insertion and erasure is O(1) time
408 406
  /// and it can be splited and spliced in O(1) time.
409 407
  template <typename _Digraph>
410 408
  class ListPath {
411 409
  public:
412 410

	
413 411
    typedef _Digraph Digraph;
414 412
    typedef typename Digraph::Arc Arc;
415 413

	
416 414
  protected:
417 415

	
418 416
    // the std::list<> is incompatible 
419 417
    // hard to create invalid iterator
420 418
    struct Node {
421 419
      Arc arc;
422 420
      Node *next, *prev;
423 421
    };
424 422

	
425 423
    Node *first, *last;
426 424

	
427 425
    std::allocator<Node> alloc;
428 426

	
429 427
  public:
430 428
 
431 429
    /// \brief Default constructor
432 430
    ///
433 431
    /// Default constructor
434 432
    ListPath() : first(0), last(0) {}
435 433

	
436 434
    /// \brief Template copy constructor
437 435
    ///
438 436
    /// This path can be initialized with any other path type. It just
439 437
    /// makes a copy of the given path.
440 438
    template <typename CPath>
441 439
    ListPath(const CPath& cpath) : first(0), last(0) {
442 440
      copyPath(*this, cpath);
443 441
    }
444 442

	
445 443
    /// \brief Destructor of the path
446 444
    ///
447 445
    /// Destructor of the path
448 446
    ~ListPath() {
449 447
      clear();
450 448
    }
451 449

	
452 450
    /// \brief Template copy assignment
453 451
    ///
454 452
    /// This path can be initialized with any other path type. It just
455 453
    /// makes a copy of the given path.
456 454
    template <typename CPath>
457 455
    ListPath& operator=(const CPath& cpath) {
458 456
      copyPath(*this, cpath);
459 457
      return *this;
460 458
    }
461 459

	
462 460
    /// \brief Iterator class to iterate on the arcs of the paths
463 461
    ///
464 462
    /// This class is used to iterate on the arcs of the paths
465 463
    ///
466 464
    /// Of course it converts to Digraph::Arc
467 465
    class ArcIt {
468 466
      friend class ListPath;
469 467
    public:
470 468
      /// Default constructor
471 469
      ArcIt() {}
472 470
      /// Invalid constructor
473 471
      ArcIt(Invalid) : path(0), node(0) {}
474 472
      /// \brief Initializate the constructor to the first arc of path
475 473
      ArcIt(const ListPath &_path) 
476 474
        : path(&_path), node(_path.first) {}
477 475

	
478 476
    protected:
479 477

	
480 478
      ArcIt(const ListPath &_path, Node *_node) 
481 479
        : path(&_path), node(_node) {}
482 480

	
483 481

	
484 482
    public:
485 483

	
486 484
      ///Conversion to Digraph::Arc
487 485
      operator const Arc&() const {
488 486
        return node->arc;
489 487
      }
490 488

	
491 489
      /// Next arc
492 490
      ArcIt& operator++() { 
493 491
        node = node->next;
494 492
        return *this; 
495 493
      }
496 494

	
497 495
      /// Comparison operator
498 496
      bool operator==(const ArcIt& e) const { return node==e.node; }
499 497
      /// Comparison operator
500 498
      bool operator!=(const ArcIt& e) const { return node!=e.node; }
501 499
      /// Comparison operator
502 500
      bool operator<(const ArcIt& e) const { return node<e.node; }
503 501

	
504 502
    private:
505 503
      const ListPath *path;
506 504
      Node *node;
507 505
    };
508 506

	
509
    /// \brief Gives back the nth arc.
507
    /// \brief The nth arc.
510 508
    ///
511
    /// Gives back the nth arc in O(n) time.
509
    /// This function looks for the nth arc in O(n) time.
512 510
    /// \pre n is in the [0..length() - 1] range
513 511
    const Arc& nth(int n) const {
514 512
      Node *node = first;
515 513
      for (int i = 0; i < n; ++i) {
516 514
        node = node->next;
517 515
      }
518 516
      return node->arc;
519 517
    }
520 518

	
521 519
    /// \brief Initializes arc iterator to point to the nth arc.
522 520
    ArcIt nthIt(int n) const {
523 521
      Node *node = first;
524 522
      for (int i = 0; i < n; ++i) {
525 523
        node = node->next;
526 524
      }
527 525
      return ArcIt(*this, node);
528 526
    }
529 527

	
530 528
    /// \brief Length of the path.
531 529
    int length() const {
532 530
      int len = 0;
533 531
      Node *node = first;
534 532
      while (node != 0) {
535 533
        node = node->next;
536 534
        ++len;
537 535
      }
538 536
      return len;
539 537
    }
540 538

	
541
    /// \brief Returns whether the path is empty.
539
    /// \brief Return true if the path is empty.
542 540
    bool empty() const { return first == 0; }
543 541

	
544
    /// \brief Resets the path to an empty path.
542
    /// \brief Reset the path to an empty one.
545 543
    void clear() {
546 544
      while (first != 0) {
547 545
        last = first->next;
548 546
        alloc.destroy(first);
549 547
        alloc.deallocate(first, 1);
550 548
        first = last;
551 549
      }
552 550
    }
553 551

	
554
    /// \brief Gives back the first arc of the path
552
    /// \brief The first arc of the path
555 553
    const Arc& front() const {
556 554
      return first->arc;
557 555
    }
558 556

	
559 557
    /// \brief Add a new arc before the current path
560 558
    void addFront(const Arc& arc) {
561 559
      Node *node = alloc.allocate(1);
562 560
      alloc.construct(node, Node());
563 561
      node->prev = 0;
564 562
      node->next = first;
565 563
      node->arc = arc;
566 564
      if (first) {
567 565
        first->prev = node;
568 566
        first = node;
569 567
      } else {
570 568
        first = last = node;
571 569
      }
572 570
    }
573 571

	
574 572
    /// \brief Erase the first arc of the path
575 573
    void eraseFront() {
576 574
      Node *node = first;
577 575
      first = first->next;
578 576
      if (first) {
579 577
        first->prev = 0;
580 578
      } else {
581 579
        last = 0;
582 580
      }
583 581
      alloc.destroy(node);
584 582
      alloc.deallocate(node, 1);
585 583
    }
586 584

	
587
    /// \brief Gives back the last arc of the path.
585
    /// \brief The last arc of the path.
588 586
    const Arc& back() const {
589 587
      return last->arc;
590 588
    }
591 589

	
592 590
    /// \brief Add a new arc behind the current path.
593 591
    void addBack(const Arc& arc) {
594 592
      Node *node = alloc.allocate(1);
595 593
      alloc.construct(node, Node());
596 594
      node->next = 0;
597 595
      node->prev = last;
598 596
      node->arc = arc;
599 597
      if (last) {
600 598
        last->next = node;
601 599
        last = node;
602 600
      } else {
603 601
        last = first = node;
604 602
      }
605 603
    }
606 604

	
607 605
    /// \brief Erase the last arc of the path
608 606
    void eraseBack() {
609 607
      Node *node = last;
610 608
      last = last->prev;
611 609
      if (last) {
612 610
        last->next = 0;
613 611
      } else {
614 612
        first = 0;
615 613
      }
616 614
      alloc.destroy(node);
617 615
      alloc.deallocate(node, 1);
618 616
    }
619 617

	
620
    /// \brief Splicing the given path to the current path.
618
    /// \brief Splice a path to the back of the current path.
621 619
    ///
622
    /// It splices the \c tpath to the back of the current path and \c
620
    /// It splices \c tpath to the back of the current path and \c
623 621
    /// tpath becomes empty. The time complexity of this function is
624 622
    /// O(1).
625 623
    void spliceBack(ListPath& tpath) {
626 624
      if (first) {
627 625
        if (tpath.first) {
628 626
          last->next = tpath.first;
629 627
          tpath.first->prev = last;
630 628
          last = tpath.last;
631 629
        }
632 630
      } else {
633 631
        first = tpath.first;
634 632
        last = tpath.last;
635 633
      }
636 634
      tpath.first = tpath.last = 0;
637 635
    }
638 636

	
639
    /// \brief Splicing the given path to the current path.
637
    /// \brief Splice a path to the front of the current path.
640 638
    ///
641
    /// It splices the \c tpath before the current path and \c tpath
639
    /// It splices \c tpath before the current path and \c tpath
642 640
    /// becomes empty. The time complexity of this function
643 641
    /// is O(1).
644 642
    void spliceFront(ListPath& tpath) {
645 643
      if (first) {
646 644
        if (tpath.first) {
647 645
          first->prev = tpath.last;
648 646
          tpath.last->next = first;
649 647
          first = tpath.first;
650 648
        }
651 649
      } else {
652 650
        first = tpath.first;
653 651
        last = tpath.last;
654 652
      }
655 653
      tpath.first = tpath.last = 0;
656 654
    }
657 655

	
658
    /// \brief Splicing the given path into the current path.
656
    /// \brief Splice a path into the current path.
659 657
    ///
660 658
    /// It splices the \c tpath into the current path before the
661 659
    /// position of \c it iterator and \c tpath becomes empty. The
662
    /// time complexity of this function is O(1). If the \c it is \c
663
    /// INVALID then it will splice behind the current path.
660
    /// time complexity of this function is O(1). If the \c it is
661
    /// \c INVALID then it will splice behind the current path.
664 662
    void splice(ArcIt it, ListPath& tpath) {
665 663
      if (it.node) {
666 664
        if (tpath.first) {
667 665
          tpath.first->prev = it.node->prev;
668 666
          if (it.node->prev) {
669 667
            it.node->prev->next = tpath.first;
670 668
          } else {
671 669
            first = tpath.first;
672 670
          }
673 671
          it.node->prev = tpath.last;
674 672
          tpath.last->next = it.node;
675 673
        }
676 674
      } else {
677 675
        if (first) {
678 676
          if (tpath.first) {
679 677
            last->next = tpath.first;
680 678
            tpath.first->prev = last;
681 679
            last = tpath.last;
682 680
          }
683 681
        } else {
684 682
          first = tpath.first;
685 683
          last = tpath.last;
686 684
        }
687 685
      }
688 686
      tpath.first = tpath.last = 0;
689 687
    }
690 688

	
691
    /// \brief Spliting the current path.
689
    /// \brief Split the current path.
692 690
    ///
693
    /// It splits the current path into two parts. The part before \c
694
    /// it iterator will remain in the current path and the part from
695
    /// the it will put into the \c tpath. If the \c tpath had arcs
696
    /// before the operation they will be removed first.  The time
697
    /// complexity of this function is O(1) plus the clearing of \c
698
    /// tpath. If the \c it is \c INVALID then it just clears \c
699
    /// tpath.
691
    /// It splits the current path into two parts. The part before
692
    /// the iterator \c it will remain in the current path and the part
693
    /// starting with
694
    /// \c it will put into \c tpath. If \c tpath have arcs
695
    /// before the operation they are removed first.  The time
696
    /// complexity of this function is O(1) plus the the time of emtying
697
    /// \c tpath. If \c it is \c INVALID then it just clears \c tpath
700 698
    void split(ArcIt it, ListPath& tpath) {
701 699
      tpath.clear();
702 700
      if (it.node) {
703 701
        tpath.first = it.node;
704 702
        tpath.last = last;
705 703
        if (it.node->prev) {
706 704
          last = it.node->prev;
707 705
          last->next = 0;
708 706
        } else {
709 707
          first = last = 0;
710 708
        }
711 709
        it.node->prev = 0;
712 710
      }
713 711
    }
714 712

	
715 713

	
716 714
    typedef True BuildTag;
717 715

	
718 716
    template <typename CPath>
719 717
    void build(const CPath& path) {
720 718
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
721 719
        addBack(it);
722 720
      }
723 721
    }
724 722

	
725 723
    template <typename CPath>
726 724
    void buildRev(const CPath& path) {
727 725
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
728 726
        addFront(it);
729 727
      }
730 728
    }
731 729

	
732 730
  };
733 731

	
734 732
  /// \brief A structure for representing directed paths in a digraph.
735 733
  ///
736 734
  /// A structure for representing directed path in a digraph.
737 735
  /// \param Digraph The digraph type in which the path is.
738 736
  ///
739 737
  /// In a sense, the path can be treated as a list of arcs. The
740 738
  /// lemon path type stores just this list. As a consequence it
741
  /// cannot enumerate the nodes in the path and the zero length paths
742
  /// cannot store the source.
739
  /// cannot enumerate the nodes in the path and the source node of
740
  /// a zero length path is undefined.
743 741
  ///
744
  /// This implementation is completly static, so it cannot be
745
  /// modified exclude the assign an other path. It is intented to be
746
  /// used when you want to store a large number of paths because it is
747
  /// the most memory efficient path type in the lemon.
742
  /// This implementation is completly static, i.e. it can be copy constucted
743
  /// or copy assigned from another path, but otherwise it cannot be
744
  /// modified.
745
  ///
746
  /// Being the the most memory efficient path type in LEMON,
747
  /// it is intented to be
748
  /// used when you want to store a large number of paths.
748 749
  template <typename _Digraph>
749 750
  class StaticPath {
750 751
  public:
751 752

	
752 753
    typedef _Digraph Digraph;
753 754
    typedef typename Digraph::Arc Arc;
754 755

	
755 756
    /// \brief Default constructor
756 757
    ///
757 758
    /// Default constructor
758 759
    StaticPath() : len(0), arcs(0) {}
759 760
    
760 761
    /// \brief Template copy constructor
761 762
    ///
762
    /// This path can be initialized with any other path type. It just
763
    /// makes a copy of the given path.
763
    /// This path can be initialized from any other path type.
764 764
    template <typename CPath>
765 765
    StaticPath(const CPath& cpath) : arcs(0) {
766 766
      copyPath(*this, cpath);
767 767
    }
768 768

	
769 769
    /// \brief Destructor of the path
770 770
    ///
771 771
    /// Destructor of the path
772 772
    ~StaticPath() {
773 773
      if (arcs) delete[] arcs;
774 774
    }
775 775

	
776 776
    /// \brief Template copy assignment
777 777
    ///
778
    /// This path can be initialized with any other path type. It just
778
    /// This path can be made equal to any other path type. It simply
779 779
    /// makes a copy of the given path.
780 780
    template <typename CPath>
781 781
    StaticPath& operator=(const CPath& cpath) {
782 782
      copyPath(*this, cpath);
783 783
      return *this;
784 784
    }
785 785

	
786 786
    /// \brief Iterator class to iterate on the arcs of the paths
787 787
    ///
788 788
    /// This class is used to iterate on the arcs of the paths
789 789
    ///
790 790
    /// Of course it converts to Digraph::Arc
791 791
    class ArcIt {
792 792
      friend class StaticPath;
793 793
    public:
794 794
      /// Default constructor
795 795
      ArcIt() {}
796 796
      /// Invalid constructor
797 797
      ArcIt(Invalid) : path(0), idx(-1) {}
798 798
      /// Initializate the constructor to the first arc of path
799 799
      ArcIt(const StaticPath &_path) 
800 800
        : path(&_path), idx(_path.empty() ? -1 : 0) {}
801 801

	
802 802
    private:
803 803

	
804 804
      /// Constructor with starting point
805 805
      ArcIt(const StaticPath &_path, int _idx) 
806 806
        : idx(_idx), path(&_path) {}
807 807

	
808 808
    public:
809 809

	
810 810
      ///Conversion to Digraph::Arc
811 811
      operator const Arc&() const {
812 812
        return path->nth(idx);
813 813
      }
814 814

	
815 815
      /// Next arc
816 816
      ArcIt& operator++() { 
817 817
        ++idx;
818 818
        if (idx >= path->length()) idx = -1; 
819 819
        return *this; 
820 820
      }
821 821

	
822 822
      /// Comparison operator
823 823
      bool operator==(const ArcIt& e) const { return idx==e.idx; }
824 824
      /// Comparison operator
825 825
      bool operator!=(const ArcIt& e) const { return idx!=e.idx; }
826 826
      /// Comparison operator
827 827
      bool operator<(const ArcIt& e) const { return idx<e.idx; }
828 828

	
829 829
    private:
830 830
      const StaticPath *path;
831 831
      int idx;
832 832
    };
833 833

	
834
    /// \brief Gives back the nth arc.
834
    /// \brief The nth arc.
835 835
    ///
836 836
    /// \pre n is in the [0..length() - 1] range
837 837
    const Arc& nth(int n) const {
838 838
      return arcs[n];
839 839
    }
840 840

	
841
    /// \brief Initializes arc iterator to point to the nth arc.
841
    /// \brief The arc iterator pointing to the nth arc.
842 842
    ArcIt nthIt(int n) const {
843 843
      return ArcIt(*this, n);
844 844
    }
845 845

	
846
    /// \brief Gives back the length of the path.
846
    /// \brief The length of the path.
847 847
    int length() const { return len; }
848 848

	
849
    /// \brief Returns true when the path is empty.
849
    /// \brief Return true when the path is empty.
850 850
    int empty() const { return len == 0; }
851 851

	
852
    /// \break Erase all arc in the digraph.
852
    /// \break Erase all arcs in the digraph.
853 853
    void clear() {
854 854
      len = 0;
855 855
      if (arcs) delete[] arcs;
856 856
      arcs = 0;
857 857
    }
858 858

	
859
    /// \brief Gives back the first arc of the path.
859
    /// \brief The first arc of the path.
860 860
    const Arc& front() const {
861 861
      return arcs[0];
862 862
    }
863 863

	
864
    /// \brief Gives back the last arc of the path.
864
    /// \brief The last arc of the path.
865 865
    const Arc& back() const {
866 866
      return arcs[len - 1];
867 867
    }
868 868

	
869 869

	
870 870
    typedef True BuildTag;
871 871

	
872 872
    template <typename CPath>
873 873
    void build(const CPath& path) {
874 874
      len = path.length();
875 875
      arcs = new Arc[len];
876 876
      int index = 0;
877 877
      for (typename CPath::ArcIt it(path); it != INVALID; ++it) {
878 878
        arcs[index] = it;
879 879
        ++index;
880 880
      }
881 881
    }
882 882

	
883 883
    template <typename CPath>
884 884
    void buildRev(const CPath& path) {
885 885
      len = path.length();
886 886
      arcs = new Arc[len];
887 887
      int index = len;
888 888
      for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
889 889
        --index;
890 890
        arcs[index] = it;
891 891
      }
892 892
    }
893 893

	
894 894
  private:
895 895
    int len;
896 896
    Arc* arcs;
897 897
  };
898 898

	
899 899
  ///@}
900 900

	
901 901
} // namespace lemon
902 902

	
903 903
#endif // LEMON_PATH_H
Ignore white space 6 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
///\ingroup paths
20 20
///\file
21 21
///\brief Classes for representing paths in digraphs.
22 22
///
23 23

	
24 24
#ifndef LEMON_PATH_UTILS_H
25 25
#define LEMON_PATH_UTILS_H
26 26

	
27 27
#include <lemon/concepts/path.h>
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  namespace _path_bits {
32 32

	
33 33
    template <typename Path, typename Enable = void>
34 34
    struct RevTagIndicator {
35 35
      static const bool value = false;
36 36
    };
37 37

	
38 38
    template <typename Digraph>
39 39
    struct RevTagIndicator<
40 40
      Digraph, 
41 41
      typename enable_if<typename Digraph::RevTag, void>::type
42 42
    > {
43 43
      static const bool value = true;
44 44
    };
45 45

	
46 46
    template <typename Target, typename Source,
47 47
              typename BuildEnable = void, typename RevEnable = void>
48 48
    struct PathCopySelector {
49 49
      static void copy(Target& target, const Source& source) {
50 50
        target.clear();
51 51
        for (typename Source::ArcIt it(source); it != INVALID; ++it) {
52 52
          target.addBack(it);
53 53
        }
54 54
      }
55 55
    };
56 56

	
57 57
    template <typename Target, typename Source, typename BuildEnable>
58 58
    struct PathCopySelector<
59 59
      Target, Source, BuildEnable, 
60 60
      typename enable_if<typename Source::RevPathTag, void>::type> {
61 61
      static void copy(Target& target, const Source& source) {
62 62
        target.clear();
63 63
        for (typename Source::RevArcIt it(source); it != INVALID; ++it) {
64 64
          target.addFront(it);
65 65
        }
66 66
      }
67 67
    };
68 68

	
69 69
    template <typename Target, typename Source, typename RevEnable>
70 70
    struct PathCopySelector<
71 71
      Target, Source, 
72 72
      typename enable_if<typename Target::BuildTag, void>::type, RevEnable> {
73 73
      static void copy(Target& target, const Source& source) {
74 74
        target.clear();
75 75
        target.build(source);
76 76
      }
77 77
    };
78 78

	
79 79
    template <typename Target, typename Source>
80 80
    struct PathCopySelector<
81 81
      Target, Source, 
82 82
      typename enable_if<typename Target::BuildTag, void>::type,
83 83
      typename enable_if<typename Source::RevPathTag, void>::type> {
84 84
      static void copy(Target& target, const Source& source) {
85 85
        target.clear();
86 86
        target.buildRev(source);
87 87
      }
88 88
    };
89 89

	
90 90
  }
91 91

	
92 92

	
93
  /// \brief Make of copy of a path.
93
  /// \brief Make a copy of a path.
94 94
  ///
95
  ///  Make of copy of a path.
95
  ///  This function makes a copy of a path.
96 96
  template <typename Target, typename Source>
97 97
  void copyPath(Target& target, const Source& source) {
98 98
    checkConcept<concepts::PathDumper<typename Source::Digraph>, Source>();
99 99
    _path_bits::PathCopySelector<Target, Source>::copy(target, source);
100 100
  }
101 101

	
102
  /// \brief Checks the path's consistency.
102
  /// \brief Check the consistency of a path.
103 103
  ///
104
  /// Checks that each arc's target is the next's source. 
104
  /// This function checks that the target of each arc is the same
105
  /// as the source of the next one. 
105 106
  /// 
106 107
  template <typename Digraph, typename Path>
107 108
  bool checkPath(const Digraph& digraph, const Path& path) {
108 109
    typename Path::ArcIt it(path);
109 110
    if (it == INVALID) return true;
110 111
    typename Digraph::Node node = digraph.target(it);
111 112
    ++it;
112 113
    while (it != INVALID) {
113 114
      if (digraph.source(it) != node) return false;
114 115
      node = digraph.target(it);
115 116
      ++it;
116 117
    }
117 118
    return true;
118 119
  }
119 120

	
120
  /// \brief Gives back the source of the path
121
  /// \brief The source of a path
121 122
  ///
122
  /// Gives back the source of the path.
123
  /// This function returns the source of the given path.
123 124
  template <typename Digraph, typename Path>
124 125
  typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) {
125 126
    return digraph.source(path.front());
126 127
  }
127 128

	
128
  /// \brief Gives back the target of the path
129
  /// \brief The target of a path
129 130
  ///
130
  /// Gives back the target of the path.
131
  /// This function returns the target of the given path.
131 132
  template <typename Digraph, typename Path>
132 133
  typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) {
133 134
    return digraph.target(path.back());
134 135
  }
135 136

	
136
  /// \brief Class which helps to iterate the nodes of a path
137
  /// \brief Class which helps to iterate through the nodes of a path
137 138
  ///
138 139
  /// In a sense, the path can be treated as a list of arcs. The
139
  /// lemon path type stores just this list. As a consequence it
140
  /// lemon path type stores only this list. As a consequence, it
140 141
  /// cannot enumerate the nodes in the path and the zero length paths
141
  /// cannot store the node.
142
  /// cannot have a source node.
142 143
  ///
143 144
  /// This class implements the node iterator of a path structure. To
144
  /// provide this feature, the underlying digraph should be given to
145
  /// provide this feature, the underlying digraph should be passed to
145 146
  /// the constructor of the iterator.
146 147
  template <typename Path>
147 148
  class PathNodeIt {
148 149
  private:
149 150
    const typename Path::Digraph *_digraph;
150 151
    typename Path::ArcIt _it;
151 152
    typename Path::Digraph::Node _nd;
152 153

	
153 154
  public:
154 155

	
155 156
    typedef typename Path::Digraph Digraph;
156 157
    typedef typename Digraph::Node Node;
157 158
    
158 159
    /// Default constructor
159 160
    PathNodeIt() {}
160 161
    /// Invalid constructor
161 162
    PathNodeIt(Invalid) 
162 163
      : _digraph(0), _it(INVALID), _nd(INVALID) {}
163 164
    /// Constructor
164 165
    PathNodeIt(const Digraph& digraph, const Path& path) 
165 166
      : _digraph(&digraph), _it(path) {
166 167
      _nd = (_it != INVALID ? _digraph->source(_it) : INVALID);
167 168
    }
168 169
    /// Constructor
169 170
    PathNodeIt(const Digraph& digraph, const Path& path, const Node& src) 
170 171
      : _digraph(&digraph), _it(path), _nd(src) {}
171 172

	
172 173
    ///Conversion to Digraph::Node
173 174
    operator Node() const {
174 175
      return _nd;
175 176
    }
176 177

	
177 178
    /// Next node
178 179
    PathNodeIt& operator++() {
179 180
      if (_it == INVALID) _nd = INVALID;
180 181
      else {
181 182
	_nd = _digraph->target(_it);
182 183
	++_it;
183 184
      }
184 185
      return *this;
185 186
    }
186 187

	
187 188
    /// Comparison operator
188 189
    bool operator==(const PathNodeIt& n) const { 
189 190
      return _it == n._it && _nd == n._nd; 
190 191
    }
191 192
    /// Comparison operator
192 193
    bool operator!=(const PathNodeIt& n) const { 
193 194
      return _it != n._it || _nd != n._nd; 
194 195
    }
195 196
    /// Comparison operator
196 197
    bool operator<(const PathNodeIt& n) const { 
197 198
      return (_it < n._it && _nd != INVALID);
198 199
    }
199 200
    
200 201
  };
201 202
  
202 203
}
203 204

	
204 205
#endif
0 comments (0 inline)