Skip to contents

The ma_setv(x, v rp) function performs the equivalent of
x[which(x == v)] <- rp
but using pass-by-reference semantics.

This is faster than using sb_set(x, i = which(x == v), rp = rp).

Inspired by collapse::setv, but written in 'C++' through 'Rcpp', with additional safety checks.

Usage

ma_setv(x, v, rp, invert = FALSE, NA.safety = TRUE)

Arguments

x

a mutable_atomic variable.

v

non-missing (so no NA or NaN) atomic scalar to find.

rp

atomic scalar giving the replacement value.

invert

Boolean.
If FALSE (default), the equivalent of x[which(x == v()] <- rp is performed;
If TRUE, the equivalent of x[which(x != v)] <- rp is performed instead.

NA.safety

Boolean.
just like in which, NA and NaN results in x==v should be ignored, thus NA.safety is TRUE by default.
However, if it is known that x contains no NAs or NaNs, setting NA.safety to FALSE will increase performance a bit.
NOTE: Setting NA.safety = FALSE when x does contain NAs or NaNs, may result in unexpected behaviour.
[for performance: set to FALSE]

Value

Returns: VOID. This function modifies the object by reference.

Do not use assignment like x <- ma_setv(x, ...).

Since this function returns void, you'll just get NULL.


Examples


x <- mutable_atomic(c(1:20, NA, NaN))
print(x)
#>  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19
#> [20]  20  NA NaN
#> mutable_atomic 
#> typeof:  double 
ma_setv(x, 2, 100)
print(x)
#>  [1]   1 100   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19
#> [20]  20  NA NaN
#> mutable_atomic 
#> typeof:  double