Allium  0.1.0
range.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_MESH_RANGE_HPP
16 #define ALLIUM_MESH_RANGE_HPP
17 
18 #include <iterator>
19 #include "point.hpp"
20 
21 namespace allium {
22  template <int> class RangeIterator;
23 
27  template <int D>
28  class Range {
29  public:
30  Range() = default;
31 
32  Range(Point<int, D> begin_pos, Point<int, D> end_pos)
33  : m_begin_pos(begin_pos),
34  m_end_pos(end_pos)
35  {}
36 
37  bool operator== (const Range& other) const {
38  return m_begin_pos == other.m_begin_pos
39  && m_end_pos == other.m_end_pos;
40  }
41 
43  bool in(Point<int, D> p) {
44  for (int i = 0; i < D; ++i) {
45  if (p[i] < m_begin_pos[i] || p[i] >= m_end_pos[i])
46  return false;
47  }
48  return true;
49  }
50 
51  int index(Point<int, D> p) {
52  int idx = p[0] - m_begin_pos[0];
53  for (int i = 1; i < D; ++i) {
54  int dim_size = m_end_pos[i] - m_begin_pos[i];
55  idx = (p[i] - m_begin_pos[i]) + dim_size * idx;
56  }
57  return idx;
58  }
59 
61  bool intersects(const Range<D>& other) {
62  for (size_t i = 0; i < D; ++i) {
63  if (other.m_begin_pos[i] >= m_end_pos[i]
64  || other.m_end_pos[i] <= m_begin_pos[i]) {
65  return false;
66  }
67  }
68  return true;
69  }
70 
71  Point<int, D> begin_pos() const { return m_begin_pos; }
72  Point<int, D> end_pos() const { return m_end_pos; }
73 
74  RangeIterator<D> begin() const;
75  RangeIterator<D> end() const;
76 
80  Point<int, D> shape() const { return m_end_pos - m_begin_pos; }
81 
85  size_t size() const {
86  return shape().prod();
87  }
88  private:
89  Point<int, D> m_begin_pos, m_end_pos;
90  };
91 
93  template <int D, int I = D>
94  struct next_in_range {
95  static void exec(const Range<D>& r, Point<int, D>& p, bool& overflow) {
96  ++p[I-1];
97  if (p[I-1] >= r.end_pos()[I-1]) {
98  p[I-1] = r.begin_pos()[I-1];
99  next_in_range<D, I-1>::exec(r, p, overflow);
100  }
101  }
102  };
103  template <int D>
104  struct next_in_range<D, 0> {
105  static void exec(const Range<D>& r, Point<int, D>& p, bool& overflow) {
106  overflow = true;
107  }
108  };
110 
114  template <int D>
116  public:
117  using iterator_category = std::input_iterator_tag;
118  using value_type = Point<int, D>;
119  //using difference_type Distance
120  using pointer = Point<int, D>*;
121  using reference = Point<int, D>&;
122 
123  RangeIterator(Point<int, D> start, const Range<D>* range)
124  : m_range(range),
125  m_value(start),
126  m_overflow(false)
127  {}
128 
129  /* Out of range position. */
130  RangeIterator() : m_overflow(true) {}
131 
132  RangeIterator(const RangeIterator&) = delete;
133  RangeIterator& operator= (const RangeIterator&) = delete;
134 
135  RangeIterator(RangeIterator&&) = default;
136 
137  bool operator==(const RangeIterator& rhs) const {
138  return m_overflow && rhs.m_overflow;
139  }
140 
141  bool operator!=(const RangeIterator& rhs) const {
142  return !(*this == rhs);
143  }
144 
145  RangeIterator& operator++ () {
146  next_in_range<D>::exec(*m_range, m_value, m_overflow);
147  return *this;
148  }
149 
150  Point<int, D> operator* () {
151  return m_value;
152  }
153 
154  private:
155  const Range<D>* m_range;
156  Point<int, D> m_value;
157  bool m_overflow;
158  };
159 
160  template <int D>
162  return RangeIterator<D>(begin_pos(), this);
163  }
164 
165  template <int D>
166  RangeIterator<D> Range<D>::end() const {
167  return RangeIterator<D>();
168  }
169 
170  template <int D>
171  std::ostream& operator<< (std::ostream& os, Range<D> r)
172  {
173  os << "Range(" << r.begin_pos() << ", " << r.end_pos() << ")";
174  return os;
175  }
176 
177 }
178 
179 #endif
Iterator over the points in an n-d range (allium::Range).
Definition: range.hpp:115
An n-d range.
Definition: range.hpp:28
size_t size() const
Definition: range.hpp:85
bool intersects(const Range< D > &other)
Definition: range.hpp:61
Point< int, D > shape() const
Definition: range.hpp:80
bool in(Point< int, D > p)
Definition: range.hpp:43