Allows for iteration of the edges of a Graph, by iterating the underlying Graph.edges_ vector while skipping over null entries.
| 499 | // Allows for iteration of the edges of a Graph, by iterating the underlying |
| 500 | // Graph.edges_ vector while skipping over null entries. |
| 501 | class GraphEdgesIterable { |
| 502 | private: |
| 503 | const std::vector<Edge*>& edges_; |
| 504 | |
| 505 | public: |
| 506 | explicit GraphEdgesIterable(const std::vector<Edge*>& edges) |
| 507 | : edges_(edges) {} |
| 508 | |
| 509 | typedef Edge* value_type; |
| 510 | |
| 511 | class const_iterator { |
| 512 | private: |
| 513 | // The underlying iterator. |
| 514 | std::vector<value_type>::const_iterator iter_; |
| 515 | |
| 516 | // The end of the underlying iterator. |
| 517 | std::vector<value_type>::const_iterator end_; |
| 518 | |
| 519 | // Advances iter_ until it reaches a non-null item, or reaches the end. |
| 520 | void apply_filter() { |
| 521 | while (iter_ != end_ && *iter_ == nullptr) { |
| 522 | ++iter_; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | public: |
| 527 | const_iterator(std::vector<value_type>::const_iterator iter, |
| 528 | std::vector<value_type>::const_iterator end) |
| 529 | : iter_(iter), end_(end) { |
| 530 | apply_filter(); |
| 531 | } |
| 532 | |
| 533 | bool operator==(const const_iterator& other) const { |
| 534 | return iter_ == other.iter_; |
| 535 | } |
| 536 | |
| 537 | bool operator!=(const const_iterator& other) const { |
| 538 | return iter_ != other.iter_; |
| 539 | } |
| 540 | |
| 541 | // This is the prefix increment operator (++x), which is the operator |
| 542 | // used by C++ range iteration (for (x : y) ...). We intentionally do not |
| 543 | // provide a postfix increment operator. |
| 544 | const_iterator& operator++() { |
| 545 | ++iter_; |
| 546 | apply_filter(); |
| 547 | return *this; |
| 548 | } |
| 549 | |
| 550 | value_type operator*() { return *iter_; } |
| 551 | }; |
| 552 | |
| 553 | const_iterator begin() { |
| 554 | return const_iterator(edges_.begin(), edges_.end()); |
| 555 | } |
| 556 | const_iterator end() { return const_iterator(edges_.end(), edges_.end()); } |
| 557 | }; |
| 558 |