opm-grid
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 /*
17 Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
18 Copyright 2009, 2010, 2022 Equinor ASA.
19 
20 This file is part of The Open Porous Media project (OPM).
21 
22 OPM is free software: you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation, either version 3 of the License, or
25 (at your option) any later version.
26 
27 OPM is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31 
32 You should have received a copy of the GNU General Public License
33 along 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/grid/utility/ErrorMacros.hpp>
42 #include "CpGridData.hpp"
43 
44 
45 #include <stack>
46 
47 namespace 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 
86  Iterator operator++(int)
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 
234 namespace 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;
242  typedef value_type* pointer;
243  typedef value_type& reference;
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;
252  typedef value_type* pointer;
253  typedef value_type& reference;
254  typedef forward_iterator_tag iterator_category;
255  };
256 
257 } // namespace std
258 
259 
260 #include <opm/grid/cpgrid/CpGridData.hpp>
261 #include "Entity.hpp"
262 
263 namespace Dune {
264 namespace cpgrid {
265 
266 template<int cd, PartitionIteratorType pitype>
267 Iterator<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
int index() const
The (positive) index of an entity.
Definition: EntityRep.hpp:125
HierarchicIterator operator++(int)
Definition: Iterators.hpp:185
bool orientation() const
Returns true if the entity has positive orientation.
Definition: EntityRep.hpp:139
HierarchicIterator(const CpGridData &grid)
Definition: Iterators.hpp:125
const Entity< cd > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:94
The namespace Dune is the main namespace for all Dune code.
Definition: CartesianIndexMapper.hpp:9
Definition: Intersection.hpp:329
const Entity< 0 > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:210
HierarchicIterator & operator++()
Definition: Iterators.hpp:166
bool operator==(const HierarchicIterator &other) const
Equality.
Definition: Iterators.hpp:152
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:117
Iterator intended to be used as LeafIterator and LevelIterator (no difference due to no adaptivity) f...
Definition: Entity.hpp:96
bool operator!=(const HierarchicIterator &other) const
Inequality.
Definition: Iterators.hpp:158
const Entity< cd > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:101
Iterator & operator++()
Increment operator.
Definition: Iterators.hpp:76
bool isValid() const
isValid method for EntitySeed
Definition: Entity.hpp:465
const Entity< 0 > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:203
Only needs to provide interface for doing nothing.
Definition: Iterators.hpp:117
Iterator(const CpGridData &grid, int index, bool orientation)
Definition: Iterators.hpp:267
A rule at what entities to stop.
Definition: PartitionIteratorRule.hpp:41
Represents an entity of a given codim, with positive or negative orientation.
Definition: CpGridData.hpp:96
void increment()
Increments the entityrep&#39;s index() by one.
Definition: EntityRep.hpp:152