[Rcpp-commits] r4230 - in pkg/Rcpp: . inst/include/Rcpp/internal inst/include/Rcpp/sugar inst/include/Rcpp/sugar/tools inst/include/Rcpp/vector inst/unitTests inst/unitTests/cpp src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Feb 2 17:09:41 CET 2013


Author: romain
Date: 2013-02-02 17:09:41 +0100 (Sat, 02 Feb 2013)
New Revision: 4230

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/internal/Proxy_Iterator.h
   pkg/Rcpp/inst/include/Rcpp/sugar/Range.h
   pkg/Rcpp/inst/include/Rcpp/sugar/tools/iterator.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h
   pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
   pkg/Rcpp/inst/unitTests/cpp/Vector.cpp
   pkg/Rcpp/inst/unitTests/runit.Vector.R
   pkg/Rcpp/src/api.cpp
Log:
correct operator++ and --

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/ChangeLog	2013-02-02 16:09:41 UTC (rev 4230)
@@ -1,3 +1,15 @@
+2013-02-02 Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/vector/MatrixRow.h: correct operator++(int) and operator--(int)
+        * include/Rcpp/internal/Proxy_Iterator.h: correct operators ++ and --
+        * include/Rcpp/sugar/Range.h: correct operators ++ and --
+        * include/Rcpp/sugar/tools/iterator.h: correct operators ++ and --
+        * include/Rcpp/vector/MatrixBase.h : correct operators ++ and --
+        * include/Rcpp/vector/VectorBase.h : correct operators ++ and --
+        * unitTests/cpp/Vector.cpp: new unit tests 
+        * unitTests/runit.Vector.R: new unit tests
+        * src/api.cpp: comment dropTrailing0 which is not used anymore 
+
 2013-01-15  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/api.cpp (Rcpp): Commented-out coerce_to_string() for real and

Modified: pkg/Rcpp/inst/include/Rcpp/internal/Proxy_Iterator.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/Proxy_Iterator.h	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/inst/include/Rcpp/internal/Proxy_Iterator.h	2013-02-02 16:09:41 UTC (rev 4230)
@@ -3,7 +3,7 @@
 //
 // Proxy_Iterator.h: Rcpp R/C++ interface class library -- 
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -48,18 +48,20 @@
 			proxy.move(1) ;
 			return *this ;
 		}
