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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Nov 7 12:45:28 CET 2011


Author: romain
Date: 2011-11-07 12:45:28 +0100 (Mon, 07 Nov 2011)
New Revision: 3302

Added:
   pkg/int64/inst/include/int64/read_string.h
Modified:
   pkg/int64/inst/include/int64/LongVector.h
   pkg/int64/inst/include/int64/as_long.h
   pkg/int64/inst/include/int64/int64.h
Log:
better LongVector(SEXP) ctor

Modified: pkg/int64/inst/include/int64/LongVector.h
===================================================================
--- pkg/int64/inst/include/int64/LongVector.h	2011-11-07 11:35:27 UTC (rev 3301)
+++ pkg/int64/inst/include/int64/LongVector.h	2011-11-07 11:45:28 UTC (rev 3302)
@@ -35,7 +35,70 @@
         
     public:
         LongVector(SEXP x) : data(x) {
-            R_PreserveObject(R_do_slot(data, Rf_install(".Data"))) ;   
+            if( Rf_inherits( x, internal::get_class<LONG>().c_str() ) ){
+                data = x ;
+                R_PreserveObject(data) ;
+            } else {
+                switch( TYPEOF(x) ){
+                    case INTSXP:
+                        {
+                            int n = Rf_length(x) ;
+                            SEXP y = PROTECT( Rf_allocVector( VECSXP, n ) ) ;
+                            int hb, lb ;
+                            LONG tmp ;
+                            int* p_i_x = INTEGER(x) ;
+                            for( int i=0; i<n; i++){
+                                tmp = (LONG) p_i_x[i] ;
+                                hb = internal::get_high_bits<LONG>(tmp) ;
+                                lb = internal::get_low_bits<LONG>(tmp) ;
+                                SET_VECTOR_ELT( y, i, int64::internal::int2(hb,lb) ) ;    
+                            }
+                            UNPROTECT(1) ; // y
+                            data = y ;
+                            R_PreserveObject(data) ;  
+                            break ;
+                        }
+                    case REALSXP: 
+                        {
+                            int n = Rf_length(x) ;
+                            SEXP y = PROTECT( Rf_allocVector( VECSXP, n ) ) ;
+                            int hb, lb ;
+                            LONG tmp ;
+                            double* p_d_x = REAL(x) ;
+                            for( int i=0; i<n; i++){
+                                tmp = (LONG) p_d_x[i] ;
+                                hb = internal::get_high_bits<LONG>(tmp) ;
+                                lb = internal::get_low_bits<LONG>(tmp) ;
+                                SET_VECTOR_ELT( y, i, int64::internal::int2(hb,lb) ) ;    
+                            }
+                            UNPROTECT(1) ; // y
+                            data = y ;
+                            R_PreserveObject(data) ;  
+                            break ;  
+                        }
+                    case STRSXP:
+                        {
+                            int n = Rf_length(x) ;
+                            SEXP y = PROTECT( Rf_allocVector( VECSXP, n ) ) ;
+                            int hb, lb ;
+                            LONG tmp ;
+                            for( int i=0; i<n; i++){
+                                tmp = internal::read_string<LONG>( CHAR(STRING_ELT(x,i)) ) ;
+                                hb = internal::get_high_bits<LONG>(tmp) ;
+                                lb = internal::get_low_bits<LONG>(tmp) ;
+                                SET_VECTOR_ELT( y, i, int64::internal::int2(hb,lb) ) ;    
+                            }
+                            UNPROTECT(1) ; // y
+                            data = y ;
+                            R_PreserveObject(data) ;  
+                            break ;        
+                        }
+                    default:
+                        {
+                            Rf_error( "unimplemented conversion" ) ;   
+                        }
+                }
+            }
         }
         
         operator SEXP(){

Modified: pkg/int64/inst/include/int64/as_long.h
===================================================================
--- pkg/int64/inst/include/int64/as_long.h	2011-11-07 11:35:27 UTC (rev 3301)
+++ pkg/int64/inst/include/int64/as_long.h	2011-11-07 11:45:28 UTC (rev 3302)
@@ -23,21 +23,8 @@
               
 namespace int64{
     namespace internal{
-
-    template <typename LONG>
-    LONG read_string(const char* s) ;
-    
-    template <>
-    int64_t read_string<int64_t>(const char* s ){
-        return strtoll( s, NULL, 0 ) ; 
-    }
+       
         
-    template <>
-    uint64_t read_string<uint64_t>(const char* s){
-        return strtoull( s, NULL, 0 ) ;
-    } 
-        
-        
 template <typename LONG>
 SEXP as_long(SEXP x){
     int n = Rf_length(x) ;

Modified: pkg/int64/inst/include/int64/int64.h
===================================================================
--- pkg/int64/inst/include/int64/int64.h	2011-11-07 11:35:27 UTC (rev 3301)
+++ pkg/int64/inst/include/int64/int64.h	2011-11-07 11:45:28 UTC (rev 3302)
@@ -30,7 +30,7 @@
 #include <int64/get_long.h>
 #include <int64/get_bits.h>
 #include <int64/get_class.h>
-
+#include <int64/read_string.h>
 #include <int64/LongVector.h>
 #include <int64/format_binary.h>
 #include <int64/as_long.h>

Added: pkg/int64/inst/include/int64/read_string.h
===================================================================
--- pkg/int64/inst/include/int64/read_string.h	                        (rev 0)
+++ pkg/int64/inst/include/int64/read_string.h	2011-11-07 11:45:28 UTC (rev 3302)
@@ -0,0 +1,44 @@
+// read_string.h : 64 bit integers
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google Inc.  All rights reserved.
+//
+// This file is part of int64.
+//
+// int64 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.
+//
+// int64 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 int64.  If not, see <http://www.gnu.org/licenses/>.    
+    
+#ifndef int64__read_string__h
+#define int64__read_string__h
+              
+namespace int64{
+    namespace internal{
+
+    template <typename LONG>
+    inline LONG read_string(const char* s) ;
+    
+    template <>
+    inline int64_t read_string<int64_t>(const char* s ){
+        return strtoll( s, NULL, 0 ) ; 
+    }
+        
+    template <>
+    inline uint64_t read_string<uint64_t>(const char* s){
+        return strtoull( s, NULL, 0 ) ;
+    } 
+        
+    
+    } // namespace internal
+} // namespace int64
+
+#endif



More information about the Rcpp-commits mailing list