Skip to content

Commit 2cd6f61

Browse files
Improve na_omit for vectors without NA
If lengths of the input and output vectors are equal simply return input vector. Also create out vector without initialization (`no_init`).
1 parent 2a33dcc commit 2cd6f61

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

inst/include/Rcpp/sugar/functions/na_omit.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ namespace sugar{
3030
R_xlen_t n = x.size() ;
3131
R_xlen_t n_out = n - sum( is_na(x) ) ;
3232

33-
Vector<RTYPE> out(n_out) ;
33+
if( n_out == n ) return x ;
34+
35+
Vector<RTYPE> out = no_init(n_out) ;
3436
for( R_xlen_t i=0, j=0; i<n; i++){
3537
if( Vector<RTYPE>::is_na( x[i] ) ) continue ;
3638
out[j++] = x[i];
@@ -43,7 +45,10 @@ namespace sugar{
4345
R_xlen_t n = x.size() ;
4446
R_xlen_t n_out = n - sum( is_na(x) ) ;
4547

46-
Vector<RTYPE> out(n_out) ;
48+
if( n_out == n ) return x;
49+
50+
Vector<RTYPE> out = no_init(n_out) ;
51+
4752
bool has_name = x.attr("names") != R_NilValue ;
4853
if( has_name ){
4954
CharacterVector names = x.attr("names") ;

0 commit comments

Comments
 (0)