Adapted from `do_sample`
| 398 | |
| 399 | // Adapted from `do_sample` |
| 400 | inline Vector<INTSXP> |
| 401 | sample(int n, int size, bool replace = false, sugar::probs_t probs = R_NilValue, bool one_based = true) |
| 402 | { |
| 403 | if (probs.isNotNull()) { |
| 404 | Vector<REALSXP> p = clone(probs.get()); |
| 405 | if (static_cast<int>(p.size()) != n) { |
| 406 | stop("probs.size() != n!"); |
| 407 | } |
| 408 | |
| 409 | sugar::Normalize(p, size, replace); |
| 410 | |
| 411 | if (replace) { |
| 412 | int i = 0, nc = 0; |
| 413 | for ( ; i < n; i++) { |
| 414 | nc += (n * p[i] > 0.1); |
| 415 | } |
| 416 | |
| 417 | return nc > 200 ? sugar::WalkerSample(p, n, size, one_based) : |
| 418 | sugar::SampleReplace(p, n, size, one_based); |
| 419 | } |
| 420 | |
| 421 | if (size > n) { |
| 422 | stop("Sample size must be <= n when not using replacement!"); |
| 423 | } |
| 424 | |
| 425 | return sugar::SampleNoReplace(p, n, size, one_based); |
| 426 | } |
| 427 | |
| 428 | if (!replace && size > n) { |
| 429 | stop("Sample size must be <= n when not using replacement!"); |
| 430 | } |
| 431 | |
| 432 | return sugar::EmpiricalSample(n, size, replace, one_based); |
| 433 | } |
| 434 | |
| 435 | template <int RTYPE> |
| 436 | inline Vector<RTYPE> |
no test coverage detected