Skip to content

Commit 33b67b8

Browse files
authored
Merge pull request #492 from artemklevtsov/patch-1
Improve na_omit for vectors without NA
2 parents 2a33dcc + 1f519e2 commit 33b67b8

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2016-06-14 Artem Klevtsov <[email protected]>
2+
3+
* inst/include/Rcpp/sugar/functions/na_omit.h: Improve na_omit for
4+
vectors without NA
5+
* inst/unitTests/cpp/sugar.cpp: Add unit test for the na_omit
6+
* inst/unitTests/runit.sugar.R: Ditto
7+
18
2016-06-02 Kirill Müller <[email protected]>
29

310
* inst/include/Rcpp/algorithm.h: Use "long long" only if available

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") ;

inst/unitTests/cpp/sugar.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,11 @@ LogicalVector runit_any_isna( NumericVector xx){
216216
return any( is_na( xx ) ) ;
217217
}
218218

219+
// [[Rcpp::export]]
220+
NumericVector runit_na_omit( NumericVector xx ){
221+
return na_omit( xx ) ;
222+
}
223+
219224
// [[Rcpp::export]]
220225
List runit_lapply( IntegerVector xx){
221226
List res = lapply( xx, seq_len );

inst/unitTests/runit.sugar.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ if (.runThisTest) {
290290
checkEquals( fx( c(1:5,NA,7:10) ) , TRUE )
291291
}
292292

293+
test.sugar.na_omit.na <- function( ){
294+
fx <- runit_na_omit
295+
checkEquals( fx( c(1:5,NA,7:10) ), fx( c(1:5,7:10) ) )
296+
}
297+
298+
test.sugar.na_omit.nona <- function( ){
299+
fx <- runit_na_omit
300+
checkEquals( fx( c(1:10) ), fx( c(1:10) ) )
301+
}
302+
293303
test.sugar.lapply <- function( ){
294304
fx <- runit_lapply
295305
checkEquals( fx( 1:10 ), lapply( 1:10, seq_len ) )

0 commit comments

Comments
 (0)