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
75 {
77 if(rule_.fullSet || rule_.emptySet)
78 return *this;
79 while(this->index()<noEntities_ && rule_.isInvalid(*this))
81 return *this;
82 }
84 const Entity<cd>* operator->() const
85 {
86 assert(Entity<cd>::isValid());
87 return (this);
88 }
89
91 const Entity<cd>& operator*() const
92 {
93 assert(Entity<cd>::isValid());
94 return (*this);
95 }
96
97 private:
99 int noEntities_;
101 };
102
103
104
105
108 {
109
110 public:
111 using Reference = const Entity<0>&;
116 : virtualEntity_(grid, EntityRep<0>::InvalidIndex, true )
117 {
118 }
119
120 // Constructor with Entity<0> target and maxLevel (begin iterator).
121 HierarchicIterator(Entity<0> target, int maxLevel)
122 : virtualEntity_(target), maxLevel_(maxLevel)
123 {
124 // Load sons of target onto the iterator stack
125 stackChildren_(target);
126
127 // Set entity target to the next child if exists
128 resetEntity_();
129 }
130
131
132 // Constructor without valid element (end iterator).
133 HierarchicIterator(int maxLevel)
134 : maxLevel_(maxLevel)
135 {
136 resetEntity_();
137 }
138
140 bool operator==(const HierarchicIterator& other) const
141 {
142 return virtualEntity_ == other.virtualEntity_;
143 }
144
146 bool operator!=(const HierarchicIterator& other) const
147 {
148 return !this->operator==(other);
149 }
150
155 {
156 if (elemStack_.empty()){
157 return *this;
158 }
159 // Reference to the top element of elemStack_
160 auto target = elemStack_.top();
161 // Remove the element on top of elemStack_
162 elemStack_.pop();
163 // Load sons of previous target onto elemStack_
164 stackChildren_(target);
165 // Set entity target to the next stacked element if exists
166 resetEntity_();
167 return *this;
168 }
169
174 {
175 if (elemStack_.empty()){
176 return *this;
177 }
178 auto copy = *this;
179 // Reference to the top element of elemStack_
180 auto target = elemStack_.top();
181 // Remove the element on top of elemStack_
182 elemStack_.pop();
183 // Load sons of previous target onto elemStack_
184 stackChildren_(target);
185 // Set entity target to the next stacked element if exists
186 resetEntity_();
187 return copy;
188 }
189
191 const Entity<0>* operator->() const
192 {
193 assert(this -> virtualEntity_.isValid());
194 return &virtualEntity_;
195 }
196
198 const Entity<0>& operator*() const
199 {
200 assert(this-> virtualEntity_.isValid());
201 return virtualEntity_;
202 }
203
204 private:
205 void stackChildren_(const Entity<0>& target);
206
207 void resetEntity_();
208
209 Entity<0> virtualEntity_;
210
212 int maxLevel_;
213
214 // For depth-first search
215 std::stack<Entity<0>> elemStack_;
216
217 }; // end class HierarchicIterator
218
219 } // namespace cpgrid
220} // namespace Dune
221
222namespace std
223{
224 template< int codim, Dune::PartitionIteratorType pitype >
225 struct iterator_traits< Dune::cpgrid::Iterator< codim, pitype > >
226 {
228 typedef ptrdiff_t difference_type;
229 typedef typename Iterator::Entity value_type;
232 typedef forward_iterator_tag iterator_category;
233 };
234
235 template <>
236 struct iterator_traits< Dune::cpgrid::HierarchicIterator >
237 {
238 typedef ptrdiff_t difference_type;
242 typedef forward_iterator_tag iterator_category;
243 };
244
245} // namespace std
246
247
249#include "Entity.hpp"
250
251namespace Dune {
252namespace cpgrid {
253
254template<int cd, PartitionIteratorType pitype>
255Iterator<cd, pitype>::Iterator(const CpGridData& grid, int index, bool orientation)
256 : Entity<cd>(grid,
257 // If the partition is empty, goto to end iterator!
258 EntityRep<cd>(PartitionIteratorRule<pitype>::emptySet?grid.size(cd):index,
259 orientation)),
260 noEntities_(grid.size(cd))
261{
262 if(rule_.fullSet || rule_.emptySet)
263 return;
264
265 while(this->index()<noEntities_ && rule_.isInvalid(*this))
267}
268}}
269
270
271#endif // OPM_ITERATORS_HEADER
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:131
Definition: Entity.hpp:65
bool isValid() const
Definition: Entity.hpp:408
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:108
HierarchicIterator(Entity< 0 > target, int maxLevel)
Definition: Iterators.hpp:121
bool operator==(const HierarchicIterator &other) const
Equality.
Definition: Iterators.hpp:140
HierarchicIterator(int maxLevel)
Definition: Iterators.hpp:133
const Entity< 0 > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:198
HierarchicIterator operator++(int)
Definition: Iterators.hpp:173
HierarchicIterator(const CpGridData &grid)
Definition: Iterators.hpp:115
HierarchicIterator & operator++()
Definition: Iterators.hpp:154
bool operator!=(const HierarchicIterator &other) const
Inequality.
Definition: Iterators.hpp:146
const Entity< 0 > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:191
Definition: Iterators.hpp:60
const Entity< cd > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:84
Iterator & operator++()
Definition: Iterators.hpp:74
Iterator(const CpGridData &grid, int index, bool orientation)
Definition: Iterators.hpp:255
const Entity< cd > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:91
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:242
value_type & reference
Definition: Iterators.hpp:241
value_type * pointer
Definition: Iterators.hpp:240
Dune::cpgrid::Entity< 0 > value_type
Definition: Iterators.hpp:239
ptrdiff_t difference_type
Definition: Iterators.hpp:238
ptrdiff_t difference_type
Definition: Iterators.hpp:228
forward_iterator_tag iterator_category
Definition: Iterators.hpp:232
value_type & reference
Definition: Iterators.hpp:231
value_type * pointer
Definition: Iterators.hpp:230
Dune::cpgrid::Iterator< codim, pitype > Iterator
Definition: Iterators.hpp:227
Iterator::Entity value_type
Definition: Iterators.hpp:229