Skip to content

Commit f24398f

Browse files
added Na_proxy. closes #60
1 parent 9704ae9 commit f24398f

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

inst/include/Rcpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#include <Rcpp/Datetime.h>
5858
#include <Rcpp/DatetimeVector.h>
5959

60+
#include <Rcpp/NA_Proxy.h>
61+
6062
#include <Rcpp/Module.h>
6163
#include <Rcpp/InternalFunction.h>
6264

inst/include/Rcpp/Na_Proxy.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (C) 2013 Romain Francois
2+
//
3+
// This file is part of Rcpp.
4+
//
5+
// Rcppis free software: you can redistribute it and/or modify it
6+
// under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 2 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Rcpp is distributed in the hope that it will be useful, but
11+
// WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#ifndef Rcpp_Na_Proxy_h
19+
#define Rcpp_Na_Proxy_h
20+
21+
namespace Rcpp{
22+
class Na_Proxy{} ;
23+
static Na_Proxy NA ;
24+
}
25+
26+
inline bool operator==(double x , Rcpp::Na_Proxy){ return Rcpp::traits::is_na<REALSXP>(x) ; }
27+
inline bool operator==(int x , Rcpp::Na_Proxy){ return Rcpp::traits::is_na<INTSXP>(x) ; }
28+
inline bool operator==(Rcpp::String x, Rcpp::Na_Proxy){ return Rcpp::traits::is_na<STRSXP>(x.get_sexp()) ; }
29+
inline bool operator==(Rcomplex x , Rcpp::Na_Proxy){ return Rcpp::traits::is_na<CPLXSXP>(x) ; }
30+
inline bool operator==(SEXP x , Rcpp::Na_Proxy){ return TYPEOF(x)==CHARSXP && Rcpp::traits::is_na<STRSXP>(x) ; }
31+
inline bool operator==(std::string , Rcpp::Na_Proxy){ return false ; }
32+
inline bool operator==(const char* , Rcpp::Na_Proxy){ return false ; }
33+
inline bool operator==(Rcpp::internal::string_proxy<STRSXP> x, Rcpp::Na_Proxy){
34+
return Rcpp::traits::is_na<STRSXP>(x.get()) ;
35+
}
36+
inline bool operator==(Rcpp::internal::const_string_proxy<STRSXP> x, Rcpp::Na_Proxy){
37+
return Rcpp::traits::is_na<STRSXP>(x.get()) ;
38+
}
39+
40+
inline bool operator==(Rcpp::Na_Proxy, double x ){ return Rcpp::traits::is_na<REALSXP>(x) ; }
41+
inline bool operator==(Rcpp::Na_Proxy, int x ){ return Rcpp::traits::is_na<INTSXP>(x) ; }
42+
inline bool operator==(Rcpp::Na_Proxy, Rcpp::String x ){ return Rcpp::traits::is_na<STRSXP>(x.get_sexp()) ; }
43+
inline bool operator==(Rcpp::Na_Proxy, SEXP x ){ return TYPEOF(x)==CHARSXP && Rcpp::traits::is_na<STRSXP>(x) ; }
44+
inline bool operator==(Rcpp::Na_Proxy, Rcomplex x ){ return Rcpp::traits::is_na<CPLXSXP>(x) ; }
45+
inline bool operator==(Rcpp::Na_Proxy, std::string ){ return false ; }
46+
inline bool operator==(Rcpp::Na_Proxy, const char* ){ return false ; }
47+
inline bool operator==(Rcpp::Na_Proxy, Rcpp::internal::string_proxy<STRSXP> x){
48+
return Rcpp::traits::is_na<STRSXP>(x.get()) ;
49+
}
50+
inline bool operator==(Rcpp::Na_Proxy, Rcpp::internal::const_string_proxy<STRSXP> x){
51+
return Rcpp::traits::is_na<STRSXP>(x.get()) ;
52+
}
53+
54+
#endif

inst/unitTests/cpp/misc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,29 @@ void test_rcout(std::string tfile, std::string teststring){
107107
testfile.close();
108108
}
109109

110+
// [[Rcpp::export]]
111+
LogicalVector na_proxy(){
112+
CharacterVector s("foo") ;
113+
return LogicalVector::create(
114+
NA_REAL == NA,
115+
NA_INTEGER == NA,
116+
NA_STRING == NA,
117+
true == NA,
118+
false == NA,
119+
1.2 == NA,
120+
12 == NA,
121+
"foo" == NA,
122+
s[0] == NA,
123+
124+
NA == NA_REAL,
125+
NA == NA_INTEGER,
126+
NA == NA_STRING,
127+
NA == true,
128+
NA == false,
129+
NA == 1.2 ,
130+
NA == 12 ,
131+
NA == "foo",
132+
NA == s[0]
133+
) ;
134+
}
135+

inst/unitTests/runit.misc.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,12 @@ test.rcout <- function(){
118118
checkEquals( readLines(rcppfile), readLines(rfile), msg="Rcout output")
119119
}
120120

121+
test.na_proxy <- function(){
122+
checkEquals(
123+
na_proxy(),
124+
rep(c(TRUE,TRUE,TRUE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE) , 2),
125+
msg = "Na_Proxy NA == handling"
126+
)
127+
}
128+
121129
}

0 commit comments

Comments
 (0)