Skip to content

Commit 8e0fcc3

Browse files
author
Qiang Kou
committed
Rf_xlength and a simple testing case
1 parent 2637ca8 commit 8e0fcc3

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

inst/include/Rcpp/api/meat/export.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace internal{
2727

2828
template <typename InputIterator, typename value_type>
2929
void export_range__dispatch( SEXP x, InputIterator first, ::Rcpp::traits::r_type_generic_tag ) {
30-
R_xlen_t n = ::Rf_length(x) ;
30+
R_xlen_t n = ::Rf_xlength(x) ;
3131
for( R_xlen_t i=0; i<n; i++, ++first ){
3232
*first = ::Rcpp::as<value_type>( VECTOR_ELT(x, i) ) ;
3333
}
@@ -49,9 +49,9 @@ namespace internal{
4949
Container get(){
5050
if( TYPEOF(object) == RTYPE ){
5151
T* start = Rcpp::internal::r_vector_start<RTYPE>(object) ;
52-
return Container( start, start + Rf_length(object) ) ;
52+
return Container( start, start + Rf_xlength(object) ) ;
5353
}
54-
Container vec( ::Rf_length(object) );
54+
Container vec( ::Rf_xlength(object) );
5555
::Rcpp::internal::export_range( object, vec.begin() ) ;
5656
return vec ;
5757
}

inst/include/Rcpp/internal/export.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace Rcpp{
5252
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
5353
Shield<SEXP> y( ::Rcpp::r_cast<RTYPE>(x) ) ;
5454
STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
55-
std::copy( start, start + ::Rf_length(y), first ) ;
55+
std::copy( start, start + ::Rf_xlength(y), first ) ;
5656
}
5757

5858
template <typename InputIterator, typename value_type>
@@ -61,7 +61,7 @@ namespace Rcpp{
6161
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
6262
Shield<SEXP> y( ::Rcpp::r_cast<RTYPE>(x) ) ;
6363
STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
64-
std::transform( start, start + ::Rf_length(y) , first, caster<STORAGE,value_type> ) ;
64+
std::transform( start, start + ::Rf_xlength(y) , first, caster<STORAGE,value_type> ) ;
6565
}
6666

6767
// implemented in meat
@@ -80,7 +80,7 @@ namespace Rcpp{
8080
template <typename InputIterator, typename value_type>
8181
void export_range__dispatch( SEXP x, InputIterator first, ::Rcpp::traits::r_type_string_tag ) {
8282
if( ! ::Rf_isString( x) ) throw ::Rcpp::not_compatible( "expecting a string vector" ) ;
83-
R_xlen_t n = ::Rf_length(x) ;
83+
R_xlen_t n = ::Rf_xlength(x) ;
8484
for( R_xlen_t i=0; i<n; i++, ++first ){
8585
*first = as_string_elt<typename std::iterator_traits<InputIterator>::value_type> ( x, i ) ;
8686
}
@@ -104,7 +104,7 @@ namespace Rcpp{
104104
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
105105
Shield<SEXP> y( ::Rcpp::r_cast<RTYPE>(x) ) ;
106106
STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
107-
R_xlen_t size = ::Rf_length(y) ;
107+
R_xlen_t size = ::Rf_xlength(y) ;
108108
for( R_xlen_t i=0; i<size; i++){
109109
res[i] = start[i] ;
110110
}
@@ -116,7 +116,7 @@ namespace Rcpp{
116116
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
117117
Shield<SEXP> y( ::Rcpp::r_cast<RTYPE>(x) );
118118
STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
119-
R_xlen_t size = ::Rf_length(y) ;
119+
R_xlen_t size = ::Rf_xlength(y) ;
120120
for( R_xlen_t i=0; i<size; i++){
121121
res[i] = caster<STORAGE,value_type>(start[i]) ;
122122
}
@@ -134,7 +134,7 @@ namespace Rcpp{
134134
template <typename T, typename value_type>
135135
void export_indexing__dispatch( SEXP x, T& res, ::Rcpp::traits::r_type_string_tag ) {
136136
if( ! ::Rf_isString( x) ) throw Rcpp::not_compatible( "expecting a string vector" ) ;
137-
R_xlen_t n = ::Rf_length(x) ;
137+
R_xlen_t n = ::Rf_xlength(x) ;
138138
for( R_xlen_t i=0; i<n; i++ ){
139139
res[i] = as_string_elt< value_type >( x, i) ;
140140
}

inst/include/Rcpp/internal/r_vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ inline Rcomplex get_zero<CPLXSXP,Rcomplex>(){
6161
template<int RTYPE> void r_init_vector(SEXP x){
6262
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE ;
6363
CTYPE* start=r_vector_start<RTYPE>(x) ;
64-
std::fill( start, start + Rf_length(x), get_zero<RTYPE,CTYPE>() ) ;
64+
std::fill( start, start + Rf_xlength(x), get_zero<RTYPE,CTYPE>() ) ;
6565
}
6666
/**
6767
* Initializes a generic vector (VECSXP). Does nothing since

inst/include/Rcpp/vector/Vector.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,17 @@ class Vector :
259259
#endif
260260

261261
/**
262-
* the length of the vector, uses Rf_length
262+
* the length of the vector, uses Rf_xlength
263263
*/
264264
inline R_xlen_t length() const {
265-
return ::Rf_length( Storage::get__() ) ;
265+
return ::Rf_xlength( Storage::get__() ) ;
266266
}
267267

268268
/**
269269
* alias of length
270270
*/
271271
inline R_xlen_t size() const {
272-
return ::Rf_length( Storage::get__() ) ;
272+
return ::Rf_xlength( Storage::get__() ) ;
273273
}
274274

275275
/**
@@ -291,7 +291,7 @@ class Vector :
291291
* it is valid
292292
*/
293293
size_t offset(const size_t& i) const {
294-
if( static_cast<R_xlen_t>(i) >= ::Rf_length(Storage::get__()) ) throw index_out_of_bounds() ;
294+
if( static_cast<R_xlen_t>(i) >= ::Rf_xlength(Storage::get__()) ) throw index_out_of_bounds() ;
295295
return i ;
296296
}
297297

@@ -549,7 +549,7 @@ class Vector :
549549
bool containsElementNamed( const char* target ) const {
550550
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
551551
if( Rf_isNull(names) ) return false ;
552-
R_xlen_t n = Rf_length(names) ;
552+
R_xlen_t n = Rf_xlength(names) ;
553553
for( R_xlen_t i=0; i<n; i++){
554554
if( !strcmp( target, CHAR(STRING_ELT(names, i)) ) )
555555
return true ;
@@ -560,7 +560,7 @@ class Vector :
560560
int findName(const std::string& name) const {
561561
SEXP names = RCPP_GET_NAMES(Storage::get__());
562562
if (Rf_isNull(names)) stop("'names' attribute is null");
563-
R_xlen_t n = Rf_length(names);
563+
R_xlen_t n = Rf_xlength(names);
564564
for (R_xlen_t i=0; i < n; ++i) {
565565
if (strcmp(name.c_str(), CHAR(STRING_ELT(names, i))) == 0) {
566566
return i;

inst/unitTests/cpp/Vector.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// You should have received a copy of the GNU General Public License
2020
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
2121

22+
#include <climits>
2223
#include <Rcpp.h>
2324
using namespace Rcpp ;
2425

@@ -646,6 +647,13 @@ IntegerVector IntegerVector_int_init(){
646647
return x ;
647648
}
648649

650+
// [[Rcpp::export]]
651+
R_xlen_t Long_IntegerVector_length(){
652+
R_xlen_t l = INT_MAX + 100L;
653+
IntegerVector x(l,4) ;
654+
return x.size() ;
655+
}
656+
649657
// [[Rcpp::export]]
650658
bool containsElementNamed( List l, CharacterVector n){
651659
return l.containsElementNamed(n[0]);

inst/unitTests/runit.Vector.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ if (.runThisTest) {
567567
checkEquals( fun(), c(4L,4L), msg = "IntegerVector int init regression test" )
568568
}
569569

570+
test.Long_IntegerVector_length <- function(){
571+
fun <- Long_IntegerVector_length
572+
checkTrue( fun() > 0, msg = "Long IntegerVector length test" )
573+
}
574+
570575
test.containsElementNamed <- function() {
571576
fun <- containsElementNamed
572577

0 commit comments

Comments
 (0)