Active_witness_iterator.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): Siargey Kachanovich
6  *
7  * Copyright (C) 2016 INRIA (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 
23 #ifndef ACTIVE_WITNESS_ACTIVE_WITNESS_ITERATOR_H_
24 #define ACTIVE_WITNESS_ACTIVE_WITNESS_ITERATOR_H_
25 
26 #include <boost/iterator/iterator_facade.hpp>
27 #include <list>
28 
29 namespace Gudhi {
30 
31 namespace witness_complex {
32 
33 /* \brief Iterator in the nearest landmark list.
34  * \details After the iterator reaches the end of the list,
35  * the list is augmented by a (nearest landmark, distance) pair if possible.
36  * If all the landmarks are present in the list, iterator returns the specific end value
37  * of the corresponding 'Active_witness' object.
38  */
39 template< typename Active_witness,
40  typename Id_distance_pair,
41  typename INS_iterator >
42 class Active_witness_iterator
43  : public boost::iterator_facade< Active_witness_iterator <Active_witness, Id_distance_pair, INS_iterator>,
44  Id_distance_pair const,
45  boost::forward_traversal_tag,
46  Id_distance_pair const> {
47  friend class boost::iterator_core_access;
48 
49  typedef typename std::list<Id_distance_pair>::iterator Pair_iterator;
50  typedef typename Gudhi::witness_complex::Active_witness_iterator<Active_witness,
51  Id_distance_pair,
52  INS_iterator> Iterator;
53 
54  Active_witness *aw_;
55  Pair_iterator lh_; // landmark handle
56  bool is_end_; // true only if the pointer is end and there are no more neighbors to add
57 
58  public:
59  Active_witness_iterator(Active_witness* aw)
60  : aw_(aw), lh_(aw_->nearest_landmark_table_.end()), is_end_(true) {
61  }
62 
63  Active_witness_iterator(Active_witness* aw, const Pair_iterator& lh)
64  : aw_(aw), lh_(lh) {
65  is_end_ = false;
66  if (lh_ == aw_->nearest_landmark_table_.end()) {
67  if (aw_->iterator_next_ == aw_->iterator_end_) {
68  is_end_ = true;
69  } else {
70  aw_->nearest_landmark_table_.push_back(*aw_->iterator_next_);
71  lh_ = --aw_->nearest_landmark_table_.end();
72  ++(aw_->iterator_next_);
73  }
74  }
75  }
76 
77  private :
78  Id_distance_pair& dereference() const {
79  return *lh_;
80  }
81 
82  bool equal(const Iterator& other) const {
83  return (is_end_ == other.is_end_) || (lh_ == other.lh_);
84  }
85 
86  void increment() {
87  // the neighbor search can't be at the end iterator of a list
88  GUDHI_CHECK(!is_end_ && lh_ != aw_->nearest_landmark_table_.end(),
89  std::logic_error("Wrong active witness increment."));
90  // if the id of the current landmark is the same as the last one
91 
92  lh_++;
93  if (lh_ == aw_->nearest_landmark_table_.end()) {
94  if (aw_->iterator_next_ == aw_->iterator_end_) {
95  is_end_ = true;
96  } else {
97  aw_->nearest_landmark_table_.push_back(*aw_->iterator_next_);
98  lh_ = std::prev(aw_->nearest_landmark_table_.end());
99  ++(aw_->iterator_next_);
100  }
101  }
102  }
103 };
104 
105 } // namespace witness_complex
106 } // namespace Gudhi
107 
108 #endif // ACTIVE_WITNESS_ACTIVE_WITNESS_ITERATOR_H_
Definition: SimplicialComplexForAlpha.h:26
GUDHI  Version 2.1.0  - C++ library for Topological Data Analysis (TDA) and Higher Dimensional Geometry Understanding.  - Copyright : GPL v3 Generated on Thu May 24 2018 09:46:12 for GUDHI by Doxygen 1.8.13