[Rcpp-commits] r4445 - in pkg/RcppArmadillo: . inst inst/include/RcppArmadilloExtensions

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Aug 24 08:56:17 CEST 2013


Author: romain
Date: 2013-08-24 08:56:16 +0200 (Sat, 24 Aug 2013)
New Revision: 4445

Modified:
   pkg/RcppArmadillo/ChangeLog
   pkg/RcppArmadillo/inst/NEWS.Rd
   pkg/RcppArmadillo/inst/include/RcppArmadilloExtensions/sample.h
Log:
fix sample.h for the multiple include problem

Modified: pkg/RcppArmadillo/ChangeLog
===================================================================
--- pkg/RcppArmadillo/ChangeLog	2013-08-24 01:33:52 UTC (rev 4444)
+++ pkg/RcppArmadillo/ChangeLog	2013-08-24 06:56:16 UTC (rev 4445)
@@ -1,3 +1,9 @@
+2013-08-24 Romain Francois <romain at r-enthusiasts.com>
+
+        * include/RcppArmadilloExtensions/sample.h: added include guard, made 
+        functions SampleReplace, etc ... templates to avoid the multiple definitions
+        problem
+
 2013-08-21 Romain Francois <romain at r-enthusiasts.com>
 
         * include/RcppArmadilloWrap.h: Less data copies for wrap< SpMat<T> >

Modified: pkg/RcppArmadillo/inst/NEWS.Rd
===================================================================
--- pkg/RcppArmadillo/inst/NEWS.Rd	2013-08-24 01:33:52 UTC (rev 4444)
+++ pkg/RcppArmadillo/inst/NEWS.Rd	2013-08-24 06:56:16 UTC (rev 4445)
@@ -11,7 +11,6 @@
       to \code{lstlisting} which permits builds on CRAN; removed
       set \code{BuildVignettes: FALSE}.
     }
