Combination_iterator.h
1 /* This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
2  * See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
3  * Author(s): Siargey Kachanovich
4  *
5  * Copyright (C) 2019 Inria
6  *
7  * Modification(s):
8  * - YYYY/MM Author: Description of the modification
9  */
10 
11 #ifndef PERMUTAHEDRAL_REPRESENTATION_COMBINATION_ITERATOR_H_
12 #define PERMUTAHEDRAL_REPRESENTATION_COMBINATION_ITERATOR_H_
13 
14 #include <vector>
15 #include <boost/range/iterator_range.hpp>
16 
17 namespace Gudhi {
18 
19 namespace coxeter_triangulation {
20 
21 typedef unsigned uint;
22 
28  : public boost::iterator_facade<Combination_iterator, std::vector<uint> const, boost::forward_traversal_tag> {
29  typedef std::vector<uint> value_t;
30 
31  protected:
32  friend class boost::iterator_core_access;
33 
34  bool equal(Combination_iterator const& other) const { return (is_end_ && other.is_end_); }
35 
36  value_t const& dereference() const { return value_; }
37 
38  void increment() {
39  if (value_[0] == n_ - k_) {
40  is_end_ = true;
41  return;
42  }
43  uint j = k_ - 1;
44  if (value_[j] < n_ - 1) {
45  value_[j]++;
46  return;
47  }
48  for (; j > 0; --j)
49  if (value_[j - 1] < n_ - k_ + j - 1) {
50  value_[j - 1]++;
51  for (uint s = j; s < k_; s++) value_[s] = value_[j - 1] + s - (j - 1);
52  return;
53  }
54  }
55 
56  public:
57  Combination_iterator(const uint& n, const uint& k) : value_(k), is_end_(n == 0), n_(n), k_(k) {
58  for (uint i = 0; i < k; ++i) value_[i] = i;
59  }
60 
61  // Used for the creating an end iterator
62  Combination_iterator() : is_end_(true), n_(0), k_(0) {}
63 
64  void reinitialize() {
65  if (n_ > 0) {
66  is_end_ = false;
67  for (uint i = 0; i < n_; ++i) value_[i] = i;
68  }
69  }
70 
71  private:
72  value_t value_; // the dereference value
73  bool is_end_; // is true when the current permutation is the final one
74 
75  uint n_;
76  uint k_;
77 };
78 
79 } // namespace coxeter_triangulation
80 
81 } // namespace Gudhi
82 
83 #endif
Class that allows the user to generate combinations of k elements in a set of n elements....
Definition: Combination_iterator.h:28
Gudhi namespace.
Definition: SimplicialComplexForAlpha.h:14