[Rcpp-commits] r1956 - in pkg/Rcpp/inst: . include include/Rcpp include/Rcpp/traits include/Rcpp/vector

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Aug 9 20:36:10 CEST 2010


Author: romain
Date: 2010-08-09 20:36:10 +0200 (Mon, 09 Aug 2010)
New Revision: 1956

Added:
   pkg/Rcpp/inst/include/Rcpp/Generator.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/traits/has_iterator.h
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
   pkg/Rcpp/inst/include/RcppCommon.h
Log:
preparing random number generators

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-09 11:09:31 UTC (rev 1955)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-09 18:36:10 UTC (rev 1956)
@@ -3,6 +3,14 @@
 	* inst/include/Rcpp/stats/lnorm.h: Added d-p-q (lnorm|weibull|logis|f)
 	generated by the script
 
+	* inst/include/Rcpp/vector/Vector.h: further dispatch to allow generators
+	to be passed to constructors of Vector
+	
+	* inst/include/Rcpp/traits/has_iterator.h: new SFINAE detection of 
+	generators
+	
+	* inst/include/Rcpp/Generator.h : base template class for generators
+	
 2010-08-06  Douglas Bates  <bates at stat.wisc.edu>
 
 	* unitTests/runit.stats.R: Corrected instances of numeric

Added: pkg/Rcpp/inst/include/Rcpp/Generator.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Generator.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/Generator.h	2010-08-09 18:36:10 UTC (rev 1956)
@@ -0,0 +1,45 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Generator.h: Rcpp R/C++ interface class library -- framework for random number generation
+//
+// Copyright (C) 2010	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__Generator_h
+#define Rcpp__Generator_h
+
+namespace Rcpp{
+
+template <bool seed, typename T>
+class Generator{
+public:
+	typedef T r_generator ;
+	Generator(){ GetRNGstate(); }
+	~Generator(){ PutRNGstate(); }	
+} ;
+
+template <typename T>
+class Generator<false,T> {
+public:
+	typedef T r_generator ;
+	Generator(){ }
+	~Generator(){ }	
+} ;
+
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/traits/has_iterator.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/has_iterator.h	2010-08-09 11:09:31 UTC (rev 1955)
+++ pkg/Rcpp/inst/include/Rcpp/traits/has_iterator.h	2010-08-09 18:36:10 UTC (rev 1956)
@@ -63,6 +63,21 @@
     public:
       static const bool value = sizeof(__test<T>(0)) == 1;
     };
+    
+  template<typename T>
+  class _is_generator_helper : __sfinae_types {
+      template<typename U> struct _Wrap_type { };
+
+      template<typename U>
+        static __one __test(_Wrap_type<typename U::r_generator>*);
+
+      template<typename U>
+        static __two __test(...);
+
+    public:
+      static const bool value = sizeof(__test<T>(0)) == 1;
+    };
+ 
   
   template<typename T>
   class _is_exporter_helper : __sfinae_types {
@@ -100,6 +115,9 @@
   template<typename T> struct is_exporter : 
     integral_constant<bool,_is_exporter_helper<T>::value> { };
 
+  template<typename T> struct is_generator : 
+    integral_constant<bool,_is_generator_helper<T>::value> { };
+
 } // traits
 } // Rcpp
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-08-09 11:09:31 UTC (rev 1955)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-08-09 18:36:10 UTC (rev 1956)
@@ -101,13 +101,31 @@
 			*start = other[i] ;
 		}
     }
+
+    template <typename T>
+    inline void fill_or_generate( const T& t){
+    	fill_or_generate__impl( t, typename traits::is_generator<T>::type() ) ;
+    }
+    
+    template <typename T>
+    inline void fill_or_generate__impl( const T& gen, traits::true_type){
+    	iterator first = begin() ;
+    	iterator last = end() ;
+    	while( first != last ) *first++ = gen() ;
+    }
+    
+    template <typename T>
+    inline void fill_or_generate__impl( const T& t, traits::false_type){
+    	fill(t) ;
+    }
+    
 	
 public:
     
     template <typename U>
     Vector( const int& size, const U& u){
     	RObject::setSEXP( Rf_allocVector( RTYPE, size) ) ;
-		fill( u ) ;	
+		fill_or_generate( u ) ;	
     }
     
     Vector( const Dimension& dims) : RObject() {

Modified: pkg/Rcpp/inst/include/RcppCommon.h
===================================================================
--- pkg/Rcpp/inst/include/RcppCommon.h	2010-08-09 11:09:31 UTC (rev 1955)
+++ pkg/Rcpp/inst/include/RcppCommon.h	2010-08-09 18:36:10 UTC (rev 1956)
@@ -248,6 +248,8 @@
 #include <Rcpp/traits/remove_const_and_reference.h>
 #include <Rcpp/traits/result_of.h>
 
+#include <Rcpp/Generator.h>
+
 #include <Rcpp/internal/caster.h>
 #include <Rcpp/internal/r_vector.h>
 #include <Rcpp/r_cast.h>



More information about the Rcpp-commits mailing list