summaryrefslogtreecommitdiff
path: root/include/gudhi/Skeleton_blocker/iterators/Skeleton_blockers_vertices_iterators.h
blob: 9e9ae9617ba20c04d5ba486b6aa5e8f75df2b55a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*    This file is part of the Gudhi Library. The Gudhi library
 *    (Geometric Understanding in Higher Dimensions) is a generic C++
 *    library for computational topology.
 *
 *    Author(s):       David Salinas
 *
 *    Copyright (C) 2014  INRIA
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_
#define SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_

#include <boost/iterator/iterator_facade.hpp>

#include <utility>  // for pair<>

namespace Gudhi {

namespace skeleton_blocker {

/**
 *@brief Iterator on the vertices of a simplicial complex
 *@remark The increment operator go to the next active vertex.
 *@remark Incrementation increases Vertex_handle.
 */
template<typename SkeletonBlockerComplex>
class Vertex_iterator : public boost::iterator_facade< Vertex_iterator <SkeletonBlockerComplex>
, typename SkeletonBlockerComplex::Vertex_handle const
, boost::forward_traversal_tag
, typename SkeletonBlockerComplex::Vertex_handle const> {
  friend class boost::iterator_core_access;

  typedef typename SkeletonBlockerComplex::boost_vertex_iterator boost_vertex_iterator;
  typedef typename SkeletonBlockerComplex::Vertex_handle Vertex_handle;
 private:
  const SkeletonBlockerComplex* complex;
  std::pair<boost_vertex_iterator, boost_vertex_iterator> vertexIterator;


 public:
  Vertex_iterator() : complex(NULL) { }

  Vertex_iterator(const SkeletonBlockerComplex* complex_) :
      complex(complex_),
      vertexIterator(vertices(complex_->skeleton)) {
    if (!finished() && !is_active()) {
      goto_next_valid();
    }
  }

  /**
   * return an iterator to the end.
   */
  Vertex_iterator(const SkeletonBlockerComplex* complex_, int end) :
      complex(complex_), vertexIterator(vertices(complex_->skeleton)) {
    vertexIterator.first = vertexIterator.second;
  }

 public:
  void increment() {
    goto_next_valid();
  }

  Vertex_handle dereference() const {
    return (Vertex_handle(*(vertexIterator.first)));
  }

  bool equal(const Vertex_iterator& other) const {
    return vertexIterator == other.vertexIterator && complex == other.complex;
  }

  bool operator<(const Vertex_iterator& other) const {
    return dereference() < other.dereference();
  }

 private:
  bool finished() const {
    return vertexIterator.first == vertexIterator.second;
  }

  void goto_next_valid() {
    ++vertexIterator.first;
    if (!finished() && !is_active()) {
      goto_next_valid();
    }
  }

  bool is_active() const {
    return ((*complex)[Vertex_handle(*vertexIterator.first)]).is_active();
  }
};

template<typename SkeletonBlockerComplex>
class Neighbors_vertices_iterator : public boost::iterator_facade < Neighbors_vertices_iterator<SkeletonBlockerComplex>
, typename SkeletonBlockerComplex::Vertex_handle const
, boost::forward_traversal_tag
, typename SkeletonBlockerComplex::Vertex_handle const> {
  friend class boost::iterator_core_access;

  typedef SkeletonBlockerComplex Complex;
  typedef typename Complex::boost_adjacency_iterator boost_adjacency_iterator;
  typedef typename Complex::Vertex_handle Vertex_handle;
  typedef typename Complex::Edge_handle Edge_handle;

 private:
  const Complex* complex;
  Vertex_handle v;

  boost_adjacency_iterator current_;
  boost_adjacency_iterator end_;

 public:
  Neighbors_vertices_iterator() : complex(NULL) { }

  Neighbors_vertices_iterator(const Complex* complex_, Vertex_handle v_) :
      complex(complex_),
      v(v_) {
    tie(current_, end_) = adjacent_vertices(v.vertex, complex->skeleton);
  }

  /**
   * returns an iterator to the end
   */
  Neighbors_vertices_iterator(const Complex* complex_, Vertex_handle v_, int end) :
      complex(complex_),
      v(v_) {
    tie(current_, end_) = adjacent_vertices(v.vertex, complex->skeleton);
    set_end();
  }

  void increment() {
    if (current_ != end_)
      ++current_;
  }

  Vertex_handle dereference() const {
    return (Vertex_handle(*current_));
  }

  bool equal(const Neighbors_vertices_iterator& other) const {
    return (complex == other.complex) && (v == other.v) && (current_ == other.current_) && (end_ == other.end_);
  }

 private:
  // TODO(DS): remove this ugly hack
  void set_end() {
    current_ = end_;
  }
};

}  // namespace skeleton_blocker

namespace skbl = skeleton_blocker;

}  // namespace Gudhi

#endif  // SKELETON_BLOCKER_ITERATORS_SKELETON_BLOCKERS_VERTICES_ITERATORS_H_