dune-spgrid 2.8.0
Loading...
Searching...
No Matches
iterator.hh
Go to the documentation of this file.
1#ifndef DUNE_SPGRID_ITERATOR_HH
2#define DUNE_SPGRID_ITERATOR_HH
3
4#include <algorithm>
5#include <type_traits>
6
10
11namespace Dune
12{
13
14 // SPPartitionIterator
15 // -------------------
16
17 template< int codim, class Grid >
19 {
21
22 public:
23 typedef typename std::remove_const< Grid >::type::Traits Traits;
24
25 static const int dimension = Traits::ReferenceCube::dimension;
26 static const int codimension = codim;
27 static const int mydimension = dimension - codimension;
28
29 typedef typename Traits::template Codim< codimension >::Entity Entity;
30
31 private:
33
34 public:
37
39
41
42 static const unsigned int numDirections = GridLevel::numDirections;
43
44 struct Begin {};
45 struct End {};
46
47 protected:
49
51
52 public:
53 SPPartitionIterator () = default;
54
55 SPPartitionIterator ( const GridLevel &gridLevel, const PartitionList &partitionList,
56 const Begin &b, const unsigned int sweepDir = 0 );
57 SPPartitionIterator ( const GridLevel &gridLevel, const PartitionList &partitionList,
58 const End &e, const unsigned int sweepDir = 0 );
59
60 operator bool () const { return bool( partition_ ); }
61
62 Entity operator* () const { return dereference(); }
63
64 bool operator== ( const This &other ) const { return equals( other ); }
65 bool operator!= ( const This &other ) const { return !equals( other ); }
66
67 This &operator++ () { increment(); return *this; }
68
69 Entity dereference () const { return EntityImpl( entityInfo() ); }
70
71 bool equals ( const This &other ) const { return entityInfo().equals( other.entityInfo() ); }
72
73 void increment ();
74
75 const EntityInfo &entityInfo () const { return entityInfo_; }
76 EntityInfo &entityInfo () { return entityInfo_; }
77
78 const GridLevel &gridLevel () const { return entityInfo().gridLevel(); }
79
80 private:
81 int begin ( int i, Direction dir ) const;
82 int end ( int i, Direction dir ) const;
83
84 void init ();
85
86 private:
87 EntityInfo entityInfo_;
88 typename PartitionList::Iterator partition_;
89 unsigned int sweepDirection_;
90 };
91
92
93
94 // Implementation of SPPartitionIterator
95 // -------------------------------------
96
97 template< int codim, class Grid >
98 inline SPPartitionIterator< codim, Grid >
99 ::SPPartitionIterator ( const GridLevel &gridLevel, const PartitionList &partitionList,
100 const Begin &b, unsigned int sweepDir )
101 : entityInfo_( gridLevel ),
102 partition_( partitionList.begin() ),
103 sweepDirection_( sweepDir )
104 {
105 assert( sweepDir < numDirections );
106 init();
107 }
108
109
110 template< int codim, class Grid >
112 ::SPPartitionIterator ( const GridLevel &gridLevel, const PartitionList &partitionList,
113 const End &e, unsigned int sweepDir )
114 : entityInfo_( gridLevel ),
115 partition_( partitionList.end() ),
116 sweepDirection_( sweepDir )
117 {
118 assert( sweepDir < numDirections );
119 init();
120 }
121
122
123 template< int codim, class Grid >
125 {
126 MultiIndex &id = entityInfo().id();
127 for( int i = 0; i < dimension; ++i )
128 {
129 const unsigned int sweep = (sweepDirection_ >> i) & 1;
130 id[ i ] += (2 - 4*sweep);
131 if( id[ i ] != end( i, entityInfo().direction() ) )
132 return entityInfo().update();
133 id[ i ] = begin( i, entityInfo().direction() );
134 }
135
136 DirectionIterator dirIt( entityInfo().direction() );
137 ++dirIt;
138 for( ; dirIt && partition_->empty( *dirIt ); ++dirIt )
139 continue;
140 if( dirIt )
141 {
142 for( int i = 0; i < dimension; ++i )
143 id[ i ] = begin( i, *dirIt );
144 entityInfo().update();
145 }
146 else
147 {
148 ++partition_;
149 init();
150 }
151 }
152
153
154 template< int codim, class Grid >
155 inline int SPPartitionIterator< codim, Grid >::begin ( int i, Direction dir ) const
156 {
157 const unsigned int s = (sweepDirection_ >> i) & 1;
158 return partition_->bound( s, i, dir[ i ] );
159 }
160
161
162 template< int codim, class Grid >
163 inline int SPPartitionIterator< codim, Grid >::end ( int i, Direction dir ) const
164 {
165 const unsigned int s = (sweepDirection_ >> i) & 1;
166 const int bnd = partition_->bound( 1-s, i, dir[ i ] );
167 return bnd + 2*(2*(1-s) - 1);
168 }
169
170
171 template< int codim, class Grid >
172 inline void SPPartitionIterator< codim, Grid >::init ()
173 {
174 MultiIndex &id = entityInfo().id();
175 if( partition_ )
176 {
177 DirectionIterator dirIt;
178 for( ; dirIt && partition_->empty( *dirIt ); ++dirIt )
179 continue;
180 if( dirIt )
181 {
182 for( int i = 0; i < dimension; ++i )
183 id[ i ] = begin( i, *dirIt );
184 entityInfo().update( partition_->number() );
185 }
186 else
187 {
188 ++partition_;
189 init();
190 }
191 }
192 else
193 std::fill( id.begin(), id.end(), std::numeric_limits< int >::max() );
194 }
195
196} // namespace Dune
197
198#endif // #ifndef DUNE_SPGRID_ITERATOR_HH
miscellaneous helper functions
Definition: iostream.hh:7
Definition: direction.hh:18
Definition: direction.hh:157
Definition: entity.hh:146
Definition: entityinfo.hh:24
const GridLevel & gridLevel() const
Definition: entityinfo.hh:66
bool equals(const This &other) const
Definition: entityinfo.hh:77
Definition: gridlevel.hh:35
static const unsigned int numDirections
Definition: gridlevel.hh:46
Definition: iterator.hh:19
bool operator!=(const This &other) const
Definition: iterator.hh:65
Traits::template Codim< codimension >::Entity Entity
Definition: iterator.hh:29
EntityInfo::Direction Direction
Definition: iterator.hh:40
EntityImpl::EntityInfo EntityInfo
Definition: iterator.hh:35
static const int codimension
Definition: iterator.hh:26
Entity operator*() const
Definition: iterator.hh:62
const GridLevel & gridLevel() const
Definition: iterator.hh:78
static const int mydimension
Definition: iterator.hh:27
bool operator==(const This &other) const
Definition: iterator.hh:64
EntityImpl::GridLevel GridLevel
Definition: iterator.hh:36
This & operator++()
Definition: iterator.hh:67
static const unsigned int numDirections
Definition: iterator.hh:42
static const int dimension
Definition: iterator.hh:25
const EntityInfo & entityInfo() const
Definition: iterator.hh:75
void increment()
Definition: iterator.hh:124
SPDirectionIterator< dimension, codimension > DirectionIterator
Definition: iterator.hh:50
SPPartitionIterator(const GridLevel &gridLevel, const PartitionList &partitionList, const End &e, const unsigned int sweepDir=0)
Definition: iterator.hh:112
Entity dereference() const
Definition: iterator.hh:69
SPPartitionIterator(const GridLevel &gridLevel, const PartitionList &partitionList, const Begin &b, const unsigned int sweepDir=0)
Definition: iterator.hh:99
SPPartitionList< dimension > PartitionList
Definition: iterator.hh:38
bool equals(const This &other) const
Definition: iterator.hh:71
EntityInfo::MultiIndex MultiIndex
Definition: iterator.hh:48
std::remove_const< Grid >::type::Traits Traits
Definition: iterator.hh:23
EntityInfo & entityInfo()
Definition: iterator.hh:76
Definition: iterator.hh:44
Definition: iterator.hh:45
Definition: partitionlist.hh:16