Antkeeper  0.0.1
brep-element-container.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_GEOM_BREP_ELEMENT_CONTAINER_HPP
21 #define ANTKEEPER_GEOM_BREP_ELEMENT_CONTAINER_HPP
22 
25 #include <cstddef>
26 #include <iterator>
27 #include <memory>
28 #include <vector>
29 
30 namespace geom {
36 template <class T>
38 {
39 public:
40  friend class brep_mesh;
41 
42  using element_type = T;
43 
45  {
46  public:
47  using iterator_category = std::random_access_iterator_tag;
48  using iterator_concept = std::random_access_iterator_tag;
49  using difference_type = std::ptrdiff_t;
51  using pointer = const value_type*;
52  using reference = const value_type&;
53 
54  [[nodiscard]] inline constexpr value_type operator*() const noexcept
55  {
56  return m_it->get();
57  }
58 
59  [[nodiscard]] inline constexpr value_type operator->() const noexcept
60  {
61  return m_it->get();
62  }
63 
64  [[nodiscard]] inline constexpr value_type operator[](difference_type i) const noexcept
65  {
66  return m_it[i]->get();
67  }
68 
69  inline const_iterator& operator++() noexcept
70  {
71  ++m_it;
72  return *this;
73  }
74 
75  [[nodiscard]] inline const_iterator operator++(int) noexcept
76  {
77  const_iterator tmp = *this;
78  ++(*this);
79  return tmp;
80  }
81 
82  inline const_iterator& operator--() noexcept
83  {
84  --m_it;
85  return *this;
86  }
87 
88  [[nodiscard]] inline const_iterator operator--(int) noexcept
89  {
90  const_iterator tmp = *this;
91  --(*this);
92  return tmp;
93  }
94 
96  {
97  m_it += n;
98  return *this;
99  }
100 
102  {
103  m_it -= n;
104  return *this;
105  }
106 
107  [[nodiscard]] inline bool operator==(const const_iterator& other) const noexcept
108  {
109  return m_it == other.m_it;
110  };
111 
112  [[nodiscard]] inline std::weak_ordering operator<=>(const const_iterator& other) const noexcept
113  {
114  return m_it <=> other.m_it;
115  }
116 
117  [[nodiscard]] inline difference_type operator-(const const_iterator& rhs) const noexcept
118  {
119  return m_it - rhs.m_it;
120  }
121 
122  [[nodiscard]] inline const_iterator operator+(difference_type n) const noexcept
123  {
124  return const_iterator{m_it + n};
125  }
126 
127  [[nodiscard]] inline const_iterator operator-(difference_type n) const noexcept
128  {
129  return const_iterator{m_it - n};
130  }
131 
132  [[nodiscard]] friend const_iterator operator+(difference_type lhs, const const_iterator& rhs) noexcept
133  {
134  return const_iterator{lhs + rhs.m_it};
135  }
136 
137  [[nodiscard]] friend const_iterator operator-(difference_type lhs, const const_iterator& rhs) noexcept
138  {
139  return const_iterator{lhs - rhs.m_it};
140  }
141 
142  private:
144 
145  std::vector<std::unique_ptr<element_type>>::const_iterator m_it;
146  };
147 
148  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
149 
152 
160  [[nodiscard]] inline constexpr element_type* operator[](std::size_t i) const
161  {
162  return m_elements[i].get();
163  }
164 
166  [[nodiscard]] inline constexpr element_type* front() const noexcept
167  {
168  return m_elements.front().get();
169  }
170 
172  [[nodiscard]] inline constexpr element_type* back() const noexcept
173  {
174  return m_elements.back().get();
175  }
176 
180 
183  [[nodiscard]] inline constexpr const_iterator begin() const noexcept
184  {
185  const_iterator it;
186  it.m_it = m_elements.begin();
187  return it;
188  }
189  [[nodiscard]] inline constexpr const_iterator cbegin() const noexcept
190  {
191  return begin();
192  }
194 
197  [[nodiscard]] inline constexpr const_iterator end() const noexcept
198  {
199  const_iterator it;
200  it.m_it = m_elements.end();
201  return it;
202  }
203  [[nodiscard]] inline constexpr const_iterator cend() const noexcept
204  {
205  return end();
206  }
208 
211  [[nodiscard]] inline constexpr const_reverse_iterator rbegin() const noexcept
212  {
213  return std::make_reverse_iterator(end());
214  }
215  [[nodiscard]] inline constexpr const_reverse_iterator crbegin() const noexcept
216  {
217  return rbegin();
218  }
220 
223  [[nodiscard]] inline constexpr const_reverse_iterator rend() const noexcept
224  {
225  return std::make_reverse_iterator(begin());
226  }
227  [[nodiscard]] inline constexpr const_reverse_iterator crend() const noexcept
228  {
229  return rend();
230  }
232 
236 
238  [[nodiscard]] inline constexpr bool empty() const noexcept
239  {
240  return m_elements.empty();
241  }
242 
244  [[nodiscard]] inline constexpr std::size_t size() const noexcept
245  {
246  return m_elements.size();
247  }
248 
252 
255  [[nodiscard]] const brep_attribute_map& attributes() const noexcept
256  {
257  return m_attribute_map;
258  }
259  [[nodiscard]] brep_attribute_map& attributes() noexcept
260  {
261  return m_attribute_map;
262  }
264 
266 
267 protected:
268  friend class brep_mesh;
269 
275  inline explicit brep_element_container(brep_mesh* mesh) noexcept:
276  m_mesh(mesh)
277  {}
278 
281 
287  virtual void erase(element_type* element)
288  {
289  const auto index = element->m_index;
290 
291  for (auto& [key, values]: m_attribute_map.m_attributes)
292  {
293  values->erase(index);
294  }
295  --m_attribute_map.m_element_count;
296 
297  m_elements.back()->m_index = index;
298  m_elements[index] = std::move(m_elements.back());
299  m_elements.pop_back();
300  }
301 
308  {
309  for (auto& [key, values]: m_attribute_map.m_attributes)
310  {
311  values->emplace_back();
312  }
313  ++m_attribute_map.m_element_count;
314 
315  return m_elements.emplace_back(std::make_unique<element_type>()).get();
316  }
317 
319 
321 
322 private:
323  std::vector<std::unique_ptr<element_type>> m_elements;
324  brep_attribute_map m_attribute_map;
325 };
326 
327 } // namespace geom
328 
329 #endif // ANTKEEPER_GEOM_BREP_ELEMENT_CONTAINER_HPP
Maps names to B-rep attributes.
Container for B-rep elements.
constexpr std::size_t size() const noexcept
Returns the number of elements in the container.
constexpr const_iterator cend() const noexcept
Returns an iterator to the element following the last element.
constexpr const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the element following the last element of the reversed container.
virtual void erase(element_type *element)
Erases an element from the container.
constexpr const_iterator cbegin() const noexcept
Returns an iterator to the first element.
const brep_attribute_map & attributes() const noexcept
Returns the element attribute map.
brep_attribute_map & attributes() noexcept
Returns the element attribute map.
constexpr element_type * operator[](std::size_t i) const
Returns a pointer to the element at the specified index.
brep_element_container(brep_mesh *mesh) noexcept
Constructs a B-rep element container.
constexpr const_reverse_iterator rend() const noexcept
Returns a reverse iterator to the element following the last element of the reversed container.
constexpr const_iterator begin() const noexcept
Returns an iterator to the first element.
constexpr const_iterator end() const noexcept
Returns an iterator to the element following the last element.
std::reverse_iterator< const_iterator > const_reverse_iterator
constexpr element_type * front() const noexcept
Returns the first element.
constexpr const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the first element of the reversed container.
constexpr const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the first element of the reversed container.
constexpr bool empty() const noexcept
Returns true if the container is empty, false otherwise.
constexpr element_type * back() const noexcept
Returns the last element.
virtual element_type * emplace_back()
Appends a new element to the end of the container.
Boundary representation (B-rep) of a mesh.
Definition: brep-mesh.hpp:34
Geometric algorithms.
difference_type operator-(const const_iterator &rhs) const noexcept
const_iterator operator-(difference_type n) const noexcept
const_iterator & operator+=(difference_type n) noexcept
const_iterator & operator-=(difference_type n) noexcept
std::weak_ordering operator<=>(const const_iterator &other) const noexcept
constexpr value_type operator*() const noexcept
friend const_iterator operator+(difference_type lhs, const const_iterator &rhs) noexcept
constexpr value_type operator[](difference_type i) const noexcept
const_iterator operator+(difference_type n) const noexcept
constexpr value_type operator->() const noexcept
friend const_iterator operator-(difference_type lhs, const const_iterator &rhs) noexcept
bool operator==(const const_iterator &other) const noexcept