-		inline Proxy_Iterator& operator++(int){
-			proxy.move(1) ;
-			return *this ;
+		inline Proxy_Iterator operator++(int){
+			Proxy_Iterator orig(*this) ;
+			++(*this) ;
+			return orig ;
 		}
 		
 		inline Proxy_Iterator& operator--(){
 			proxy.move(-1) ;
 			return *this ;
 		}
-		inline Proxy_Iterator& operator--(int){
-			proxy.move(-1) ;
-			return *this ;
+		inline Proxy_Iterator operator--(int){
+			Proxy_Iterator orig(*this) ;
+			--(*this) ;
+			return orig ;
 		}
 		                    
 		inline Proxy_Iterator operator+(difference_type n) const {

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/Range.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/Range.h	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/Range.h	2013-02-02 16:09:41 UTC (rev 4230)
@@ -2,7 +2,7 @@
 //
 // Range.h: Rcpp R/C++ interface class library -- 
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -44,18 +44,20 @@
             start++ ; end_++ ;
             return *this ;
         }
-        Range& operator++(int) {
-            start++ ; end_++ ;
-            return *this ;
+        Range operator++(int) {
+            Range orig(*this) ;
+            ++(*this);
+            return orig ;
         }
                 
         Range& operator--() {
             start-- ; end_-- ;
             return *this ;
         }
-        Range& operator--(int) {
-            start-- ; end_-- ;
-            return *this ;
+        Range operator--(int) {
+            Range orig(*this) ;
+            --(*this);
+            return orig ;
         }
                 
         Range& operator+=(int n) {

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/tools/iterator.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/tools/iterator.h	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/tools/iterator.h	2013-02-02 16:09:41 UTC (rev 4230)
@@ -2,7 +2,7 @@
 //
 // iterator.h: Rcpp R/C++ interface class library -- 
 //
-// Copyright (C) 2012   Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2012 - 2013    Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -42,9 +42,17 @@
         SugarIterator( const SugarIterator& other) : ref(other.ref), index(other.index){}
         
         inline iterator& operator++(){ index++; return *this ; }
-        inline iterator& operator++(int){ index++; return *this ; }
+        inline iterator operator++(int){ 
+            iterator orig(*this) ;
+            ++(*this); 
+            return orig ;
+        }
         inline iterator& operator--(){ index--; return *this ; }
-        inline iterator& operator--(int){ index--; return *this ; }
+        inline iterator operator--(int){ 
+            iterator orig(*this) ;
+            --(*this); 
+            return orig ;
+        }
         inline iterator operator+(difference_type n) const {
 			return iterator( ref, index+n ) ;
 		}

Modified: pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h	2013-02-02 16:09:41 UTC (rev 4230)
@@ -2,7 +2,7 @@
 //
 // MatrixBase.h: Rcpp R/C++ interface class library -- 
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -72,13 +72,10 @@
                 }
                 return *this ;
             }
-            inline iterator& operator++(int){
-                i++ ;
-                if( i == nr ){
-                    j++ ;
-                    i=0 ;
-                }
-                return *this ;
+            inline iterator operator++(int){
+                iterator orig(*this) ;
+                ++(*this) ;
+                return orig ;
             }
                 
             inline iterator& operator--(){
@@ -89,13 +86,10 @@
                 }
                 return *this ;
             }
-            inline iterator& operator--(int){
-                i-- ;
-                if( i == -1 ){
-                    j-- ;
-                    i = nr - 1 ;
-                }
-                return *this ;
+            inline iterator operator--(int){
+                iterator orig(*this) ;
+                --(*this) ;
+                return orig ;
             }
                                     
             inline iterator operator+(difference_type n) const {
@@ -115,9 +109,6 @@
             }
 
             inline reference operator*() {
-                //  TODO: it might be better to call object( i, j )
-                //        as in many cases the sugar expression 
-                //        is faster with two indexes
                 return object(i,j) ;
             }
             inline pointer operator->(){

Modified: pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/inst/include/Rcpp/vector/MatrixRow.h	2013-02-02 16:09:41 UTC (rev 4230)
@@ -2,7 +2,7 @@
 //
 // MatrixRow.h: Rcpp R/C++ interface class library -- matrices row
 //
-// Copyright (C) 2010 - 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -40,25 +40,28 @@
         typedef typename std::iterator_traits<vector_iterator>::pointer pointer ;
         
         typedef std::random_access_iterator_tag iterator_category ;
-                
+            
+        iterator( const iterator& other) : row(other.row), index(other.index){}
         iterator( MatrixRow& row_, int index_ ) : row(row_), index(index_){}
                 
         iterator& operator++(){ 
             index++;
             return *this ;
         }
-        iterator& operator++(int) { 
-            index++;
-            return *this ;
+        iterator operator++(int) { 
+            iterator orig(*this); 
+            index++ ;
+            return orig ;
         }
         
         iterator& operator--(){ 
             index-- ;
             return *this ;
         } 
-        iterator& operator--(int){ 
+        iterator operator--(int){ 
+            iterator orig(*this); 
             index-- ;
-            return *this ;
+            return orig ;
         }
                                     
         iterator operator+(difference_type n) const { return iterator( row, index + n ) ; }

Modified: pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h	2013-02-02 16:09:41 UTC (rev 4230)
@@ -2,7 +2,7 @@
 //
 // VectorBase.h: Rcpp R/C++ interface class library -- 
 //
-// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2010 - 2013 Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -63,18 +63,20 @@
 			index++ ;
 			return *this ;
 		}
-		inline iterator& operator++(int){
-			index++;
-			return *this ;
+		inline iterator operator++(int){
+			iterator orig(*this); 
+		    ++(*this) ;
+			return orig ;
 		}
 		
 		inline iterator& operator--(){
 			index-- ;
 			return *this ;
 		}
-		inline iterator& operator--(int){
-			index--; 
-			return *this ;
+		inline iterator operator--(int){
+			iterator orig(*this); 
+		    --(*this) ;
+			return orig ;
 		}
 		                    
 		inline iterator operator+(difference_type n) const {

Modified: pkg/Rcpp/inst/unitTests/cpp/Vector.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/cpp/Vector.cpp	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/inst/unitTests/cpp/Vector.cpp	2013-02-02 16:09:41 UTC (rev 4230)
@@ -2,7 +2,7 @@
 //
 // Vector.cpp: Rcpp R/C++ interface class library -- Vector unit tests
 //
-// Copyright (C) 2012 Dirk Eddelbuettel and Romain Francois
+// Copyright (C) 2012 - 2013    Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -456,6 +456,32 @@
 }
 
 // [[Rcpp::export]]
+CharacterVector character_matrix_row_iteration_incr( CharacterMatrix m ){
+    std::string pasted_row;
+    CharacterMatrix::Row row(m(1, _));
+    CharacterMatrix::Row::iterator i_row(row.begin());
+    for( size_t i=0 ; i<4; i++){
+        pasted_row += *i_row++;
+    }
+    return wrap( pasted_row ) ;
+}
+
+// [[Rcpp::export]]
+CharacterVector character_matrix_row_iteration_decr( CharacterMatrix m ){
+    std::string pasted_row;
+    CharacterMatrix::Row row(m(1, _));
+    CharacterMatrix::Row::iterator i_row(row.end());
+    i_row--; // Step back from 'one past the end' to 'last element'.
+    // Only copy the last three elements, to avoid creating an invalid
+    // 'one before the beginning' iterator:
+    for( size_t i=0 ; i<3; i++){
+        pasted_row += *i_row--;
+    }
+    return wrap( pasted_row ) ;
+}
+
+
+// [[Rcpp::export]]
 CharacterVector character_assign1(){
     const char* x[] = { "foo", "bar", "bling", "boom" } ;
     CharacterVector y ;

Modified: pkg/Rcpp/inst/unitTests/runit.Vector.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Vector.R	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/inst/unitTests/runit.Vector.R	2013-02-02 16:09:41 UTC (rev 4230)
@@ -1,7 +1,7 @@
 #!/usr/bin/r -t
 #       hey emacs, please make this use  -*- tab-width: 4 -*-
 #
-# Copyright (C) 2010 - 2012  Dirk Eddelbuettel and Romain Francois
+# Copyright (C) 2010 - 2013  Dirk Eddelbuettel and Romain Francois
 #
 # This file is part of Rcpp.
 #
@@ -426,6 +426,16 @@
     checkEquals( diag(fun(x)), rep("foo", 4) , msg = "matrix indexing lhs" )
 }
 
+test.CharacterVector.matrix.row.iteration <- function() {
+    x <- matrix(letters[1:16], nrow = 4)
+
+    fun <- character_matrix_row_iteration_incr
+    checkEquals( fun(x), "bfjn", msg = "matrix row iteration post-incr" )
+
+    fun <- character_matrix_row_iteration_decr
+    checkEquals( fun(x), "njf", msg = "matrix row iteration post-decr" )
+}
+
 test.CharacterVector.assign <- function(){
     fun <- character_assign1
     checkEquals( fun(), c("foo", "bar", "bling", "boom"), msg = "assign(char**, char**)" )

Modified: pkg/Rcpp/src/api.cpp
===================================================================
--- pkg/Rcpp/src/api.cpp	2013-02-01 13:41:50 UTC (rev 4229)
+++ pkg/Rcpp/src/api.cpp	2013-02-02 16:09:41 UTC (rev 4230)
@@ -1655,23 +1655,23 @@
 }
 
 
-static const char* dropTrailing0(char *s, char cdec) {
-    /* Note that  's'  is modified */
-    char *p = s;
-    for (p = s; *p; p++) {
-	if(*p == cdec) {
-	    char *replace = p++;
-	    while ('0' <= *p  &&  *p <= '9')
-		if(*(p++) != '0')
-		    replace = p;
-	    if(replace != p)
-		while((*(replace++) = *(p++)))
-		    ;
-	    break;
-	}
-    }
-    return s;
-}
+// static const char* dropTrailing0(char *s, char cdec) {
+//     /* Note that  's'  is modified */
+//     char *p = s;
+//     for (p = s; *p; p++) {
+// 	if(*p == cdec) {
+// 	    char *replace = p++;
+// 	    while ('0' <= *p  &&  *p <= '9')
+// 		if(*(p++) != '0')
+// 		    replace = p;
+// 	    if(replace != p)
+// 		while((*(replace++) = *(p++)))
+// 		    ;
+// 	    break;
+// 	}
+//     }
+//     return s;
+// }
 
 // template <> const char* coerce_to_string<REALSXP>(double x){
 //     int w,d,e ;



More information about the Rcpp-commits mailing list