32 #ifndef VSMC_RESAMPLE_INDEX_HPP 33 #define VSMC_RESAMPLE_INDEX_HPP 37 #define VSMC_RUNTIME_ASSERT_RESAMPLE_INDEX_ITER(test, func) \ 38 VSMC_RUNTIME_ASSERT( \ 39 (test), "**StateIndex::" #func "ITERATION NUMBER OUT OF RANGE") 46 template <
typename IntType = std::
size_t>
56 identity_[i] = static_cast<index_type>(i);
62 std::size_t
iter_size()
const {
return iter_size_; }
65 void reset() { iter_size_ = 0; }
68 void clear() { index_.clear(); }
73 (index_.size() >= iter_size_),
push_back);
76 if (index_.size() < iter_size_)
77 index_.push_back(identity_);
79 index_[iter_size_ - 1] = identity_;
82 template <
typename InputIter>
86 std::copy_n(first, size_, index_[iter_size_ - 1].data());
92 (iter_size_ > 0 && index_.size() >= iter_size_),
insert);
94 std::copy_n(identity_.data(), size_, index_[iter_size_ - 1].data());
97 template <
typename InputIter>
101 (iter_size_ > 0 && index_.size() >= iter_size_),
insert);
103 std::copy_n(first, size_, index_[iter_size_ - 1].begin());
106 template <
typename InputIter>
107 void insert(std::size_t iter, InputIter first)
110 (iter_size_ > iter && index_.size() >= iter_size_),
insert);
112 std::copy_n(first, size_, index_[iter].begin());
124 (iter_size_ > iter && index_.size() >= iter_size_),
index);
126 std::size_t iter_current = iter_size_ - 1;
128 while (iter_current != iter) {
130 idx = index_[iter_current][idx];
136 template <MatrixLayout Layout>
139 return index_matrix_dispatch(
140 std::integral_constant<MatrixLayout, Layout>());
143 template <MatrixLayout Layout,
typename OutputIter>
147 std::copy(idxmat.begin(), idxmat.end(), first);
152 std::size_t iter_size_;
157 std::integral_constant<MatrixLayout, RowMajor>)
const 160 if (size_ * iter_size_ == 0)
163 index_type *back = idxmat.data() + iter_size_ - 1;
164 for (
size_type i = 0; i != size_; ++i, back += iter_size_)
165 *back = index_[iter_size_ - 1][i];
169 for (std::size_t iter = iter_size_ - 1; iter != 0; --iter) {
170 const index_type *idx = index_[iter - 1].data();
171 const index_type *last = idxmat.data() + iter;
174 *next = idx[
static_cast<size_type>(*last)];
184 std::integral_constant<MatrixLayout, ColMajor>)
const 187 if (size_ * iter_size_ == 0)
190 index_type *back = idxmat.data() + size_ * (iter_size_ - 1);
192 back[i] = index_[iter_size_ - 1][i];
196 for (std::size_t iter = iter_size_ - 1; iter != 0; --iter) {
197 const index_type *idx = index_[iter - 1].data();
198 const index_type *last = idxmat.data() + size_ * iter;
199 index_type *next = idxmat.data() + size_ * (iter - 1);
201 next[i] = idx[static_cast<size_type>(last[i])];
210 #endif // VSMC_RESAMPLE_INDEX_HPP void read_index_matrix(OutputIter first) const
typename std::conditional< std::is_scalar< T >::value, AlignedVector< T >, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
void reset()
Reset history.
std::size_t iter_size() const
Number of iterations recorded.
Record and trace resample index.
Vector< index_type > index_matrix() const
void insert(InputIter first)
void push_back(InputIter first)
#define VSMC_RUNTIME_ASSERT_RESAMPLE_INDEX_ITER(test, func)
ResampleIndex(size_type N)
void clear()
Release memory.
index_type index(size_type id, std::size_t iter) const
Get the index given the particle ID and iteration number, starting with zero.
void insert(std::size_t iter, InputIter first)