-  }
 }
 
 \section{Changes in RcppArmadillo version 0.3.910.0 (2013-08-12)}{

Modified: pkg/RcppArmadillo/inst/include/RcppArmadilloExtensions/sample.h
===================================================================
--- pkg/RcppArmadillo/inst/include/RcppArmadilloExtensions/sample.h	2013-08-24 01:33:52 UTC (rev 4444)
+++ pkg/RcppArmadillo/inst/include/RcppArmadilloExtensions/sample.h	2013-08-24 06:56:16 UTC (rev 4445)
@@ -7,6 +7,7 @@
 // (note that Walker's alias method is not implemented).
 //
 // Copyright (C)  2012 - 2013  Christian Gunning
+// Copyright (C)  2013  Romain Francois
 //
 // This file is part of RcppArmadillo.
 //
@@ -23,19 +24,30 @@
 // You should have received a copy of the GNU General Public License
 // along with RcppArmadillo.  If not, see <http://www.gnu.org/licenses/>.
 
+#ifndef RCPPARMADILLO__EXTENSIONS__SAMPLE_H
+#define RCPPARMADILLO__EXTENSIONS__SAMPLE_H
+
 #include <RcppArmadillo.h>
 
-
 namespace Rcpp{
 
     namespace RcppArmadillo{
 
         //Declarations
-        void SampleReplace( IntegerVector &index, int nOrig, int size);
-        void SampleNoReplace( IntegerVector &index, int nOrig, int size);
-        void ProbSampleReplace(IntegerVector &index, int nOrig, int size, arma::vec &prob);
-        void ProbSampleNoReplace(IntegerVector &index, int nOrig, int size, arma::vec &prob);
-        void FixProb(NumericVector &prob, int size, bool replace);
+        template <typename INDEX>
+        void SampleReplace( INDEX &index, int nOrig, int size);
+        
+        template <typename INDEX>
+        void SampleNoReplace( INDEX &index, int nOrig, int size);
+        
+        template <typename INDEX>
+        void ProbSampleReplace(INDEX &index, int nOrig, int size, arma::vec &prob);
+        
+        template <typename INDEX>
+        void ProbSampleNoReplace(INDEX &index, int nOrig, int size, arma::vec &prob);
+        
+        template <typename PROB>
+        void FixProb(PROB &prob, int size, bool replace);
 
         template <class T> 
         T sample(const T &x, const int size, const bool replace, NumericVector prob_ = NumericVector(0) ) {
@@ -50,16 +62,16 @@
             IntegerVector index(size);
             if (probsize == 0) { // No probabilities given
                 if (replace) {
-                    SampleReplace(index, nOrig, size);
+                    SampleReplace<IntegerVector>(index, nOrig, size);
                 } else {
-                    SampleNoReplace(index, nOrig, size);
+                    SampleNoReplace<IntegerVector>(index, nOrig, size);
                 }
             } else { 
                 if (probsize != nOrig) throw std::range_error( "Number of probabilities must equal input vector length" ) ;
                 // normalize, error-check probability vector
                 // fixprob will be modified in-place
                 NumericVector fixprob = clone(prob_);
-                FixProb(fixprob, size, replace);
+                FixProb<NumericVector>(fixprob, size, replace);
                 // 
                 // Copy the given probabilities into an arma vector
                 arma::vec prob(fixprob.begin(), fixprob.size());
@@ -67,13 +79,13 @@
                     // check for walker alias conditions 
                     int walker_test = sum( (probsize * prob) > 0.1);
                     if (walker_test < 200) { 
-                        ProbSampleReplace(index, nOrig, size, prob);
+                        ProbSampleReplace<IntegerVector>(index, nOrig, size, prob);
                     } else {
                         throw std::range_error( "Walker Alias method not implemented. R-core sample() is likely faster for this problem.");
                         // WalkerProbSampleReplace(index, nOrig, size, prob);
                     }
                 } else {
-                    ProbSampleNoReplace(index, nOrig, size, prob);
+                    ProbSampleNoReplace<IntegerVector>(index, nOrig, size, prob);
                 }
             }
             // copy the results into the return vector
@@ -84,14 +96,16 @@
             return(ret);
         }
 
-        void SampleReplace( IntegerVector &index, int nOrig, int size) {
+        template <typename INDEX>
+        void SampleReplace( INDEX &index, int nOrig, int size) {
             int ii;
             for (ii = 0; ii < size; ii++) {
                 index[ii] = nOrig * unif_rand();
             }
         }
 
-        void SampleNoReplace( IntegerVector &index, int nOrig, int size) {
+        template <typename INDEX>
+        void SampleNoReplace( INDEX &index, int nOrig, int size) {
             int ii, jj;
             IntegerVector sub(nOrig);
             for (ii = 0; ii < nOrig; ii++) {
@@ -105,7 +119,8 @@
             }
         }
 
-        void FixProb(NumericVector &prob, const int size, const bool replace) {
+        template <typename PROB>
+        void FixProb(PROB &prob, const int size, const bool replace) {
             // prob is modified in-place.  
             // Is this better than cloning it?
             double sum = 0.0;
@@ -128,7 +143,8 @@
         }
 
         // Unequal probability sampling with replacement 
-        void ProbSampleReplace(IntegerVector &index, int nOrig, int size, arma::vec &prob){
+        template <typename INDEX>
+        void ProbSampleReplace(INDEX &index, int nOrig, int size, arma::vec &prob){
             double rU;
             int ii, jj;
             int nOrig_1 = nOrig - 1;
@@ -148,7 +164,8 @@
         }
 
         // Unequal probability sampling without replacement 
-        void ProbSampleNoReplace(IntegerVector &index, int nOrig, int size, arma::vec &prob){
+        template <typename INDEX>
+        void ProbSampleNoReplace(INDEX &index, int nOrig, int size, arma::vec &prob){
             int ii, jj, kk;
             int nOrig_1 = nOrig - 1;
             double rT, mass, totalmass = 1.0;
@@ -173,3 +190,5 @@
         }
     }
 }
+
+#endif



More information about the Rcpp-commits mailing list