vSMC  v3.0.0
Scalable Monte Carlo
single_particle.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/core/single_particle.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2016, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_CORE_SINGLE_PARTICLE_HPP
33 #define VSMC_CORE_SINGLE_PARTICLE_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 
37 #define VSMC_RUNTIME_ASSERT_SINGLE_PARTICLE_COMPARE(sp1, sp2) \
38  VSMC_RUNTIME_ASSERT((sp1.particle_ptr() == sp2.particle_ptr()), \
39  "COMPARE TWO SingleParticle OBJECTS THAT BELONG TO TWO PARTICLE " \
40  "SYSTEMS");
41 
42 #define VSMC_RUNTIME_ASSERT_SINGLE_PARTICLE_DIFFERENCE(sp1, sp2) \
43  VSMC_RUNTIME_ASSERT((sp1.particle_ptr() == sp2.particle_ptr()), \
44  "SUBSTRACT TWO SingleParticle OBJECTS THAT BELONG TO TWO PARTICLE " \
45  "SYSTEMS");
46 
47 namespace vsmc
48 {
49 
52 template <typename T>
54 {
55  public:
57  : pptr_(pptr), id_(id)
58  {
59  }
60 
61  Particle<T> &particle() const { return *pptr_; }
62 
63  Particle<T> *particle_ptr() const { return pptr_; }
64 
65  typename Particle<T>::size_type id() const { return id_; }
66 
67  typename Particle<T>::rng_type &rng() const { return pptr_->rng(id_); }
68 
69  private:
70  Particle<T> *pptr_;
71  typename Particle<T>::size_type id_;
72 }; // class SingleParticleBase
73 
78 
79 template <typename T>
103 {
104  public:
106  : SingleParticleBaseType<T>(id, pptr)
107  {
108  }
109 
110  template <typename IntType>
112  {
113  return SingleParticle<T>(static_cast<typename Particle<T>::size_type>(
114  static_cast<std::ptrdiff_t>(this->id()) +
115  static_cast<std::ptrdiff_t>(n)),
116  this->particle_ptr());
117  }
118 
119  SingleParticle<T> &operator*() { return *this; }
120 
121  const SingleParticle<T> &operator*() const { return *this; }
122 }; // class SingleParticle
123 
124 template <typename T>
125 inline bool operator==(
126  const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
127 {
129 
130  return sp1.id() == sp2.id();
131 }
132 
133 template <typename T>
134 inline bool operator!=(
135  const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
136 {
138 
139  return sp1.id() != sp2.id();
140 }
141 
142 template <typename T>
143 inline bool operator<(
144  const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
145 {
147 
148  return sp1.id() < sp2.id();
149 }
150 
151 template <typename T>
152 inline bool operator>(
153  const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
154 {
156 
157  return sp1.id() > sp2.id();
158 }
159 
160 template <typename T>
161 inline bool operator<=(
162  const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
163 {
165 
166  return sp1.id() <= sp2.id();
167 }
168 
169 template <typename T>
170 inline bool operator>=(
171  const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
172 {
174 
175  return sp1.id() >= sp2.id();
176 }
177 
178 template <typename T>
180 {
181  sp = SingleParticle<T>(sp.id() + 1, sp.particle_ptr());
182 
183  return sp;
184 }
185 
186 template <typename T>
188 {
189  SingleParticle<T> sp_tmp(sp);
190  sp = SingleParticle<T>(sp.id() + 1, sp.particle_ptr());
191 
192  return sp_tmp;
193 }
194 
195 template <typename T>
197 {
198  sp = SingleParticle<T>(sp.id() - 1, sp.particle_ptr());
199 
200  return sp;
201 }
202 
203 template <typename T>
205 {
206  SingleParticle<T> sp_tmp(sp);
207  sp = SingleParticle<T>(sp.id() - 1, sp.particle_ptr());
208 
209  return sp_tmp;
210 }
211 
212 template <typename T, typename IntType>
213 inline SingleParticle<T> operator+(const SingleParticle<T> &sp, IntType n)
214 {
215  return SingleParticle<T>(static_cast<typename Particle<T>::size_type>(
216  static_cast<std::ptrdiff_t>(sp.id()) +
217  static_cast<std::ptrdiff_t>(n)),
218  sp.particle_ptr());
219 }
220 
221 template <typename T, typename IntType>
222 inline SingleParticle<T> operator+(IntType n, const SingleParticle<T> &sp)
223 {
224  return SingleParticle<T>(static_cast<typename Particle<T>::size_type>(
225  static_cast<std::ptrdiff_t>(sp.id()) +
226  static_cast<std::ptrdiff_t>(n)),
227  sp.particle_ptr());
228 }
229 
230 template <typename T, typename IntType>
231 inline SingleParticle<T> operator-(const SingleParticle<T> &sp, IntType n)
232 {
233  return SingleParticle<T>(static_cast<typename Particle<T>::size_type>(
234  static_cast<std::ptrdiff_t>(sp.id()) -
235  static_cast<std::ptrdiff_t>(n)),
236  sp.particle_ptr());
237 }
238 
239 template <typename T, typename IntType>
241 {
242  sp = sp + n;
243 
244  return sp;
245 }
246 
247 template <typename T, typename IntType>
249 {
250  sp = sp - n;
251 
252  return sp;
253 }
254 
255 template <typename T>
256 inline std::ptrdiff_t operator-(
257  const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
258 {
260 
261  return static_cast<std::ptrdiff_t>(sp1.id()) -
262  static_cast<std::ptrdiff_t>(sp2.id());
263 }
264 
265 } // namespace vsmc
266 
267 #endif // VSMC_CORE_SINGLE_PARTICLE_HPP
Definition: monitor.hpp:48
SizeType< T > size_type
Definition: particle.hpp:86
Particle class representing the whole particle set.
Definition: particle.hpp:83
typename rng_set_type::rng_type rng_type
Definition: particle.hpp:90
SingleParticle< T > operator-(const SingleParticle< T > &sp, IntType n)
SingleParticle< T > & operator*()
SingleParticle(typename Particle< T >::size_type id, Particle< T > *pptr)
#define VSMC_DEFINE_TYPE_TEMPLATE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:103
Particle< T >::size_type id() const
#define VSMC_RUNTIME_ASSERT_SINGLE_PARTICLE_COMPARE(sp1, sp2)
bool operator!=(const SingleParticle< T > &sp1, const SingleParticle< T > &sp2)
#define VSMC_RUNTIME_ASSERT_SINGLE_PARTICLE_DIFFERENCE(sp1, sp2)
SingleParticle< T > & operator++(SingleParticle< T > &sp)
SingleParticleBase(typename Particle< T >::size_type id, Particle< T > *pptr)
SingleParticle< T > & operator-=(SingleParticle< T > &sp, IntType n)
Particle< T > & particle() const
SingleParticle operator[](IntType n)
bool operator==(const SingleParticle< T > &sp1, const SingleParticle< T > &sp2)
bool operator<(const SingleParticle< T > &sp1, const SingleParticle< T > &sp2)
A thin wrapper over a complete Particle.
Particle< T > * particle_ptr() const
SingleParticle< T > & operator+=(SingleParticle< T > &sp, IntType n)
const SingleParticle< T > & operator*() const
A thin wrapper over a complete Particle.
Particle< T >::rng_type & rng() const
bool operator<=(const SingleParticle< T > &sp1, const SingleParticle< T > &sp2)
SingleParticle< T > & operator--(SingleParticle< T > &sp)
bool operator>(const SingleParticle< T > &sp1, const SingleParticle< T > &sp2)
bool operator>=(const SingleParticle< T > &sp1, const SingleParticle< T > &sp2)
SingleParticle< T > operator+(const SingleParticle< T > &sp, IntType n)