Skip to content

Commit 4291b5c

Browse files
committed
Add a free function 'c' for concatenating; extend concat to handle SEXPs
1 parent 080c4f6 commit 4291b5c

5 files changed

Lines changed: 191 additions & 113 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
src/*.o
22
src/*.so
33

4+
.Rproj*
5+
.Rhistory
6+
Rcpp11.Rproj

inst/include/Rcpp/Vector.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@
3838
#include <Rcpp/String.h>
3939
#include <Rcpp/NA_Proxy.h>
4040

41+
#include <Rcpp/vector/concat_free.h>
42+
4143
#include <Rcpp/vector/LazyVector.h>
4244
#include <Rcpp/vector/swap.h>
4345
#include <Rcpp/vector/Demangler.h>
4446

45-
#endif
47+
#endif

inst/include/Rcpp/vector/Vector.h

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
#include <Rcpp/vector/concat.h>
55

66
namespace Rcpp{
7-
7+
88
template <bool NA,typename T> class SingleLogicalResult ;
99

1010
template <int RTYPE, template <class> class StoragePolicy>
1111
class Vector :
12-
public StoragePolicy<Vector<RTYPE,StoragePolicy>>,
13-
public SlotProxyPolicy<Vector<RTYPE,StoragePolicy>>,
14-
public AttributeProxyPolicy<Vector<RTYPE,StoragePolicy>>,
15-
public VectorBase< RTYPE, true, Vector<RTYPE,StoragePolicy> >,
16-
public internal::eval_methods<RTYPE, StoragePolicy >,
17-
public NamesProxyPolicy<Vector<RTYPE, StoragePolicy>>,
12+
public StoragePolicy<Vector<RTYPE,StoragePolicy>>,
13+
public SlotProxyPolicy<Vector<RTYPE,StoragePolicy>>,
14+
public AttributeProxyPolicy<Vector<RTYPE,StoragePolicy>>,
15+
public VectorBase< RTYPE, true, Vector<RTYPE,StoragePolicy> >,
16+
public internal::eval_methods<RTYPE, StoragePolicy >,
17+
public NamesProxyPolicy<Vector<RTYPE, StoragePolicy>>,
1818
public AttributesProxyPolicy<Vector<RTYPE, StoragePolicy>>
1919
{
2020
public:
@@ -29,15 +29,15 @@ class Vector :
2929
typedef typename traits::init_type<RTYPE>::type init_type ;
3030
typedef typename traits::r_vector_element_converter<RTYPE>::type converter_type ;
3131
typedef typename traits::storage_type<RTYPE>::type stored_type ;
32-
33-
/**
32+
33+
/**
3434
* Default constructor. Creates a vector of the appropriate type
3535
* and 0 length
3636
*/
3737
Vector() ;
38-
38+
3939
RCPP_GENERATE_CTOR_ASSIGN(Vector)
40-
40+
4141
// we can't define these 3 in meat for some reason
4242
// maybe because of the typedef in instantiation.h
4343
Vector( SEXP x ) {
@@ -49,15 +49,15 @@ class Vector :
4949
fill( u ) ;
5050
}
5151
Vector( const int& size ) ;
52-
52+
5353
Vector( const Dimension& dims) ;
54-
55-
template <typename U>
54+
55+
template <typename U>
5656
Vector( const Dimension& dims, const U& u) ;
57-
58-
template <typename U>
57+
58+
template <typename U>
5959
Vector( const int& size, const U& u) ;
60-
60+
6161
Vector( const std::string& st ) {
6262
RCPP_DEBUG_CTOR(Vector, "( const std::string& = %s )", st.c_str() )
6363
Storage::set__( internal::vector_from_string<RTYPE>(st) ) ;
@@ -66,33 +66,33 @@ class Vector :
6666
RCPP_DEBUG_CTOR(Vector, "( const char* = %s )", st )
6767
Storage::set__(internal::vector_from_string<RTYPE>(st));
6868
}
69-
70-
template <bool NA, typename VEC>
69+
70+
template <bool NA, typename VEC>
7171
Vector( const VectorBase<RTYPE,NA,VEC>& other ) ;
72-
73-
template <bool NA, typename T>
72+
73+
template <bool NA, typename T>
7474
Vector( const sugar::SingleLogicalResult<NA,T>& obj ) ;
75-
75+
7676
Vector( std::initializer_list<init_type> list ) {
7777
Storage::set__( r_cast<RTYPE>( wrap( list.begin(), list.end() ) ) );
7878
}
7979

