32 #ifndef VSMC_CORE_STATE_MATRIX_HPP 33 #define VSMC_CORE_STATE_MATRIX_HPP 38 #define VSMC_RUNTIME_ASSERT_CORE_STATE_MATRIX_UNPACK_SIZE(psize, dim) \ 39 VSMC_RUNTIME_ASSERT((psize >= dim), \ 40 "**StateMatrix::state_unpack** INPUT PACK SIZE TOO SMALL") 48 template <std::
size_t Dim>
52 static constexpr std::size_t
dim() {
return Dim; }
66 std::size_t
dim()
const {
return dim_; }
84 template <MatrixLayout Layout, std::
size_t Dim,
typename T>
102 std::size_t
dim()
const {
return this->particle().state().dim(); }
106 return this->particle().state()(
107 static_cast<size_type>(this->id()), pos);
123 data_.reserve(N * (Dim ==
Dynamic ? dim : this->
dim()));
136 data_.swap(other.data_);
144 static_assert(Dim ==
Dynamic,
"**StateMatrix::StateMatrix** USED WITH " 145 "AN OBJECT WITH FIXED DIMENSION");
168 data_.resize(N * dim);
176 if (state1.
dim() != state2.
dim())
178 if (state1.size_ != state2.size_)
180 if (state1.data_ != state2.data_)
188 return !(state1 == state2);
198 template <MatrixLayout Layout, std::
size_t Dim,
typename T>
207 template <std::
size_t Dim,
typename T>
226 static_assert(Dim ==
Dynamic,
"**StateMatrix::resize** USED WITH AN " 227 "OBJECT WITH FIXED DIMENSION");
233 static_assert(Dim ==
Dynamic,
"**StateMatrix::resize_dim** USED WITH " 234 "AN OBJECT WITH FIXED DIMENSION");
235 resize_both(this->size(), dim);
240 return this->data()[
id * this->
dim() + pos];
245 return this->data()[
id * this->
dim() + pos];
248 template <
typename OutputIter>
249 OutputIter
read_state(std::size_t pos, OutputIter first)
const 251 for (
size_type i = 0; i != this->size(); ++i, ++first)
252 *first =
operator()(i, pos);
257 template <
typename OutputIter>
261 first = std::copy_n(this->data(), this->data_size(), first);
264 for (std::size_t d = 0; d != this->
dim(); ++d)
265 for (
size_type i = 0; i != this->size(); ++i, ++first)
266 *first =
operator()(i, d);
273 return this->data() +
id * this->
dim();
278 return this->data() +
id * this->
dim();
291 template <
typename IntType,
typename InputIter>
300 if (n > this->size())
302 for (
size_type dst = 0; dst != n; ++dst, ++index)
303 duplicate(static_cast<size_type>(*index), dst);
304 if (n < this->size())
315 duplicate_dispatch(src, dst, std::integral_constant <
bool,
316 Dim ==
Dynamic || 8 < Dim > ());
322 std::copy(row_data(
id), row_data(
id) + this->
dim(), pack.data());
330 pack.size(), this->
dim());
333 std::copy(ptr, ptr + this->
dim(), row_data(
id));
339 pack.size(), this->
dim());
342 std::move(ptr, ptr + this->
dim(), row_data(
id));
348 if (N == this->size() && dim == this->
dim())
351 if (dim == this->
dim()) {
352 this->resize_data(N, dim);
358 const size_type K = std::min(N, this->size());
359 const std::size_t D = std::min(dim, this->
dim());
362 std::copy_n(row_data(k), D, tmp.
row_data(k));
363 *
this = std::move(tmp);
368 std::copy(row_data(src), row_data(src) + this->
dim(), row_data(dst));
373 duplicate_pos<0>(row_data(src), row_data(dst),
374 std::integral_constant<bool, 0 < Dim>());
377 template <std::
size_t D>
382 template <std::
size_t D>
386 duplicate_pos<D + 1>(
387 src, dst, std::integral_constant<bool, D + 1 < Dim>());
393 template <std::
size_t Dim,
typename T>
412 static_assert(Dim ==
Dynamic,
"**StateMatrix::resize** USED WITH AN " 413 "OBJECT WITH FIXED DIMENSION");
419 static_assert(Dim ==
Dynamic,
"**StateMatrix::resize_dim** USED WITH " 420 "AN OBJECT WITH FIXED DIMENSION");
421 resize_both(this->size(), dim);
426 return this->data()[pos * this->size() + id];
431 return this->data()[pos * this->size() + id];
434 template <
typename OutputIter>
435 OutputIter
read_state(std::size_t pos, OutputIter first)
const 437 return std::copy_n(col_data(pos), this->size(), first);
440 template <
typename OutputIter>
444 for (
size_type i = 0; i != this->size(); ++i)
445 for (std::size_t d = 0; d != this->size(); ++d, ++first)
446 *first =
operator()(i, d);
449 first = std::copy_n(this->data(), this->data_size(), first);
456 return this->data() + pos * this->size();
461 return this->data() + pos * this->size();
474 template <
typename InputIter>
483 InputIter idx = index;
484 if (n == this->size()) {
485 for (std::size_t d = 0; d != this->
dim(); ++d) {
487 for (
size_type dst = 0; dst != n; ++dst, ++idx) {
488 operator()(dst, d) = operator()(
489 static_cast<size_type>(*idx), d);
495 for (std::size_t d = 0; d != this->
dim(); ++d) {
497 for (
size_type dst = 0; dst != n; ++dst, ++idx)
498 tmp(dst, d) = operator()(static_cast<size_type>(*idx), d);
500 *
this = std::move(tmp);
511 duplicate_dispatch(src, dst, std::integral_constant <
bool,
512 Dim ==
Dynamic || 8 < Dim > ());
518 for (std::size_t d = 0; d != this->
dim(); ++d)
519 pack[d] =
operator()(id, d);
527 pack.size(), this->
dim());
529 for (std::size_t d = 0; d != this->
dim(); ++d)
530 operator()(id, d) = pack[d];
536 pack.size(), this->
dim());
538 for (std::size_t d = 0; d != this->
dim(); ++d)
539 operator()(id, d) = std::move(pack[d]);
545 if (N == this->size() && dim == this->
dim())
548 if (N == this->size()) {
549 this->resize_data(N, dim);
555 const size_type K = std::min(N, this->size());
556 const std::size_t D = std::min(dim, this->
dim());
558 for (std::size_t d = 0; d != D; ++d)
559 std::copy_n(col_data(d), K, tmp.
col_data(d));
560 *
this = std::move(tmp);
565 for (std::size_t d = 0; d != this->
dim(); ++d)
566 operator()(dst, d) =
operator()(src, d);
571 duplicate_pos<0>(this->data() + src, this->data() + dst,
572 std::integral_constant<bool, 0 < Dim>());
575 template <std::
size_t D>
580 template <std::
size_t D>
583 dst[D * this->size()] = src[D * this->size()];
584 duplicate_pos<D + 1>(
585 src, dst, std::integral_constant<bool, D + 1 < Dim>());
591 #endif // VSMC_CORE_STATE_MATRIX_HPP
single_particle_type(typename Particle< S >::size_type id, Particle< S > *pptr)
StateMatrix(size_type N, std::size_t dim)
Particle class representing the whole particle set.
void state_unpack(size_type id, const pack_type &pack)
void select(IntType N, InputIter index)
Copy particles.
Particle::value_type subtype.
Base type of StateMatrix.
pack_type state_pack(size_type id) const
MatrixLayout
Matrix layout.
OutputIter read_state(std::size_t pos, OutputIter first) const
size_type size() const
The numbrer of particles.
void duplicate(size_type src, size_type dst)
friend bool operator==(const StateMatrixBase< Layout, Dim, T > &state1, const StateMatrixBase< Layout, Dim, T > &state2)
typename state_matrix_base_type::size_type size_type
StateMatrix(size_type N=0)
void state_unpack(size_type id, pack_type &&pack)
const value_type * row_data(size_type id) const
void resize_dim(std::size_t dim)
typename state_matrix_base_type::pack_type pack_type
void duplicate(size_type src, size_type dst)
pack_type state_pack(size_type id) const
void state_unpack(size_type id, pack_type &&pack)
typename state_matrix_base_type::pack_type pack_type
static constexpr std::size_t dim()
OutputIter read_state_matrix(MatrixLayout layout, OutputIter first) const
value_type * col_data(std::size_t pos)
value_type * row_data(size_type id)
typename state_matrix_base_type::value_type value_type
void reserve(size_type N, std::size_t dim)
Reserve space for specified number of particles and dimensions.
Particle::value_type subtype.
const T & operator()(size_type id, std::size_t pos) const
void resize_data(size_type N, std::size_t dim)
StateMatrixBase(size_type N)
StateMatrix(size_type N, std::size_t dim)
StateMatrixBase(size_type N, std::size_t dim)
void reserve(size_type N)
Reserve space for specified number of particles.
void set_dim(std::size_t dim)
void state_unpack(size_type id, const pack_type &pack)
T & operator()(size_type id, std::size_t pos)
A thin wrapper over a complete Particle.
void swap(StateMatrixBase< Layout, Dim, T > &other) noexcept
std::size_t data_size() const
void resize(size_type N, std::size_t dim)
void swap(StateMatrixDim< Dim > &) noexcept
bool is_nullptr(T ptr, std::true_type)
const value_type * data() const
value_type & operator()(std::size_t pos) const
friend bool operator!=(const StateMatrixBase< Layout, Dim, T > &state1, const StateMatrixBase< Layout, Dim, T > &state2)
OutputIter read_state_matrix(MatrixLayout layout, OutputIter first) const
#define VSMC_RUNTIME_ASSERT_CORE_STATE_MATRIX_UNPACK_SIZE(psize, dim)
StateMatrixBase< Layout, Dim, T > & operator=(StateMatrixBase< Layout, Dim, T > &&other) noexcept
typename state_matrix_base_type::value_type value_type
void swap(StateMatrixDim< Dynamic > &other) noexcept
T & operator()(size_type id, std::size_t pos)
void resize_dim(std::size_t dim)
OutputIter read_state(std::size_t pos, OutputIter first) const
void resize(size_type N, std::size_t dim)
const T & operator()(size_type id, std::size_t pos) const
const value_type * col_data(std::size_t pos) const
static void set_dim(std::size_t)
void select(size_type N, InputIter index)
Copy particles.
void swap(StateMatrixBase< Layout, Dim, T > &state1, StateMatrixBase< Layout, Dim, T > &state2) noexcept
Swap two StateMatrixBase objects.
typename state_matrix_base_type::size_type size_type
StateMatrix(size_type N=0)