| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2017 - 2023 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_particles_particle_iterator_h |
| 16 |
#define dealii_particles_particle_iterator_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/particles/particle_accessor.h> |
| 21 |
|
| 22 |
DEAL_II_NAMESPACE_OPEN |
| 23 |
|
| 24 |
namespace Particles |
| 25 |
{ |
| 26 |
// Forward declaration |
| 27 |
#ifndef DOXYGEN |
| 28 |
template <int, int> |
| 29 |
class ParticleHandler; |
| 30 |
#endif |
| 31 |
|
| 32 |
/** |
| 33 |
* A class that is used to iterate over particles. Together with the |
| 34 |
* ParticleAccessor class this is used to hide the internal implementation |
| 35 |
* of the particle class and the particle container. |
| 36 |
*/ |
| 37 |
template <int dim, int spacedim = dim> |
| 38 |
class ParticleIterator |
| 39 |
{ |
| 40 |
public: |
| 41 |
/** |
| 42 |
* A type for the storage container for particles. |
| 43 |
*/ |
| 44 |
using particle_container = |
| 45 |
typename ParticleAccessor<dim, spacedim>::particle_container; |
| 46 |
|
| 47 |
/** |
| 48 |
* Empty constructor. Such an object is not usable! |
| 49 |
*/ |
| 50 |
ParticleIterator() = default; |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor of the iterator. Takes a reference to the particle |
| 54 |
* container, an iterator to the cell, and the particle index within that |
| 55 |
* cell. |
| 56 |
*/ |
| 57 |
ParticleIterator( |
| 58 |
const typename particle_container::iterator particles_in_cell, |
| 59 |
const PropertyPool<dim, spacedim> &property_pool, |
| 60 |
const unsigned int particle_index_within_cell); |
| 61 |
|
| 62 |
/** |
| 63 |
* Dereferencing operator, returns a reference to an accessor. Usage is thus |
| 64 |
* like <tt>(*i).get_id ();</tt> |
| 65 |
*/ |
| 66 |
const ParticleAccessor<dim, spacedim> & |
| 67 |
operator*() const; |
| 68 |
|
| 69 |
/** |
| 70 |
* Dereferencing operator, non-@p const version. |
| 71 |
*/ |
| 72 |
ParticleAccessor<dim, spacedim> & |
| 73 |
operator*(); |
| 74 |
|
| 75 |
/** |
| 76 |
* Dereferencing operator, returns a pointer of the particle pointed to. |
| 77 |
* Usage is thus like <tt>i->get_id ();</tt> |
| 78 |
* |
| 79 |
* There is a @p const and a non-@p const version. |
| 80 |
*/ |
| 81 |
const ParticleAccessor<dim, spacedim> * |
| 82 |
operator->() const; |
| 83 |
|
| 84 |
/** |
| 85 |
* Dereferencing operator, non-@p const version. |
| 86 |
*/ |
| 87 |
ParticleAccessor<dim, spacedim> * |
| 88 |
operator->(); |
| 89 |
|
| 90 |
/** |
| 91 |
* Compare for equality. |
| 92 |
*/ |
| 93 |
bool |
| 94 |
operator==(const ParticleIterator<dim, spacedim> &) const; |
| 95 |
|
| 96 |
/** |
| 97 |
* Compare for inequality. |
| 98 |
*/ |
| 99 |
bool |
| 100 |
operator!=(const ParticleIterator<dim, spacedim> &) const; |
| 101 |
|
| 102 |
/** |
| 103 |
* Prefix <tt>++</tt> operator: <tt>++iterator</tt>. This operator advances |
| 104 |
* the iterator to the next element and returns a reference to |
| 105 |
* <tt>*this</tt>. |
| 106 |
*/ |
| 107 |
ParticleIterator & |
| 108 |
operator++(); |
| 109 |
|
| 110 |
/** |
| 111 |
* Postfix <tt>++</tt> operator: <tt>iterator++</tt>. This operator advances |
| 112 |
* the iterator to the next element, but returns an iterator to the element |
| 113 |
* previously pointed to. |
| 114 |
*/ |
| 115 |
ParticleIterator |
| 116 |
operator++(int); |
| 117 |
|
| 118 |
/** |
| 119 |
* Prefix <tt>\--</tt> operator: <tt>\--iterator</tt>. This operator moves |
| 120 |
* the iterator to the previous element and returns a reference to |
| 121 |
* <tt>*this</tt>. |
| 122 |
*/ |
| 123 |
ParticleIterator & |
| 124 |
operator--(); |
| 125 |
|
| 126 |
/** |
| 127 |
* Postfix <tt>\--</tt> operator: <tt>iterator\--</tt>. This operator moves |
| 128 |
* the iterator to the previous element, but returns an iterator to the |
| 129 |
* element previously pointed to. |
| 130 |
*/ |
| 131 |
ParticleIterator |
| 132 |
operator--(int); |
| 133 |
|
| 134 |
/** |
| 135 |
* Return the state of the iterator. |
| 136 |
*/ |
| 137 |
IteratorState::IteratorStates |
| 138 |
state() const; |
| 139 |
|
| 140 |
/** |
| 141 |
* Mark the class as bidirectional iterator and declare some alias which |
| 142 |
* are standard for iterators and are used by algorithms to enquire about |
| 143 |
* the specifics of the iterators they work on. |
| 144 |
*/ |
| 145 |
using iterator_category = std::bidirectional_iterator_tag; |
| 146 |
using value_type = ParticleAccessor<dim, spacedim>; |
| 147 |
using difference_type = std::ptrdiff_t; |
| 148 |
using pointer = ParticleAccessor<dim, spacedim> *; |
| 149 |
using reference = ParticleAccessor<dim, spacedim> &; |
| 150 |
|
| 151 |
private: |
| 152 |
/** |
| 153 |
* The accessor to the actual particle. |
| 154 |
*/ |
| 155 |
ParticleAccessor<dim, spacedim> accessor; |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
// ------------------------------ inline functions ------------------------- |
| 161 |
|
| 162 |
template <int dim, int spacedim> |
| 163 |
inline ParticleIterator<dim, spacedim>::ParticleIterator( |
| 164 |
const typename particle_container::iterator particles_in_cell, |
| 165 |
const PropertyPool<dim, spacedim> &property_pool, |
| 166 |
const unsigned int particle_index_within_cell) |
| 167 |
: accessor(particles_in_cell, property_pool, particle_index_within_cell) |
| 168 |
{} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
template <int dim, int spacedim> |
| 173 |
inline ParticleAccessor<dim, spacedim> & |
| 174 |
ParticleIterator<dim, spacedim>::operator*() |
| 175 |
{ |
| 176 |
return accessor; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
template <int dim, int spacedim> |
| 182 |
inline ParticleAccessor<dim, spacedim> * |
| 183 |
ParticleIterator<dim, spacedim>::operator->() |
| 184 |
{ |
| 185 |
return &(this->operator*()); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
template <int dim, int spacedim> |
| 191 |
inline const ParticleAccessor<dim, spacedim> & |
| 192 |
ParticleIterator<dim, spacedim>::operator*() const |
| 193 |
{ |
| 194 |
return accessor; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
template <int dim, int spacedim> |
| 200 |
inline const ParticleAccessor<dim, spacedim> * |
| 201 |
ParticleIterator<dim, spacedim>::operator->() const |
| 202 |
{ |
| 203 |
return &(this->operator*()); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
template <int dim, int spacedim> |
| 209 |
inline bool |
| 210 |
ParticleIterator<dim, spacedim>::operator!=( |
| 211 |
const ParticleIterator<dim, spacedim> &other) const |
| 212 |
{ |
| 213 |
return accessor != other.accessor; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
template <int dim, int spacedim> |
| 219 |
inline bool |
| 220 |
ParticleIterator<dim, spacedim>::operator==( |
| 221 |
const ParticleIterator<dim, spacedim> &other) const |
| 222 |
{ |
| 223 |
return accessor == other.accessor; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
template <int dim, int spacedim> |
| 229 |
inline ParticleIterator<dim, spacedim> & |
| 230 |
ParticleIterator<dim, spacedim>::operator++() |
| 231 |
{ |
| 232 |
accessor.next(); |
| 233 |
return *this; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
template <int dim, int spacedim> |
| 239 |
inline ParticleIterator<dim, spacedim> |
| 240 |
ParticleIterator<dim, spacedim>::operator++(int) |
| 241 |
{ |
| 242 |
ParticleIterator tmp(*this); |
| 243 |
operator++(); |
| 244 |
|
| 245 |
return tmp; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
template <int dim, int spacedim> |
| 251 |
inline ParticleIterator<dim, spacedim> & |
| 252 |
ParticleIterator<dim, spacedim>::operator--() |
| 253 |
{ |
| 254 |
accessor.prev(); |
| 255 |
return *this; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
template <int dim, int spacedim> |
| 261 |
inline ParticleIterator<dim, spacedim> |
| 262 |
ParticleIterator<dim, spacedim>::operator--(int) |
| 263 |
{ |
| 264 |
ParticleIterator tmp(*this); |
| 265 |
operator--(); |
| 266 |
|
| 267 |
return tmp; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
template <int dim, int spacedim> |
| 273 |
inline IteratorState::IteratorStates |
| 274 |
ParticleIterator<dim, spacedim>::state() const |
| 275 |
{ |
| 276 |
return accessor.state(); |
| 277 |
} |
| 278 |
|
| 279 |
} // namespace Particles |
| 280 |
|
| 281 |
DEAL_II_NAMESPACE_CLOSE |
| 282 |
|
| 283 |
#endif |