80-
template <typename T> Vector&
80+
template <typename T> Vector&
8181
operator=( const T& x) ;
82-
82+
8383
static inline stored_type get_na() { return traits::get_na<RTYPE>(); }
8484
static inline bool is_na( stored_type x){ return traits::is_na<RTYPE>(x); }
85-
85+
8686
/**
8787
* the length of the vector, uses Rf_length
8888
*/
8989
inline R_len_t length() const { return ::Rf_length( Storage::get__() ) ; }
90-
90+
9191
/**
9292
* alias of length
9393
*/
9494
inline R_len_t size() const { return ::Rf_length( Storage::get__() ) ; }
95-
95+
9696
/**
9797
* one dimensional offset doing bounds checking to ensure
9898
* it is valid
@@ -101,14 +101,14 @@ class Vector :
101101
if( static_cast<R_len_t>(i) >= size() ) throw index_out_of_bounds() ;
102102
return i ;
103103
}
104-
104+
105105
R_len_t offset(const std::string& name) const {
106106
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
107-
if( names == R_NilValue ) throw index_out_of_bounds();
107+
if( names == R_NilValue ) throw index_out_of_bounds();
108108
int n = size() ;
109-
109+
110110
SEXP* data = internal::r_vector_start<STRSXP>(names) ;
111-
int index = std::find( data, data+n, Rf_mkChar(name.c_str()) ) - data ;
111+
int index = std::find( data, data+n, Rf_mkChar(name.c_str()) ) - data ;
112112
if( index == n ) throw index_out_of_bounds() ;
113113
return index ;
114114
}
@@ -122,13 +122,13 @@ class Vector :
122122
inline iterator end() { return cache.get() + size() ; }
123123
inline const_iterator begin() const{ return cache.get_const() ; }
124124
inline const_iterator end() const{ return cache.get_const() + size() ; }
125-
125+
126126
inline Proxy operator[]( int i ){ return cache.ref(i) ; }
127127
inline const_Proxy operator[]( int i ) const { return cache.ref(i) ; }
128-
128+
129129
inline Proxy at( int i ){ return cache.ref(i) ; }
130130
inline const_Proxy at( int i ) const { return cache.ref(i) ; }
131-
131+
132132
inline NameProxy operator[]( const std::string& name ){
133133
return NameProxy( *this, name ) ;
134134
}
@@ -141,90 +141,90 @@ class Vector :
141141
inline NameProxy operator[]( const std::string& name ) const {
142142
return NameProxy( const_cast<Vector&>(*this), name ) ;
143143
}
144-
144+
145145
Vector& sort(){
146-
std::sort(
147-
internal::r_vector_start<RTYPE>(Storage::get__()),
148-
internal::r_vector_start<RTYPE>(Storage::get__()) + size(),
146+
std::sort(
147+
internal::r_vector_start<RTYPE>(Storage::get__()),
148+
internal::r_vector_start<RTYPE>(Storage::get__()) + size(),
149149
typename traits::comparator_type<RTYPE>::type()
150150
) ;
151151
return *this ;
152152
}
153153

154154
template <typename T>
155155
void push_back( const T& object){
156-
push_back__impl( converter_type::get(object),
156+
push_back__impl( converter_type::get(object),
157157
typename std::is_same<stored_type,SEXP>()
158158
) ;
159159
}
160-
160+
161161
template <typename T>
162162
void push_back( const T& object, const std::string& name ){
163-
push_back_name__impl( converter_type::get(object), name,
163+
push_back_name__impl( converter_type::get(object), name,
164164
typename std::is_same<stored_type,SEXP>()
165165
) ;
166166
}
167-
167+
168168
template <typename T>
169169
void push_front( const T& object){
170-
push_front__impl( converter_type::get(object),
170+
push_front__impl( converter_type::get(object),
171171
typename std::is_same<stored_type,SEXP>() ) ;
172172
}
173-
173+
174174
template <typename T>
175175
void push_front( const T& object, const std::string& name){
176-
push_front_name__impl( converter_type::get(object), name,
176+
push_front_name__impl( converter_type::get(object), name,
177177
typename std::is_same<stored_type,SEXP>() ) ;
178178
}
179-
179+
180180
public:
181-
181+
182182
template <typename T>
183-
iterator insert( iterator position, const T& object) ;
184-
183+
iterator insert( iterator position, const T& object) ;
184+
185185
template <typename T>
186186
iterator insert( int position, const T& object){
187-
return insert(
188-
cache.get() + position,
187+
return insert(
188+
cache.get() + position,
189189
object
190-
);
190+
);
191191
}
192-
192+
193193
iterator erase( int position){
194194
return erase_single__impl( cache.get() + position) ;
195195
}
196-
196+
197197
iterator erase( iterator position){
198198
return erase_single__impl( position ) ;
199199
}
200-
200+
201201
iterator erase( int first, int last){
202202
iterator start = cache.get() ;
203203
return erase_range__impl( start + first, start + last ) ;
204204
}
205-
205+
206206
iterator erase( iterator first, iterator last){
207207
return erase_range__impl( first, last ) ;
208208
}
209-
209+
210210
void update(SEXP /* x */){
211211
cache.update(*this) ;
212212
}
213-
213+
214214
template <typename EXPR_VEC>
215215
Vector& operator+=( const VectorBase<RTYPE,true,EXPR_VEC>& rhs ) ;
216-
216+
217217
template <typename EXPR_VEC>
218218
Vector& operator+=( const VectorBase<RTYPE,false,EXPR_VEC>& rhs ) ;
219-
220-
/**
221-
* Does this vector have an element with the target name
219+
220+
/**
221+
* Does this vector have an element with the target name
222222
*/
223223
bool containsElementNamed( const char* target ) const ;
224-
224+
225225

