Loading...
Searching...
No Matches
SubsetIterator.h
// Copyright (C) 2010 Anders Logg
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// Modified by Andre Massing 2011.
//
// First added: 2010-11-17
// Last changed: 2011-11-10
#ifndef __SUBSET_ITERATOR_H
#define __SUBSET_ITERATOR_H
#include <memory>
#include <vector>
#include "Mesh.h"
#include "MeshEntity.h"
#include "MeshEntityIterator.h"
#include "MeshFunction.h"
namespace dolfin
{
class SubsetIterator
{
public:
SubsetIterator(const MeshFunction<std::size_t>& labels, std::size_t label)
: _entity(*labels.mesh(), labels.dim(), 0),
_subset(new std::vector<std::size_t>()), subset(*_subset)
{
// Extract subset
subset.clear();
for (MeshEntityIterator entity(*labels.mesh(), labels.dim());
!entity.end(); ++entity)
{
if (labels[*entity] == label)
subset.push_back(entity->index());
}
info("Iterating over subset, found %d entities out of %d.",
subset.size(), labels.size());
// Set iterator
it = subset.begin();
}
SubsetIterator(const SubsetIterator& subset_iter)
: _entity(subset_iter._entity), _subset(subset_iter._subset),
subset(*_subset), it(subset_iter.it) {}
virtual ~SubsetIterator() {}
SubsetIterator& operator++()
{
++it;
return *this;
}
SubsetIterator& operator--()
{
--it;
return *this;
}
bool operator==(const SubsetIterator& sub_iter) const
{
return ((const_cast<SubsetIterator *>(this))->operator*()
== (const_cast<SubsetIterator *>(&sub_iter))->operator*()
&& it == sub_iter.it && &subset == &sub_iter.subset);
}
bool operator!=(const SubsetIterator & sub_iter) const
{ return !operator==(sub_iter); }
MeshEntity& operator*()
{ return *operator->(); }
MeshEntity* operator->()
{ _entity._local_index = *it; return &_entity; }
bool end() const
{ return it == subset.end(); }
SubsetIterator end_iterator()
{
SubsetIterator sg(*this);
sg.set_end();
return sg;
}
private:
// Set pos to end position. To create a kind of mesh.end()
// iterator.
void set_end()
{ it = subset.end(); }
// Mesh entity
MeshEntity _entity;
// Subset in shared data form
std::shared_ptr<std::vector<std::size_t>> _subset;
//Subset reference for convenience / speed
std::vector<std::size_t>& subset;
// Iterator
std::vector<std::size_t>::iterator it;
};
}
#endif
dolfin::MeshEntityIterator
Definition MeshEntityIterator.h:65
dolfin::MeshEntityIterator::end
bool end() const
Check if iterator has reached the end.
Definition MeshEntityIterator.h:188
dolfin::MeshEntity
Definition MeshEntity.h:43
dolfin::MeshFunction
Definition MeshFunction.h:58
dolfin::MeshFunction::mesh
std::shared_ptr< const Mesh > mesh() const
Definition MeshFunction.h:491
dolfin::MeshFunction::dim
std::size_t dim() const
Definition MeshFunction.h:498
dolfin::MeshFunction::size
std::size_t size() const
Definition MeshFunction.h:510
dolfin::SubsetIterator::operator==
bool operator==(const SubsetIterator &sub_iter) const
Comparison operator.
Definition SubsetIterator.h:89
dolfin::SubsetIterator::SubsetIterator
SubsetIterator(const SubsetIterator &subset_iter)
Copy Constructor.
Definition SubsetIterator.h:67
dolfin::SubsetIterator::operator++
SubsetIterator & operator++()
Step to next mesh entity (prefix increment).
Definition SubsetIterator.h:75
dolfin::SubsetIterator::operator->
MeshEntity * operator->()
Member access operator.
Definition SubsetIterator.h:105
dolfin::SubsetIterator::~SubsetIterator
virtual ~SubsetIterator()
Destructor.
Definition SubsetIterator.h:72
dolfin::SubsetIterator::operator--
SubsetIterator & operator--()
Step back to previous mesh entity (prefix decrement).
Definition SubsetIterator.h:82
dolfin::SubsetIterator::SubsetIterator
SubsetIterator(const MeshFunction< std::size_t > &labels, std::size_t label)
Definition SubsetIterator.h:47
dolfin::SubsetIterator::operator*
MeshEntity & operator*()
Dereference operator.
Definition SubsetIterator.h:101
dolfin::SubsetIterator::end
bool end() const
Check if iterator has reached the end.
Definition SubsetIterator.h:109
dolfin::SubsetIterator::operator!=
bool operator!=(const SubsetIterator &sub_iter) const
Comparison operator.
Definition SubsetIterator.h:97
dolfin::SubsetIterator::end_iterator
SubsetIterator end_iterator()
Beyond end iterator.
Definition SubsetIterator.h:113
dolfin
Definition adapt.h:30
dolfin::info
void info(std::string msg,...)
Print message.
Definition log.cpp:72