[Rcpp-commits] r3890 - in pkg/Rcpp: . inst/include/Rcpp/module inst/include/Rcpp/sugar/functions src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 3 08:58:25 CET 2012


Author: romain
Date: 2012-11-03 08:58:25 +0100 (Sat, 03 Nov 2012)
New Revision: 3890

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/which_max.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/which_min.h
Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/module/Module.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/src/Module.cpp
Log:
which.min, which.max

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-03 02:06:02 UTC (rev 3889)
+++ pkg/Rcpp/ChangeLog	2012-11-03 07:58:25 UTC (rev 3890)
@@ -1,3 +1,10 @@
+2012-11-03  Romain Francois <romain at r-enthusiasts.com>
+
+        * include/Rcpp/sugar/functions/which_min.h : sugar which.min
+        * include/Rcpp/sugar/functions/which_max.h : sugar which.max
+        * include/Rcpp/module/Module.h : added get_function_ptr which returns
+        the pointer of the target function as a DL_FUNC
+
 2012-11-02  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/unitTests/runit.rmath.R: More tests added
@@ -14,7 +21,7 @@
 	s/get_function_ptr/get_function/
 
 2012-11-01  Dirk Eddelbuettel  <edd at debian.org>
-
+      
 	* inst/unitTests/runit.rmath.R: New unit test file added
 
 2012-11-01  JJ Allaire <jj at rstudio.org>

Modified: pkg/Rcpp/inst/include/Rcpp/module/Module.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/module/Module.h	2012-11-03 02:06:02 UTC (rev 3889)
+++ pkg/Rcpp/inst/include/Rcpp/module/Module.h	2012-11-03 07:58:25 UTC (rev 3890)
@@ -83,7 +83,12 @@
          * object
          */
         SEXP get_function( const std::string& ) ;
-                
+           
+        /**
+         * get the underlying C++ function pointer as a DL_FUNC
+         */
+        DL_FUNC get_function_ptr( const std::string& ) ;
+        
         inline void Add( const char* name_ , CppFunction* ptr){
             functions.insert( FUNCTION_PAIR( name_ , ptr ) ) ;
         }

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2012-11-03 02:06:02 UTC (rev 3889)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2012-11-03 07:58:25 UTC (rev 3890)
@@ -56,5 +56,7 @@
 #include <Rcpp/sugar/functions/var.h>
 #include <Rcpp/sugar/functions/sd.h>
 #include <Rcpp/sugar/functions/cumsum.h>
+#include <Rcpp/sugar/functions/which_min.h>
+#include <Rcpp/sugar/functions/which_max.h>
 
 #endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/which_max.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/which_max.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/which_max.h	2012-11-03 07:58:25 UTC (rev 3890)
