Skip to content

Commit 79c4dc6

Browse files
committed
Merge pull request #404 from dcdillon/Issue362
const_iterators for CharacterVector now work analogously to iterators
2 parents 1d40e4a + bb81693 commit 79c4dc6

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

inst/include/Rcpp/vector/const_generic_proxy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ namespace internal{
4747
operator bool() const { return ::Rcpp::as<bool>(get()) ; }
4848
operator int() const { return ::Rcpp::as<int>(get()) ; }
4949

50+
inline void move(R_xlen_t n) { index += n ; }
51+
5052
const VECTOR* parent;
5153
R_xlen_t index ;
5254

inst/include/Rcpp/vector/const_string_proxy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace internal{
4040
* @param v reference to the associated character vector
4141
* @param index index
4242
*/
43-
const_string_proxy( VECTOR& v, R_xlen_t index_ ) : parent(&v), index(index_){}
43+
const_string_proxy( const VECTOR& v, R_xlen_t index_ ) : parent(&v), index(index_){}
4444

4545
const_string_proxy(SEXP x): parent(0), index(0) {
4646
Vector<RTYPE> tmp(x);

inst/include/Rcpp/vector/proxy.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ namespace traits {
249249
template<> struct r_vector_iterator<VECSXP> : proxy_based_iterator<VECSXP>{} ;
250250
template<> struct r_vector_iterator<EXPRSXP> : proxy_based_iterator<EXPRSXP>{} ;
251251
template<> struct r_vector_iterator<STRSXP> : proxy_based_iterator<STRSXP>{} ;
252+
253+
template <int RTYPE> struct proxy_based_const_iterator{
254+
typedef ::Rcpp::internal::Proxy_Iterator< typename r_vector_const_proxy<RTYPE>::type > type ;
255+
} ;
256+
template<> struct r_vector_const_iterator<VECSXP> : proxy_based_const_iterator<VECSXP>{} ;
257+
template<> struct r_vector_const_iterator<EXPRSXP> : proxy_based_const_iterator<EXPRSXP>{} ;
258+
template<> struct r_vector_const_iterator<STRSXP> : proxy_based_const_iterator<STRSXP>{} ;
252259

253260
} // traits
254261
}

inst/include/Rcpp/vector/traits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace traits{
6767
}
6868
inline iterator get() const { return iterator( proxy(*p, 0 ) ) ;}
6969
// inline const_iterator get_const() const { return const_iterator( *p ) ;}
70-
inline const_iterator get_const() const { return get_vector_ptr(*p) ; }
70+
inline const_iterator get_const() const { return const_iterator( const_proxy(*p, 0) ) ; }
7171

7272
inline proxy ref() { return proxy(*p,0) ; }
7373
inline proxy ref(R_xlen_t i) { return proxy(*p,i);}

inst/unitTests/cpp/Vector.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,13 +576,31 @@ std::string character_iterator1( CharacterVector letters ){
576576
return res ;
577577
}
578578

579+
// [[Rcpp::export]]
580+
std::string character_const_iterator1( const CharacterVector letters ){
581+
std::string res ;
582+
CharacterVector::const_iterator first = letters.begin() ;
583+
CharacterVector::const_iterator last = letters.end() ;
584+
while( first != last ){
585+
res += *first ;
586+
++first ;
587+
}
588+
return res ;
589+
}
590+
579591

580592
// [[Rcpp::export]]
581593
std::string character_iterator2( CharacterVector letters ){
582594
std::string res(std::accumulate(letters.begin(), letters.end(), std::string()));
583595
return res ;
584596
}
585597

598+
// [[Rcpp::export]]
599+
std::string character_const_iterator2( const CharacterVector letters ){
600+
std::string res(std::accumulate(letters.begin(), letters.end(), std::string()));
601+
return res ;
602+
}
603+
586604
// [[Rcpp::export]]
587605
CharacterVector character_reverse( CharacterVector y ){
588606
std::reverse( y.begin(), y.end() ) ;

inst/unitTests/runit.Vector.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,18 @@ if (.runThisTest) {
499499
paste(letters, collapse=""),
500500
msg = "CharacterVector::iterator using std::accumulate" )
501501
}
502+
503+
test.CharacterVector.iterator <- function(){
504+
fun <- character_const_iterator1
505+
checkEquals(fun(letters),
506+
paste(letters, collapse=""),
507+
msg = "CharacterVector::iterator explicit looping" )
508+
509+
fun <- character_const_iterator2
510+
checkEquals(fun(letters),
511+
paste(letters, collapse=""),
512+
msg = "CharacterVector::iterator using std::accumulate" )
513+
}
502514

503515
test.CharacterVector.reverse <- function(){
504516
fun <- character_reverse

0 commit comments

Comments
 (0)