[Rcpp-commits] r3611 - in pkg/Rcpp: . inst/unitTests/testRcppModule/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu May 24 15:51:13 CEST 2012


Author: edd
Date: 2012-05-24 15:51:13 +0200 (Thu, 24 May 2012)
New Revision: 3611

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/inst/unitTests/testRcppModule/src/Num.cpp
   pkg/Rcpp/inst/unitTests/testRcppModule/src/rcpp_module.cpp
   pkg/Rcpp/inst/unitTests/testRcppModule/src/stdVector.cpp
Log:
some editing and cleanup in main Module regression test file


Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-05-21 12:29:28 UTC (rev 3610)
+++ pkg/Rcpp/ChangeLog	2012-05-24 13:51:13 UTC (rev 3611)
@@ -1,3 +1,12 @@
+2012-05-24  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/unitTests/testRcppModule/src/rcpp_module.cpp: added more
+	comments, added copyright header, corrected output of example
+	functions by not escaping "backslash-n" twice
+	* inst/unitTests/testRcppModule/src/stdVector.cpp: added some
+	comments, added copyright header
+	* inst/unitTests/testRcppModule/src/Num.cpp: Idem
+
 2012-05-13  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/include/Rcpp/iostream/Rostream.h: Added Rstreambuf.h include

Modified: pkg/Rcpp/inst/unitTests/testRcppModule/src/Num.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/testRcppModule/src/Num.cpp	2012-05-21 12:29:28 UTC (rev 3610)
+++ pkg/Rcpp/inst/unitTests/testRcppModule/src/Num.cpp	2012-05-24 13:51:13 UTC (rev 3611)
@@ -1,12 +1,33 @@
-#include "rcpp_hello_world.h"
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// Num.cpp: Rcpp R/C++ interface class library -- Rcpp Module example
+//
+// Copyright (C) 2010 - 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/>.
 
