Gudhi  1.2.0
 All Classes Functions Variables Typedefs Friends Groups Pages
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 Sophia Antipolis-Mediterranee (France)
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 #ifndef SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
23 #define SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
24 
25 #include <boost/iterator/iterator_facade.hpp>
26 
27 #include <utility> // for pair<>
28 
29 namespace Gudhi {
30 
31 namespace skbl {
32 
38 template<typename SkeletonBlockerComplex>
39 class Vertex_iterator : public boost::iterator_facade< Vertex_iterator <SkeletonBlockerComplex>
40 , typename SkeletonBlockerComplex::Vertex_handle const
41 , boost::forward_traversal_tag
42 , typename SkeletonBlockerComplex::Vertex_handle const> {
43  friend class boost::iterator_core_access;
44 
45  typedef typename SkeletonBlockerComplex::boost_vertex_iterator boost_vertex_iterator;
46  typedef typename SkeletonBlockerComplex::Vertex_handle Vertex_handle;
47  private:
48  const SkeletonBlockerComplex* complex;
49  std::pair<boost_vertex_iterator, boost_vertex_iterator> vertexIterator;
50 
51 
52  public:
53  Vertex_iterator() : complex(NULL) { }
54 
55  Vertex_iterator(const SkeletonBlockerComplex* complex_) :
56  complex(complex_),
57  vertexIterator(vertices(complex_->skeleton)) {
58  if (!finished() && !is_active()) {
59  goto_next_valid();
60  }
61  }
62 
66  Vertex_iterator(const SkeletonBlockerComplex* complex_, int end) :
67  complex(complex_), vertexIterator(vertices(complex_->skeleton)) {
68  vertexIterator.first = vertexIterator.second;
69  }
70 
71  public:
72  void increment() {
73  goto_next_valid();
74  }
75 
76  Vertex_handle dereference() const {
77  return (Vertex_handle(*(vertexIterator.first)));
78  }
79 
80  bool equal(const Vertex_iterator& other) const {
81  return vertexIterator == other.vertexIterator && complex == other.complex;
82  }
83 
84  bool operator<(const Vertex_iterator& other) const {
85  return dereference() < other.dereference();
86  }
87 
88  private:
89  bool finished() const {
90  return vertexIterator.first == vertexIterator.second;
91  }
92 
93  void goto_next_valid() {
94  ++vertexIterator.first;
95  if (!finished() && !is_active()) {
96  goto_next_valid();
97  }
98  }
99 
100  bool is_active() const {
101  return ((*complex)[Vertex_handle(*vertexIterator.first)]).is_active();
102  }
103 };
104 
105 template<typename SkeletonBlockerComplex>
106 class Neighbors_vertices_iterator: public boost::iterator_facade < Neighbors_vertices_iterator<SkeletonBlockerComplex>
107 , typename SkeletonBlockerComplex::Vertex_handle const
108 , boost::forward_traversal_tag
109 , typename SkeletonBlockerComplex::Vertex_handle const> {
110  friend class boost::iterator_core_access;
111 
112  typedef SkeletonBlockerComplex Complex;
113  typedef typename Complex::boost_adjacency_iterator boost_adjacency_iterator;
114  typedef typename Complex::Vertex_handle Vertex_handle;
115  typedef typename Complex::Edge_handle Edge_handle;
116 
117  private:
118  const Complex* complex;
119  Vertex_handle v;
120 
121  boost_adjacency_iterator current_;
122  boost_adjacency_iterator end_;
123 
124  public:
125  // boost_adjacency_iterator ai, ai_end;
126  // for (tie(ai, ai_end) = adjacent_vertices(v.vertex, skeleton); ai != ai_end; ++ai) {
127 
128  Neighbors_vertices_iterator() : complex(NULL) { }
129 
130  Neighbors_vertices_iterator(const Complex* complex_, Vertex_handle v_) :
131  complex(complex_),
132  v(v_) {
133  tie(current_, end_) = adjacent_vertices(v.vertex, complex->skeleton);
134  }
135 
139  Neighbors_vertices_iterator(const Complex* complex_, Vertex_handle v_, int end) :
140  complex(complex_),
141  v(v_) {
142  tie(current_, end_) = adjacent_vertices(v.vertex, complex->skeleton);
143  set_end();
144  }
145 
146  void increment() {
147  if (current_ != end_)
148  ++current_;
149  }
150 
151  Vertex_handle dereference() const {
152  return (Vertex_handle(*current_));
153  }
154 
155  bool equal(const Neighbors_vertices_iterator& other) const {
156  return (complex == other.complex) && (v == other.v) && (current_ == other.current_) && (end_ == other.end_);
157  }
158 
159  private:
160  // todo remove this ugly hack
161  void set_end() {
162  current_ = end_;
163  }
164 };
165 
166 } // namespace skbl
167 
168 } // namespace Gudhi
169 
170 #endif // SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
171 
172 
Vertex_iterator(const SkeletonBlockerComplex *complex_, int end)
Definition: Skeleton_blockers_vertices_iterators.h:66
Definition: SkeletonBlockerDS.h:60
Iterator on the vertices of a simplicial complex.
Definition: Skeleton_blockers_vertices_iterators.h:39