Iterators.hpp
Go to the documentation of this file.
1//===========================================================================
2//
3// File: Iterators.hpp
4//
5// Created: Fri May 29 23:29:09 2009
6//
7// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
8// Bård Skaflestad <bard.skaflestad@sintef.no>
9//
10// $Date$
11//
12// $Revision$
13//
14//===========================================================================
15
16/*
17Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
18Copyright 2009, 2010, 2022 Equinor ASA.
19
20This file is part of The Open Porous Media project (OPM).
21
22OPM is free software: you can redistribute it and/or modify
23it under the terms of the GNU General Public License as published by
24the Free Software Foundation, either version 3 of the License, or
25(at your option) any later version.
26
27OPM is distributed in the hope that it will be useful,
28but WITHOUT ANY WARRANTY; without even the implied warranty of
29MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30GNU General Public License for more details.
31
32You should have received a copy of the GNU General Public License
33along with OPM. If not, see <http://www.gnu.org/licenses/>.
34*/
35
36#ifndef OPM_ITERATORS_HEADER
37#define OPM_ITERATORS_HEADER
38
39#include <dune/grid/common/gridenums.hh>
41#include <opm/common/ErrorMacros.hpp>
42#include "CpGridData.hpp"
43
44
45#include <stack>
46
47namespace Dune
48{
49 namespace cpgrid
50 {
51 class CpGridData;
52
53
58 template<int cd, PartitionIteratorType pitype>
59 class Iterator : public Entity<cd>
60 {
61 public:
62 using Reference = const Entity<cd>&;
66 Iterator(const CpGridData& grid, int index, bool orientation);
67
68 Iterator() = default;
69
77 {
79 if(rule_.fullSet || rule_.emptySet)
80 return *this;
81 while(this->index()<noEntities_ && rule_.isInvalid(*this))
83 return *this;
84 }
85
87 {
88 Iterator tmp(*this);
89 ++(*this);
90 return tmp;
91 }
92
94 const Entity<cd>* operator->() const
95 {
96 assert(Entity<cd>::isValid());
97 return (this);
98 }
99
101 const Entity<cd>& operator*() const
102 {
103 assert(Entity<cd>::isValid());
104 return (*this);
105 }
106
107 private:
109 int noEntities_;
111 };
112
113
114
115
118 {
119
120 public:
121 using Reference = const Entity<0>&;
125 explicit HierarchicIterator(const CpGridData& grid)
126 : virtualEntity_(grid, EntityRep<0>::InvalidIndex, true )
127 {
128 }
129
130 explicit HierarchicIterator() = default;
131
132 // Constructor with Entity<0> target and maxLevel (begin iterator).
133 HierarchicIterator(Entity<0> target, int maxLevel)
134 : virtualEntity_(target), maxLevel_(maxLevel)
135 {
136 // Load sons of target onto the iterator stack
137 stackChildren_(target);
138
139 // Set entity target to the next child if exists
140 resetEntity_();
141 }
142
143
144 // Constructor without valid element (end iterator).
145 explicit HierarchicIterator(int maxLevel)
146 : maxLevel_(maxLevel)
147 {
148 resetEntity_();
149 }
150
152 bool operator==(const HierarchicIterator& other) const
153 {
154 return virtualEntity_ == other.virtualEntity_;
155 }
156
158 bool operator!=(const HierarchicIterator& other) const
159 {
160 return !this->operator==(other);
161 }
162
167 {
168 if (elemStack_.empty()){
169 return *this;
170 }
171 // Reference to the top element of elemStack_
172 auto target = elemStack_.top();
173 // Remove the element on top of elemStack_
174 elemStack_.pop();
175 // Load sons of previous target onto elemStack_
176 stackChildren_(target);
177 // Set entity target to the next stacked element if exists
178 resetEntity_();
179 return *this;
180 }
181
186 {
187 if (elemStack_.empty()){
188 return *this;
189 }
190 auto copy = *this;
191 // Reference to the top element of elemStack_
192 auto target = elemStack_.top();
193 // Remove the element on top of elemStack_
194 elemStack_.pop();
195 // Load sons of previous target onto elemStack_
196 stackChildren_(target);
197 // Set entity target to the next stacked element if exists
198 resetEntity_();
199 return copy;
200 }
201
203 const Entity<0>* operator->() const
204 {
205 assert(this -> virtualEntity_.isValid());
206 return &virtualEntity_;
207 }
208
210 const Entity<0>& operator*() const
211 {
212 assert(this-> virtualEntity_.isValid());
213 return virtualEntity_;
214 }
215
216 private:
217 void stackChildren_(const Entity<0>& target);
218
219 void resetEntity_();
220
221 Entity<0> virtualEntity_;
222
224 int maxLevel_;
225
226 // For depth-first search
227 std::stack<Entity<0>> elemStack_;
228
229 }; // end class HierarchicIterator
230
231 } // namespace cpgrid
232} // namespace Dune
233
234namespace std
235{
236 template< int codim, Dune::PartitionIteratorType pitype >
237 struct iterator_traits< Dune::cpgrid::Iterator< codim, pitype > >
238 {
240 typedef ptrdiff_t difference_type;
241 typedef typename Iterator::Entity value_type;
244 typedef forward_iterator_tag iterator_category;
245 };
246
247 template <>
248 struct iterator_traits< Dune::cpgrid::HierarchicIterator >
249 {
250 typedef ptrdiff_t difference_type;
254 typedef forward_iterator_tag iterator_category;
255 };
256
257} // namespace std
258
259
261#include "Entity.hpp"
262
263namespace Dune {
264namespace cpgrid {
265
266template<int cd, PartitionIteratorType pitype>
267Iterator<cd, pitype>::Iterator(const CpGridData& grid, int index, bool orientation)
268 : Entity<cd>(grid,
269 // If the partition is empty, goto to end iterator!
270 EntityRep<cd>(PartitionIteratorRule<pitype>::emptySet?grid.size(cd):index,
271 orientation)),
272 noEntities_(grid.size(cd))
273{
274 if(rule_.fullSet || rule_.emptySet)
275 return;
276
277 while(this->index()<noEntities_ && rule_.isInvalid(*this))
279}
280}}
281
282
283#endif // OPM_ITERATORS_HEADER
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:118
Definition: Entity.hpp:72
bool isValid() const
Definition: Entity.hpp:434
Represents an entity of a given codim, with positive or negative orientation.
Definition: EntityRep.hpp:99
bool orientation() const
Returns true if the entity has positive orientation. Not a Dune interface method.
Definition: EntityRep.hpp:140
int index() const
The (positive) index of an entity. Not a Dune interface method.
Definition: EntityRep.hpp:126
void increment()
Increments the entityrep's index() by one.
Definition: EntityRep.hpp:153
Only needs to provide interface for doing nothing.
Definition: Iterators.hpp:118
HierarchicIterator(Entity< 0 > target, int maxLevel)
Definition: Iterators.hpp:133
bool operator==(const HierarchicIterator &other) const
Equality.
Definition: Iterators.hpp:152
HierarchicIterator(int maxLevel)
Definition: Iterators.hpp:145
const Entity< 0 > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:210
HierarchicIterator operator++(int)
Definition: Iterators.hpp:185
HierarchicIterator(const CpGridData &grid)
Definition: Iterators.hpp:125
HierarchicIterator & operator++()
Definition: Iterators.hpp:166
bool operator!=(const HierarchicIterator &other) const
Inequality.
Definition: Iterators.hpp:158
const Entity< 0 > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:203
Definition: Iterators.hpp:60
const Entity< cd > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:94
Iterator & operator++()
Definition: Iterators.hpp:76
Iterator operator++(int)
Definition: Iterators.hpp:86
const Entity< cd > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:101
The namespace Dune is the main namespace for all Dune code.
Definition: common/CartesianIndexMapper.hpp:10
STL namespace.
Definition: PartitionIteratorRule.hpp:42
forward_iterator_tag iterator_category
Definition: Iterators.hpp:254
value_type & reference
Definition: Iterators.hpp:253
value_type * pointer
Definition: Iterators.hpp:252
Dune::cpgrid::Entity< 0 > value_type
Definition: Iterators.hpp:251
ptrdiff_t difference_type
Definition: Iterators.hpp:250
ptrdiff_t difference_type
Definition: Iterators.hpp:240
forward_iterator_tag iterator_category
Definition: Iterators.hpp:244
value_type & reference
Definition: Iterators.hpp:243
value_type * pointer
Definition: Iterators.hpp:242
Dune::cpgrid::Iterator< codim, pitype > Iterator
Definition: Iterators.hpp:239
Iterator::Entity value_type
Definition: Iterators.hpp:241