-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathVector.h
More file actions
495 lines (452 loc) · 19 KB
/
Vector.h
File metadata and controls
495 lines (452 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#ifndef Rcpp_api_meat_Vector_h
#define Rcpp_api_meat_Vector_h
namespace Rcpp{
template <int RTYPE, template <class> class StoragePolicy>
template <bool NA, typename VEC>
Vector<RTYPE,StoragePolicy>::Vector( const VectorBase<RTYPE,NA,VEC>& other ) {
RCPP_DEBUG_CTOR( Vector, "( const VectorBase<%d,%s,VEC>& ) [VEC = %s]", RTYPE, (NA?"true":"false"), DEMANGLE(VEC) )
import_sugar_expression( other, typename std::is_same<Vector,VEC>::type() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
template <typename T>
Vector<RTYPE,StoragePolicy>& Vector<RTYPE,StoragePolicy>::operator=( const T &x ){
assign_object( x, typename traits::is_vector_expression<T>::type() ) ;
return *this ;
}
template <int RTYPE, template <class> class StoragePolicy>
template <bool NA, typename T>
inline void Vector<RTYPE,StoragePolicy>::assign_sugar_expression( const VectorBase<RTYPE,NA,T>& x ){
int n = size() ;
if( n == x.size() ){
// just copy the data
import_expression<T>(x.get_ref(), n ) ;
} else{
// different size, so we change the memory
Vector<RTYPE,StoragePolicy> tmp(x) ;
Storage::set__( tmp );
}
}
// sugar
template <int RTYPE, template <class> class StoragePolicy>
template <typename T>
inline void Vector<RTYPE,StoragePolicy>::assign_object( const T& x, std::true_type ){
assign_sugar_expression( x ) ;
}
// anything else
template <int RTYPE, template <class> class StoragePolicy>
template <typename T>
inline void Vector<RTYPE,StoragePolicy>::assign_object( const T& x, std::false_type ){
Storage::set__( r_cast<RTYPE>( wrap(x) ) ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
template <bool NA, typename VEC>
inline void Vector<RTYPE,StoragePolicy>::import_sugar_expression( const Rcpp::VectorBase<RTYPE,NA,VEC>& other, std::false_type ){
RCPP_DEBUG_CLASS( Vector, "::import_sugar_expression( VectorBase<%d,%s,%s>, false_type )", RTYPE, (NA?"true":"false"), RTYPE, DEMANGLE(VEC) ) ;
int n = other.size() ;
Storage::set__( Rf_allocVector( RTYPE, n ) ) ;
import_expression<VEC>( other.get_ref() , n ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
template <bool NA, typename VEC>
inline void Vector<RTYPE,StoragePolicy>::import_sugar_expression( const Rcpp::VectorBase<RTYPE,NA,VEC>& other, std::true_type ){
RCPP_DEBUG_CLASS( Vector, "::import_sugar_expression( VectorBase<%d,%s,%s>, true_type )", RTYPE, (NA?"true":"false"), RTYPE, DEMANGLE(VEC) ) ;
Storage::set__( other.get_ref() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
template <typename T>
inline void Vector<RTYPE,StoragePolicy>::import_expression( const T& other, int n ){
iterator start = begin() ;
for( int i=0; i<n; i++){
start[i] = other[i] ;
}
}
template <int RTYPE, template <class> class StoragePolicy>
typename Vector<RTYPE,StoragePolicy>::iterator Vector<RTYPE,StoragePolicy>::erase_single__impl( iterator position ){
if( position < begin() || position > end() ) throw index_out_of_bounds( ) ;
int n = size() ;
Vector target( n - 1 ) ;
iterator target_it(target.begin()) ;
iterator it(begin()) ;
iterator this_end(end()) ;
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
if( names == R_NilValue ){
int i=0;
for( ; it < position; ++it, ++target_it, i++){
*target_it = *it;
}
++it ;
for( ; it < this_end ; ++it, ++target_it){
*target_it = *it;
}
Storage::set__( target.get__() ) ;
return begin()+i ;
} else {
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n-1 );
int i= 0 ;
for( ; it < position; ++it, ++target_it,i++){
*target_it = *it;
SET_STRING_ELT( newnames, i , STRING_ELT(names,i) ) ;
}
int result=i ;
++it ;
i++ ;
for( ; it < this_end ; ++it, ++target_it, i++){
*target_it = *it;
SET_STRING_ELT( newnames, i-1, STRING_ELT(names,i) ) ;
}
target.attr( "names" ) = newnames ;
Storage::set__( target.get__() ) ;
return begin()+result ;
}
}
template <int RTYPE, template <class> class StoragePolicy>
typename Vector<RTYPE,StoragePolicy>::iterator Vector<RTYPE,StoragePolicy>::erase_range__impl( iterator first, iterator last ){
if( first > last ) throw std::range_error("invalid range") ;
if( last > end() || first < begin() ) throw index_out_of_bounds() ;
iterator it = begin() ;
iterator this_end = end() ;
int nremoved = std::distance(first,last) ;
int target_size = size() - nremoved ;
Vector target( target_size ) ;
iterator target_it = target.begin() ;
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
iterator result ;
if( names == R_NilValue ){
int i=0;
for( ; it < first; ++it, ++target_it, i++ ){
*target_it = *it ;
}
result = begin() + i + 1 ;
for( it = last ; it < this_end; ++it, ++target_it ){
*target_it = *it ;
}
} else{
Shield<SEXP> newnames = ::Rf_allocVector(STRSXP, target_size);
int i= 0 ;
for( ; it < first; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) );
}
result = begin() + i + 1 ;
for( it = last ; it < this_end; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i + nremoved ) );
}
target.attr("names" ) = newnames ;
}
Storage::set__( target.get__() );
return result ;
}
template <int RTYPE, template <class> class StoragePolicy>
void Vector<RTYPE,StoragePolicy>::push_back__impl(const stored_type& object, std::false_type){
int n = size() ;
Vector target( n + 1 ) ;
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
iterator target_it( target.begin() ) ;
iterator it(begin()) ;
iterator this_end(end());
if( names == R_NilValue ){
for( ; it < this_end; ++it, ++target_it ){
*target_it = *it ;
}
} else {
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n + 1) ;
int i = 0 ;
for( ; it < this_end; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
}
SET_STRING_ELT( newnames, i, Rf_mkChar("") ) ;
target.attr("names") = newnames ;
}
*target_it = object;
Storage::set__( target.get__() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
void Vector<RTYPE,StoragePolicy>::push_back__impl(const stored_type& object, std::true_type){
Shield<SEXP> object_sexp = object ;
int n = size() ;
Vector target( n + 1 ) ;
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
iterator target_it( target.begin() ) ;
iterator it(begin()) ;
iterator this_end(end());
if( names == R_NilValue ){
for( ; it < this_end; ++it, ++target_it ){
*target_it = *it ;
}
} else {
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n + 1) ;
int i = 0 ;
for( ; it < this_end; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
}
SET_STRING_ELT( newnames, i, Rf_mkChar("") ) ;
target.attr("names") = newnames ;
}
*target_it = object_sexp;
Storage::set__( target.get__() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
void Vector<RTYPE,StoragePolicy>::push_back_name__impl(const stored_type& object, const std::string& name, std::false_type ){
int n = size() ;
Vector target( n + 1 ) ;
iterator target_it( target.begin() ) ;
iterator it(begin()) ;
iterator this_end(end());
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n+1 ) ;
int i=0;
if( names == R_NilValue ){
SEXP dummy = Rf_mkChar("") ;
for( ; it < this_end; ++it, ++target_it,i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i , dummy );
}
} else {
for( ; it < this_end; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
}
}
SET_STRING_ELT( newnames, i, Rf_mkChar( name.c_str() ) );
target.attr("names") = newnames ;
*target_it = object;
Storage::set__( target.get__() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
void Vector<RTYPE,StoragePolicy>::push_back_name__impl(const stored_type& object, const std::string& name, std::true_type ){
Shield<SEXP> object_sexp = object ;
int n = size() ;
Vector target( n + 1 ) ;
iterator target_it( target.begin() ) ;
iterator it(begin()) ;
iterator this_end(end());
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n+1 ) ;
int i=0;
if( names == R_NilValue ){
SEXP dummy = Rf_mkChar("") ;
for( ; it < this_end; ++it, ++target_it,i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i , dummy );
}
} else {
for( ; it < this_end; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
}
}
SET_STRING_ELT( newnames, i, Rf_mkChar( name.c_str() ) );
target.attr("names") = newnames ;
*target_it = object_sexp;
Storage::set__( target.get__() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
void Vector<RTYPE,StoragePolicy>::push_front__impl(const stored_type& object, std::false_type ){
int n = size() ;
Vector target( n+1);
iterator target_it(target.begin());
iterator it(begin());
iterator this_end(end());
*target_it = object ;
++target_it ;
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
if( names == R_NilValue ){
for( ; it<this_end; ++it, ++target_it){
*target_it = *it ;
}
} else{
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n + 1);
int i=1 ;
SET_STRING_ELT( newnames, 0, Rf_mkChar("") ) ;
for( ; it<this_end; ++it, ++target_it, i++){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
}
target.attr("names") = newnames ;
}
Storage::set__( target.get__() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
void Vector<RTYPE,StoragePolicy>::push_front__impl(const stored_type& object, std::true_type ){
Shield<SEXP> object_sexp = object ;
int n = size() ;
Vector target( n+1);
iterator target_it(target.begin());
iterator it(begin());
iterator this_end(end());
*target_it = object_sexp ;
++target_it ;
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
if( names == R_NilValue ){
for( ; it<this_end; ++it, ++target_it){
*target_it = *it ;
}
} else{
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n + 1);
int i=1 ;
SET_STRING_ELT( newnames, 0, Rf_mkChar("") ) ;
for( ; it<this_end; ++it, ++target_it, i++){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
}
target.attr("names") = newnames ;
}
Storage::set__( target.get__() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
void Vector<RTYPE,StoragePolicy>::push_front_name__impl(const stored_type& object, const std::string& name, std::false_type ){
int n = size() ;
Vector target( n + 1 ) ;
iterator target_it( target.begin() ) ;
iterator it(begin()) ;
iterator this_end(end());
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n+1 ) ;
int i=1;
SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
*target_it = object;
++target_it ;
if( names == R_NilValue ){
Shield<SEXP> dummy = Rf_mkChar("");
for( ; it < this_end; ++it, ++target_it,i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i , dummy );
}
} else {
for( ; it < this_end; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
}
}
target.attr("names") = newnames ;
Storage::set__( target.get__() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
void Vector<RTYPE,StoragePolicy>::push_front_name__impl(const stored_type& object, const std::string& name, std::true_type ){
Shield<SEXP> object_sexp = object ;
int n = size() ;
Vector target( n + 1 ) ;
iterator target_it( target.begin() ) ;
iterator it(begin()) ;
iterator this_end(end());
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n+1 ) ;
int i=1;
SET_STRING_ELT( newnames, 0, Rf_mkChar( name.c_str() ) );
*target_it = object_sexp;
++target_it ;
if( names == R_NilValue ){
Shield<SEXP> dummy = Rf_mkChar("");
for( ; it < this_end; ++it, ++target_it,i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i , dummy );
}
} else {
for( ; it < this_end; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i-1 ) ) ;
}
}
target.attr("names") = newnames ;
Storage::set__( target.get__() ) ;
}
template <int RTYPE, template <class> class StoragePolicy>
template <typename T>
typename Vector<RTYPE,StoragePolicy>::iterator Vector<RTYPE,StoragePolicy>::insert( iterator position, const T& object){
int n = size() ;
Vector target( n+1 ) ;
iterator target_it = target.begin();
iterator it = begin() ;
iterator this_end = end() ;
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
iterator result ;
if( names == R_NilValue ){
for( ; it < position; ++it, ++target_it){
*target_it = *it ;
}
result = target_it;
*target_it = converter_type::get( object ) ;
++target_it ;
for( ; it < this_end; ++it, ++target_it ){
*target_it = *it ;
}
} else{
Shield<SEXP> newnames = ::Rf_allocVector( STRSXP, n + 1 ) ;
int i=0;
for( ; it < position; ++it, ++target_it, i++){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i ) ) ;
}
result = target_it;
*target_it = converter_type::get(object) ;
SET_STRING_ELT( newnames, i, Rf_mkChar( internal::get_object_name(object) ) ) ;
i++ ;
++target_it ;
for( ; it < this_end; ++it, ++target_it, i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i, STRING_ELT(names, i - 1) ) ;
}
target.attr( "names" ) = newnames ;
}
Storage::set__( target.get__() );
return result ;
}
template <int RTYPE, template <class> class StoragePolicy>
template <typename EXPR_VEC>
Vector<RTYPE, StoragePolicy>& Vector<RTYPE,StoragePolicy>::operator+=( const VectorBase<RTYPE,true,EXPR_VEC>& rhs ){
const EXPR_VEC& ref = rhs.get_ref() ;
iterator start = begin() ;
int n = size() ;
stored_type tmp ;
for( int i=0; i<n; i++){
Proxy left = start[i] ;
if( ! traits::is_na<RTYPE>( left ) ){
tmp = ref[i] ;
left = traits::is_na<RTYPE>( tmp ) ? tmp : ( left + tmp ) ;
}
}
return *this ;
}
template <int RTYPE, template <class> class StoragePolicy>
template <typename EXPR_VEC>
Vector<RTYPE, StoragePolicy>& Vector<RTYPE,StoragePolicy>::operator+=( const VectorBase<RTYPE,false,EXPR_VEC>& rhs ){
const EXPR_VEC& ref = rhs.get_ref() ;
iterator start = begin() ;
int n = size() ;
stored_type tmp ;
for( int i=0; i<n; i++){
if( ! traits::is_na<RTYPE>(start[i]) ){
start[i] += ref[i] ;
}
}
return *this ;
}
namespace internal {
template <typename T>
inline SEXP wrap_range_sugar_expression( const T& object) {
const int RTYPE = T::r_type::value ;
return Rcpp::Vector<RTYPE, PreserveStorage>(object) ;
}
template <template <class> class StoragePolicy>
inline SEXP eval_methods<EXPRSXP, StoragePolicy>::eval(){
SEXP xp = ( static_cast<Vector<EXPRSXP,StoragePolicy>&>(*this) ).get__() ;
SEXP evalSym = Rf_install( "eval" );
return Rcpp_eval( Rf_lang2( evalSym, xp ) ) ;
}
template <template <class> class StoragePolicy>
inline SEXP eval_methods<EXPRSXP,StoragePolicy>::eval( SEXP env ){
SEXP xp = ( static_cast<Vector<EXPRSXP,StoragePolicy>&>(*this) ).get__() ;
SEXP evalSym = Rf_install( "eval" );
return Rcpp_eval( Rf_lang3( evalSym, xp, env ) ) ;
}
}
template <int RTYPE, template <class> class StoragePolicy>
template <typename... Args>
Vector<RTYPE, StoragePolicy> Vector<RTYPE,StoragePolicy>::create(Args... args){
return typename create_type<RTYPE, Args...>::type( args... ) ;
}
} // namespace Rcpp
#endif