9 #include <morphio/exceptions.h>
10 #include <morphio/types.h>
14 template <
typename SectionT,
typename MorphologyT>
15 std::vector<SectionT> getChildren(
const MorphologyT& morphology) noexcept {
16 return morphology.rootSections();
19 template <
typename SectionT>
20 std::vector<SectionT> getChildren(
const SectionT& section) {
21 return section.children();
24 template <
typename SectionT>
25 std::vector<std::shared_ptr<SectionT>> getChildren(
const std::shared_ptr<SectionT>& section) {
26 return section->children();
29 template <
typename SectionT>
30 bool isRoot(
const SectionT& current) {
31 return current.isRoot();
34 template <
typename SectionT>
35 bool isRoot(
const std::shared_ptr<SectionT>& current) {
36 return current->isRoot();
39 template <
typename SectionT>
40 SectionT getParent(
const SectionT& current) {
41 return current.parent();
44 template <
typename SectionT>
45 std::shared_ptr<SectionT> getParent(
const std::shared_ptr<SectionT>& current) {
46 return current->parent();
52 template <
typename SectionT,
typename MorphologyT>
56 using iterator_category = std::input_iterator_tag;
57 using value_type = SectionT;
58 using difference_type = std::ptrdiff_t;
59 using pointer = SectionT*;
60 using reference = SectionT&;
68 inline SectionT operator*()
const;
77 std::deque<SectionT> deque_;
80 template <
typename SectionT,
typename MorphologyT>
84 using iterator_category = std::input_iterator_tag;
85 using value_type = SectionT;
86 using difference_type = std::ptrdiff_t;
87 using pointer = SectionT*;
88 using reference = SectionT&;
96 inline SectionT operator*()
const;
105 std::deque<SectionT> deque_;
108 template <
typename SectionT>
112 using iterator_category = std::input_iterator_tag;
113 using value_type = SectionT;
114 using difference_type = std::ptrdiff_t;
115 using pointer = SectionT*;
116 using reference = SectionT&;
123 inline SectionT operator*()
const;
146 template <
typename SectionT,
typename MorphologyT>
148 deque_.push_front(section);
151 template <
typename SectionT,
typename MorphologyT>
152 inline breadth_iterator_t<SectionT, MorphologyT>::breadth_iterator_t(
153 const MorphologyT& morphology) {
154 const auto& children = detail::getChildren<SectionT, MorphologyT>(morphology);
155 std::copy(children.begin(), children.end(), std::back_inserter(deque_));
158 template <
typename SectionT,
typename MorphologyT>
159 inline breadth_iterator_t<SectionT, MorphologyT>::breadth_iterator_t(
160 const breadth_iterator_t& other)
161 : deque_(other.deque_) {}
163 template <
typename SectionT,
typename MorphologyT>
164 inline SectionT breadth_iterator_t<SectionT, MorphologyT>::operator*()
const {
165 return deque_.front();
168 template <
typename SectionT,
typename MorphologyT>
169 inline breadth_iterator_t<SectionT, MorphologyT>&
170 breadth_iterator_t<SectionT, MorphologyT>::operator++() {
171 if (deque_.empty()) {
172 throw MorphioError(
"Can't iterate past the end");
175 const auto& children = detail::getChildren(deque_.front());
177 std::copy(children.begin(), children.end(), std::back_inserter(deque_));
182 template <
typename SectionT,
typename MorphologyT>
183 inline breadth_iterator_t<SectionT, MorphologyT>
184 breadth_iterator_t<SectionT, MorphologyT>::operator++(
int) {
185 breadth_iterator_t ret(*
this);
190 template <
typename SectionT,
typename MorphologyT>
191 inline bool breadth_iterator_t<SectionT, MorphologyT>::operator==(
192 const breadth_iterator_t& other)
const {
193 return deque_ == other.deque_;
196 template <
typename SectionT,
typename MorphologyT>
197 inline bool breadth_iterator_t<SectionT, MorphologyT>::operator!=(
198 const breadth_iterator_t& other)
const {
199 return !(*
this == other);
204 template <
typename SectionT,
typename MorphologyT>
205 inline depth_iterator_t<SectionT, MorphologyT>::depth_iterator_t(
const SectionT& section) {
206 deque_.push_front(section);
209 template <
typename SectionT,
typename MorphologyT>
210 inline depth_iterator_t<SectionT, MorphologyT>::depth_iterator_t(
const MorphologyT& morphology) {
211 const auto& children = detail::getChildren<SectionT, MorphologyT>(morphology);
212 std::copy(children.rbegin(), children.rend(), std::front_inserter(deque_));
215 template <
typename SectionT,
typename MorphologyT>
216 inline depth_iterator_t<SectionT, MorphologyT>::depth_iterator_t(
const depth_iterator_t& other)
217 : deque_(other.deque_) {}
219 template <
typename SectionT,
typename MorphologyT>
220 inline SectionT depth_iterator_t<SectionT, MorphologyT>::operator*()
const {
221 return deque_.front();
224 template <
typename SectionT,
typename MorphologyT>
225 inline depth_iterator_t<SectionT, MorphologyT>&
226 depth_iterator_t<SectionT, MorphologyT>::operator++() {
227 if (deque_.empty()) {
228 throw MorphioError(
"Can't iterate past the end");
231 const auto children = detail::getChildren(deque_.front());
233 std::copy(children.rbegin(), children.rend(), std::front_inserter(deque_));
238 template <
typename SectionT,
typename MorphologyT>
239 inline depth_iterator_t<SectionT, MorphologyT> depth_iterator_t<SectionT, MorphologyT>::operator++(
241 depth_iterator_t ret(*
this);
246 template <
typename SectionT,
typename MorphologyT>
247 inline bool depth_iterator_t<SectionT, MorphologyT>::operator==(
248 const depth_iterator_t& other)
const {
249 return deque_ == other.deque_;
252 template <
typename SectionT,
typename MorphologyT>
253 inline bool depth_iterator_t<SectionT, MorphologyT>::operator!=(
254 const depth_iterator_t& other)
const {
255 return !(*
this == other);
260 template <
typename SectionT>
261 inline upstream_iterator_t<SectionT>::upstream_iterator_t()
265 template <
typename SectionT>
266 inline upstream_iterator_t<SectionT>::upstream_iterator_t(
const SectionT& section)
270 template <
typename SectionT>
271 inline upstream_iterator_t<SectionT>::upstream_iterator_t(
const upstream_iterator_t& other) {
274 new (¤t) SectionT(other.current);
278 template <
typename SectionT>
279 inline upstream_iterator_t<SectionT>::~upstream_iterator_t() {
281 this->current.~SectionT();
285 template <
typename SectionT>
286 inline SectionT upstream_iterator_t<SectionT>::operator*()
const {
290 template <
typename SectionT>
291 inline upstream_iterator_t<SectionT>& upstream_iterator_t<SectionT>::operator++() {
293 throw MissingParentError(
"Cannot call iterate upstream past the root node");
294 }
else if (detail::isRoot(current)) {
296 this->current.~SectionT();
298 current = detail::getParent(current);
303 template <
typename SectionT>
304 inline upstream_iterator_t<SectionT> upstream_iterator_t<SectionT>::operator++(
int) {
305 upstream_iterator_t ret(*
this);
310 template <
typename SectionT>
311 inline bool upstream_iterator_t<SectionT>::operator==(
const upstream_iterator_t& other)
const {
312 if (end || other.end) {
313 return end == other.end;
315 return current == other.current;
318 template <
typename SectionT>
319 inline bool upstream_iterator_t<SectionT>::operator!=(
const upstream_iterator_t& other)
const {
320 return !(*
this == other);
Definition: section_iterators.hpp:54
Definition: section_iterators.hpp:82
Definition: section_iterators.hpp:110
Definition: endoplasmic_reticulum.h:5