@@ -0,0 +1,96 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// which_max.h: Rcpp R/C++ interface class library -- which.max
+//
+// Copyright (C) 2012   Dirk Eddelbuettel and Romain Francois
+//
+// 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__sugar__which_max_h
+#define Rcpp__sugar__which_max_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename T>
+class WhichMax {
+public:
+    typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+    typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	WhichMax(const VEC_TYPE& obj_ ) : obj(obj_){}
+	
+	int get() const {
+	    STORAGE current = obj[0] ;
+	    STORAGE min = current ;
+	    int index = 0 ;
+	    if( Rcpp::traits::is_na<RTYPE>(current) ) return NA_INTEGER ;
+	    int n = obj.size() ;
+	    for( int i=1; i<n; i++){
+		    current = obj[i] ;
+		    if( Rcpp::traits::is_na<RTYPE>(current) ) return NA_INTEGER ;
+		    if( current > min ){
+		        min = current ;
+		        index = i ;
+		    }
+		}
+		return index ;
+	}
+	
+private:
+    const VEC_TYPE& obj ;	
+	
+} ;
+   
+template <int RTYPE, typename T>
+class WhichMax<RTYPE,false,T> {
+public:
+    typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
+    typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	WhichMax(const VEC_TYPE& obj_ ) : obj(obj_){}
+	
+	int get() const {
+	    STORAGE current = obj[0] ;
+	    STORAGE min = current ;
+	    int index = 0 ;
+	    int n = obj.size() ;
+	    for( int i=1; i<n; i++){
+		    current = obj[i] ;
+		    if( current > min ){
+		        min = current ;
+		        index = i ;
+		    }
+		}
+		return index ;
+	}
+	
+private:
+    const VEC_TYPE& obj ;	
+	
+} ;
+
+    
+} // sugar
+
+
+
+template <int RTYPE, bool NA, typename T>
+int which_max( const VectorBase<RTYPE,NA,T>& t ){
+	return sugar::WhichMax<RTYPE,NA,T>(t).get() ; 
+}
+  
+} // Rcpp
+#endif
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/which_min.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/which_min.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/which_min.h	2012-11-03 07:58:25 UTC (rev 3890)
@@ -0,0 +1,96 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// which_min.h: Rcpp R/C++ interface class library -- which.min
+//
+// Copyright (C) 2012   Dirk Eddelbuettel and Romain Francois
+//
+// 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__sugar__which_min_h
+#define Rcpp__sugar__which_min_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename T>
+class WhichMin {
+public:
+    typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+    typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	WhichMin(const VEC_TYPE& obj_ ) : obj(obj_){}
+	
+	int get() const {
+	    STORAGE current = obj[0] ;
+	    STORAGE min = current ;
+	    int index = 0 ;
+	    if( Rcpp::traits::is_na<RTYPE>(current) ) return NA_INTEGER ;
+	    int n = obj.size() ;
+	    for( int i=1; i<n; i++){
+		    current = obj[i] ;
+		    if( Rcpp::traits::is_na<RTYPE>(current) ) return NA_INTEGER ;
+		    if( current < min ){
+		        min = current ;
+		        index = i ;
+		    }
+		}
+		return index ;
+	}
+	
+private:
+    const VEC_TYPE& obj ;	
+	
+} ;
+   
+template <int RTYPE, typename T>
+class WhichMin<RTYPE,false,T> {
+public:
+    typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
+    typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	WhichMin(const VEC_TYPE& obj_ ) : obj(obj_){}
+	
+	int get() const {
+	    STORAGE current = obj[0] ;
+	    STORAGE min = current ;
+	    int index = 0 ;
+	    int n = obj.size() ;
+	    for( int i=1; i<n; i++){
+		    current = obj[i] ;
+		    if( current < min ){
+		        min = current ;
+		        index = i ;
+		    }
+		}
+		return index ;
+	}
+	
+private:
+    const VEC_TYPE& obj ;	
+	
+} ;
+
+    
+} // sugar
+
+
+
+template <int RTYPE, bool NA, typename T>
+int which_min( const VectorBase<RTYPE,NA,T>& t ){
+	return sugar::WhichMin<RTYPE,NA,T>(t).get() ; 
+}
+  
+} // Rcpp
+#endif
+

Modified: pkg/Rcpp/src/Module.cpp
===================================================================
--- pkg/Rcpp/src/Module.cpp	2012-11-03 02:06:02 UTC (rev 3889)
+++ pkg/Rcpp/src/Module.cpp	2012-11-03 07:58:25 UTC (rev 3890)
@@ -311,6 +311,19 @@
 	        ) ;
 	}
 	
+	DL_FUNC Module::get_function_ptr( const std::string& name ){
+	    MAP::iterator it = functions.begin() ;
+	    int n = functions.size() ;
+	    CppFunction* fun = 0 ;
+	    for( int i=0; i<n; i++, ++it){
+	        if( name.compare( it->first ) == 0){
+	            fun = it->second ;
+	            break ;
+	        }
+	    }
+	    return fun->get_function_ptr() ;
+	}
+	
 	Rcpp::List Module::classes_info(){
 		int n = classes.size() ;
 		Rcpp::CharacterVector names(n) ;



More information about the Rcpp-commits mailing list