Loading...
Searching...
No Matches
Iterators.hpp
//===========================================================================
//
// File: Iterators.hpp
//
// Created: Fri May 29 23:29:09 2009
//
// Author(s): Atgeirr F Rasmussen <atgeirr@sintef.no>
// Bård Skaflestad <bard.skaflestad@sintef.no>
//
// $Date$
//
// $Revision$
//
//===========================================================================
/*
Copyright 2009, 2010 SINTEF ICT, Applied Mathematics.
Copyright 2009, 2010, 2022 Equinor ASA.
This file is part of The Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef OPM_ITERATORS_HEADER
#define OPM_ITERATORS_HEADER
#include <dune/grid/common/gridenums.hh>
#include "PartitionIteratorRule.hpp"
#include <opm/common/ErrorMacros.hpp>
#include "CpGridData.hpp"
#include <stack>
namespace Dune
{
namespace cpgrid
{
class CpGridData;
template<int cd, PartitionIteratorType pitype>
class Iterator : public Entity<cd>
{
public:
using Reference = const Entity<cd>&;
Iterator(const CpGridData& grid, int index, bool orientation);
Iterator() = default;
Iterator& operator++()
{
EntityRep<cd>::increment();
if(rule_.fullSet || rule_.emptySet)
return *this;
while(this->index()<noEntities_ && rule_.isInvalid(*this))
EntityRep<cd>::increment();
return *this;
}
Iterator operator++(int)
{
Iterator tmp(*this);
++(*this);
return tmp;
}
const Entity<cd>* operator->() const
{
assert(Entity<cd>::isValid());
return (this);
}
const Entity<cd>& operator*() const
{
assert(Entity<cd>::isValid());
return (*this);
}
private:
int noEntities_;
PartitionIteratorRule<pitype> rule_;
};
class HierarchicIterator
{
public:
using Reference = const Entity<0>&;
explicit HierarchicIterator(const CpGridData& grid)
: virtualEntity_(grid, EntityRep<0>::InvalidIndex, true )
{
}
explicit HierarchicIterator() = default;
// Constructor with Entity<0> target and maxLevel (begin iterator).
HierarchicIterator(Entity<0> target, int maxLevel)
: virtualEntity_(target), maxLevel_(maxLevel)
{
// Load sons of target onto the iterator stack
stackChildren_(target);
// Set entity target to the next child if exists
resetEntity_();
}
// Constructor without valid element (end iterator).
explicit HierarchicIterator(int maxLevel)
: maxLevel_(maxLevel)
{
resetEntity_();
}
bool operator==(const HierarchicIterator& other) const
{
return virtualEntity_ == other.virtualEntity_;
}
bool operator!=(const HierarchicIterator& other) const
{
return !this->operator==(other);
}
HierarchicIterator& operator++()
{
if (elemStack_.empty()){
return *this;
}
// Reference to the top element of elemStack_
auto target = elemStack_.top();
// Remove the element on top of elemStack_
elemStack_.pop();
// Load sons of previous target onto elemStack_
stackChildren_(target);
// Set entity target to the next stacked element if exists
resetEntity_();
return *this;
}
HierarchicIterator operator++(int)
{
if (elemStack_.empty()){
return *this;
}
auto copy = *this;
// Reference to the top element of elemStack_
auto target = elemStack_.top();
// Remove the element on top of elemStack_
elemStack_.pop();
// Load sons of previous target onto elemStack_
stackChildren_(target);
// Set entity target to the next stacked element if exists
resetEntity_();
return copy;
}
const Entity<0>* operator->() const
{
assert(this -> virtualEntity_.isValid());
return &virtualEntity_;
}
const Entity<0>& operator*() const
{
assert(this-> virtualEntity_.isValid());
return virtualEntity_;
}
private:
void stackChildren_(const Entity<0>& target);
void resetEntity_();
Entity<0> virtualEntity_;
int maxLevel_;
// For depth-first search
std::stack<Entity<0>> elemStack_;
}; // end class HierarchicIterator
} // namespace cpgrid
} // namespace Dune
namespace std
{
template< int codim, Dune::PartitionIteratorType pitype >
struct iterator_traits< Dune::cpgrid::Iterator< codim, pitype > >
{
typedef Dune::cpgrid::Iterator< codim, pitype > Iterator;
typedef ptrdiff_t difference_type;
typedef typename Iterator::Entity value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef forward_iterator_tag iterator_category;
};
template <>
struct iterator_traits< Dune::cpgrid::HierarchicIterator >
{
typedef ptrdiff_t difference_type;
typedef Dune::cpgrid::Entity<0> value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef forward_iterator_tag iterator_category;
};
} // namespace std
#include <opm/grid/cpgrid/CpGridData.hpp>
#include "Entity.hpp"
namespace Dune {
namespace cpgrid {
template<int cd, PartitionIteratorType pitype>
Iterator<cd, pitype>::Iterator(const CpGridData& grid, int index, bool orientation)
: Entity<cd>(grid,
// If the partition is empty, goto to end iterator!
EntityRep<cd>(PartitionIteratorRule<pitype>::emptySet?grid.size(cd):index,
orientation)),
noEntities_(grid.size(cd))
{
if(rule_.fullSet || rule_.emptySet)
return;
while(this->index()<noEntities_ && rule_.isInvalid(*this))
EntityRep<cd>::increment();
}
}}
#endif // OPM_ITERATORS_HEADER
Dune::cpgrid::CpGridData
Struct that hods all the data needed to represent a Cpgrid.
Definition CpGridData.hpp:118
Dune::cpgrid::EntityRep
Represents an entity of a given codim, with positive or negative orientation.
Definition EntityRep.hpp:99
Dune::cpgrid::EntityRep::orientation
bool orientation() const
Returns true if the entity has positive orientation.
Definition EntityRep.hpp:140
Dune::cpgrid::EntityRep::index
int index() const
The (positive) index of an entity.
Definition EntityRep.hpp:126
Dune::cpgrid::EntityRep::increment
void increment()
Increments the entityrep's index() by one.
Definition EntityRep.hpp:153
Dune::cpgrid::Entity
Definition Entity.hpp:72
Dune::cpgrid::Entity::isValid
bool isValid() const
isValid method for EntitySeed
Definition Entity.hpp:434
Dune::cpgrid::Entity< cd >::Entity
Entity()
Definition Entity.hpp:121
Dune::cpgrid::HierarchicIterator::operator==
bool operator==(const HierarchicIterator &other) const
Equality.
Definition Iterators.hpp:152
Dune::cpgrid::HierarchicIterator::operator*
const Entity< 0 > & operator*() const
Const dereferencing operator.
Definition Iterators.hpp:210
Dune::cpgrid::HierarchicIterator::operator++
HierarchicIterator operator++(int)
Definition Iterators.hpp:185
Dune::cpgrid::HierarchicIterator::HierarchicIterator
HierarchicIterator(const CpGridData &grid)
Definition Iterators.hpp:125
Dune::cpgrid::HierarchicIterator::operator++
HierarchicIterator & operator++()
Definition Iterators.hpp:166
Dune::cpgrid::HierarchicIterator::operator!=
bool operator!=(const HierarchicIterator &other) const
Inequality.
Definition Iterators.hpp:158
Dune::cpgrid::HierarchicIterator::operator->
const Entity< 0 > * operator->() const
Const member by pointer operator.
Definition Iterators.hpp:203
Dune::cpgrid::Iterator
Iterator intended to be used as LeafIterator and LevelIterator (no difference due to no adaptivity) f...
Definition Iterators.hpp:60
Dune::cpgrid::Iterator::operator->
const Entity< cd > * operator->() const
Const member by pointer operator.
Definition Iterators.hpp:94
Dune::cpgrid::Iterator::operator++
Iterator & operator++()
Increment operator.
Definition Iterators.hpp:76
Dune::cpgrid::Iterator::Iterator
Iterator(const CpGridData &grid, int index, bool orientation)
Definition Iterators.hpp:267
Dune::cpgrid::Iterator::operator*
const Entity< cd > & operator*() const
Const dereferencing operator.
Definition Iterators.hpp:101
Dune::cpgrid
Copyright 2019 Equinor AS.
Definition GridPartitioning.cpp:673
Dune
The namespace Dune is the main namespace for all Dune code.
Definition CartesianIndexMapper.hpp:10
Dune::cpgrid::PartitionIteratorRule
A rule at what entities to stop.
Definition PartitionIteratorRule.hpp:42