Version: SMASH-1.5
particles.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2018
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 #ifndef SRC_INCLUDE_PARTICLES_H_
8 #define SRC_INCLUDE_PARTICLES_H_
9 
10 #include <memory>
11 #include <type_traits>
12 #include <vector>
13 
14 #include "macros.h"
15 #include "particledata.h"
16 #include "particletype.h"
17 #include "pdgcode.h"
18 
19 namespace smash {
20 
33 class Particles {
34  public:
36  Particles();
37 
39  Particles(const Particles &) = delete;
41  Particles &operator=(const Particles &) = delete;
42 
44  ParticleList copy_to_vector() const {
45  if (dirty_.empty()) {
46  return {&data_[0], &data_[data_size_]};
47  }
48  return {begin(), end()};
49  }
50 
68  const ParticleData &insert(const ParticleData &p);
69 
76  void create(size_t n, PdgCode pdg);
77 
84  ParticleData &create(const PdgCode pdg);
85 
87  size_t size() const { return data_size_ - dirty_.size(); }
88 
90  bool is_empty() const { return data_size_ == 0; }
91 
100  double time() const {
101  assert(!is_empty());
102  return front().position().x0();
103  }
104 
109  void reset();
110 
120  bool is_valid(const ParticleData &copy) const {
121  if (data_size_ <= copy.index_) {
122  return false;
123  }
124  /* Check if the particles still exists. If it decayed
125  * or scattered inelastically it is gone. */
126  return data_[copy.index_].id() == copy.id()
127  /* If the particle has scattered
128  * elastically, its id_process has
129  * changed and we consider it invalid. */
130  && data_[copy.index_].id_process() == copy.id_process();
131  }
132 
141  void remove(const ParticleData &p);
142 
157  void replace(const ParticleList &to_remove, ParticleList &to_add);
158 
176  const ParticleData &new_state) {
177  assert(is_valid(p));
178  assert(p.type() == new_state.type());
179  ParticleData &original = data_[p.index_];
180  new_state.copy_to(original);
181  return original;
182  }
183 
200  void update(const ParticleList &old_state, ParticleList &new_state,
201  bool do_replace) {
202  if (do_replace) {
203  replace(old_state, new_state);
204  } else {
205  for (std::size_t i = 0; i < old_state.size(); ++i) {
206  new_state[i] = update_particle(old_state[i], new_state[i]);
207  }
208  }
209  }
210 
222  const ParticleData &lookup(const ParticleData &old_state) const {
223  assert(is_valid(old_state));
224  return data_[old_state.index_];
225  }
226 
232  template <typename T>
234  : public std::iterator<std::bidirectional_iterator_tag, T> {
235  friend class Particles;
236 
237  public:
239  using value_type = typename std::remove_const<T>::type;
241  using pointer = typename std::add_pointer<T>::type;
243  using reference = typename std::add_lvalue_reference<T>::type;
245  using const_pointer = typename std::add_const<pointer>::type;
247  using const_reference = typename std::add_const<reference>::type;
248 
249  private:
256  GenericIterator(pointer p) : ptr_(p) {} // NOLINT(runtime/explicit)
257 
260 
261  public:
272  do {
273  ++ptr_;
274  } while (ptr_->hole_);
275  return *this;
276  }
283  GenericIterator old = *this;
284  operator++();
285  return old;
286  }
287 
300  do {
301  --ptr_;
302  } while (ptr_->hole_);
303  return *this;
304  }
311  GenericIterator old = *this;
312  operator--();
313  return old;
314  }
315 
317  reference operator*() { return *ptr_; }
319  const_reference operator*() const { return *ptr_; }
320 
322  pointer operator->() { return ptr_; }
324  const_pointer operator->() const { return ptr_; }
325 
327  bool operator==(const GenericIterator &rhs) const {
328  return ptr_ == rhs.ptr_;
329  }
331  bool operator!=(const GenericIterator &rhs) const {
332  return ptr_ != rhs.ptr_;
333  }
335  bool operator<(const GenericIterator &rhs) const { return ptr_ < rhs.ptr_; }
337  bool operator>(const GenericIterator &rhs) const { return ptr_ > rhs.ptr_; }
342  bool operator<=(const GenericIterator &rhs) const {
343  return ptr_ <= rhs.ptr_;
344  }
349  bool operator>=(const GenericIterator &rhs) const {
350  return ptr_ >= rhs.ptr_;
351  }
352  };
357 
359  ParticleData &front() { return *begin(); }
365  const ParticleData &front() const { return *begin(); }
366 
368  ParticleData &back() { return *(--end()); }
374  const ParticleData &back() const { return *(--end()); }
375 
381  ParticleData *first = &data_[0];
382  while (first->hole_) {
383  ++first;
384  }
385  return first;
386  }
393  ParticleData *first = &data_[0];
394  while (first->hole_) {
395  ++first;
396  }
397  return first;
398  }
399 
404  iterator end() { return &data_[data_size_]; }
410  const_iterator end() const { return &data_[data_size_]; }
411 
413  const_iterator cbegin() const { return begin(); }
415  const_iterator cend() const { return end(); }
416 
423  friend std::ostream &operator<<(std::ostream &out,
424  const Particles &particles);
425 
426  private:
431  int id_max_ = -1;
432 
440  void increase_capacity(unsigned new_capacity);
449  inline void ensure_capacity(unsigned to_add);
465  inline void copy_in(ParticleData &to, const ParticleData &from);
466 
472  unsigned data_size_ = 0u;
477  unsigned data_capacity_ = 100u;
483  std::unique_ptr<ParticleData[]> data_;
484 
489  std::vector<unsigned> dirty_;
490 };
491 
492 } // namespace smash
493 
494 #endif // SRC_INCLUDE_PARTICLES_H_
GenericIterator operator++(int)
Postfix variant of the above prefix increment operator.
Definition: particles.h:282
bool is_empty() const
Definition: particles.h:90
void reset()
Reset the state of the Particles object to an empty list and a new id counter.
Definition: particles.cc:139
ParticleData & front()
Definition: particles.h:359
std::vector< unsigned > dirty_
Stores the indexes in data_ that do not hold valid particle data and should be reused when new partic...
Definition: particles.h:489
double time() const
Returns the time of the computational frame.
Definition: particles.h:100
GenericIterator & operator--()
Advance the iterator to the previous valid (not a hole) entry in Particles.
Definition: particles.h:299
typename std::remove_const< T >::type value_type
remove const qualification
Definition: particles.h:239
int id_max_
Highest id of a given particle.
Definition: particles.h:431
iterator begin()
Definition: particles.h:380
typename std::add_const< reference >::type const_reference
add const qualification to a reference
Definition: particles.h:247
void create(size_t n, PdgCode pdg)
Add n particles of the same type (pdg) to the list.
Definition: particles.cc:66
void ensure_capacity(unsigned to_add)
Definition: particles.cc:23
const_iterator end() const
const overload of end()
Definition: particles.h:410
bool operator!=(const GenericIterator &rhs) const
Definition: particles.h:331
const ParticleData & lookup(const ParticleData &old_state) const
Returns the particle that is currently stored in this object given an old copy of that particle...
Definition: particles.h:222
typename std::add_pointer< T >::type pointer
provide the pointer of a reference
Definition: particles.h:241
unsigned data_size_
Definition: particles.h:472
void copy_in(ParticleData &to, const ParticleData &from)
Definition: particles.cc:44
bool is_valid(const ParticleData &copy) const
Check whether the ParticleData copy is still a valid copy of the one stored in the Particles object...
Definition: particles.h:120
bool operator<(const GenericIterator &rhs) const
Definition: particles.h:335
bool operator==(const GenericIterator &rhs) const
Definition: particles.h:327
std::unique_ptr< ParticleData[]> data_
Points to a dynamically allocated array of ParticleData objects.
Definition: particles.h:483
double x0() const
Definition: fourvector.h:290
GenericIterator & operator++()
Advance the iterator to the next valid (not a hole) entry in Particles.
Definition: particles.h:271
const_iterator cbegin() const
Definition: particles.h:413
bool operator>=(const GenericIterator &rhs) const
Definition: particles.h:349
typename std::add_const< pointer >::type const_pointer
add const qualification to a pointer
Definition: particles.h:245
void copy_to(ParticleData &dst) const
Copies some information of the particle to the given particle dst.
Definition: particledata.h:378
Particles()
Creates a new (empty) Particles object.
Definition: particles.cc:17
void update(const ParticleList &old_state, ParticleList &new_state, bool do_replace)
Updates the Particles object, replacing the particles in old_state with the particles in new_state...
Definition: particles.h:200
const ParticleData & update_particle(const ParticleData &p, const ParticleData &new_state)
Updates the particle identified by p with the state stored in new_state.
Definition: particles.h:175
size_t size() const
Definition: particles.h:87
pointer ptr_
The entry in Particles this iterator points to.
Definition: particles.h:259
ParticleData & back()
Definition: particles.h:368
GenericIterator operator--(int)
Postfix variant of the above prefix decrement operator.
Definition: particles.h:310
bool hole_
If true, the object is an entry in Particles::data_ and does not hold valid particle data...
Definition: particledata.h:425
const FourVector & position() const
Get the particle&#39;s position in Minkowski space.
Definition: particledata.h:185
const ParticleData & insert(const ParticleData &p)
Inserts the particle into the list of particles.
Definition: particles.cc:50
uint32_t id_process() const
Get the id of the last action.
Definition: particledata.h:115
const_pointer operator->() const
Definition: particles.h:324
const_iterator cend() const
Definition: particles.h:415
PdgCode stores a Particle Data Group Particle Numbering Scheme particle type number.
Definition: pdgcode.h:108
ParticleList copy_to_vector() const
Definition: particles.h:44
bool operator>(const GenericIterator &rhs) const
Definition: particles.h:337
GenericIterator(pointer p)
Constructs an iterator pointing to the ParticleData pointed to by p.
Definition: particles.h:256
typename std::add_lvalue_reference< T >::type reference
creates a lvalue reference
Definition: particles.h:243
iterator end()
Definition: particles.h:404
const ParticleData & front() const
const overload of &front()
Definition: particles.h:365
void replace(const ParticleList &to_remove, ParticleList &to_add)
Replace the particles in to_remove with the particles in to_add in the list of current particles...
Definition: particles.cc:120
constexpr int p
Proton.
const ParticleType & type() const
Get the type of the particle.
Definition: particledata.h:109
unsigned index_
Internal index in the Particles list.
Definition: particledata.h:405
Particles & operator=(const Particles &)=delete
Cannot be copied.
friend std::ostream & operator<<(std::ostream &out, const Particles &particles)
Print effective mass and type name for all particles to the stream.
Definition: particles.cc:148
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
unsigned data_capacity_
Definition: particles.h:477
void increase_capacity(unsigned new_capacity)
Definition: particles.cc:30
constexpr int n
Neutron.
const_reference operator*() const
Definition: particles.h:319
bool operator<=(const GenericIterator &rhs) const
Definition: particles.h:342
int id() const
Get the id of the particle.
Definition: particledata.h:70
const ParticleData & back() const
const overload of &back()
Definition: particles.h:374
ParticleData contains the dynamic information of a certain particle.
Definition: particledata.h:52
Definition: action.h:24
const_iterator begin() const
const overload of begin()
Definition: particles.h:392