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/grid/utility/ErrorMacros.hpp>
42
43namespace Dune
44{
45 namespace cpgrid
46 {
47 class CpGridData;
48
49
54 template<int cd, PartitionIteratorType pitype>
55 class Iterator : public Entity<cd>
56 {
57 public:
58 using Reference = const Entity<cd>&;
62 Iterator(const CpGridData& grid, int index, bool orientation);
63
71 {
73 if(rule_.fullSet || rule_.emptySet)
74 return *this;
75 while(this->index()<noEntities_ && rule_.isInvalid(*this))
77 return *this;
78 }
80 const Entity<cd>* operator->() const
81 {
82 assert(Entity<cd>::isValid());
83 return (this);
84 }
85
87 const Entity<cd>& operator*() const
88 {
89 assert(Entity<cd>::isValid());
90 return (*this);
91 }
92
93 private:
95 int noEntities_;
97 };
98
99
100
101
103 class HierarchicIterator : public Entity<0>
104 {
105 public:
106 using Reference = const Entity<0>&;
111 : Entity<0>(grid, EntityRep<0>::InvalidIndex, true )
112 {
113 }
114
119 {
120 OPM_THROW(std::runtime_error, "Calling operator++() on HierarchicIterator for CpGrid, which has no refinement.");
121 return *this;
122 }
124 const Entity<0>* operator->() const
125 {
126 assert(Entity<0>::isValid());
127 return (this);
128 }
129
131 const Entity<0>& operator*() const
132 {
133 assert(Entity<0>::isValid());
134 return (*this);
135 }
136 };
137
138
139
140
141
142 } // namespace cpgrid
143} // namespace Dune
144
145namespace std
146{
147 template< int codim, Dune::PartitionIteratorType pitype >
148 struct iterator_traits< Dune::cpgrid::Iterator< codim, pitype > >
149 {
151 typedef ptrdiff_t difference_type;
152 typedef typename Iterator::Entity value_type;
153 typedef value_type* pointer;
154 typedef value_type& reference;
155 typedef forward_iterator_tag iterator_category;
156 };
157
158 template <>
159 struct iterator_traits< Dune::cpgrid::HierarchicIterator >
160 {
161 typedef ptrdiff_t difference_type;
163 typedef value_type* pointer;
164 typedef value_type& reference;
165 typedef forward_iterator_tag iterator_category;
166 };
167
168} // namespace std
169
170
171#include <opm/grid/cpgrid/CpGridData.hpp>
172#include "Entity.hpp"
173
174namespace Dune {
175namespace cpgrid {
176
177template<int cd, PartitionIteratorType pitype>
178Iterator<cd, pitype>::Iterator(const CpGridData& grid, int index, bool orientation)
179 : Entity<cd>(grid,
180 // If the partition is empty, goto to end iterator!
181 EntityRep<cd>(PartitionIteratorRule<pitype>::emptySet?grid.size(cd):index,
182 orientation)),
183 noEntities_(grid.size(cd))
184{
185 if(rule_.fullSet || rule_.emptySet)
186 return;
187
188 while(this->index()<noEntities_ && rule_.isInvalid(*this))
190}
191}}
192
193
194#endif // OPM_ITERATORS_HEADER
Struct that hods all the data needed to represent a Cpgrid.
Definition: CpGridData.hpp:123
Represents an entity of a given codim, with positive or negative orientation.
Definition: EntityRep.hpp:98
bool orientation() const
Returns true if the entity has positive orientation.
Definition: EntityRep.hpp:139
int index() const
The (positive) index of an entity.
Definition: EntityRep.hpp:125
void increment()
Increments the entityrep's index() by one.
Definition: EntityRep.hpp:152
Definition: Entity.hpp:64
Entity()
Constructor taking a grid and an integer entity representation.
Definition: Entity.hpp:108
Only needs to provide interface for doing nothing.
Definition: Iterators.hpp:104
const Entity< 0 > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:131
HierarchicIterator(const CpGridData &grid)
Definition: Iterators.hpp:110
HierarchicIterator & operator++()
Definition: Iterators.hpp:118
const Entity< 0 > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:124
Iterator intended to be used as LeafIterator and LevelIterator (no difference due to no adaptivity) f...
Definition: Iterators.hpp:56
const Entity< cd > * operator->() const
Const member by pointer operator.
Definition: Iterators.hpp:80
Iterator & operator++()
Increment operator.
Definition: Iterators.hpp:70
Iterator(const CpGridData &grid, int index, bool orientation)
Definition: Iterators.hpp:178
const Entity< cd > & operator*() const
Const dereferencing operator.
Definition: Iterators.hpp:87
Copyright 2019 Equinor AS.
Definition: CartesianIndexMapper.hpp:10
A rule at what entities to stop.
Definition: PartitionIteratorRule.hpp:42