DOLFIN
DOLFIN C++ interface
SubsetIterator.h
1 // Copyright (C) 2010 Anders Logg
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Andre Massing 2011.
19 //
20 // First added: 2010-11-17
21 // Last changed: 2011-11-10
22 
23 #ifndef __SUBSET_ITERATOR_H
24 #define __SUBSET_ITERATOR_H
25 
26 #include <memory>
27 #include <vector>
28 
29 #include "Mesh.h"
30 #include "MeshEntity.h"
31 #include "MeshEntityIterator.h"
32 #include "MeshFunction.h"
33 
34 namespace dolfin
35 {
36 
40 
42  {
43  public:
44 
47  SubsetIterator(const MeshFunction<std::size_t>& labels, std::size_t label)
48  : _entity(*labels.mesh(), labels.dim(), 0),
49  _subset(new std::vector<std::size_t>()), subset(*_subset)
50  {
51  // Extract subset
52  subset.clear();
53  for (MeshEntityIterator entity(*labels.mesh(), labels.dim());
54  !entity.end(); ++entity)
55  {
56  if (labels[*entity] == label)
57  subset.push_back(entity->index());
58  }
59  info("Iterating over subset, found %d entities out of %d.",
60  subset.size(), labels.size());
61 
62  // Set iterator
63  it = subset.begin();
64  }
65 
67  SubsetIterator(const SubsetIterator& subset_iter)
68  : _entity(subset_iter._entity), _subset(subset_iter._subset),
69  subset(*_subset), it(subset_iter.it) {}
70 
72  virtual ~SubsetIterator() {}
73 
76  {
77  ++it;
78  return *this;
79  }
80 
83  {
84  --it;
85  return *this;
86  }
87 
89  bool operator==(const SubsetIterator& sub_iter) const
90  {
91  return ((const_cast<SubsetIterator *>(this))->operator*()
92  == (const_cast<SubsetIterator *>(&sub_iter))->operator*()
93  && it == sub_iter.it && &subset == &sub_iter.subset);
94  }
95 
97  bool operator!=(const SubsetIterator & sub_iter) const
98  { return !operator==(sub_iter); }
99 
102  { return *operator->(); }
103 
106  { _entity._local_index = *it; return &_entity; }
107 
109  bool end() const
110  { return it == subset.end(); }
111 
114  {
115  SubsetIterator sg(*this);
116  sg.set_end();
117  return sg;
118  }
119 
120  private:
121 
122  // Set pos to end position. To create a kind of mesh.end()
123  // iterator.
124  void set_end()
125  { it = subset.end(); }
126 
127  // Mesh entity
128  MeshEntity _entity;
129 
130  // Subset in shared data form
131  std::shared_ptr<std::vector<std::size_t>> _subset;
132 
133  //Subset reference for convenience / speed
134  std::vector<std::size_t>& subset;
135 
136  // Iterator
137  std::vector<std::size_t>::iterator it;
138 
139  };
140 
141 }
142 
143 #endif
std::size_t dim() const
Definition: MeshFunction.h:498
SubsetIterator(const SubsetIterator &subset_iter)
Copy Constructor.
Definition: SubsetIterator.h:67
std::size_t size() const
Definition: MeshFunction.h:510
Definition: SubsetIterator.h:41
bool end() const
Check if iterator has reached the end.
Definition: SubsetIterator.h:109
SubsetIterator & operator--()
Step back to previous mesh entity (prefix decrement)
Definition: SubsetIterator.h:82
SubsetIterator end_iterator()
Beyond end iterator.
Definition: SubsetIterator.h:113
Definition: adapt.h:29
virtual ~SubsetIterator()
Destructor.
Definition: SubsetIterator.h:72
Definition: MeshEntityIterator.h:64
SubsetIterator(const MeshFunction< std::size_t > &labels, std::size_t label)
Definition: SubsetIterator.h:47
bool operator==(const SubsetIterator &sub_iter) const
Comparison operator.
Definition: SubsetIterator.h:89
bool end() const
Check if iterator has reached the end.
Definition: MeshEntityIterator.h:188
SubsetIterator & operator++()
Step to next mesh entity (prefix increment)
Definition: SubsetIterator.h:75
bool operator!=(const SubsetIterator &sub_iter) const
Comparison operator.
Definition: SubsetIterator.h:97
void info(std::string msg,...)
Print message.
Definition: log.cpp:72
Definition: MeshEntity.h:42
std::shared_ptr< const Mesh > mesh() const
Definition: MeshFunction.h:491
MeshEntity * operator->()
Member access operator.
Definition: SubsetIterator.h:105
MeshEntity & operator*()
Dereference operator.
Definition: SubsetIterator.h:101