-
-
Notifications
You must be signed in to change notification settings - Fork 209
/
Vector.h
1228 lines (1078 loc) · 43.7 KB
/
Vector.h
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Vector.h: Rcpp R/C++ interface class library -- vectors
//
// Copyright (C) 2010 - 2023 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
#ifndef Rcpp__vector__Vector_h
#define Rcpp__vector__Vector_h
#include <Rcpp/vector/Subsetter.h>
namespace Rcpp{
template <int RTYPE, template <class> class StoragePolicy = PreserveStorage >
class Vector :
public StoragePolicy< Vector<RTYPE,StoragePolicy> >,
public SlotProxyPolicy< Vector<RTYPE,StoragePolicy> >,
public AttributeProxyPolicy< Vector<RTYPE,StoragePolicy> >,
public NamesProxyPolicy< Vector<RTYPE, StoragePolicy> >,
public RObjectMethods< Vector<RTYPE, StoragePolicy> >,
public VectorBase< RTYPE, true, Vector<RTYPE,StoragePolicy> >
{
public:
typedef StoragePolicy<Vector> Storage ;
typename traits::r_vector_cache_type<RTYPE, StoragePolicy>::type cache ;
typedef typename traits::r_vector_proxy<RTYPE, StoragePolicy>::type Proxy ;
typedef typename traits::r_vector_const_proxy<RTYPE, StoragePolicy>::type const_Proxy ;
typedef typename traits::r_vector_name_proxy<RTYPE, StoragePolicy>::type NameProxy ;
typedef typename traits::r_vector_proxy<RTYPE, StoragePolicy>::type value_type ;
typedef typename traits::r_vector_iterator<RTYPE, StoragePolicy>::type iterator ;
typedef typename traits::r_vector_const_iterator<RTYPE, StoragePolicy>::type const_iterator ;
typedef typename traits::init_type<RTYPE>::type init_type ;
typedef typename traits::r_vector_element_converter<RTYPE>::type converter_type ;
typedef typename traits::storage_type<RTYPE>::type stored_type ;
/**
* Default constructor. Creates a vector of the appropriate type
* and 0 length
*/
Vector() {
Storage::set__( Rf_allocVector(RTYPE, 0 ) );
init() ;
}
/**
* copy constructor. shallow copy of the SEXP
*/
Vector( const Vector& other){
Storage::copy__(other) ;
}
Vector& operator=(const Vector& rhs) {
return Storage::copy__(rhs) ;
}
Vector( SEXP x ) {
Rcpp::Shield<SEXP> safe(x);
Storage::set__( r_cast<RTYPE>(safe) ) ;
}
template <typename Proxy>
Vector( const GenericProxy<Proxy>& proxy ){
Rcpp::Shield<SEXP> safe(proxy.get());
Storage::set__( r_cast<RTYPE>(safe) ) ;
}
explicit Vector( const no_init_vector& obj) {
Storage::set__( Rf_allocVector( RTYPE, obj.get() ) ) ;
}
template<typename T>
Vector( const T& size, const stored_type& u,
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
RCPP_DEBUG_2( "Vector<%d>( const T& size = %d, const stored_type& u )", RTYPE, size)
Storage::set__( Rf_allocVector( RTYPE, size) ) ;
fill( u ) ;
}
Vector( const int& size, const stored_type& u) {
RCPP_DEBUG_2( "Vector<%d>( const int& size = %d, const stored_type& u )", RTYPE, size)
Storage::set__( Rf_allocVector( RTYPE, size) ) ;
fill( u ) ;
}
// constructor for CharacterVector()
Vector( const std::string& st ){
RCPP_DEBUG_2( "Vector<%d>( const std::string& = %s )", RTYPE, st.c_str() )
Storage::set__( internal::vector_from_string<RTYPE>(st) ) ;
}
// constructor for CharacterVector()
Vector( const char* st ) {
RCPP_DEBUG_2( "Vector<%d>( const char* = %s )", RTYPE, st )
Storage::set__(internal::vector_from_string<RTYPE>(st) ) ;
}
template<typename T>
Vector( const T& siz, stored_type (*gen)(void),
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
RCPP_DEBUG_2( "Vector<%d>( const int& siz = %s, stored_type (*gen)(void) )", RTYPE, siz )
Storage::set__( Rf_allocVector( RTYPE, siz) ) ;
std::generate( begin(), end(), gen );
}
// Add template class T and then restict T to arithmetic.
template <typename T>
Vector(T size,
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
Storage::set__( Rf_allocVector( RTYPE, size) ) ;
init() ;
}
Vector( const int& size ) {
Storage::set__( Rf_allocVector( RTYPE, size) ) ;
init() ;
}
Vector( const Dimension& dims) {
Storage::set__( Rf_allocVector( RTYPE, dims.prod() ) ) ;
init() ;
if( dims.size() > 1 ){
AttributeProxyPolicy<Vector>::attr( "dim" ) = dims;
}
}
// Enable construction from bool for LogicalVectors
// SFINAE only work for template. Add template class T and then restict T to
// bool.
template <typename T>
Vector(T value,
typename Rcpp::traits::enable_if<traits::is_bool<T>::value && RTYPE == LGLSXP, void>::type* = 0) {
Storage::set__(Rf_allocVector(RTYPE, 1));
fill(value);
}
template <typename U>
Vector( const Dimension& dims, const U& u) {
RCPP_DEBUG_2( "Vector<%d>( const Dimension& (%d), const U& )", RTYPE, dims.size() )
Storage::set__( Rf_allocVector( RTYPE, dims.prod() ) ) ;
fill(u) ;
if( dims.size() > 1 ){
AttributeProxyPolicy<Vector>::attr( "dim" ) = dims;
}
}
template <bool NA, typename VEC>
Vector( const VectorBase<RTYPE,NA,VEC>& other ) {
RCPP_DEBUG_2( "Vector<%d>( const VectorBase<RTYPE,NA,VEC>& ) [VEC = %s]", RTYPE, DEMANGLE(VEC) )
import_sugar_expression( other, typename traits::same_type<Vector,VEC>::type() ) ;
}
template <typename T, typename U>
Vector( const T& size, const U& u,
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
RCPP_DEBUG_2( "Vector<%d>( const T& size, const U& u )", RTYPE, size )
Storage::set__( Rf_allocVector( RTYPE, size) ) ;
fill_or_generate( u ) ;
}
template <bool NA, typename T>
Vector( const sugar::SingleLogicalResult<NA,T>& obj ) {
Rcpp::Shield<SEXP> safe(const_cast<sugar::SingleLogicalResult<NA,T>&>(obj).get_sexp() );
Storage::set__( r_cast<RTYPE>(safe) ) ;
RCPP_DEBUG_2( "Vector<%d>( const sugar::SingleLogicalResult<NA,T>& ) [T = %s]", RTYPE, DEMANGLE(T) )
}
template <typename T, typename U1>
Vector( const T& siz, stored_type (*gen)(U1), const U1& u1,
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
Storage::set__( Rf_allocVector( RTYPE, siz) ) ;
RCPP_DEBUG_2( "const T& siz, stored_type (*gen)(U1), const U1& u1 )", RTYPE, siz )
iterator first = begin(), last = end() ;
while( first != last ) *first++ = gen(u1) ;
}
template <typename T, typename U1, typename U2>
Vector( const T& siz, stored_type (*gen)(U1,U2), const U1& u1, const U2& u2,
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
Storage::set__( Rf_allocVector( RTYPE, siz) ) ;
RCPP_DEBUG_2( "const T& siz, stored_type (*gen)(U1,U2), const U1& u1, const U2& u2)", RTYPE, siz )
iterator first = begin(), last = end() ;
while( first != last ) *first++ = gen(u1,u2) ;
}
template <typename T, typename U1, typename U2, typename U3>
Vector( const T& siz, stored_type (*gen)(U1,U2,U3), const U1& u1, const U2& u2, const U3& u3,
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
Storage::set__( Rf_allocVector( RTYPE, siz) ) ;
RCPP_DEBUG_2( "const T& siz, stored_type (*gen)(U1,U2,U3), const U1& u1, const U2& u2, const U3& u3)", RTYPE, siz )
iterator first = begin(), last = end() ;
while( first != last ) *first++ = gen(u1,u2,u3) ;
}
template <typename InputIterator>
Vector( InputIterator first, InputIterator last){
RCPP_DEBUG_1( "Vector<%d>( InputIterator first, InputIterator last", RTYPE )
Storage::set__( Rf_allocVector(RTYPE, std::distance(first, last) ) ) ;
std::copy( first, last, begin() ) ;
}
template <typename InputIterator, typename T>
Vector( InputIterator first, InputIterator last, T n,
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0) {
Storage::set__(Rf_allocVector(RTYPE, n)) ;
RCPP_DEBUG_2( "Vector<%d>( InputIterator first, InputIterator last, T n = %d)", RTYPE, n )
std::copy( first, last, begin() ) ;
}
template <typename InputIterator, typename Func>
Vector( InputIterator first, InputIterator last, Func func) {
Storage::set__( Rf_allocVector( RTYPE, std::distance(first,last) ) );
RCPP_DEBUG_1( "Vector<%d>( InputIterator, InputIterator, Func )", RTYPE )
std::transform( first, last, begin(), func) ;
}
template <typename InputIterator, typename Func, typename T>
Vector( InputIterator first, InputIterator last, Func func, T n,
typename Rcpp::traits::enable_if<traits::is_arithmetic<T>::value, void>::type* = 0){
Storage::set__( Rf_allocVector( RTYPE, n ) );
RCPP_DEBUG_2( "Vector<%d>( InputIterator, InputIterator, Func, T n = %d )", RTYPE, n )
std::transform( first, last, begin(), func) ;
}
#ifdef HAS_CXX0X_INITIALIZER_LIST
Vector( std::initializer_list<init_type> list ) {
assign( list.begin() , list.end() ) ;
}
#endif
template <typename T>
Vector& operator=( const T& x) {
assign_object( x, typename traits::is_sugar_expression<T>::type() ) ;
return *this ;
}
static inline stored_type get_na() {
return traits::get_na<RTYPE>();
}
static inline bool is_na( stored_type x){
return traits::is_na<RTYPE>(x);
}
#ifdef RCPP_COMMA_INITIALIZATION
internal::ListInitialization<iterator,init_type> operator=( init_type x){
iterator start = begin() ; *start = x;
return internal::ListInitialization<iterator,init_type>( start + 1 ) ; ;
}
#endif
/**
* the length of the vector, uses Rf_xlength
*/
inline R_xlen_t length() const {
return ::Rf_xlength( Storage::get__() ) ;
}
/**
* alias of length
*/
inline R_xlen_t size() const {
return ::Rf_xlength( Storage::get__() ) ;
}
/**
* offset based on the dimensions of this vector
*/
R_xlen_t offset(const int& i, const int& j) const {
if( !::Rf_isMatrix(Storage::get__()) ) throw not_a_matrix() ;
/* we need to extract the dimensions */
const int* dim = dims() ;
const int nrow = dim[0] ;
const int ncol = dim[1] ;
if(i < 0|| i >= nrow || j < 0 || j >= ncol ) {
const char* fmt = "Location index is out of bounds: "
"[row index=%i; row extent=%i; "
"column index=%i; column extent=%i].";
throw index_out_of_bounds(fmt, i, nrow, j, ncol);
}
return i + static_cast<R_xlen_t>(nrow)*j ;
}
/**
* one dimensional offset doing bounds checking to ensure
* it is valid
*/
R_xlen_t offset(const R_xlen_t& i) const { // #nocov start
if(i < 0 || i >= ::Rf_xlength(Storage::get__()) ) {
const char* fmt = "Index out of bounds: [index=%i; extent=%i].";
throw index_out_of_bounds(fmt, i, ::Rf_xlength(Storage::get__()) ) ;
}
return i ; // #nocov end
}
R_xlen_t offset(const std::string& name) const {
SEXP names = RCPP_GET_NAMES( Storage::get__() ) ;
if( Rf_isNull(names) ) {
throw index_out_of_bounds("Object was created without names.");
}
R_xlen_t n=size() ;
for( R_xlen_t i=0; i<n; ++i){
if( ! name.compare( CHAR(STRING_ELT(names, i)) ) ){
return i ;
}
}
const char* fmt = "Index out of bounds: [index='%s'].";
throw index_out_of_bounds(fmt, name);
return -1 ; /* -Wall */
}
template <typename U>
void fill( const U& u){
fill__dispatch( typename traits::is_trivial<RTYPE>::type(), u ) ;
}
inline iterator begin() { return cache.get() ; }
inline iterator end() { return cache.get() + static_cast<int>(size()) ; }
inline const_iterator begin() const{ return cache.get_const() ; }
inline const_iterator end() const{ return cache.get_const() + size() ; }
inline const_iterator cbegin() const{ return cache.get_const() ; }
inline const_iterator cend() const{ return cache.get_const() + size() ; }
inline Proxy operator[]( R_xlen_t i ){ return cache.ref(i) ; }
inline const_Proxy operator[]( R_xlen_t i ) const { return cache.ref(i) ; }
inline Proxy operator()( const size_t& i) {
return cache.ref( offset(i) ) ;
}
inline const_Proxy operator()( const size_t& i) const {
return cache.ref( offset(i) ) ;
}
inline Proxy at( const size_t& i) {
return cache.ref( offset(i) ) ;
}
inline const_Proxy at( const size_t& i) const { // #nocov start
return cache.ref( offset(i) ) ;
} // #nocov end
inline Proxy operator()( const size_t& i, const size_t& j) {
return cache.ref( offset(i,j) ) ;
}
inline const_Proxy operator()( const size_t& i, const size_t& j) const {
return cache.ref( offset(i,j) ) ;
}
inline NameProxy operator[]( const std::string& name ){
return NameProxy( *this, name ) ;
}
inline NameProxy operator()( const std::string& name ){
return NameProxy( *this, name ) ;
}
inline NameProxy operator[]( const std::string& name ) const {
return NameProxy( const_cast<Vector&>(*this), name ) ;
}
inline NameProxy operator()( const std::string& name ) const {
return NameProxy( const_cast<Vector&>(*this), name ) ;
}
inline operator RObject() const {
return RObject( Storage::get__() );
}
// sugar subsetting requires dispatch on VectorBase
template <int RHS_RTYPE, bool RHS_NA, typename RHS_T>
SubsetProxy<RTYPE, StoragePolicy, RHS_RTYPE, RHS_NA, RHS_T>
operator[](const VectorBase<RHS_RTYPE, RHS_NA, RHS_T>& rhs) {
return SubsetProxy<RTYPE, StoragePolicy, RHS_RTYPE, RHS_NA, RHS_T>(
*this,
rhs
);
}
template <int RHS_RTYPE, bool RHS_NA, typename RHS_T>
const SubsetProxy<RTYPE, StoragePolicy, RHS_RTYPE, RHS_NA, RHS_T>
operator[](const VectorBase<RHS_RTYPE, RHS_NA, RHS_T>& rhs) const {
return SubsetProxy<RTYPE, StoragePolicy, RHS_RTYPE, RHS_NA, RHS_T>(
const_cast< Vector<RTYPE, StoragePolicy>& >(*this),
rhs
);
}
Vector& sort(bool decreasing = false) {
// sort() does not apply to List, RawVector or ExpressionVector.
//
// The function below does nothing for qualified Vector types,
// and is undefined for other types. Hence there will be a
// compiler error when sorting List, RawVector or ExpressionVector.
internal::Sort_is_not_allowed_for_this_type<RTYPE>::do_nothing();
typename traits::storage_type<RTYPE>::type* start = internal::r_vector_start<RTYPE>( Storage::get__() );
if (!decreasing) {
std::sort(
start,
start + size(),
internal::NAComparator<typename traits::storage_type<RTYPE>::type>()
);
} else {
std::sort(
start,
start + size(),
internal::NAComparatorGreater<typename traits::storage_type<RTYPE>::type>()
);
}
return *this;
}
template <typename InputIterator>
void assign( InputIterator first, InputIterator last){
/* FIXME: we can do better than this r_cast to avoid
allocating an unnecessary temporary object
*/
Shield<SEXP> wrapped(wrap(first, last));
Shield<SEXP> casted(r_cast<RTYPE>(wrapped));
Storage::set__(casted) ;
}
template <typename InputIterator>
static Vector import( InputIterator first, InputIterator last){
Vector v ;
v.assign( first , last ) ;
return v ;
}
template <typename InputIterator, typename F>
static Vector import_transform( InputIterator first, InputIterator last, F f){
return Vector( first, last, f) ;
}
template <typename T>
void push_back( const T& object){
push_back__impl( converter_type::get(object),
typename traits::same_type<stored_type,SEXP>()
) ;
}
template <typename T>
void push_back( const T& object, const std::string& name ){
push_back_name__impl( converter_type::get(object), name,
typename traits::same_type<stored_type,SEXP>()
) ;
}
template <typename T>
void push_front( const T& object){
push_front__impl( converter_type::get(object),
typename traits::same_type<stored_type,SEXP>() ) ;
}
template <typename T>
void push_front( const T& object, const std::string& name){
push_front_name__impl( converter_type::get(object), name,
typename traits::same_type<stored_type,SEXP>() ) ;
}
template <typename T>
iterator insert( iterator position, const T& object){
return insert__impl( position, converter_type::get(object),
typename traits::same_type<stored_type,SEXP>()
) ;
}
template <typename T>
iterator insert( int position, const T& object){
return insert__impl( cache.get() + position, converter_type::get(object),
typename traits::same_type<stored_type,SEXP>()
);
}
iterator erase( int position){
return erase_single__impl( cache.get() + position) ;
}
iterator erase( iterator position){
return erase_single__impl( position ) ;
}
iterator erase( int first, int last){
iterator start = cache.get() ;
return erase_range__impl( start + first, start + last ) ;
}
iterator erase( iterator first, iterator last){
return erase_range__impl( first, last ) ;
}
void update(SEXP){
cache.update(*this) ;
}
template <typename U>
static void replace_element( iterator it, SEXP names, R_xlen_t index, const U& u){
replace_element__dispatch( typename traits::is_named<U>::type(),
it, names, index, u ) ;
}
template <typename U>
static void replace_element__dispatch( traits::false_type, iterator it, SEXP names, R_xlen_t index, const U& u){
*it = converter_type::get(u);
}
template <typename U>
static void replace_element__dispatch( traits::true_type, iterator it, SEXP names, R_xlen_t index, const U& u){
replace_element__dispatch__isArgument( typename traits::same_type<U,Argument>(), it, names, index, u ) ;
}
template <typename U>
static void replace_element__dispatch__isArgument( traits::false_type, iterator it, SEXP names, R_xlen_t index, const U& u){
RCPP_DEBUG_2( " Vector::replace_element__dispatch<%s>(true, index= %d) ", DEMANGLE(U), index ) ;
*it = converter_type::get(u.object ) ;
SET_STRING_ELT( names, index, ::Rf_mkChar( u.name.c_str() ) ) ;
}
template <typename U>
static void replace_element__dispatch__isArgument( traits::true_type, iterator it, SEXP names, R_xlen_t index, const U& u){
RCPP_DEBUG_2( " Vector::replace_element__dispatch<%s>(true, index= %d) ", DEMANGLE(U), index ) ;
*it = R_MissingArg ;
SET_STRING_ELT( names, index, ::Rf_mkChar( u.name.c_str() ) ) ;
}
typedef internal::RangeIndexer<RTYPE,true,Vector> Indexer ;
inline Indexer operator[]( const Range& range ){
return Indexer( const_cast<Vector&>(*this), range );
}
template <typename EXPR_VEC>
Vector& operator+=( const VectorBase<RTYPE,true,EXPR_VEC>& rhs ) {
const EXPR_VEC& ref = rhs.get_ref() ;
iterator start = begin() ;
R_xlen_t n = size() ;
// TODO: maybe unroll this
stored_type tmp ;
for( R_xlen_t 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 <typename EXPR_VEC>
Vector& operator+=( const VectorBase<RTYPE,false,EXPR_VEC>& rhs ) {
const EXPR_VEC& ref = rhs.get_ref() ;
iterator start = begin() ;
R_xlen_t n = size() ;
stored_type tmp ;
for( R_xlen_t i=0; i<n; i++){
if( ! traits::is_na<RTYPE>(start[i]) ){
start[i] += ref[i] ;
}
}
return *this ;
}
/**
* Does this vector have an element with the target name
*/
bool containsElementNamed( const char* target ) const {
SEXP names = RCPP_GET_NAMES(Storage::get__()) ;
if( Rf_isNull(names) ) return false ;
R_xlen_t n = Rf_xlength(names) ;
for( R_xlen_t i=0; i<n; i++){
if( !strcmp( target, CHAR(STRING_ELT(names, i)) ) )
return true ;
}
return false ;
}
R_xlen_t findName(const std::string& name) const {
SEXP names = RCPP_GET_NAMES(Storage::get__());
if (Rf_isNull(names)) stop("'names' attribute is null");
R_xlen_t n = Rf_xlength(names);
for (R_xlen_t i=0; i < n; ++i) {
if (strcmp(name.c_str(), CHAR(STRING_ELT(names, i))) == 0) {
return i;
}
}
std::stringstream ss;
ss << "no name '" << name << "' found";
stop(ss.str());
return -1;
}
protected:
inline int* dims() const {
if( !::Rf_isMatrix(Storage::get__()) ) throw not_a_matrix() ;
return INTEGER( ::Rf_getAttrib( Storage::get__(), R_DimSymbol ) ) ;
}
void init(){
RCPP_DEBUG_2( "VECTOR<%d>::init( SEXP = <%p> )", RTYPE, Storage::get__() )
internal::r_init_vector<RTYPE>(Storage::get__()) ;
}
private:
void push_back__impl(const stored_type& object, traits::true_type ) {
Shield<SEXP> object_sexp( object ) ;
R_xlen_t 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( Rf_isNull(names) ){
for( ; it < this_end; ++it, ++target_it ){
*target_it = *it ; // #nocov start
}
} 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 ; // #nocov end
}
*target_it = object_sexp;
Storage::set__( target.get__() ) ;
}
void push_back__impl(const stored_type& object, traits::false_type ) {
R_xlen_t 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( Rf_isNull(names) ){
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__() ) ;
}
void push_back_name__impl(const stored_type& object, const std::string& name, traits::true_type ) {
Shield<SEXP> object_sexp( object ) ;
R_xlen_t 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( Rf_isNull(names) ){
for( ; it < this_end; ++it, ++target_it,i++ ){
*target_it = *it ; // #nocov
SET_STRING_ELT( newnames, i , R_BlankString ); // #nocov
}
} 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__() ) ;
}
void push_back_name__impl(const stored_type& object, const std::string& name, traits::false_type ) {
R_xlen_t 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( Rf_isNull(names) ){
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 ) ) ;
}
}
SET_STRING_ELT( newnames, i, Rf_mkChar( name.c_str() ) );
target.attr("names") = newnames ;
*target_it = object;
Storage::set__( target.get__() ) ;
}
void push_front__impl(const stored_type& object, traits::true_type ) {
Shield<SEXP> object_sexp( object ) ;
R_xlen_t 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( Rf_isNull(names) ){
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__() ) ;
}
void push_front__impl(const stored_type& object, traits::false_type ) {
R_xlen_t 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( Rf_isNull(names) ){
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__() ) ;
}
void push_front_name__impl(const stored_type& object, const std::string& name, traits::true_type ) {
Shield<SEXP> object_sexp(object) ;
R_xlen_t 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( Rf_isNull(names) ){
for( ; it < this_end; ++it, ++target_it,i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i , R_BlankString );
}
} 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__() ) ;
}
void push_front_name__impl(const stored_type& object, const std::string& name, traits::false_type ) {
R_xlen_t 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( Rf_isNull(names) ){
for( ; it < this_end; ++it, ++target_it,i++ ){
*target_it = *it ;
SET_STRING_ELT( newnames, i , R_BlankString );
}
} 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__() ) ;
}
iterator insert__impl( iterator position, const stored_type& object_, traits::true_type ) {
Shield<SEXP> object( object_ ) ;
R_xlen_t 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( Rf_isNull(names) ){
for( ; it < position; ++it, ++target_it){
*target_it = *it ;
}
result = target_it;
*target_it = 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 = object ;
SET_STRING_ELT( newnames, i, ::Rf_mkChar("") ) ;
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 ;
}
iterator insert__impl( iterator position, const stored_type& object, traits::false_type ) {
R_xlen_t 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( Rf_isNull(names) ){
for( ; it < position; ++it, ++target_it){
*target_it = *it ;
}
result = target_it;
*target_it = 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 = object ;
SET_STRING_ELT( newnames, i, ::Rf_mkChar("") ) ;
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 ;
}
iterator erase_single__impl( iterator position ) {
if( position < begin() || position > end() ) {
R_xlen_t requested_loc;
R_xlen_t available_locs = std::distance(begin(), end());
if(position > end()){
requested_loc = std::distance(position, begin());
} else {
// This will be a negative number
requested_loc = std::distance(begin(), position);
}
const char* fmt = "Iterator index is out of bounds: "
"[iterator index=%i; iterator extent=%i]";
throw index_out_of_bounds(fmt, requested_loc, available_locs ) ;
}
R_xlen_t 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( Rf_isNull(names) ){
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 ;
}
}
iterator erase_range__impl( iterator first, iterator last ) {
if( first > last ) throw std::range_error("invalid range") ;
if( last > end() || first < begin() ) {
R_xlen_t requested_loc;
R_xlen_t available_locs = std::distance(begin(), end());
std::string iter_problem;
if(last > end()){
requested_loc = std::distance(last, begin());
iter_problem = "last";
} else {
// This will be a negative number
requested_loc = std::distance(begin(), first);
iter_problem = "first";
}
const char* fmt = "Iterator index is out of bounds: "
"[iterator=%s; index=%i; extent=%i]";
throw index_out_of_bounds(fmt, iter_problem,
requested_loc, available_locs ) ;
}
iterator it = begin() ;
iterator this_end = end() ;
R_xlen_t nremoved = std::distance(first,last) ;