Tatooine
vertex_iterator.h
Go to the documentation of this file.
1#ifndef TATOOINE_DETAIL_RECTILINEAR_GRID_VERTEX_ITERATOR_H
2#define TATOOINE_DETAIL_RECTILINEAR_GRID_VERTEX_ITERATOR_H
3//==============================================================================
6
7#include <boost/iterator/iterator_facade.hpp>
8#include <cassert>
9#include <cstddef>
10#include <functional>
11#include <utility>
12//==============================================================================
13namespace tatooine {
14//==============================================================================
15template <floating_point_range... Dimensions>
16requires(sizeof...(Dimensions) > 1)
17class rectilinear_grid;
18//==============================================================================
19} // namespace tatooine
20//==============================================================================
21namespace tatooine::detail::rectilinear_grid {
22//==============================================================================
23template <typename... Dimensions>
25 : boost::iterator_facade<vertex_iterator<Dimensions...>,
26 vertex_handle<sizeof...(Dimensions)>,
27 boost::bidirectional_traversal_tag> {
28 static constexpr auto num_dimensions() -> std::size_t {
29 return sizeof...(Dimensions);
30 }
31 using difference_type = size_t;
32 using grid_t = tatooine::rectilinear_grid<Dimensions...>;
33 using value_type = vertex_handle<num_dimensions()>;
36 using iterator_category = std::bidirectional_iterator_tag;
37 friend class boost::iterator_core_access;
38 //============================================================================
39 private:
40 grid_t const* const m_grid;
41 mutable vertex_handle<num_dimensions()> m_handle;
42 //============================================================================
43 public:
44 vertex_iterator(grid_t const* const g,
45 vertex_handle<num_dimensions()> const& h)
46 : m_grid{g}, m_handle{h} {}
47 //----------------------------------------------------------------------------
49 vertex_iterator(vertex_iterator&&) noexcept = default;
50 //----------------------------------------------------------------------------
51 auto operator=(vertex_iterator const&) -> vertex_iterator& = default;
52 auto operator=(vertex_iterator&&) noexcept -> vertex_iterator& = default;
53 //============================================================================
54 private:
55 constexpr auto equal(vertex_iterator const& other) const {
56 return m_handle == other.m_handle;
57 }
58 //----------------------------------------------------------------------------
59 constexpr auto dereference() const -> auto& { return m_handle; }
60 //----------------------------------------------------------------------------
61 template <size_t I>
62 constexpr auto decrement_check(bool& stop) {
63 if (!stop && m_handle.indices()[I] == 0) {
64 m_handle.indices()[I] = m_grid->template size<I>() - 1;
65 } else {
66 --m_handle.indices()[I];
67 stop = true;
68 }
69 }
70 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
71 template <size_t... Is>
72 constexpr auto decrement(std::index_sequence<Is...> /*seq*/) {
73 --m_handle.plain_index();
74 bool stop = false;
75 (decrement_check<Is>(stop), ...);
76 return *this;
77 }
78 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
79 constexpr auto decrement() {
80 return decrement(std::make_index_sequence<num_dimensions() - 1>{});
81 }
82 //----------------------------------------------------------------------------
83 template <size_t I>
84 constexpr auto increment_check(bool& stop) {
85 if (!stop && m_handle.indices()[I] == m_grid->template size<I>()) {
86 m_handle.indices()[I] = 0;
87 ++m_handle.indices()[I + 1];
88 } else {
89 stop = true;
90 }
91 }
92 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
93 template <size_t... Is>
94 constexpr auto increment(std::index_sequence<Is...> /*seq*/) {
95 ++m_handle.plain_index();
96 ++m_handle.indices().front();
97 bool stop = false;
98 (increment_check<Is>(stop), ...);
99 return *this;
100 }
101 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
102 constexpr auto increment() {
103 return increment(std::make_index_sequence<num_dimensions() - 1>{});
104 }
105
106 public:
107 //--------------------------------------------------------------------------
108 constexpr auto operator<(vertex_iterator const& other) const -> bool {
109 return m_handle < other.m_handle;
110 }
111 //--------------------------------------------------------------------------
112 constexpr auto operator<=(vertex_iterator const& other) const -> bool {
113 return m_handle <= other.m_handle;
114 }
115 //--------------------------------------------------------------------------
116 constexpr auto operator>(vertex_iterator const& other) const -> bool {
117 return m_handle > other.m_handle;
118 }
119 //--------------------------------------------------------------------------
120 constexpr auto operator>=(vertex_iterator const& other) const -> bool {
121 return m_handle >= other.m_handle;
122 }
123};
124//==============================================================================
125template <dimension... Dimensions>
126auto next(vertex_iterator<Dimensions...> it, size_t num = 1) {
127 for (size_t i = 0; i < num; ++i) {
128 ++it;
129 }
130 return it;
131}
132//==============================================================================
133} // namespace tatooine::detail::rectilinear_grid
134//==============================================================================
135#endif
Definition: rectilinear_grid.h:38
Definition: vtp_writer.h:3
Definition: algorithm.h:6
auto next(Iter iter)
Definition: iterator_facade.h:325
constexpr auto plain_index() const -> auto const &
Definition: vertex_handle.h:37
constexpr auto indices() const -> auto const &
Definition: vertex_handle.h:31
constexpr auto operator<(vertex_iterator const &other) const -> bool
Definition: vertex_iterator.h:108
constexpr auto increment()
Definition: vertex_iterator.h:102
constexpr auto dereference() const -> auto &
Definition: vertex_iterator.h:59
constexpr auto operator>(vertex_iterator const &other) const -> bool
Definition: vertex_iterator.h:116
constexpr auto increment_check(bool &stop)
Definition: vertex_iterator.h:84
vertex_iterator(vertex_iterator &&) noexcept=default
constexpr auto decrement()
Definition: vertex_iterator.h:79
std::bidirectional_iterator_tag iterator_category
Definition: vertex_iterator.h:36
grid_t const *const m_grid
Definition: vertex_iterator.h:40
vertex_handle< num_dimensions()> m_handle
Definition: vertex_iterator.h:41
constexpr auto operator>=(vertex_iterator const &other) const -> bool
Definition: vertex_iterator.h:120
vertex_iterator(grid_t const *const g, vertex_handle< num_dimensions()> const &h)
Definition: vertex_iterator.h:44
size_t difference_type
Definition: vertex_iterator.h:31
constexpr auto decrement_check(bool &stop)
Definition: vertex_iterator.h:62
vertex_iterator(vertex_iterator const &)=default
constexpr auto decrement(std::index_sequence< Is... >)
Definition: vertex_iterator.h:72
static constexpr auto num_dimensions() -> std::size_t
Definition: vertex_iterator.h:28
constexpr auto increment(std::index_sequence< Is... >)
Definition: vertex_iterator.h:94
constexpr auto operator<=(vertex_iterator const &other) const -> bool
Definition: vertex_iterator.h:112