My Project
Iterators.hpp
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>
40#include "PartitionIteratorRule.hpp"
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
107 class HierarchicIterator : public Entity<0>
108 {
109 public:
110 using Reference = const Entity<0>&;
115 : Entity<0>(grid, EntityRep<0>::InvalidIndex, true )
116 {
117 }
118
119 // Constructor with Entity<0> target and maxLevel (begin iterator).
120 HierarchicIterator(Entity<0> target, int maxLevel)
121 : maxLevel_(maxLevel)
122 {
123 // Load sons of target onto the iterator stack
124 stackChildren_(target);
125
126 // Set entity target to the next child if exists
127 resetEntity_();
128 }
129
130
131 // Constructor without valid element (end iterator).
132 HierarchicIterator(int maxLevel)
133 : maxLevel_(maxLevel)
134 {
135 resetEntity_();
136 }
137
138
139
140
141 // COMPARISON ON TARGET ENTITY. hierarchiIt == other is target is the same, or both are invalid
143 // bool operator==(const HierarchicIterator& other) const
144 // {
145 // return (pgrid_ == other.pgrid_) || (/* bath invalid entitties*/);
146 // }
147 // Check wheter one of them if invalid, the other has to be also invalid.
148 // If both are valid, check that they are equal.
149
154 {
155 if (elemStack_.empty()){
156 return *this;
157 }
158 // Reference to the top element of elemStack_
159 auto target = elemStack_.top();
160 // Remove the element on top of elemStack_
161 elemStack_.pop();
162 // Load sons of previous target onto elemStack_
163 stackChildren_(target);
164 // Set entity target to the next stacked element if exists
165 resetEntity_();
166 return *this;
167 }
168
170 const Entity<0>* operator->() const
171 {
172 assert(Entity<0>::isValid());
173 return (this);
174 }
175
177 const Entity<0>& operator*() const
178 {
179 assert(Entity<0>::isValid());
180 return (*this);
181 }
182
183 private:
184 void stackChildren_(const Entity<0>& target);
185
186 void resetEntity_();
187
189 Entity<0> virtualEntity_;
190
192 int maxLevel_;
193
194 // For depth-first search
195 std::stack<Entity<0>> elemStack_;
196
197 }; // end class HierarchicIterator
198
199 } // namespace cpgrid
200} // namespace Dune
201
202namespace std
203{
204 template< int codim, Dune::PartitionIteratorType pitype >
205 struct iterator_traits< Dune::cpgrid::Iterator< codim, pitype > >
206 {
208 typedef ptrdiff_t difference_type;
209 typedef typename Iterator::Entity value_type;
210 typedef value_type* pointer;
211 typedef value_type& reference;
212 typedef forward_iterator_tag iterator_category;
213 };
214
215 template <>
216 struct iterator_traits< Dune::cpgrid::HierarchicIterator >
217 {
218 typedef ptrdiff_t difference_type;
220 typedef value_type* pointer;
221 typedef value_type& reference;
222 typedef forward_iterator_tag iterator_category;
223 };
224
225} // namespace std
226
227
228#include <opm/grid/cpgrid/CpGridData.hpp>
229#include "Entity.hpp"
230
231namespace Dune {
232namespace cpgrid {
233
234template<int cd, PartitionIteratorType pitype>
235Iterator<cd, pitype>::Iterator(const CpGridData& grid, int index, bool orientation)
236 : Entity<cd>(grid,
237 // If the partition is empty, goto to end iterator!
238 EntityRep<cd>(PartitionIteratorRule<pitype>::emptySet?grid.size(cd):index,
239 orientation)),
240 noEntities_(grid.size(cd))
241{
242 if(rule_.fullSet || rule_.emptySet)
243 return;
244
245 while(this->index()<noEntities_ && rule_.isInvalid(*this))
247}
248}}
249
250
251#endif // OPM_ITERATORS_HEADER
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:122
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.
Definition: EntityRep.hpp:140
int index() const
The (positive) index of an entity.
Definition: EntityRep.hpp:126
void increment()
Increments the entityrep's index() by one.
Definition: EntityRep.hpp:153
Definition: Entity.hpp:64
Entity()
Constructor taking a grid and an integer entity representation.
Definition: Entity.hpp:110
Only needs to provide interface for doing nothing.
Definition: Iterators.hpp:108
const Entity< 0 > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:177
HierarchicIterator(const CpGridData &grid)
Definition: Iterators.hpp:114
HierarchicIterator & operator++()
Equality.
Definition: Iterators.hpp:153
const Entity< 0 > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:170
Iterator intended to be used as LeafIterator and LevelIterator (no difference due to no adaptivity) f...
Definition: Iterators.hpp:60
const Entity< cd > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:84
Iterator & operator++()
Increment operator.
Definition: Iterators.hpp:74
Iterator(const CpGridData &grid, int index, bool orientation)
Definition: Iterators.hpp:235
const Entity< cd > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:91
Copyright 2019 Equinor AS.
Definition: CartesianIndexMapper.hpp:10
A rule at what entities to stop.
Definition: PartitionIteratorRule.hpp:42