Skip to content

Commit 4b5a7df

Browse files
move DataFrame to headers with Storage policy
1 parent c501e5d commit 4b5a7df

5 files changed

Lines changed: 100 additions & 83 deletions

File tree

inst/include/Rcpp/DataFrame.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,31 @@
2020

2121
namespace Rcpp{
2222

23-
class DataFrame : public List {
24-
public:
25-
DataFrame() ;
26-
DataFrame(SEXP x) ;
27-
DataFrame( const DataFrame& other) ;
23+
template < template <class> class StoragePolicy>
24+
class DataFrame_Impl : public Vector_Impl<VECSXP, StoragePolicy> {
25+
public:
26+
using Storage = typename Vector_Impl<VECSXP, StoragePolicy>::Storage ;
27+
DataFrame_Impl() ;
28+
DataFrame_Impl(SEXP x) ;
29+
DataFrame_Impl( const DataFrame_Impl& other) ;
2830

29-
template <typename T> DataFrame( const T& obj) ;
31+
template <typename T> DataFrame_Impl( const T& obj) ;
3032

31-
DataFrame& operator=( DataFrame& other) ;
32-
DataFrame& operator=( SEXP x) ;
33+
DataFrame_Impl& operator=( DataFrame_Impl& other) ;
34+
DataFrame_Impl& operator=( SEXP x) ;
3335

34-
~DataFrame() ;
36+
~DataFrame_Impl() ;
3537

3638
int nrows() const ;
3739

3840
template <typename... Args>
39-
static DataFrame create(const Args&... args){
41+
static DataFrame_Impl create(const Args&... args){
4042
return from_list( List::create( args...) ) ;
4143
}
4244

4345
private:
4446
void set_sexp(SEXP x) ;
45-
static DataFrame from_list( Rcpp::List ) ;
47+
static DataFrame_Impl from_list( Rcpp::List ) ;
4648

4749
} ;
4850

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

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,91 @@
2020

2121
namespace Rcpp{
2222

23+
namespace internal{
24+
inline SEXP empty_data_frame(){
25+
SEXP dataFrameSym = ::Rf_install( "data.frame");
26+
return ::Rf_eval( ::Rf_lang1( dataFrameSym ), R_GlobalEnv ) ;
27+
}
28+
}
29+
30+
template <template <class> class StoragePolicy>
2331
template <typename T>
24-
DataFrame::DataFrame( const T& obj) : DataFrame( wrap(obj) ) {}
32+
DataFrame_Impl<StoragePolicy>::DataFrame_Impl( const T& obj) : DataFrame_Impl( wrap(obj) ) {}
33+
34+
template <template <class> class StoragePolicy>
35+
DataFrame_Impl<StoragePolicy>::DataFrame_Impl(): List( internal::empty_data_frame() ){}
36+
37+
template <template <class> class StoragePolicy>
38+
DataFrame_Impl<StoragePolicy>::DataFrame_Impl(SEXP x) {
39+
set_sexp(x) ;
40+
}
41+
42+
template <template <class> class StoragePolicy>
43+
DataFrame_Impl<StoragePolicy>::DataFrame_Impl( const DataFrame_Impl& other): List(other.get__()) {}
44+
45+
template <template <class> class StoragePolicy>
46+
DataFrame_Impl<StoragePolicy>& DataFrame_Impl<StoragePolicy>::operator=( DataFrame_Impl& other) {
47+
Storage::set__( other.get__() ) ;
48+
return *this ;
49+
}
50+
51+
template <template <class> class StoragePolicy>
52+
DataFrame_Impl<StoragePolicy>& DataFrame_Impl<StoragePolicy>::operator=( SEXP x) {
53+
Storage::set__(x) ;
54+
return *this ;
55+
}
56+
57+
template <template <class> class StoragePolicy>
58+
DataFrame_Impl<StoragePolicy>::~DataFrame_Impl(){}
59+
60+
template <template <class> class StoragePolicy>
61+
void DataFrame_Impl<StoragePolicy>::set_sexp(SEXP x) {
62+
if( ::Rf_inherits( x, "data.frame" )){
63+
Storage::set__( x ) ;
64+
} else{
65+
SEXP y = internal::convert_using_rfunction( x, "as.data.frame" ) ;
66+
Storage::set__( y ) ;
67+
x = y ;
68+
}
69+
}
70+
template <template <class> class StoragePolicy>
71+
int DataFrame_Impl<StoragePolicy>::nrows() const {
72+
return Rf_length( VECTOR_ELT(Storage::get__(), 0) );
73+
}
74+
75+
template <template <class> class StoragePolicy>
76+
DataFrame_Impl<StoragePolicy> DataFrame_Impl<StoragePolicy>::from_list( List obj ){
77+
bool use_default_strings_as_factors = true ;
78+
bool strings_as_factors = true ;
79+
int strings_as_factors_index = -1 ;
80+
int n = obj.size() ;
81+
CharacterVector names = obj.attr( "names" ) ;
82+
if( !names.isNULL() ){
83+
for( int i=0; i<n; i++){
84+
if( names[i] == "stringsAsFactors" ){
85+
strings_as_factors_index = i ;
86+
use_default_strings_as_factors = false ;
87+
if( !as<bool>(obj[i]) ) strings_as_factors = false ;
88+
break ;
89+
}
90+
}
91+
}
92+
if( use_default_strings_as_factors )
93+
return DataFrame(obj) ;
94+
SEXP as_df_symb = Rf_install("as.data.frame");
95+
SEXP strings_as_factors_symb = Rf_install("stringsAsFactors");
96+
97+
obj.erase(strings_as_factors_index) ;
98+
names.erase(strings_as_factors_index) ;
99+
obj.attr( "names") = names ;
100+
Scoped<SEXP> call = Rf_lang3(as_df_symb, obj, wrap( strings_as_factors ) ) ;
101+
SET_TAG( CDDR(call), strings_as_factors_symb ) ;
102+
Scoped<SEXP> res = Rcpp_eval( call ) ;
103+
DataFrame out( res ) ;
104+
return out ;
105+
}
106+
107+
25108

26109
} // namespace Rcpp
27110

inst/include/Rcpp/vector/Vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ class Vector_Impl :
3131
public VectorBase< RTYPE, true, Vector_Impl<RTYPE,StoragePolicy> >,
3232
public internal::eval_methods<RTYPE>
3333
{
34+
public:
3435
typename traits::r_vector_cache_type<RTYPE>::type cache ;
3536

3637
using Storage = RObjectStorage<Vector_Impl> ;
3738
using AttributeProxy_ = AttributeProxyPolicy<Vector_Impl> ;
3839

39-
public:
4040
typedef typename traits::r_vector_proxy<RTYPE>::type Proxy ;
4141
typedef typename traits::r_vector_const_proxy<RTYPE>::type const_Proxy ;
4242
typedef typename traits::r_vector_name_proxy<RTYPE>::type NameProxy ;

inst/include/RcppCommon.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ namespace Rcpp{
115115
template < template <class> class StoragePolicy > class Symbol_Impl ;
116116
using Symbol = Symbol_Impl<NoProtectStorage> ;
117117

118-
class DataFrame ;
118+
template < template <class> class StoragePolicy > class DataFrame_Impl ;
119+
using DataFrame = DataFrame_Impl<RObjectStorage> ;
119120

120121
}
121122
namespace Rcpp{

src/api.cpp

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <Rcpp.h>
2323
#include "internal.h"
2424

25-
// Rcpp api classes
2625
namespace Rcpp {
2726

2827
// Evaluator
@@ -63,74 +62,6 @@ namespace Rcpp {
6362
return res ;
6463
}
6564

66-
// DataFrame
67-
namespace internal{
68-
inline SEXP empty_data_frame(){
69-
SEXP dataFrameSym = ::Rf_install( "data.frame"); // cannot be gc()ed once in symbol table
70-
return ::Rf_eval( ::Rf_lang1( dataFrameSym ), R_GlobalEnv ) ;
71-
}
72-
}
73-
74-
DataFrame::DataFrame(): List( internal::empty_data_frame() ){}
75-
DataFrame::DataFrame(SEXP x) {
76-
set_sexp(x) ;
77-
}
78-
DataFrame::DataFrame( const DataFrame& other): List(other.get__()) {}
79-
80-
DataFrame& DataFrame::operator=( DataFrame& other) {
81-
set__( other.get__() ) ;
82-
return *this ;
83-
}
84-
85-
DataFrame& DataFrame::operator=( SEXP x) {
86-
set__(x) ;
87-
return *this ;
88-
}
89-
DataFrame::~DataFrame(){}
90-
void DataFrame::set_sexp(SEXP x) {
91-
if( ::Rf_inherits( x, "data.frame" )){
92-
set__( x ) ;
93-
} else{
94-
SEXP y = internal::convert_using_rfunction( x, "as.data.frame" ) ;
95-
set__( y ) ;
96-
x = y ;
97-
}
98-
}
99-
int DataFrame::nrows() const { return Rf_length( VECTOR_ELT(get__(), 0) ); }
100-
101-
DataFrame DataFrame::from_list( Rcpp::List obj ){
102-
bool use_default_strings_as_factors = true ;
103-
bool strings_as_factors = true ;
104-
int strings_as_factors_index = -1 ;
105-
int n = obj.size() ;
106-
CharacterVector names = obj.attr( "names" ) ;
107-
if( !names.isNULL() ){
108-
for( int i=0; i<n; i++){
109-
if( names[i] == "stringsAsFactors" ){
110-
strings_as_factors_index = i ;
111-
use_default_strings_as_factors = false ;
112-
if( !as<bool>(obj[i]) ) strings_as_factors = false ;
113-
break ;
114-
}
115-
}
116-
}
117-
if( use_default_strings_as_factors )
118-
return DataFrame(obj) ;
119-
SEXP as_df_symb = Rf_install("as.data.frame");
120-
SEXP strings_as_factors_symb = Rf_install("stringsAsFactors");
121-
122-
obj.erase(strings_as_factors_index) ;
123-
names.erase(strings_as_factors_index) ;
124-
obj.attr( "names") = names ;
125-
Scoped<SEXP> call = Rf_lang3(as_df_symb, obj, wrap( strings_as_factors ) ) ;
126-
SET_TAG( CDDR(call), strings_as_factors_symb ) ;
127-
Scoped<SEXP> res = Rcpp_eval( call ) ;
128-
DataFrame out( res ) ;
129-
return out ;
130-
}
131-
132-
133-
13465
} // namespace Rcpp
13566

13667

0 commit comments

Comments
 (0)