STIR 6.4.0
NestedIterator.inl
Go to the documentation of this file.
1//
2//
3/*
4 Copyright (C) 2000 PARAPET partners
5 Copyright (C) 2000- 2011, Hammersmith Imanet Ltd
6 This file is part of STIR.
7
8 SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
9
10 See STIR/LICENSE.txt for details
11*/
12
23
24START_NAMESPACE_STIR
25
26template <class topleveliterT, class GetRestRangeFunctionT>
27NestedIterator<topleveliterT, GetRestRangeFunctionT>::NestedIterator()
28{}
29
30#if defined __GNUC__
31// Attempt to switch off warning about unitialised _current_rest_iter
32// as this is only initialised/used if the top_level_iter specifies a valid range
33// Unfortunately, the relevant pragma only exists from gcc 4.6
34# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
35# pragma GCC diagnostic push
36# pragma GCC diagnostic ignored "-Wuninitialized"
37# endif
38#endif
39template <class topleveliterT, class GetRestRangeFunctionT>
40void
41NestedIterator<topleveliterT, GetRestRangeFunctionT>::_set_rest_iters_for_current_top_level_iter()
42{
43 if (this->_current_top_level_iter != this->_end_top_level_iter)
44 {
45 this->_current_rest_iter = GetRestRangeFunctionT().begin(this->_current_top_level_iter);
46 this->_end_rest_iter = GetRestRangeFunctionT().end(this->_current_top_level_iter);
47 }
48}
49#if defined __GNUC__
50# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
51# pragma GCC diagnostic pop
52# endif
53#endif
54
55template <class topleveliterT, class GetRestRangeFunctionT>
56NestedIterator<topleveliterT, GetRestRangeFunctionT>::NestedIterator(const topleveliterT& top_level_iter,
57 const topleveliterT& end_top_level_iter)
58 : _current_top_level_iter(top_level_iter),
59 _end_top_level_iter(end_top_level_iter)
60{
61 this->_set_rest_iters_for_current_top_level_iter();
62}
63
64template <class topleveliterT, class GetRestRangeFunctionT>
65bool
67 const NestedIterator<topleveliterT, GetRestRangeFunctionT>& iter2) const
68{
69 return this->_current_top_level_iter == iter2._current_top_level_iter
70 && (this->_current_top_level_iter == this->_end_top_level_iter || this->_current_rest_iter == iter2._current_rest_iter);
71 /*
72 alternative:
73 comparing rest_iter is only necessary when the first iterator is not at the end.
74 This probably doesn't matter too much though as usually we are comparing with
75 end_all(), in which case the top_level_iters will only be equal when we are
76 at the end. So, the extra test would only occurs once in the loop over the whole
77 sequence.
78
79 A (possibly sligthly faster) implementation would be:
80
81 _current_top_level_iter == iter2._current_top_level_iter &&
82 _current_rest_iter == iter2._current_rest_iter;
83
84 However, the above relies on the fact that incrementing the iterator
85 ends up exactly in end_all(). This seems tricky to implement in general.
86 */
87}
88template <class topleveliterT, class GetRestRangeFunctionT>
89bool
91 const NestedIterator<topleveliterT, GetRestRangeFunctionT>& iter2) const
92{
93 return !(*this == iter2);
94}
95
96template <class topleveliterT, class GetRestRangeFunctionT>
99{
100 // TODO can only do assert for random-access iterators
101 // assert(this->_current_top_level_iter < this->_end_top_level_iter);
102 ++this->_current_rest_iter;
103 if (this->_current_rest_iter == this->_end_rest_iter)
104 {
105 // advance the top-level iterator and reset rest_iters
106 ++this->_current_top_level_iter;
107 this->_set_rest_iters_for_current_top_level_iter();
108 }
109 return *this;
111
112template <class topleveliterT, class GetRestRangeFunctionT>
115{
116 const NestedIterator<topleveliterT, GetRestRangeFunctionT> was = *this;
117 ++(*this);
118 return was;
119}
120
121template <class topleveliterT, class GetRestRangeFunctionT>
122typename NestedIterator<topleveliterT, GetRestRangeFunctionT>::reference
124{
125 return *this->_current_rest_iter;
126}
127
128template <class topleveliterT, class GetRestRangeFunctionT>
129typename NestedIterator<topleveliterT, GetRestRangeFunctionT>::pointer
131{
132 return &(this->operator*());
134
135END_NAMESPACE_STIR
Class NestedIterator implements a (forward) iterator using a pair of 'nested' iterators.
Definition NestedIterator.h:94
NestedIterator & operator++()
prefix increment
Definition NestedIterator.inl:98
bool operator==(const NestedIterator &) const
test equality
Definition NestedIterator.inl:66
bool operator!=(const NestedIterator &) const
test equality
Definition NestedIterator.inl:90
reference operator*() const
dereferencing operator
Definition NestedIterator.inl:123
pointer operator->() const
member-selection operator
Definition NestedIterator.inl:130