[Rcpp-commits] r4234 - in pkg/Rcpp: . inst/include inst/include/Rcpp/internal inst/include/Rcpp/traits src
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Feb 3 12:19:09 CET 2013
Author: romain
Date: 2013-02-03 12:19:09 +0100 (Sun, 03 Feb 2013)
New Revision: 4234
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/include/Rcpp/internal/export.h
pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
pkg/Rcpp/inst/include/Rcpp/traits/wrap_type_traits.h
pkg/Rcpp/inst/include/RcppCommon.h
pkg/Rcpp/src/api.cpp
Log:
support for wrap( wstring ) and wrap( container of wstring )
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2013-02-02 20:06:04 UTC (rev 4233)
+++ pkg/Rcpp/ChangeLog 2013-02-03 11:19:09 UTC (rev 4234)
@@ -1,3 +1,11 @@
+2013-02-03 Romain Francois <romain at r-enthusiasts.com>
+
+ * include/Rcpp/traits/wrap_type_traits.h : support for wrap( wstring )
+ * include/Rcpp/internal/export.h : exporting wstring and containers of wstring
+ * include/Rcpp/internal/wrap.h : support for wrap( wstring ) and containers of wstring
+ * include/RcppCommon.h : definition of charsexp_from_wstring
+ * src/api.cpp: implementation of charsexp_from_wstring
+
2013-02-02 Romain Francois <romain at r-enthusiasts.com>
* include/Rcpp/vector/MatrixRow.h: correct operator++(int) and operator--(int)
@@ -10,6 +18,7 @@
* unitTests/runit.Vector.R: new unit tests
* src/api.cpp: comment dropTrailing0 which is not used anymore
* include/Rcpp/as.h: support for as<wstring>
+ * include/Rcpp/internal/export.h: more support for wstring
2013-01-15 Dirk Eddelbuettel <edd at debian.org>
Modified: pkg/Rcpp/inst/include/Rcpp/internal/export.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/export.h 2013-02-02 20:06:04 UTC (rev 4233)
+++ pkg/Rcpp/inst/include/Rcpp/internal/export.h 2013-02-03 11:19:09 UTC (rev 4234)
@@ -3,7 +3,8 @@
//
// export.h: Rcpp R/C++ interface class library --
//
-// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2013 Rice University
//
// This file is part of Rcpp.
//
@@ -29,107 +30,130 @@
/* iterating */
template <typename InputIterator, typename value_type>
- void export_range__impl( SEXP x, InputIterator first, ::Rcpp::traits::false_type ) {
- const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
- typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
- SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
- STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
- std::copy( start, start + ::Rf_length(y), first ) ;
- UNPROTECT(1) ;
+ void export_range__impl( SEXP x, InputIterator first, ::Rcpp::traits::false_type ) {
+ const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
+ typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
+ STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
+ std::copy( start, start + ::Rf_length(y), first ) ;
+ UNPROTECT(1) ;
}
template <typename InputIterator, typename value_type>
void export_range__impl( SEXP x, InputIterator first, ::Rcpp::traits::true_type ) {
- const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
- typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
- SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
- STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
- std::transform( start, start + ::Rf_length(y) , first, caster<STORAGE,value_type> ) ;
- UNPROTECT(1) ;
+ const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
+ typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
+ STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
+ std::transform( start, start + ::Rf_length(y) , first, caster<STORAGE,value_type> ) ;
+ UNPROTECT(1) ;
}
template <typename InputIterator, typename value_type>
void export_range__dispatch( SEXP x, InputIterator first, ::Rcpp::traits::r_type_primitive_tag ) {
- export_range__impl<InputIterator,value_type>(
- x,
- first,
- typename ::Rcpp::traits::r_sexptype_needscast<value_type>() );
- }
+ export_range__impl<InputIterator,value_type>(
+ x,
+ first,
+ typename ::Rcpp::traits::r_sexptype_needscast<value_type>()
+ );
+ }
- template <typename InputIterator, typename value_type>
- void export_range__dispatch( SEXP x, InputIterator first, ::Rcpp::traits::r_type_string_tag ) {
- if( ! ::Rf_isString( x) ) throw ::Rcpp::not_compatible( "expecting a string vector" ) ;
- std::string buf ;
- R_len_t n = ::Rf_length(x) ;
- for( R_len_t i=0; i<n; i++, ++first ){
- *first = CHAR( STRING_ELT(x, i )) ;
- }
- }
+ template <typename InputIterator, typename value_type>
+ void export_range__dispatch( SEXP x, InputIterator first, ::Rcpp::traits::r_type_string_tag ) {
+ if( ! ::Rf_isString( x) ) throw ::Rcpp::not_compatible( "expecting a string vector" ) ;
+ R_len_t n = ::Rf_length(x) ;
+ for( R_len_t i=0; i<n; i++, ++first ){
+ *first = CHAR( STRING_ELT(x, i )) ;
+ }
+ }
- template <typename InputIterator>
- void export_range( SEXP x, InputIterator first ) {
- export_range__dispatch<InputIterator,typename std::iterator_traits<InputIterator>::value_type>(
- x,
- first,
- typename ::Rcpp::traits::r_type_traits<typename std::iterator_traits<InputIterator>::value_type>::r_category()
- );
- }
+ // InputIterator is an iterator over std::wstring
+ template <typename InputIterator, typename value_type>
+ void export_range__dispatch( SEXP x, InputIterator first, ::Rcpp::traits::r_type_wstring_tag ) {
+ if( ! ::Rf_isString( x) ) throw ::Rcpp::not_compatible( "expecting a string vector" ) ;
+ R_len_t n = ::Rf_length(x) ;
+ for( R_len_t i=0; i<n; i++, ++first ){
+ const char* st = CHAR( STRING_ELT(x, i )) ;
+ *first = std::wstring( st, st + strlen(st) ) ;
+ }
+ }
+ template <typename InputIterator>
+ void export_range( SEXP x, InputIterator first ) {
+ export_range__dispatch<InputIterator,typename std::iterator_traits<InputIterator>::value_type>(
+ x,
+ first,
+ typename ::Rcpp::traits::r_type_traits<typename std::iterator_traits<InputIterator>::value_type>::r_category()
+ );
+ }
+
/* indexing */
- template <typename T, typename value_type>
- void export_indexing__impl( SEXP x, T& res, ::Rcpp::traits::false_type ) {
- const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
- typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
- SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
- STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
- R_len_t size = ::Rf_length(y) ;
- for( R_len_t i=0; i<size; i++){
- res[i] = start[i] ;
- }
- UNPROTECT(1) ; /* y */
- }
+ template <typename T, typename value_type>
+ void export_indexing__impl( SEXP x, T& res, ::Rcpp::traits::false_type ) {
+ const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
+ typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
+ STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
+ R_len_t size = ::Rf_length(y) ;
+ for( R_len_t i=0; i<size; i++){
+ res[i] = start[i] ;
+ }
+ UNPROTECT(1) ; /* y */
+ }
- template <typename T, typename value_type>
- void export_indexing__impl( SEXP x, T& res, ::Rcpp::traits::true_type ) {
- const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
- typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
- SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
- STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
- R_len_t size = ::Rf_length(y) ;
- for( R_len_t i=0; i<size; i++){
- res[i] = caster<STORAGE,value_type>(start[i]) ;
- }
- UNPROTECT(1) ; /* y */
- }
+ template <typename T, typename value_type>
+ void export_indexing__impl( SEXP x, T& res, ::Rcpp::traits::true_type ) {
+ const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype ;
+ typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ SEXP y = PROTECT( ::Rcpp::r_cast<RTYPE>(x) ) ;
+ STORAGE* start = ::Rcpp::internal::r_vector_start<RTYPE>(y) ;
+ R_len_t size = ::Rf_length(y) ;
+ for( R_len_t i=0; i<size; i++){
+ res[i] = caster<STORAGE,value_type>(start[i]) ;
+ }
+ UNPROTECT(1) ; /* y */
+ }
- template <typename T, typename value_type>
- void export_indexing__dispatch( SEXP x, T& res, ::Rcpp::traits::r_type_primitive_tag ) {
- export_indexing__impl<T,value_type>(
- x,
- res,
- typename ::Rcpp::traits::r_sexptype_needscast<value_type>() );
- }
+ template <typename T, typename value_type>
+ void export_indexing__dispatch( SEXP x, T& res, ::Rcpp::traits::r_type_primitive_tag ) {
+ export_indexing__impl<T,value_type>(
+ x,
+ res,
+ typename ::Rcpp::traits::r_sexptype_needscast<value_type>()
+ );
+ }
- template <typename T, typename value_type>
- void export_indexing__dispatch( SEXP x, T& res, ::Rcpp::traits::r_type_string_tag ) {
- if( ! ::Rf_isString( x) ) throw Rcpp::not_compatible( "expecting a string vector" ) ;
- std::string buf ;
- R_len_t n = ::Rf_length(x) ;
- for( R_len_t i=0; i<n; i++ ){
- res[i] = CHAR( STRING_ELT(x, i )) ;
- }
- }
+ template <typename T, typename value_type>
+ void export_indexing__dispatch( SEXP x, T& res, ::Rcpp::traits::r_type_string_tag ) {
+ if( ! ::Rf_isString( x) ) throw Rcpp::not_compatible( "expecting a string vector" ) ;
+ R_len_t n = ::Rf_length(x) ;
+ for( R_len_t i=0; i<n; i++ ){
+ res[i] = CHAR( STRING_ELT(x, i )) ;
+ }
+ }
- template <typename T, typename value_type>
- void export_indexing( SEXP x, T& res ) {
- export_indexing__dispatch<T,value_type>(
- x,
- res,
- typename ::Rcpp::traits::r_type_traits<value_type>::r_category() );
- }
+ // T is an array of wstring
+ template <typename T, typename value_type>
+ void export_indexing__dispatch( SEXP x, T& res, ::Rcpp::traits::r_type_wstring_tag ) {
+ if( ! ::Rf_isString( x) ) throw Rcpp::not_compatible( "expecting a string vector" ) ;
+ R_len_t n = ::Rf_length(x) ;
+ for( R_len_t i=0; i<n; i++ ){
+ const char* st = CHAR( STRING_ELT(x, i )) ;
+ res[i] = std::wstring( st, st+strlen(st)) ;
+ }
+ }
+ template <typename T, typename value_type>
+ void export_indexing( SEXP x, T& res ) {
+ export_indexing__dispatch<T,value_type>(
+ x,
+ res,
+ typename ::Rcpp::traits::r_type_traits<value_type>::r_category()
+ );
+ }
+
}
}
Modified: pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/wrap.h 2013-02-02 20:06:04 UTC (rev 4233)
+++ pkg/Rcpp/inst/include/Rcpp/internal/wrap.h 2013-02-03 11:19:09 UTC (rev 4234)
@@ -3,7 +3,8 @@
//
// wrap.h: Rcpp R/C++ interface class library -- wrap implementations
//
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2013 Rice University
//
// This file is part of Rcpp.
//
@@ -172,6 +173,28 @@
UNPROTECT(1) ;
return x ;
}
+
+/**
+ * Range based wrap implementation for iterators over std::wstring
+ *
+ * This produces an unnamed character vector
+ */
+template<typename InputIterator, typename T>
+inline SEXP range_wrap_dispatch___impl( InputIterator first, InputIterator last, ::Rcpp::traits::r_type_wstring_tag ){
+ size_t size = std::distance( first, last ) ;
+ SEXP x = PROTECT( Rf_allocVector( STRSXP, size ) ) ;
+ size_t i = 0 ;
+ std::wstring buffer ;
+ while( i < size ){
+ buffer = *first ;
+ SET_STRING_ELT( x, i, charsexp_from_wstring(buffer) ) ;
+ i++ ;
+ ++first ;
+ }
+ UNPROTECT(1) ;
+ return x ;
+}
+
// }}}
// {{{ named range wrap
@@ -386,8 +409,22 @@
UNPROTECT(1) ;
return x;
}
+/**
+ * primitive wrap for types that can be converted implicitely to std::wstring
+ *
+ * This produces a character vector of length 1 containing the std::wstring
+ */
+template <typename T>
+inline SEXP primitive_wrap__impl( const T& object, ::Rcpp::traits::r_type_wstring_tag){
+ SEXP x = PROTECT( ::Rf_allocVector( STRSXP, 1) ) ;
+ SET_STRING_ELT( x, 0, charsexp_from_wstring(object) ) ;
+ UNPROTECT(1) ;
+ return x;
+}
+
+
/**
* called when T is a primitive type : int, bool, double, std::string, etc ...
* This uses the Rcpp::traits::r_type_traits on the type T to perform
@@ -515,7 +552,25 @@
UNPROTECT(2) ;
return res ;
}
+template <typename T>
+inline SEXP wrap_dispatch_matrix_not_logical( const T& object, ::Rcpp::traits::r_type_wstring_tag ){
+ int nr = object.nrow(), nc = object.ncol() ;
+ SEXP res = PROTECT( Rf_allocVector( STRSXP, nr*nc ) ) ;
+
+ int k=0 ;
+ for( int j=0; j<nc; j++)
+ for( int i=0; i<nr; i++, k++)
+ SET_STRING_ELT( res, k, charsexp_from_wstring(object(i,j)) ) ;
+ SEXP dim = PROTECT( Rf_allocVector( INTSXP, 2) ) ;
+ INTEGER(dim)[0] = nr ;
+ INTEGER(dim)[1] = nc ;
+ Rf_setAttrib( res, R_DimSymbol , dim ) ;
+ UNPROTECT(2) ;
+ return res ;
+}
+
+
template <typename T>
inline SEXP wrap_dispatch_matrix_not_logical( const T& object, ::Rcpp::traits::r_type_generic_tag ){
int nr = object.nrow(), nc = object.ncol() ;
@@ -609,6 +664,18 @@
UNPROTECT(1) ;
return x ;
}
+template <typename T, typename elem_type>
+inline SEXP wrap_dispatch_importer__impl( const T& object, ::Rcpp::traits::r_type_wstring_tag ){
+ int size = object.size() ;
+ SEXP x = PROTECT( Rf_allocVector( STRSXP, size ) );
+ std::wstring buf ;
+ for( int i=0; i<size; i++){
+ buf = object.get(i) ;
+ SET_STRING_ELT( x, i, charsexp_from_wstring(buf) ) ;
+ }
+ UNPROTECT(1) ;
+ return x ;
+}
template <typename T, typename elem_type>
inline SEXP wrap_dispatch_importer__impl( const T& object, ::Rcpp::traits::r_type_generic_tag ){
@@ -740,6 +807,24 @@
UNPROTECT(2); /* out, dims */
return out ;
}
+template <typename value_type, typename InputIterator>
+inline SEXP rowmajor_wrap__dispatch( InputIterator first, int nrow, int ncol, ::Rcpp::traits::r_type_wstring_tag ){
+ SEXP out = PROTECT( ::Rf_allocVector( STRSXP, nrow * ncol) );
+ int i=0, j=0 ;
+ std::wstring buffer ;
+ for( j=0; j<ncol; j++){
+ for( i=0; i<nrow; i++, ++first ){
+ buffer = *first ;
+ SET_STRING_ELT( out, j + ncol*i, charsexp_from_wstring(buffer) ) ;
+ }
+ }
+ SEXP dims = PROTECT( ::Rf_allocVector( INTSXP, 2) );
+ INTEGER(dims)[0] = nrow;
+ INTEGER(dims)[1] = ncol;
+ ::Rf_setAttrib( out, R_DimSymbol, dims) ;
+ UNPROTECT(2); /* out, dims */
+ return out ;
+}
template <typename value_type, typename InputIterator>
inline SEXP primitive_rowmajor_wrap__dispatch( InputIterator first, int nrow, int ncol, ::Rcpp::traits::false_type ){
@@ -804,6 +889,7 @@
*/
template <typename T>
inline SEXP wrap(const T& object){
+ RCPP_DEBUG_1( "inline SEXP wrap<%s>(const T& object)", DEMANGLE(T) )
return internal::wrap_dispatch( object, typename ::Rcpp::traits::wrap_type_traits<T>::wrap_category() ) ;
}
Modified: pkg/Rcpp/inst/include/Rcpp/traits/wrap_type_traits.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/wrap_type_traits.h 2013-02-02 20:06:04 UTC (rev 4233)
+++ pkg/Rcpp/inst/include/Rcpp/traits/wrap_type_traits.h 2013-02-03 11:19:09 UTC (rev 4234)
@@ -3,7 +3,7 @@
//
// wrap_type_traits.h: Rcpp R/C++ interface class library -- traits to help wrap
//
-// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -74,8 +74,10 @@
template <> struct wrap_type_traits<unsigned int> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<bool> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<std::string> { typedef wrap_type_primitive_tag wrap_category; } ;
+template <> struct wrap_type_traits<std::wstring> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<Rcpp::String> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<char> { typedef wrap_type_primitive_tag wrap_category; } ;
+template <> struct wrap_type_traits<wchar_t> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits<float> { typedef wrap_type_primitive_tag wrap_category; } ;
template <> struct wrap_type_traits< std::complex<float> > { typedef wrap_type_primitive_tag wrap_category; } ;
Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h 2013-02-02 20:06:04 UTC (rev 4233)
+++ pkg/Rcpp/inst/include/RcppCommon.h 2013-02-03 11:19:09 UTC (rev 4234)
@@ -3,7 +3,7 @@
// RcppCommon.h: Rcpp R/C++ interface class library -- common include and defines statements
//
// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
-// Copyright (C) 2009 - 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2009 - 2013 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -30,6 +30,7 @@
#include <Rcpp/macros/macros.h>
// include R headers, but set R_NO_REMAP and access everything via Rf_ prefixes
+#define MAXELTSIZE 8192
#define R_NO_REMAP
#include <R.h>
#include <Rinternals.h>
@@ -89,6 +90,8 @@
class String ;
namespace internal{
template <typename Class> SEXP make_new_object( Class* ptr ) ;
+
+ SEXP charsexp_from_wstring( const std::wstring& s) ;
}
}
Modified: pkg/Rcpp/src/api.cpp
===================================================================
--- pkg/Rcpp/src/api.cpp 2013-02-02 20:06:04 UTC (rev 4233)
+++ pkg/Rcpp/src/api.cpp 2013-02-03 11:19:09 UTC (rev 4234)
@@ -3,7 +3,7 @@
//
// api.cpp: Rcpp R/C++ interface class library -- Rcpp api
//
-// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2012 - 2013 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
@@ -1654,6 +1654,11 @@
return buff ;
}
+SEXP charsexp_from_wstring( const std::wstring& s){
+ static char buffer[MAXELTSIZE];
+ wcstombs( buffer, s.data(), MAXELTSIZE ) ;
+ return Rf_mkChar(buffer) ;
+}
// static const char* dropTrailing0(char *s, char cdec) {
// /* Note that 's' is modified */
More information about the Rcpp-commits
mailing list