DOLFIN
DOLFIN C++ interface
MeshEntityIteratorBase.h
1 // Copyright (C) 2012 Garth N. Wells
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 2009
19 //
20 // First added: 2006-05-09
21 // Last changed: 2014-07-02
22 
23 #ifndef __MESH_ENTITY_ITERATOR_BASE_H
24 #define __MESH_ENTITY_ITERATOR_BASE_H
25 
26 #include <cstddef>
27 #include "Mesh.h"
28 #include "MeshEntity.h"
29 
30 namespace dolfin
31 {
32 
34 
35  template<class T>
37  {
38  public:
39 
41  explicit MeshEntityIteratorBase(const Mesh& mesh)
42  : _entity(mesh, 0), _pos(0), pos_end(0), index(0)
43  {
44  // Check if mesh is empty
45  if (mesh.num_vertices() == 0)
46  return;
47 
48  // Get number of entities (excluding ghosts)
49  const std::size_t dim = _entity.dim();
50  mesh.init(dim);
51 
52  // End at ghost cells for normal iterator
53  pos_end = mesh.topology().ghost_offset(dim);
54  }
55 
58  MeshEntityIteratorBase(const Mesh& mesh, std::string opt)
59  : _entity(mesh, 0), _pos(0), pos_end(0), index(0)
60  {
61  // Check if mesh is empty
62  if (mesh.num_vertices() == 0)
63  return;
64 
65  const std::size_t dim = _entity.dim();
66  mesh.init(dim);
67 
68  pos_end = mesh.topology().size(dim);
69  if (opt == "regular")
70  pos_end = mesh.topology().ghost_offset(dim);
71  else if (opt == "ghost")
72  _pos = mesh.topology().ghost_offset(dim);
73  else if (opt != "all")
74  dolfin_error("MeshEntityIterator.h",
75  "initialize MeshEntityIterator",
76  "unknown opt=\"%s\", choose from "
77  "opt=[\"regular\", \"ghost\", \"all\"]", opt.c_str());
78  }
79 
81  explicit MeshEntityIteratorBase(const MeshEntity& entity)
82  : _entity(entity.mesh(), 0), _pos(0), index(0)
83  {
84  // Get connectivity
85  const MeshConnectivity& c = entity.mesh().topology()(entity.dim(), _entity.dim());
86 
87  // Compute connectivity if empty
88  if (c.empty())
89  entity.mesh().init(entity.dim(), _entity.dim());
90 
91  // Get size and index map
92  if (c.empty())
93  {
94  pos_end = 0;
95  index = 0;
96  }
97  else
98  {
99  pos_end = c.size(entity.index());
100  index = c(entity.index());
101  }
102  }
103 
106  : _entity(it._entity), _pos(it._pos), pos_end(it.pos_end), index(it.index) {}
107 
110 
113  {
114  ++_pos;
115  return *this;
116  }
117 
120  {
121  --_pos;
122  return *this;
123  }
124 
126  std::size_t pos() const
127  { return _pos; }
128 
130  bool operator==(const MeshEntityIteratorBase & it) const
131  {
132  // Use const_cast to use operator* inside comparison, which automatically
133  // updates the entity index corresponding to pos *before* comparison (since
134  // update of entity delays until request for entity)
135 
136  return ((const_cast<MeshEntityIteratorBase<T> *>(this))->operator*()
137  == (const_cast<MeshEntityIteratorBase<T> *>(&it))->operator*()
138  && _pos == it._pos && index == it.index);
139  }
140 
142  bool operator!=(const MeshEntityIteratorBase & it) const
143  { return !operator==(it); }
144 
147  { return *operator->(); }
148 
151  { _entity._local_index = (index ? index[_pos] : _pos); return &_entity; }
152 
154  bool end() const
155  { return _pos >= pos_end; }
156 
162  {
163  MeshEntityIteratorBase sg(*this);
164  sg.set_end();
165  return sg;
166  }
167 
168  // Note: Not a subclass of Variable for efficiency!
169  // Commented out to avoid warning about shadowing str() for MeshEntity
170  // Return informal string representation (pretty-print)
171  // std::string str(bool verbose) const;
172 
173  private:
174 
176  void set_end()
177  { _pos = pos_end; }
178 
179  // Mesh entity
180  T _entity;
181 
182  // Current position
183  std::size_t _pos;
184 
185  // End position
186  std::size_t pos_end;
187 
188  // Mapping from pos to index (if any)
189  const unsigned int* index;
190 
191  };
192 
193 }
194 
195 #endif
T & operator*()
Dereference operator.
Definition: MeshEntityIteratorBase.h:146
MeshEntityIteratorBase(const Mesh &mesh)
Create iterator for mesh entities over given topological dimension.
Definition: MeshEntityIteratorBase.h:41
const Mesh & mesh() const
Definition: MeshEntity.h:99
std::size_t index() const
Definition: MeshEntity.h:113
std::size_t size() const
Return total number of connections.
Definition: MeshConnectivity.h:60
std::size_t size(std::size_t dim) const
Return number of entities for given dimension.
Definition: MeshTopology.cpp:75
Definition: adapt.h:29
std::size_t pos() const
Return current position.
Definition: MeshEntityIteratorBase.h:126
std::size_t init(std::size_t dim) const
Definition: Mesh.cpp:138
MeshEntityIteratorBase end_iterator()
Definition: MeshEntityIteratorBase.h:161
MeshEntityIteratorBase(const MeshEntityIteratorBase &it)
Copy constructor.
Definition: MeshEntityIteratorBase.h:105
std::size_t dim() const
Definition: MeshEntity.h:106
Definition: MeshConnectivity.h:39
MeshEntityIteratorBase & operator--()
Step to the previous mesh entity (prefix decrease)
Definition: MeshEntityIteratorBase.h:119
MeshEntityIteratorBase & operator++()
Step to next mesh entity (prefix increment)
Definition: MeshEntityIteratorBase.h:112
~MeshEntityIteratorBase()
Destructor.
Definition: MeshEntityIteratorBase.h:109
MeshEntityIteratorBase(const Mesh &mesh, std::string opt)
Definition: MeshEntityIteratorBase.h:58
bool end() const
Check if iterator has reached the end.
Definition: MeshEntityIteratorBase.h:154
std::size_t ghost_offset(std::size_t dim) const
Definition: MeshTopology.cpp:93
bool empty() const
Return true if the total number of connections is equal to zero.
Definition: MeshConnectivity.h:56
MeshTopology & topology()
Definition: Mesh.h:219
std::size_t num_vertices() const
Definition: Mesh.h:134
bool operator==(const MeshEntityIteratorBase &it) const
Comparison operator.
Definition: MeshEntityIteratorBase.h:130
Definition: MeshEntity.h:42
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
MeshEntityIteratorBase(const MeshEntity &entity)
Create iterator for entities of given dimension connected to given entity.
Definition: MeshEntityIteratorBase.h:81
bool operator!=(const MeshEntityIteratorBase &it) const
Comparison operator.
Definition: MeshEntityIteratorBase.h:142
T * operator->()
Member access operator.
Definition: MeshEntityIteratorBase.h:150
Definition: Mesh.h:82
Base class for MeshEntityIterators.
Definition: MeshEntityIteratorBase.h:36