[Rcpp-commits] r3328 - pkg/int64/inst/include/int64

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 12 11:29:49 CET 2011


Author: romain
Date: 2011-11-12 11:29:48 +0100 (Sat, 12 Nov 2011)
New Revision: 3328

Modified:
   pkg/int64/inst/include/int64/LongVector.h
   pkg/int64/inst/include/int64/math.h
Log:
cummin, cummax, cumprod, cumsum with NA handling

Modified: pkg/int64/inst/include/int64/LongVector.h
===================================================================
--- pkg/int64/inst/include/int64/LongVector.h	2011-11-12 10:23:39 UTC (rev 3327)
+++ pkg/int64/inst/include/int64/LongVector.h	2011-11-12 10:29:48 UTC (rev 3328)
@@ -170,6 +170,18 @@
             R_PreserveObject(data) ;
         }
         
+        LongVector(int n, LONG value) : data(R_NilValue) {
+            SEXP x = PROTECT( Rf_allocVector( VECSXP, n ) ) ;
+            int hb = get_high_bits<LONG>( value ) ;
+            int lb = get_low_bits<LONG>( value ) ;
+            for( int i=0; i<n; i++){
+                SET_VECTOR_ELT( x, i, int64::internal::int2(hb,lb) ) ;    
+            }
+            UNPROTECT(1) ; // x
+            data = x ;
+            R_PreserveObject(data) ;
+        }
+        
         template <typename ITERATOR>
         LongVector(int n, ITERATOR start, ITERATOR end) : data(R_NilValue) {
             SEXP x = PROTECT( Rf_allocVector( VECSXP, n ) ) ;

Modified: pkg/int64/inst/include/int64/math.h
===================================================================
--- pkg/int64/inst/include/int64/math.h	2011-11-12 10:23:39 UTC (rev 3327)
+++ pkg/int64/inst/include/int64/math.h	2011-11-12 10:29:48 UTC (rev 3328)
@@ -63,15 +63,17 @@
 
 template <typename LONG>
 SEXP cummax( SEXP x){
+    const LONG na = int64::LongVector<LONG>::na ;
     int64::LongVector<LONG> data(x) ;
     int n = data.size() ;
-    int64::LongVector<LONG> res(x) ;
+    int64::LongVector<LONG> res(n, na) ;
     LONG max = data.get(0) ;
     res.set( 0, max) ;
     LONG tmp = 0 ;
     
     for( int i=1; i<n; i++){
         tmp = data.get(i) ;
+        if( tmp == na ) break ;
         if( tmp > max ) max=tmp ;
         res.set( i, max ) ;
     }
@@ -80,15 +82,17 @@
 
 template <typename LONG>
 SEXP cummin( SEXP x){
+    const LONG na = int64::LongVector<LONG>::na ;
     int64::LongVector<LONG> data(x) ;
     int n = data.size() ;
-    int64::LongVector<LONG> res(x) ;
+    int64::LongVector<LONG> res(n, na) ;
     LONG max = data.get(0) ;
     res.set( 0, max) ;
     LONG tmp = 0 ;
     
     for( int i=1; i<n; i++){
         tmp = data.get(i) ;
+        if( tmp == na ) break ;
         if( tmp < max ) max=tmp ;
         res.set( i, max ) ;
     }
@@ -97,14 +101,16 @@
 
 template <typename LONG>
 SEXP cumprod( SEXP x){
+    const LONG na = int64::LongVector<LONG>::na ;
     int64::LongVector<LONG> data(x) ;
     int n = data.size() ;
-    int64::LongVector<LONG> res(x) ;
+    int64::LongVector<LONG> res(n, na) ;
     LONG prod = data.get(0) ;
     res.set( 0, prod) ;
     
     for( int i=1; i<n; i++){
-        prod *= data.get(i) ;
+        prod = times<LONG>( prod, data.get(i) );
+        if( prod == na ) break ;
         res.set( i, prod ) ;
     }
     return res ;
@@ -112,6 +118,7 @@
 
 template <typename LONG>
 SEXP cumsum( SEXP x){
+    const LONG na = int64::LongVector<LONG>::na ;
     int64::LongVector<LONG> data(x) ;
     int n = data.size() ;
     int64::LongVector<LONG> res(x) ;
@@ -119,7 +126,8 @@
     res.set( 0, prod) ;
     
     for( int i=1; i<n; i++){
-        prod += data.get(i) ;
+        prod = plus<LONG>( prod, data.get(i) );
+        if( prod == na ) break ;
         res.set( i, prod ) ;
     }
     return res ;



More information about the Rcpp-commits mailing list