226226
protected:
227-
227+
228228
// TODO: REMOVE
229229
inline int* dims() const {
230230
if( !::Rf_isMatrix(Storage::get__()) ) throw not_a_matrix() ;
@@ -236,64 +236,64 @@ class Vector :
236236
}
237237

238238
private:
239-
240-
void push_back__impl(const stored_type& object, std::true_type ) ;
241-
void push_back__impl(const stored_type& object, std::false_type ) ;
242-
239+
240+
void push_back__impl(const stored_type& object, std::true_type ) ;
241+
void push_back__impl(const stored_type& object, std::false_type ) ;
242+
243243
void push_back_name__impl(const stored_type& object, const std::string& name, std::true_type ) ;
244244
void push_back_name__impl(const stored_type& object, const std::string& name, std::false_type ) ;
245-
245+
246246
void push_front__impl(const stored_type& object, std::true_type ) ;
247247
void push_front__impl(const stored_type& object, std::false_type ) ;
248-
249-
void push_front_name__impl(const stored_type& object, const std::string& name, std::true_type ) ;
250-
void push_front_name__impl(const stored_type& object, const std::string& name, std::false_type ) ;
251-
248+
249+
void push_front_name__impl(const stored_type& object, const std::string& name, std::true_type ) ;
250+
void push_front_name__impl(const stored_type& object, const std::string& name, std::false_type ) ;
251+
252252
iterator erase_single__impl( iterator position ) ;
253-
253+
254254
iterator erase_range__impl( iterator first, iterator last ) ;
255255

256256
template <bool NA, typename T> inline void assign_sugar_expression( const VectorBase<RTYPE,NA,T>& x ) ;
257-
257+
258258
// sugar
259259
template <typename T> inline void assign_object( const T& x, std::true_type ) ;
260-
260+
261261
// anything else
262262
template <typename T> inline void assign_object( const T& x, std::false_type ) ;
263-
263+
264264
// we are importing a real sugar expression, i.e. not a vector
265265
template <bool NA, typename VEC>
266266
inline void import_sugar_expression( const Rcpp::VectorBase<RTYPE,NA,VEC>& other, std::false_type ) ;
267-
267+
268268
// we are importing a sugar expression that actually is a vector
269269
template <bool NA, typename VEC>
270270
inline void import_sugar_expression( const Rcpp::VectorBase<RTYPE,NA,VEC>& other, std::true_type ) ;
271-
271+
272272
template <typename T>
273273
inline void import_expression( const T& other, int n ) ;
274-
274+
275275
template <typename U>
276276
void fill__dispatch( std::false_type, const U& u){
277277
// when this is not trivial, this is SEXP
278-
Shield<SEXP> elem = converter_type::get( u );
278+
Shield<SEXP> elem = converter_type::get( u );
279279
iterator it(begin());
280280
for( int i=0; i<size() ; i++, ++it){
281281
*it = ::Rf_duplicate( elem ) ;
282282
}
283283
}
284-
284+
285285
template <typename U>
286286
void fill__dispatch( std::true_type, const U& u){
287287
std::fill( begin(), end(), converter_type::get( u ) ) ;
288288
}
289289

290290
public:
291291
template <typename... Args> static Vector create(Args... args) ;
292-
292+
293293
template <typename... Args> static Vector concat(Args... args){
294-
return concatenate<RTYPE,Args...>(args...) ;
294+
return concatenate<RTYPE,Args...>(args...) ;
295295
}
296-
296+
297297
} ; /* Vector */
298298

299299
}

0 commit comments

Comments
 (0)