24 #ifndef RCPPARMADILLO__EXTENSIONS__SAMPLE_H
26 #ifndef Rcpp__sugar__sample_h
27 #define Rcpp__sugar__sample_h
60 R_xlen_t npos = 0, i = 0, n = p.
size();
63 if (!R_FINITE(p[i]) || (p[i] < 0)) {
64 stop(
"Probabilities must be finite and non-negative!");
70 if ((!npos) || (!replace && (require_k > npos))) {
71 stop(
"Too few positive probabilities!");
74 for (i = 0; i < n; i++) {
85 int i = 0, j = 0, nm1 = n - 1;
87 int adj = one_based ? 0 : 1;
95 for (i = 1; i < n; i++) {
99 for (i = 0; i < k; i++) {
101 for (j = 0; j < nm1; j++) {
106 ans[i] = perm[j] - adj;
122 int i = 0, j = 0, nm1 = n - 1;
124 for ( ; i < n; i++) {
130 for (i = 1; i < n; i++) {
134 for (i = 0; i < k; i++) {
136 for (j = 0; j < nm1; j++) {
141 ans[i] = ref[perm[j] - 1];
153 std::vector<double> q(n);
156 std::vector<int> HL(n);
157 std::vector<int>::iterator H, L;
159 int adj = one_based ? 1 : 0;
161 H = HL.begin() - 1; L = HL.begin() + n;
162 for (i = 0; i < n; i++) {
171 if (H >= HL.begin() && L < HL.begin() + n) {
172 for (k = 0; k < n - 1; k++) {
180 if (L >= HL.begin() + n) {
186 for (i = 0; i < n; i++) {
190 for (i = 0; i < nans; i++) {
192 k =
static_cast<int>(rU);
193 ans[i] = (rU < q[k]) ? k + adj : a[k] + adj;
209 std::vector<double> q(n);
212 std::vector<int> HL(n);
213 std::vector<int>::iterator H, L;
215 H = HL.begin() - 1; L = HL.begin() + n;
216 for (i = 0; i < n; i++) {
225 if (H >= HL.begin() && L < HL.begin() + n) {
226 for (k = 0; k < n - 1; k++) {
234 if (L >= HL.begin() + n) {
240 for (i = 0; i < n; i++) {
244 for (i = 0; i < nans; i++) {
246 k =
static_cast<int>(rU);
247 ans[i] = (rU < q[k]) ? ref[k] : ref[a[k]];
258 double rT, mass, totalmass;
261 int adj = one_based ? 0 : 1;
263 for (i = 0; i < n; i++) {
270 for (i = 0, n1 = n - 1; i < nans; i++, n1--) {
274 for (j = 0; j < n1; j++) {
281 ans[i] = perm[j] - adj;
284 for (k = j; k < n1; k++) {
286 perm[k] = perm[k + 1];
302 double rT, mass, totalmass;
305 for (i = 0; i < n; i++) {
312 for (i = 0, n1 = n - 1; i < nans; i++, n1--) {
316 for (j = 0; j < n1; j++) {
323 ans[i] = ref[perm[j] - 1];
326 for (k = j; k < n1; k++) {
328 perm[k] = perm[k + 1];
342 int adj = one_based ? 1 : 0;
344 if (replace || size < 2) {
345 for ( ; ians != eans; ++ians) {
346 *ians =
static_cast<int>(n *
unif_rand() + adj);
352 for (
int i = 0; i < n; i++) {
356 for ( ; ians != eans; ++ians) {
357 int j =
static_cast<int>(n *
unif_rand());
374 if (replace || size < 2) {
375 for ( ; ians != eans; ++ians) {
376 *ians = ref[
static_cast<int>(n *
unif_rand())];
382 for (
int i = 0; i < n; i++) {
386 for ( ; ians != eans; ++ians) {
387 int j =
static_cast<int>(n *
unif_rand());
403 if (probs.isNotNull()) {
405 if (
static_cast<int>(p.
size()) != n) {
406 stop(
"probs.size() != n!");
413 for ( ; i < n; i++) {
414 nc += (n * p[i] > 0.1);
422 stop(
"Sample size must be <= n when not using replacement!");
428 if (!replace && size > n) {
429 stop(
"Sample size must be <= n when not using replacement!");
441 if (probs.isNotNull()) {
443 if (
static_cast<int>(p.
size()) != n) {
444 stop(
"probs.size() != n!");
451 for ( ; i < n; i++) {
452 nc += (n * p[i] > 0.1);
460 stop(
"Sample size must be <= n when not using replacement!");
466 if (!replace && size > n) {
467 stop(
"Sample size must be <= n when not using replacement!");
traits::r_vector_iterator< RTYPE, StoragePolicy >::type iterator
void Normalize(Vector< REALSXP > &p, int require_k, bool replace)
Vector< INTSXP > SampleReplace(Vector< REALSXP > &p, int n, int k, bool one_based)
Nullable< Vector< REALSXP > > probs_t
Vector< INTSXP > SampleNoReplace(Vector< REALSXP > &p, int n, int nans, bool one_based)
Vector< INTSXP > EmpiricalSample(int n, int size, bool replace, bool one_based)
Vector< INTSXP > WalkerSample(const Vector< REALSXP > &p, int n, int nans, bool one_based)
sugar::Sum< INTSXP, NA, T > sum(const VectorBase< INTSXP, NA, T > &t)
no_init_vector no_init(R_xlen_t size)
void NORET stop(const char *fmt, Args &&... args)
Vector< INTSXP > sample(int n, int size, bool replace=false, sugar::probs_t probs=R_NilValue, bool one_based=true)