[Rcpp-commits] r1573 - in pkg/Rcpp/inst: include/Rcpp/internal include/Rcpp/vector unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Jun 17 17:41:50 CEST 2010
Author: romain
Date: 2010-06-17 17:41:50 +0200 (Thu, 17 Jun 2010)
New Revision: 1573
Modified:
pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
pkg/Rcpp/inst/unitTests/runit.sugar.assign.R
Log:
VectorBase gains an iterator so it can wrap itself
Modified: pkg/Rcpp/inst/include/Rcpp/internal/wrap.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/internal/wrap.h 2010-06-17 14:09:41 UTC (rev 1572)
+++ pkg/Rcpp/inst/include/Rcpp/internal/wrap.h 2010-06-17 15:41:50 UTC (rev 1573)
@@ -39,9 +39,6 @@
template <typename InputIterator> SEXP range_wrap(InputIterator first, InputIterator last) ;
template <typename InputIterator> SEXP rowmajor_wrap(InputIterator first, int nrow, int ncol) ;
-// {{{ information about R vectors
-// }}}
-
// {{{ range wrap
// {{{ unnamed range wrap
Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h 2010-06-17 14:09:41 UTC (rev 1572)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h 2010-06-17 15:41:50 UTC (rev 1573)
@@ -70,7 +70,7 @@
RObject::setSEXP( r_cast<RTYPE>( wrap(x) ) ) ;
return *this ;
}
-
+
internal::ListInitialization<iterator,init_type> operator=( init_type x){
iterator start = begin() ; *start = x;
return internal::ListInitialization<iterator,init_type>( start + 1 ) ; ;
@@ -88,14 +88,23 @@
}
template <bool __NA__, typename __VEC__>
- Vector( const VectorBase<RTYPE,__NA__,__VEC__>& other ){
+ Vector( const VectorBase<RTYPE,__NA__,__VEC__>& other ) : RObject() {
int n = other.size() ;
RObject::setSEXP( Rf_allocVector( RTYPE, other.size() ) ) ;
- iterator start = begin() ;
+ import_expression<__NA__,__VEC__>( other, n ) ;
+ }
+
+private:
+
+ template <bool __NA__, typename __VEC__>
+ void import_expression( const VectorBase<RTYPE,__NA__,__VEC__>& other, int n ){
+ iterator start = begin() ;
for( int i=0; i<n; i++, ++start){
*start = other[i] ;
}
}
+
+public:
template <typename U>
Vector( const int& size, const U& u){
@@ -657,5 +666,4 @@
} ; /* Vector */
-
#endif
Modified: pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h 2010-06-17 14:09:41 UTC (rev 1572)
+++ pkg/Rcpp/inst/include/Rcpp/vector/VectorBase.h 2010-06-17 15:41:50 UTC (rev 1573)
@@ -35,16 +35,95 @@
VECTOR& get_ref(){
return static_cast<VECTOR&>(*this) ;
}
-
- // FIXME (or not): cannot get the iterator stuff to work
- // we can probaly live without
- // typedef typename traits::get_iterator<VECTOR>::type iterator ;
- // inline iterator begin(){ return static_cast<VECTOR*>(this)->begin() ; }
- // inline iterator end(){ return static_cast<VECTOR*>(this)->end() ; }
inline stored_type operator[]( int i) const { return static_cast<const VECTOR*>(this)->operator[](i) ; }
inline int size() const { return static_cast<const VECTOR*>(this)->size() ; }
+
+ class iterator {
+ public:
+ typedef stored_type reference ;
+ typedef stored_type* pointer ;
+ typedef int difference_type ;
+ typedef stored_type value_type;
+ typedef std::random_access_iterator_tag iterator_category ;
+
+ iterator( const VectorBase& object_, int index_ ) : object(object_), index(index_){}
+ iterator( const iterator& other) : object(other.object), index(other.index){};
+
+ inline iterator& operator++(){
+ index++ ;
+ return *this ;
+ }
+ inline iterator& operator++(int){
+ index++;
+ return *this ;
+ }
+
+ inline iterator& operator--(){
+ index-- ;
+ return *this ;
+ }
+ inline iterator& operator--(int){
+ index--;
+ return *this ;
+ }
+
+ inline iterator operator+(difference_type n) const {
+ return iterator( object, index+n ) ;
+ }
+ inline iterator operator-(difference_type n) const {
+ return iterator( object, index-n ) ;
+ }
+
+ inline iterator& operator+=(difference_type n) {
+ index += n ;
+ return *this ;
+ }
+ inline iterator& operator-=(difference_type n) {
+ index -= n;
+ return *this ;
+ }
+
+ inline reference operator*() {
+ return object[index] ;
+ }
+ inline pointer operator->(){
+ return &object[index] ;
+ }
+
+ inline bool operator==( const iterator& y) const {
+ return ( index == y.index ) ;
+ }
+ inline bool operator!=( const iterator& y) const {
+ return ( index != y.index ) ;
+ }
+ inline bool operator<( const iterator& other ) const {
+ return index < other.index ;
+ }
+ inline bool operator>( const iterator& other ) const {
+ return index > other.index ;
+ }
+ inline bool operator<=( const iterator& other ) const {
+ return index <= other.index ;
+ }
+ inline bool operator>=( const iterator& other ) const {
+ return index >= other.index ;
+ }
+
+ inline difference_type operator-(const iterator& other) const {
+ return index - other.index ;
+ }
+
+
+ private:
+ const VectorBase& object ;
+ int index;
+ } ;
+
+ inline iterator begin() const { return iterator(*this, 0) ; }
+ inline iterator end() const { return iterator(*this, size() ) ; }
+
} ;
} // namespace Rcpp
Modified: pkg/Rcpp/inst/unitTests/runit.sugar.assign.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.assign.R 2010-06-17 14:09:41 UTC (rev 1572)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.assign.R 2010-06-17 15:41:50 UTC (rev 1573)
@@ -17,14 +17,14 @@
# You should have received a copy of the GNU General Public License
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
-test.sugar.assign <- function( ){
+test.sugar.constructor <- function( ){
fx <- cxxfunction( signature( x = "numeric", y = "numeric" ), '
NumericVector xx(x) ;
NumericVector yy(y) ;
- LogicalVector res = xx < yy ;
+ LogicalVector res( xx < yy ) ;
return res ;
', plugin = "Rcpp" )
@@ -37,3 +37,24 @@
}
+test.sugar.assignment <- function( ){
+
+ fx <- cxxfunction( signature( x = "numeric", y = "numeric" ), '
+
+ NumericVector xx(x) ;
+ NumericVector yy(y) ;
+
+ LogicalVector res;
+ res = xx < yy ;
+ return res ;
+
+ ', plugin = "Rcpp" )
+
+
+ checkEquals( fx( 1, 0 ), FALSE )
+ checkEquals( fx( 1:10, 2:11 ), rep(TRUE,10) )
+ checkEquals( fx( 0, 1 ), TRUE )
+ checkTrue( identical( fx( NA, 1 ), NA ) )
+
+}
+
More information about the Rcpp-commits
mailing list