vSMC
vSMC: 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  : id_(id), pptr_(pptr)
58  {
59  }
60 
61  typename Particle<T>::size_type id() const { return id_; }
62 
63  Particle<T> &particle() const { return *pptr_; }
64 
65  Particle<T> *particle_ptr() const { return pptr_; }
66 
67  typename Particle<T>::rng_type &rng() { return pptr_->rng(id_); }
68 
69  private:
70  typename Particle<T>::size_type id_;
71  Particle<T> *pptr_;
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 bool operator==(const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
126 {
128 
129  return sp1.id() == sp2.id();
130 }
131 
132 template <typename T>
133 bool operator!=(const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
134 {
136 
137  return sp1.id() != sp2.id();
138 }
139 
140 template <typename T>
141 bool operator<(const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
142 {
144 
145  return sp1.id() < sp2.id();
146 }
147 
148 template <typename T>
149 bool operator>(const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
150 {
152 
153  return sp1.id() > sp2.id();
154 }
155 
156 template <typename T>
157 bool operator<=(const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
158 {
160 
161  return sp1.id() <= sp2.id();
162 }
163 
164 template <typename T>
165 bool operator>=(const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
166 {
168 
169  return sp1.id() >= sp2.id();
170 }
171 
172 template <typename T>
174 {
175  sp = SingleParticle<T>(sp.id() + 1, sp.particle_ptr());
176 
177  return sp;
178 }
179 
180 template <typename T>
182 {
183  auto sp_tmp = sp;
184  sp = SingleParticle<T>(sp.id() + 1, sp.particle_ptr());
185 
186  return sp_tmp;
187 }
188 
189 template <typename T>
191 {
192  sp = SingleParticle<T>(sp.id() - 1, sp.particle_ptr());
193 
194  return sp;
195 }
196 
197 template <typename T>
199 {
200  auto sp_tmp = sp;
201  sp = SingleParticle<T>(sp.id() - 1, sp.particle_ptr());
202 
203  return sp_tmp;
204 }
205 
206 template <typename T, typename IntType>
208 {
209  return SingleParticle<T>(static_cast<typename Particle<T>::size_type>(
210  static_cast<std::ptrdiff_t>(sp.id()) +
211  static_cast<std::ptrdiff_t>(n)),
212  sp.particle_ptr());
213 }
214 
215 template <typename T, typename IntType>
217 {
218  return SingleParticle<T>(static_cast<typename Particle<T>::size_type>(
219  static_cast<std::ptrdiff_t>(sp.id()) +
220  static_cast<std::ptrdiff_t>(n)),
221  sp.particle_ptr());
222 }
223 
224 template <typename T, typename IntType>
226 {
227  return SingleParticle<T>(static_cast<typename Particle<T>::size_type>(
228  static_cast<std::ptrdiff_t>(sp.id()) -
229  static_cast<std::ptrdiff_t>(n)),
230  sp.particle_ptr());
231 }
232 
233 template <typename T, typename IntType>
235 {
236  sp = sp + n;
237 
238  return sp;
239 }
240 
241 template <typename T, typename IntType>
243 {
244  sp = sp - n;
245 
246  return sp;
247 }
248 
249 template <typename T>
250 std::ptrdiff_t operator-(
251  const SingleParticle<T> &sp1, const SingleParticle<T> &sp2)
252 {
254 
255  return static_cast<std::ptrdiff_t>(sp1.id()) -
256  static_cast<std::ptrdiff_t>(sp2.id());
257 }
258 
259 } // namespace vsmc
260 
261 #endif // VSMC_CORE_SINGLE_PARTICLE_HPP
Definition: monitor.hpp:49
SizeType< T > size_type
Definition: particle.hpp:51
Particle class representing the whole particle set.
Definition: particle.hpp:48
typename rng_set_type::rng_type rng_type
Definition: particle.hpp:55
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)
Particle< T >::rng_type & rng()
#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)
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.
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)