32 #ifndef VSMC_RESAMPLE_INDEX_HPP 33 #define VSMC_RESAMPLE_INDEX_HPP 37 #define VSMC_RUNTIME_ASSERT_RESAMPLE_INDEX_ITER(iter, iter_back, func) \ 38 VSMC_RUNTIME_ASSERT((iter <= iter_back && iter_back < iter_size()), \ 39 "**ResampleIndex::" #func "** ITERATION NUMBERS OUT OF RANGE") 46 template <
typename IntType = std::
size_t>
55 std::size_t
iter_size()
const {
return iter_size_; }
62 return index_.back().size();
66 std::size_t
size(std::size_t iter)
const 70 return index_[iter].size();
74 void reset() { iter_size_ = 0; }
77 void clear() { index_.clear(); }
84 if (index_.size() < iter_size_)
85 index_.push_back(identity_);
87 index_[iter_size_ - 1] = identity_;
91 template <
typename InputIter>
95 if (index_.size() < iter_size_)
98 index_[iter_size_ - 1].resize(N);
99 std::copy_n(first, N, index_[iter_size_ - 1].begin());
108 index_[iter_size_ - 1].resize(N);
109 std::copy_n(identity_.begin(), N, index_[iter_size_ - 1].begin());
113 template <
typename InputIter>
114 void insert(std::size_t N, InputIter first)
118 index_[iter_size_ - 1].resize(N);
119 std::copy_n(first, N, index_[iter_size_ - 1].begin());
123 template <
typename InputIter>
124 void insert(std::size_t N, std::size_t iter, InputIter first)
128 index_[iter].resize(N);
129 std::copy_n(first, N, index_[iter].begin());
134 return index(
id, iter_size_ - 1, 0);
139 return index(
id, iter_back, 0);
144 std::size_t
id, std::size_t iter_back, std::size_t iter)
const 149 while (iter_back > iter) {
151 idx = index_[iter_back][idx];
166 return index_[iter_back].size();
180 std::size_t iter_back, std::size_t iter)
const 184 return iter_back - iter + 1;
200 MatrixLayout layout, std::size_t iter_back, std::size_t iter)
const 211 template <
typename RandomIter>
217 template <
typename RandomIter>
219 MatrixLayout layout, RandomIter first, std::size_t iter_back)
const 233 template <
typename RandomIter>
235 std::size_t iter_back, std::size_t iter)
const 239 using difference_type =
240 typename std::iterator_traits<RandomIter>::difference_type;
245 RandomIter back = first +
static_cast<difference_type
>(R - 1);
246 const index_type *idx = index_[iter_back].data();
247 for (std::size_t i = 0; i != N; ++i)
248 back[static_cast<difference_type>(i * R)] = idx[i];
249 for (std::size_t r = 1; r != R; ++r) {
250 const std::size_t j = iter_back - r;
251 RandomIter last = back;
253 idx = index_[j].data();
254 for (std::size_t i = 0; i != N; ++i) {
255 back[
static_cast<difference_type
>(i * R)] =
256 idx[static_cast<std::size_t>(
257 last[static_cast<difference_type>(i * R)])];
264 first +
static_cast<difference_type
>(N * (R - 1));
265 const index_type *idx = index_[iter_back].data();
266 std::copy_n(idx, N, back);
267 for (std::size_t r = 1; r != R; ++r) {
268 const std::size_t j = iter_back - r;
269 RandomIter last = back;
270 back -=
static_cast<difference_type
>(N);
271 idx = index_[j].data();
272 for (std::size_t i = 0; i != N; ++i) {
273 back[
static_cast<difference_type
>(i)] =
274 idx[static_cast<std::size_t>(
275 last[static_cast<difference_type>(i)])];
280 return first +
static_cast<difference_type
>(N * R);
284 std::size_t iter_size_;
288 void resize_identity(std::size_t N)
290 std::size_t n = identity_.size();
293 for (std::size_t i = n; i != N; ++i)
294 identity_[i] = static_cast<index_type>(i);
300 #endif // VSMC_RESAMPLE_INDEX_HPP std::vector< T, Alloc > Vector
std::vector with Allocator as default allocator
std::size_t size(std::size_t iter) const
The sample size of a given iteration.
void push_back(std::size_t N, InputIter first)
Append a resampling index.
void insert(std::size_t N, std::size_t iter, InputIter first)
Insert at a given iteration a resampling index.
void push_back(std::size_t N)
Append an identity resampling index.
MatrixLayout
Matrix layout.
index_type index(std::size_t id, std::size_t iter_back) const
std::size_t index_matrix_ncol(std::size_t iter_back) const
void reset()
Reset history.
std::size_t iter_size() const
Number of iterations recorded.
void insert(std::size_t N, InputIter first)
Insert at last iteration an identity resampling index.
Vector< index_type > index_matrix(MatrixLayout layout, std::size_t iter_back, std::size_t iter) const
Get the resampling index matrix.
Record and trace resampling index.
RandomIter read_index_matrix(MatrixLayout layout, RandomIter first) const
RandomIter read_index_matrix(MatrixLayout layout, RandomIter first, std::size_t iter_back, std::size_t iter) const
Read the resampling index matrix into an random access iterator.
index_type index(std::size_t id, std::size_t iter_back, std::size_t iter) const
Get the index given the particle ID and iteration number.
std::size_t index_matrix_nrow(std::size_t iter_back) const
Vector< index_type > index_matrix(MatrixLayout layout) const
std::size_t index_matrix_ncol(std::size_t iter_back, std::size_t iter) const
index_type index(std::size_t id) const
Vector< index_type > index_matrix(MatrixLayout layout, std::size_t iter_back) const
void clear()
Release memory.
#define VSMC_RUNTIME_ASSERT_RESAMPLE_INDEX_ITER(iter, iter_back, func)
std::size_t index_matrix_nrow() const
void insert(std::size_t N)
Insert at last iteration an identity resampling index.
RandomIter read_index_matrix(MatrixLayout layout, RandomIter first, std::size_t iter_back) const
std::size_t index_matrix_ncol() const
std::size_t size() const
The sample size of the last iteration.