Skeleton_blockers_vertices_iterators.h
1 /* This file is part of the Gudhi Library. The Gudhi library
2  * (Geometric Understanding in Higher Dimensions) is a generic C++
3  * library for computational topology.
4  *
5  * Author(s): David Salinas
6  *
7  * Copyright (C) 2014 Inria
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
24 #define SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
25 
26 #include <boost/iterator/iterator_facade.hpp>
27 
28 #include <utility> // for pair<>
29 
30 namespace Gudhi {
31 
32 namespace skeleton_blocker {
33 
39 template<typename SkeletonBlockerComplex>
40 class Vertex_iterator : public boost::iterator_facade< Vertex_iterator <SkeletonBlockerComplex>
41 , typename SkeletonBlockerComplex::Vertex_handle const
42 , boost::forward_traversal_tag
43 , typename SkeletonBlockerComplex::Vertex_handle const> {
44  friend class boost::iterator_core_access;
45 
46  typedef typename SkeletonBlockerComplex::boost_vertex_iterator boost_vertex_iterator;
47  typedef typename SkeletonBlockerComplex::Vertex_handle Vertex_handle;
48  private:
49  const SkeletonBlockerComplex* complex;
50  std::pair<boost_vertex_iterator, boost_vertex_iterator> vertexIterator;
51 
52 
53  public:
54  Vertex_iterator() : complex(NULL) { }
55 
56  Vertex_iterator(const SkeletonBlockerComplex* complex_) :
57  complex(complex_),
58  vertexIterator(vertices(complex_->skeleton)) {
59  if (!finished() && !is_active()) {
60  goto_next_valid();
61  }
62  }
63 
67  Vertex_iterator(const SkeletonBlockerComplex* complex_, int end) :
68  complex(complex_), vertexIterator(vertices(complex_->skeleton)) {
69  vertexIterator.first = vertexIterator.second;
70  }
71 
72  public:
73  void increment() {
74  goto_next_valid();
75  }
76 
77  Vertex_handle dereference() const {
78  return (Vertex_handle(*(vertexIterator.first)));
79  }
80 
81  bool equal(const Vertex_iterator& other) const {
82  return vertexIterator == other.vertexIterator && complex == other.complex;
83  }
84 
85  bool operator<(const Vertex_iterator& other) const {
86  return dereference() < other.dereference();
87  }
88 
89  private:
90  bool finished() const {
91  return vertexIterator.first == vertexIterator.second;
92  }
93 
94  void goto_next_valid() {
95  ++vertexIterator.first;
96  if (!finished() && !is_active()) {
97  goto_next_valid();
98  }
99  }
100 
101  bool is_active() const {
102  return ((*complex)[Vertex_handle(*vertexIterator.first)]).is_active();
103  }
104 };
105 
106 template<typename SkeletonBlockerComplex>
107 class Neighbors_vertices_iterator : public boost::iterator_facade < Neighbors_vertices_iterator<SkeletonBlockerComplex>
108 , typename SkeletonBlockerComplex::Vertex_handle const
109 , boost::forward_traversal_tag
110 , typename SkeletonBlockerComplex::Vertex_handle const> {
111  friend class boost::iterator_core_access;
112 
113  typedef SkeletonBlockerComplex Complex;
114  typedef typename Complex::boost_adjacency_iterator boost_adjacency_iterator;
115  typedef typename Complex::Vertex_handle Vertex_handle;
116  typedef typename Complex::Edge_handle Edge_handle;
117 
118  private:
119  const Complex* complex;
120  Vertex_handle v;
121 
122  boost_adjacency_iterator current_;
123  boost_adjacency_iterator end_;
124 
125  public:
126  Neighbors_vertices_iterator() : complex(NULL) { }
127 
128  Neighbors_vertices_iterator(const Complex* complex_, Vertex_handle v_) :
129  complex(complex_),
130  v(v_) {
131  tie(current_, end_) = adjacent_vertices(v.vertex, complex->skeleton);
132  }
133 
137  Neighbors_vertices_iterator(const Complex* complex_, Vertex_handle v_, int end) :
138  complex(complex_),
139  v(v_) {
140  tie(current_, end_) = adjacent_vertices(v.vertex, complex->skeleton);
141  set_end();
142  }
143 
144  void increment() {
145  if (current_ != end_)
146  ++current_;
147  }
148 
149  Vertex_handle dereference() const {
150  return (Vertex_handle(*current_));
151  }
152 
153  bool equal(const Neighbors_vertices_iterator& other) const {
154  return (complex == other.complex) && (v == other.v) && (current_ == other.current_) && (end_ == other.end_);
155  }
156 
157  private:
158  // TODO(DS): remove this ugly hack
159  void set_end() {
160  current_ = end_;
161  }
162 };
163 
164 } // namespace skeleton_blocker
165 
166 namespace skbl = skeleton_blocker;
167 
168 } // namespace Gudhi
169 
170 #endif // SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
Iterator on the vertices of a simplicial complex.
Definition: Skeleton_blockers_vertices_iterators.h:40
Definition: SimplicialComplexForAlpha.h:26
Vertex_iterator(const SkeletonBlockerComplex *complex_, int end)
Definition: Skeleton_blockers_vertices_iterators.h:67
GUDHI  Version 2.2.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu Jun 14 2018 15:00:54 for GUDHI by Doxygen 1.8.13