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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Nov 7 12:35:27 CET 2011


Author: romain
Date: 2011-11-07 12:35:27 +0100 (Mon, 07 Nov 2011)
New Revision: 3301

Added:
   pkg/Rcpp/inst/include/Rcpp/int64/read_string.h
Modified:
   pkg/Rcpp/inst/include/Rcpp/int64/LongVector.h
   pkg/Rcpp/inst/include/Rcpp/int64/int64_lite.h
Log:
better constructor for LongVector(SEXP)

Modified: pkg/Rcpp/inst/include/Rcpp/int64/LongVector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/int64/LongVector.h	2011-11-07 09:10:44 UTC (rev 3300)
+++ pkg/Rcpp/inst/include/Rcpp/int64/LongVector.h	2011-11-07 11:35:27 UTC (rev 3301)
@@ -30,8 +30,71 @@
         SEXP data ;
         
     public:
-        LongVector(SEXP x) : data(x) {
-            R_PreserveObject(data) ;   
+        LongVector(SEXP x) : data(R_NilValue) {
+            if( Rf_inherits( x, 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 = get_high_bits<LONG>(tmp) ;
+                                lb = get_low_bits<LONG>(tmp) ;
+                                SET_VECTOR_ELT( y, i, Rcpp::int64::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 = get_high_bits<LONG>(tmp) ;
+                                lb = get_low_bits<LONG>(tmp) ;
+                                SET_VECTOR_ELT( y, i, Rcpp::int64::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 = read_string<LONG>( CHAR(STRING_ELT(x,i)) ) ;
+                                hb = get_high_bits<LONG>(tmp) ;
+                                lb = get_low_bits<LONG>(tmp) ;
+                                SET_VECTOR_ELT( y, i, Rcpp::int64::int2(hb,lb) ) ;    
+                            }
+                            UNPROTECT(1) ; // y
+                            data = y ;
+                            R_PreserveObject(data) ;  
+                            break ;        
+                        }
+                    default:
+                        {
+                            Rf_error( "unimplemented conversion" ) ;   
+                        }
+                }
+            }
         }
         
         operator SEXP(){ 
@@ -72,7 +135,7 @@
         
         
         ~LongVector(){
-            R_ReleaseObject(data) ;   
+            if( !Rf_isNull(data)) R_ReleaseObject(data) ;   
         }
         
         inline LONG get(int i) const {

Modified: pkg/Rcpp/inst/include/Rcpp/int64/int64_lite.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/int64/int64_lite.h	2011-11-07 09:10:44 UTC (rev 3300)
+++ pkg/Rcpp/inst/include/Rcpp/int64/int64_lite.h	2011-11-07 11:35:27 UTC (rev 3301)
@@ -31,6 +31,7 @@
 
 #include <Rcpp/int64/get_class.h>
 #include <Rcpp/int64/get_long.h>
+#include <Rcpp/int64/read_string.h>
 #include <Rcpp/int64/get_bits.h>
 #include <Rcpp/int64/new_long.h>
 #include <Rcpp/int64/LongVector.h>

Added: pkg/Rcpp/inst/include/Rcpp/int64/read_string.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/int64/read_string.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/int64/read_string.h	2011-11-07 11:35:27 UTC (rev 3301)
@@ -0,0 +1,45 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// read_string.h : Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2011 Romain Francois
+// Copyright (C) 2011 Google Inc.  All rights reserved.
+//
+// This file is part of Rcpp.
+//
+// Rcpp 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.
+//
+// Rcpp 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 Rcpp.  If not, see <http://www.gnu.org/licenses/>.    
+    
+#ifndef Rcpp__int64__read_string__h
+#define Rcpp__int64__read_string__h
+
+namespace Rcpp{
+    namespace int64{
+
+        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 int64
+} // namespace Rcpp
+#endif



More information about the Rcpp-commits mailing list