dune-foamgrid  2.8-git
foamgridleafiterator.hh
Go to the documentation of this file.
1 #ifndef DUNE_FOAMGRID_LEAFITERATOR_HH
2 #define DUNE_FOAMGRID_LEAFITERATOR_HH
3 
8 namespace Dune {
9 
13 template<int codim, PartitionIteratorType pitype, class GridImp>
15 {
16  enum {dimgrid = GridImp::dimension};
17  enum {dimworld = GridImp::dimensionworld};
18 
19 public:
20 
21  using Entity = typename GridImp::template Codim<codim>::Entity;
22  enum { codimension = codim };
23 
24  FoamGridLeafIterator(const GridImp& grid)
25  : grid_(&grid)
26  {
27 
29  const int fullRefineLevel = 0;
30 
31  const auto& entities = std::get<dimgrid-codim>(grid_->entityImps_[fullRefineLevel]);
32  levelIterator_ = entities.begin();
33 
34  if (levelIterator_ != entities.end())
35  {
36 
37  // The &* turns an iterator into a plain pointer
38  virtualEntity_.impl().setToTarget(&*entities.begin());
39 
40  if (!virtualEntity_.impl().target_->isLeaf())
41  increment();
42  }
43 
44  // grid doesn't contain entities of this codimension
45  else
46  virtualEntity_.impl().setToTarget(nullptr);
47  }
48 
51  : grid_(nullptr)
52  {
53  virtualEntity_.impl().setToTarget(nullptr);
54  }
55 
57  void increment() {
58  // Increment until you find a leaf entity
59  do {
60  globalIncrement();
61 
62  } while (levelIterator_!=std::get<dimgrid-codim>(grid_->entityImps_[grid_->maxLevel()]).end()
63  && !virtualEntity_.impl().target_->isLeaf());
64  }
65 
67  const Entity& dereference() const { return virtualEntity_; }
68 
71  return virtualEntity_ == other.virtualEntity_;
72  }
73 
74 private:
75 
77  void globalIncrement() {
78 
79  // Backup current level because it may not be accessible anymore after
80  // setting the pointer to the next entity.
81  const int oldLevel = virtualEntity_.level();
82 
83  // Increment on this level
84  ++levelIterator_;
85  virtualEntity_.impl().setToTarget(&(*levelIterator_));
86  if (levelIterator_==std::get<dimgrid-codim>(grid_->entityImps_[oldLevel]).end())
87  virtualEntity_.impl().setToTarget(nullptr);
88 
89  // If beyond the end of this level set to first of next level
90  if (levelIterator_==std::get<dimgrid-codim>(grid_->entityImps_[oldLevel]).end() && oldLevel < grid_->maxLevel()) {
91 
92  const std::list<FoamGridEntityImp<dimgrid-codim, dimgrid, dimworld, typename GridImp::ctype> >& entities = std::get<dimgrid-codim>(grid_->entityImps_[oldLevel+1]);
93  levelIterator_ = entities.begin();
94  virtualEntity_.impl().setToTarget(&*entities.begin());
95 
96  }
97 
98  }
99 
100  // /////////////////////////////////////
101  // Data members
102  // /////////////////////////////////////
103  const GridImp* grid_;
104 
106  Entity virtualEntity_;
107 
108  // This iterator derives from FoamGridEntityPointer, and that base class stores the value
109  // of the iterator, i.e. the 'pointer' to the entity. However, that pointer can not be
110  // set to its successor in the level std::list, not even by magic. Therefore we keep the
111  // same information redundantly in this iterator, which can be incremented.
112  typename std::list<FoamGridEntityImp<dimgrid-codim, dimgrid, dimworld, typename GridImp::ctype> >::const_iterator levelIterator_;
113 };
114 
115 
116 } // namespace Dune
117 
118 #endif
Definition: dgffoam.cc:6
Iterator over all entities of a given codimension and level of a grid.
Definition: foamgridleafiterator.hh:15
const Entity & dereference() const
dereferencing
Definition: foamgridleafiterator.hh:67
typename GridImp::template Codim< codim >::Entity Entity
Definition: foamgridleafiterator.hh:21
FoamGridLeafIterator()
Default constructor.
Definition: foamgridleafiterator.hh:50
bool equals(const FoamGridLeafIterator< codim, pitype, GridImp > &other) const
equality
Definition: foamgridleafiterator.hh:70
FoamGridLeafIterator(const GridImp &grid)
Definition: foamgridleafiterator.hh:24
void increment()
prefix increment
Definition: foamgridleafiterator.hh:57
@ codimension
Definition: foamgridleafiterator.hh:22
The actual entity implementation.
Definition: foamgridvertex.hh:47