-class Num{
-public:
+#include <Rcpp.h>
+
+class Num {                     // simple class with two private variables
+public:                         // which have a getter/setter and getter
     Num() : x(0.0), y(0){} ;
 
     double getX() { return x ; }
     void setX(double value){ x = value ; }
-
+    
     int getY() { return y ; }
 
 private:
@@ -15,16 +36,16 @@
 };
 
 RCPP_MODULE(NumEx){
-	using namespace Rcpp ;
+    using namespace Rcpp ;
 
-	class_<Num>( "Num" )
+    class_<Num>( "Num" )
 	
-	    .default_constructor()
+        .default_constructor()
 
-		// read and write property
-		.property( "x", &Num::getX, &Num::setX )
+        // read and write property
+        .property( "x", &Num::getX, &Num::setX )
 
-		// read-only property
-		.property( "y", &Num::getY )
+        // read-only property
+        .property( "y", &Num::getY )
 	;
 }

Modified: pkg/Rcpp/inst/unitTests/testRcppModule/src/rcpp_module.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/testRcppModule/src/rcpp_module.cpp	2012-05-21 12:29:28 UTC (rev 3610)
+++ pkg/Rcpp/inst/unitTests/testRcppModule/src/rcpp_module.cpp	2012-05-24 13:51:13 UTC (rev 3611)
@@ -1,26 +1,49 @@
-#include <Rcpp.h>
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// rcpp_module.cpp: Rcpp R/C++ interface class library -- Rcpp Module examples
+//
+// Copyright (C) 2010 - 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/>.
 
-int bar( int x){
-	return x*2 ;
+
+#include <Rcpp.h>               // need to include the main Rcpp header file only
+
+int bar(int x) {                // simple free function returning an int
+    return x*2;
 }
         
-double foo( int x, double y){
-	return x * y ;
+double foo(int x, double y) {   // simple free function returning a double
+    return x * y;
 }
 
-void bla( ){
-	Rprintf( "hello\\n" ) ;
+void bla( ) {                   // no input or return but output side effect
+    Rprintf( "hello\n" );
 }
 
-void bla1( int x){
-	Rprintf( "hello (x = %d)\\n", x ) ;
+void bla1(int x) {              // output reflecting a single input
+    Rprintf( "hello (x = %d)\n", x);
 }
   
-void bla2( int x, double y){
-	Rprintf( "hello (x = %d, y = %5.2f)\\n", x, y ) ;
+void bla2(int x, double y) {    // output reflecting two inputs
+    Rprintf( "hello (x = %d, y = %5.2f)\n", x, y);
 }
 
-class World {
+
+class World {                   // a simple class with a setter and getter
 public:
     World() : msg("hello"){}
     void set(std::string msg) { this->msg = msg; }
@@ -33,20 +56,18 @@
 
 
 RCPP_MODULE(yada){
-	using namespace Rcpp ;
+    using namespace Rcpp;
 	                  
-	function( "bar"   , &bar   ) ;
-	function( "foo"   , &foo   ) ;
-	function( "bla"   , &bla   ) ;
-	function( "bla1"  , &bla1   ) ;
-	function( "bla2"  , &bla2   ) ;
+    function( "bar"   , &bar   );
+    function( "foo"   , &foo   );
+    function( "bla"   , &bla   );
+    function( "bla1"  , &bla1   );
+    function( "bla2"  , &bla2   );
 	
-	class_<World>( "World" )
-	
-	    .default_constructor()
-	    
-		.method( "greet", &World::greet )
-		.method( "set", &World::set )
+    class_<World>( "World" )
+        .default_constructor()
+        .method( "greet", &World::greet )
+        .method( "set", &World::set )
 	;
 }                     
 

Modified: pkg/Rcpp/inst/unitTests/testRcppModule/src/stdVector.cpp
===================================================================
--- pkg/Rcpp/inst/unitTests/testRcppModule/src/stdVector.cpp	2012-05-21 12:29:28 UTC (rev 3610)
+++ pkg/Rcpp/inst/unitTests/testRcppModule/src/stdVector.cpp	2012-05-24 13:51:13 UTC (rev 3611)
@@ -1,28 +1,49 @@
-#include "rcpp_hello_world.h"
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// stdVector.cpp: Rcpp R/C++ interface class library -- Rcpp Module class example
+//
+// Copyright (C) 2010 - 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/>.
 
+#include <Rcpp.h>               // need to include the main Rcpp header file only
+
 // convenience typedef
-typedef std::vector<double> vec ;
+typedef std::vector<double> vec;
 
 // helpers
-void vec_assign( vec* obj, Rcpp::NumericVector data ){
-	obj->assign( data.begin(), data.end() ) ;
+void vec_assign( vec* obj, Rcpp::NumericVector data) {
+    obj->assign( data.begin(), data.end() ) ;
 }
 
-void vec_insert( vec* obj, int position, Rcpp::NumericVector data){
-	vec::iterator it = obj->begin() + position ;
-	obj->insert( it, data.begin(), data.end() ) ;
+void vec_insert( vec* obj, int position, Rcpp::NumericVector data) {
+    vec::iterator it = obj->begin() + position;
+    obj->insert( it, data.begin(), data.end() );
 }
 
-Rcpp::NumericVector vec_asR( vec* obj ){
-	return Rcpp::wrap( *obj ) ;
+Rcpp::NumericVector vec_asR( vec* obj) {
+    return Rcpp::wrap( *obj );
 }
 
-void vec_set( vec* obj, int i, double value ){
-	obj->at( i ) = value ;
+void vec_set( vec* obj, int i, double value) {
+    obj->at( i ) = value;
 }
 
-void vec_resize( vec* obj, int n){ obj->resize( n ) ; }
-void vec_push_back( vec* obj, double x ){ obj->push_back( x ); }
+void vec_resize( vec* obj, int n) { obj->resize( n ); }
+void vec_push_back( vec* obj, double x ) { obj->push_back( x ); }
 
 // Wrappers for member functions that return a reference
 // Required on Solaris 
@@ -34,36 +55,36 @@
     using namespace Rcpp ;
 
     // we expose the class std::vector<double> as "vec" on the R side
-    class_<vec>( "vec")
+    class_<vec>("vec")
     
-    // exposing the default constructor
-    .constructor() 
+        // exposing the default constructor
+        .constructor() 
 
-    // exposing member functions
-    .method( "size", &vec::size)
-    .method( "max_size", &vec::max_size)
-    .method( "capacity", &vec::capacity)
-    .method( "empty", &vec::empty)
-    .method( "reserve", &vec::reserve)
-    .method( "pop_back", &vec::pop_back )
-    .method( "clear", &vec::clear )
-
-    // specifically exposing const member functions
-    .method( "back", &vec_back )
-    .method( "front", &vec_front )
-    .method( "at", &vec_at )
+        // exposing member functions -- taken directly from std::vector<double>
+        .method( "size",     &vec::size)
+        .method( "max_size", &vec::max_size)
+        .method( "capacity", &vec::capacity)
+        .method( "empty",    &vec::empty)
+        .method( "reserve",  &vec::reserve)
+        .method( "pop_back", &vec::pop_back )
+        .method( "clear",    &vec::clear )
+        
+        // specifically exposing const member functions defined above
+        .method( "back",     &vec_back )
+        .method( "front",    &vec_front )
+        .method( "at",       &vec_at )
     
-    // exposing free functions taking a std::vector<double>*
-    // as their first argument
-    .method( "assign", &vec_assign )
-    .method( "insert", &vec_insert )
-    .method( "as.vector", &vec_asR )
-    .method( "push_back", &vec_push_back )
-    .method( "resize", &vec_resize)
+        // exposing free functions taking a std::vector<double>*
+        // as their first argument
+        .method( "assign",   &vec_assign )
+        .method( "insert",   &vec_insert )
+        .method( "as.vector",&vec_asR )
+        .method( "push_back",&vec_push_back )
+        .method( "resize",   &vec_resize)
     
-    // special methods for indexing
-    .method( "[[", &vec_at )
-    .method( "[[<-", &vec_set )
+        // special methods for indexing
+        .method( "[[",       &vec_at )
+        .method( "[[<-",     &vec_set )
 
 	;
 }



More information about the Rcpp-commits mailing list