[Rcpp-commits] r1775 - in pkg/Rcpp: . inst inst/examples/SugarPerformance inst/include inst/include/Rcpp/sugar inst/include/Rcpp/sugar/functions inst/include/Rcpp/sugar/matrix inst/include/Rcpp/traits inst/include/Rcpp/vector inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jul 4 10:05:30 CEST 2010


Author: romain
Date: 2010-07-04 10:05:29 +0200 (Sun, 04 Jul 2010)
New Revision: 1775

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep_each.h
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep_len.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/col.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/lower_tri.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/row.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/tools.h
   pkg/Rcpp/inst/include/Rcpp/sugar/matrix/upper_tri.h
   pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h
   pkg/Rcpp/inst/unitTests/runit.sugar.matrix.R
Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/examples/SugarPerformance/sugarBenchmarks.R
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
   pkg/Rcpp/inst/include/Rcpp/traits/result_of.h
   pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
   pkg/Rcpp/inst/include/RcppCommon.h
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
some more sugar blocks

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-07-04 03:40:42 UTC (rev 1774)
+++ pkg/Rcpp/TODO	2010-07-04 08:05:29 UTC (rev 1775)
@@ -36,7 +36,9 @@
     o   Remove the C++ObjectS3 class as it is no longer needed:
         http://permalink.gmane.org/gmane.comp.lang.r.devel/24610
 
-    o   Order of classes in POSIXt, ...
+    o   Order of classes in POSIXt: this currently is implemented internally
+    	with conditional compiling based on the version of R. The conditional
+    	compiling must disappear when we can depend on R 2.12.0
         
 Syntactic sugar
 
@@ -51,21 +53,23 @@
         
         min( x ) < 4
     
-    o   for matrices: we first need CRTP with matrix interface, so we need to
-        make Matrix inherit from MatrixBase and make MatrixBase a template tha  t
-        implements CRTP and a matrix interface: 
-                - operator[]                (treating the matrix as a vector)
-                - operator()( int, int )    (matrix indexing)
-                - nrow                      (number of rows)
-                - ncol                      (number of columns)     
-                - size                      (nrow*ncol)
-        
-    o   matrix functions : row, col, lower_tri, upper_tri, outer, diag, apply
+    o   matrix functions : diag, apply
     
     o   for character vectors: nchar, grepl, sub, gsub
     
     o   Compound operators: ++,--,+=, -=, ...
         
+    o	lazy evaluation might not be the best thing for the sugar 
+    	implementation of outer. For example : 
+    	
+    	outer( x + y, z , plus<double>() )
+    	
+    	has to evaluate x[i] + y[i] as many times as the length
+    	of z. perhaps a better implementation would be to 
+    	first force the evaluation of x + y.
+
+    o	primitive should be allowed in ifelse, on the lhs, rhs or both
+    
 Testing
 
     o   we need to update the doRUnit.R file and replace : 

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-07-04 03:40:42 UTC (rev 1774)
+++ pkg/Rcpp/inst/ChangeLog	2010-07-04 08:05:29 UTC (rev 1775)
@@ -1,3 +1,22 @@
+2010-07-02  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/traits/result_of.h : also deal with functions
+	taking two arguments (useful for e.g. outer)
+
+	* inst/include/Rcpp/vector/MatrixBase.h : new CRTP base class
+	for Matrix to facilitate sugar syntax on matrices.
+	
+	* inst/include/Rcpp/sugar/matrix/outer.h : new sugar function: outer
+	* inst/include/Rcpp/sugar/matrix/row.h: new sugar function: row
+	* inst/include/Rcpp/sugar/matrix/col.h: new sugar function: col
+	* inst/include/Rcpp/sugar/matrix/lower_tri.h: new sugar function: lower_tri
+	* inst/include/Rcpp/sugar/matrix/upper_tri.h: new sugar function: upper_tri
+
+	* inst/include/Rcpp/sugar/functions/rep.h: new sugar function : rep
+	* inst/include/Rcpp/sugar/functions/rep.h: new sugar function : rep_len
+	* inst/include/Rcpp/sugar/functions/rep.h: new sugar function : rep_each
+	
+	
 2010-07-02  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/RcppStringVector: Now uses std::vector<std::string>

Modified: pkg/Rcpp/inst/examples/SugarPerformance/sugarBenchmarks.R
===================================================================
--- pkg/Rcpp/inst/examples/SugarPerformance/sugarBenchmarks.R	2010-07-04 03:40:42 UTC (rev 1774)
+++ pkg/Rcpp/inst/examples/SugarPerformance/sugarBenchmarks.R	2010-07-04 08:05:29 UTC (rev 1775)
@@ -193,7 +193,7 @@
 
 results[[ "hand/sugar" ]] <- results[["hand.written" ]] / results[["sugar"]] 
 results[[ "R/sugar" ]]    <- results[["R" ]]            / results[["sugar"]] 
-results <- results[ order( results[["expr"]], results[["runs"]] ), ]
+# results <- results[ order( results[["expr"]], results[["runs"]] ), ]
 
 options( width = 300 )
 print( results )

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-07-04 03:40:42 UTC (rev 1774)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -38,11 +38,13 @@
 #include <Rcpp/sugar/functions/floor.h>
 #include <Rcpp/sugar/functions/ceil.h>
 #include <Rcpp/sugar/functions/pow.h>
+#include <Rcpp/sugar/functions/rep.h>
+#include <Rcpp/sugar/functions/rep_len.h>
+#include <Rcpp/sugar/functions/rep_each.h>
 
 #include <Rcpp/sugar/functions/Re.h>
 #include <Rcpp/sugar/functions/Im.h>
 #include <Rcpp/sugar/functions/Conj.h>
 #include <Rcpp/sugar/functions/Mod.h>
 
-
 #endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,57 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// rep.h: Rcpp R/C++ interface class library -- rep
+//
+// 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__sugar__rep_h
+#define Rcpp__sugar__rep_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename T>
+class Rep : public Rcpp::VectorBase< RTYPE ,NA, Rep<RTYPE,NA,T> > {
+public:
+	typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	
+	Rep( const VEC_TYPE& object_, int times_ ) : 
+		object(object_), times(times_), n(object_.size()){}
+	
+	inline STORAGE operator[]( int i ) const {
+		return object[ i % n ] ;
+	}
+	inline int size() const { return times * n ; }
+
+private:
+	const VEC_TYPE& object ;
+	int times, n ;
+} ;
+	
+} // sugar
+
+template <int RTYPE, bool NA, typename T>
+inline sugar::Rep<RTYPE,NA,T> rep( const VectorBase<RTYPE,NA,T>& t, int n ){
+	return sugar::Rep<RTYPE,NA,T>( t, n ) ;
+}
+
+
+} // Rcpp
+#endif
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep_each.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep_each.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep_each.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,58 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// rep_each.h: Rcpp R/C++ interface class library -- rep_each
+//
+// 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__sugar__rep_each_h
+#define Rcpp__sugar__rep_each_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename T>
+class Rep_each : public Rcpp::VectorBase< RTYPE ,NA, Rep_each<RTYPE,NA,T> > {
+public:
+	typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+	Rep_each( const VEC_TYPE& object_, int times_ ) : 
+		object(object_), times(times_), n(object.size()) {}
+
+	inline STORAGE operator[]( int i ) const {
+		return object[ i / times ] ;
+	}
+	inline int size() const { return n * times ; }
+
+private:
+	const VEC_TYPE& object ;
+	int times; 
+	int n ;
+} ;
+
+} // sugar
+
+template <int RTYPE, bool NA, typename T>
+inline sugar::Rep_each<RTYPE,NA,T> rep_each( const VectorBase<RTYPE,NA,T>& t, int times ){
+	return sugar::Rep_each<RTYPE,NA,T>( t, times ) ;
+}
+
+
+} // Rcpp
+#endif
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep_len.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep_len.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/rep_len.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,57 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// rep_len.h: Rcpp R/C++ interface class library -- rep_len
+//
+// 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__sugar__rep_len_h
+#define Rcpp__sugar__rep_len_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename T>
+class Rep_len : public Rcpp::VectorBase< RTYPE ,NA, Rep_len<RTYPE,NA,T> > {
+public:
+	typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	
+	Rep_len( const VEC_TYPE& object_, int len_ ) : 
+		object(object_), len(len_), n(object_.size()){}
+	
+	inline STORAGE operator[]( int i ) const {
+		return object[ i % n ] ;
+	}
+	inline int size() const { return len ; }
+	         
+private:
+	const VEC_TYPE& object ;
+	int len, n ;
+} ;
+	
+} // sugar
+
+template <int RTYPE, bool NA, typename T>
+inline sugar::Rep_len<RTYPE,NA,T> rep_len( const VectorBase<RTYPE,NA,T>& t, int len ){
+	return sugar::Rep_len<RTYPE,NA,T>( t, len ) ;
+}
+
+
+} // Rcpp
+#endif
+

Added: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/col.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/col.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/col.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,64 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// col.h: Rcpp R/C++ interface class library -- col
+//
+// 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__sugar__col_h
+#define Rcpp__sugar__col_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool LHS_NA, typename LHS_T>
+class Col : public MatrixBase< 
+	INTSXP , 
+	false ,
+	Col<RTYPE,LHS_NA,LHS_T>
+> {
+public:
+	typedef Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+	
+	Col( const LHS_TYPE& lhs) : nr( lhs.ncol() ), nc( lhs.ncol() ) {}
+	
+	inline int operator[]( int index ) const {
+		return Rcpp::internal::get_column( index, nr) ;
+	}
+	inline int operator()( int i, int j ) const {
+		return j ;
+	}
+	
+	inline int size() const { return nr * nc ; }
+	inline int nrow() const { return nr; }
+	inline int ncol() const { return nc; }
+	         
+private:
+	int nr, nc ;
+} ;
+	
+} // sugar
+
+template <int RTYPE, bool LHS_NA, typename LHS_T>
+inline sugar::Col<RTYPE,LHS_NA,LHS_T> 
+col( const Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T>& lhs){
+	return sugar::Col<RTYPE,LHS_NA,LHS_T>( lhs ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/lower_tri.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/lower_tri.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/lower_tri.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,81 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// lower_tri.h: Rcpp R/C++ interface class library -- lower.tri
+//
+// 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__sugar__lower_tri_h
+#define Rcpp__sugar__lower_tri_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool LHS_NA, typename LHS_T>
+class LowerTri : public VectorBase< 
+	LGLSXP , 
+	false ,
+	LowerTri<RTYPE,LHS_NA,LHS_T>
+> {
+public:
+	typedef Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+	
+	LowerTri( const LHS_TYPE& lhs, bool diag) : 
+		nr( lhs.nrow() ), nc( lhs.ncol() ), 
+		getter( diag ? (&LowerTri::get_diag_true) : (&LowerTri::get_diag_false) ){}
+	
+	inline int operator[]( int index ) const {
+		int i = Rcpp::internal::get_line( index, nr ) ;
+		int j = Rcpp::internal::get_column( index, nr, i ) ;
+		return get(i,j) ; 
+	}
+	inline int operator()( int i, int j ) const {
+		return get(i,j) ;
+	}
+	
+	inline int size() const { return nr * nc ; }
+	inline int nrow() const { return nr; }
+	inline int ncol() const { return nc; }
+	         
+private:
+	int nr, nc ;
+	typedef bool (LowerTri::*Method)(int,int) ;
+	
+	Method getter ;
+	inline bool get_diag_true( int i, int j ){
+		return i <= j ;
+	}
+	inline bool get_diag_false( int i, int j ){
+		return i < j ;
+	}
+	inline bool get( int i, int j){
+		return (this->*getter)(i, j ) ;
+	}
+	
+} ;
+	
+} // sugar
+
+template <int RTYPE, bool LHS_NA, typename LHS_T>
+inline sugar::LowerTri<RTYPE,LHS_NA,LHS_T>
+lower_tri( const Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T>& lhs, bool diag = false){
+	return sugar::LowerTri<RTYPE,LHS_NA,LHS_T>( lhs, diag ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/matrix_functions.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,32 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// matrix_functions.h: Rcpp R/C++ interface class library -- matrix sugar functions
+//
+// 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_SUGAR_MATRIX_FUNCTIONS_H
+#define RCPP_SUGAR_MATRIX_FUNCTIONS_H
+
+#include <Rcpp/sugar/matrix/tools.h>
+#include <Rcpp/sugar/matrix/outer.h>
+#include <Rcpp/sugar/matrix/row.h>
+#include <Rcpp/sugar/matrix/col.h>
+#include <Rcpp/sugar/matrix/lower_tri.h>
+#include <Rcpp/sugar/matrix/upper_tri.h>
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/outer.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,89 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// outer.h: Rcpp R/C++ interface class library -- outer
+//
+// 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__sugar__outer_h
+#define Rcpp__sugar__outer_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, 
+	bool LHS_NA, typename LHS_T,
+	bool RHS_NA, typename RHS_T,
+	typename Function >
+class Outer : public MatrixBase< 
+	Rcpp::traits::r_sexptype_traits<
+		typename ::Rcpp::traits::result_of<Function>::type
+	>::rtype , 
+	true ,
+	Outer<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T,Function>
+> {
+public:
+	typedef typename ::Rcpp::traits::result_of<Function>::type result_type ;
+	const static int RESULT_R_TYPE = 
+		Rcpp::traits::r_sexptype_traits<result_type>::rtype ;
+	
+	typedef Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+	typedef Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
+	typedef typename Rcpp::traits::r_vector_element_converter<RESULT_R_TYPE>::type converter_type ;
+	typedef typename Rcpp::traits::storage_type<RESULT_R_TYPE>::type STORAGE ;
+	
+	Outer( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_, Function fun_ ) : 
+		lhs(lhs_), rhs(rhs_), fun(fun_), nr(lhs_.size()), nc(rhs_.size()) {}
+	
+	inline STORAGE operator[]( int index ) const {
+		int i = Rcpp::internal::get_line( index, nr ) ;
+		int j = Rcpp::internal::get_column( index, nr, i ) ;
+		return converter_type::get( fun( lhs[i], rhs[j] ) );
+	}
+	inline STORAGE operator()( int i, int j ) const {
+		return converter_type::get( fun( lhs[i], rhs[j] ) );
+	}
+	
+	inline int size() const { return nr * nc ; }
+	inline int nrow() const { return nr; }
+	inline int ncol() const { return nc; }
+	         
+private:
+	const LHS_TYPE& lhs ;
+	const RHS_TYPE& rhs ;
+	Function fun ;
+	int nr, nc ;
+} ;
+	
+} // sugar
+
+template <int RTYPE, 
+	bool LHS_NA, typename LHS_T,
+	bool RHS_NA, typename RHS_T,
+	typename Function >
+inline sugar::Outer<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T,Function> 
+outer( 
+	const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs,
+	const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs,
+	Function fun ){
+
+	return sugar::Outer<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T,Function>( lhs, rhs, fun ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/row.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/row.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/row.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,64 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// row.h: Rcpp R/C++ interface class library -- row
+//
+// 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__sugar__row_h
+#define Rcpp__sugar__row_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool LHS_NA, typename LHS_T>
+class Row : public VectorBase< 
+	INTSXP , 
+	false ,
+	Row<RTYPE,LHS_NA,LHS_T>
+> {
+public:
+	typedef Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+	
+	Row( const LHS_TYPE& lhs) : nr( lhs.nrow() ), nc( lhs.ncol() ) {}
+	
+	inline int operator[]( int index ) const {
+		return Rcpp::internal::get_line( index, nr) ;
+	}
+	inline int operator()( int i, int j ) const {
+		return i ;
+	}
+	
+	inline int size() const { return nr * nc ; }
+	inline int nrow() const { return nr; }
+	inline int ncol() const { return nc; }
+	         
+private:
+	int nr, nc ;
+} ;
+	
+} // sugar
+
+template <int RTYPE, bool LHS_NA, typename LHS_T>
+inline sugar::Row<RTYPE,LHS_NA,LHS_T>
+row( const Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T>& lhs){
+	return sugar::Row<RTYPE,LHS_NA,LHS_T>( lhs ) ;
+}
+
+} // Rcpp
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/tools.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/tools.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/tools.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,44 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// matrix_functions.h: Rcpp R/C++ interface class library -- matrix sugar functions
+//
+// 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_SUGAR_MATRIX_TOOLS_H
+#define RCPP_SUGAR_MATRIX_TOOLS_H
+
+namespace Rcpp{
+namespace internal{
+
+	inline int get_line( int index, int nr ){
+		return index % nr ;
+	}
+
+	inline int get_column( int index, int nr ){
+		int i = get_line( index, nr ); 
+		return (index-i) / nr ;
+	}
+	
+	inline int get_column( int index, int nr, int i ){
+		return (index-i) / nr ;
+	}
+
+}
+}
+
+#endif

Added: pkg/Rcpp/inst/include/Rcpp/sugar/matrix/upper_tri.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/matrix/upper_tri.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/matrix/upper_tri.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,81 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// upper_tri.h: Rcpp R/C++ interface class library -- lower.tri
+//
+// 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__sugar__upper_tri_h
+#define Rcpp__sugar__upper_tri_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool LHS_NA, typename LHS_T>
+class UpperTri : public VectorBase< 
+	LGLSXP , 
+	false ,
+	UpperTri<RTYPE,LHS_NA,LHS_T>
+> {
+public:
+	typedef Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+	
+	UpperTri( const LHS_TYPE& lhs, bool diag) : 
+		nr( lhs.nrow() ), nc( lhs.ncol() ), 
+		getter( diag ? (&UpperTri::get_diag_true) : (&UpperTri::get_diag_false) ){}
+	
+	inline int operator[]( int index ) const {
+		int i = Rcpp::internal::get_line( index, nr ) ;
+		int j = Rcpp::internal::get_column( index, nr, i ) ;
+		return get(i,j) ; 
+	}
+	inline int operator()( int i, int j ) const {
+		return get(i,j) ;
+	}
+	
+	inline int size() const { return nr * nc ; }
+	inline int nrow() const { return nr; }
+	inline int ncol() const { return nc; }
+	         
+private:
+	int nr, nc ;
+	typedef bool (UpperTri::*Method)(int,int) ;
+	
+	Method getter ;
+	inline bool get_diag_true( int i, int j ){
+		return i >= j ;
+	}
+	inline bool get_diag_false( int i, int j ){
+		return i > j ;
+	}
+	inline bool get( int i, int j){
+		return (this->*getter)(i, j ) ;
+	}
+	
+} ;
+	
+} // sugar
+
+template <int RTYPE, bool LHS_NA, typename LHS_T>
+inline sugar::UpperTri<RTYPE,LHS_NA,LHS_T>
+upper_tri( const Rcpp::MatrixBase<RTYPE,LHS_NA,LHS_T>& lhs, bool diag = false){
+	return sugar::UpperTri<RTYPE,LHS_NA,LHS_T>( lhs, diag ) ;
+}
+
+} // Rcpp
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-07-04 03:40:42 UTC (rev 1774)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -24,5 +24,6 @@
 
 #include <Rcpp/sugar/operators/operators.h>
 #include <Rcpp/sugar/functions/functions.h>
+#include <Rcpp/sugar/matrix/matrix_functions.h>
 
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/traits/result_of.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/traits/result_of.h	2010-07-04 03:40:42 UTC (rev 1774)
+++ pkg/Rcpp/inst/include/Rcpp/traits/result_of.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -36,6 +36,11 @@
 	typedef RESULT_TYPE type ;
 } ;
 
+template <typename RESULT_TYPE, typename U1, typename U2>
+struct result_of< RESULT_TYPE (*)(U1, U2) >{
+	typedef RESULT_TYPE type ;
+} ;
+
 }
 }     
 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-07-04 03:40:42 UTC (rev 1774)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -23,13 +23,14 @@
 #define Rcpp__vector__Matrix_h
    
 template <int RTYPE> 
-class Matrix : public Vector<RTYPE> {
+class Matrix : public Vector<RTYPE>, public MatrixBase<RTYPE,true, Matrix<RTYPE> > {
 public:
 	typedef Vector<RTYPE> VECTOR ;
 	typedef typename VECTOR::iterator iterator ;
     typedef typename VECTOR::converter_type converter_type ;
     typedef typename VECTOR::stored_type stored_type ;
-    	
+    typedef typename VECTOR::Proxy Proxy ;
+    
 	Matrix() : VECTOR() {}
 	
 	Matrix(SEXP x) throw(not_compatible) : VECTOR(){
@@ -67,6 +68,32 @@
 		return *this ;
 	}
 	
+	template <bool NA, typename MAT>
+    Matrix( const MatrixBase<RTYPE,NA,MAT>& other ) : VECTOR() {
+    	int nr = other.nrow() ;
+    	int nc = other.ncol() ;
+    	SEXP x = PROTECT( Rf_allocVector( RTYPE, nr * nc ) ) ;
+    	SEXP d = PROTECT( Rf_allocVector( INTSXP, 2) ) ;
+    	INTEGER(d)[0] = nr ;
+    	INTEGER(d)[1] = nc ;
+    	Rf_setAttrib( x, Rf_install("dim"), d ) ;
+    	RObject::setSEXP( x ) ;
+    	UNPROTECT( 2 ) ;
+    	import_matrix_expression<NA,MAT>( other, nr * nc ) ;
+	}
+   
+private:
+	
+	template <bool NA, typename MAT>
+    void import_matrix_expression( const MatrixBase<RTYPE,NA,MAT>& other, int n ){
+    	iterator start = VECTOR::begin() ; 
+		for( int i=0; i<n; i++, ++start){
+			*start = other[i] ;
+		}
+    }
+
+public:
+	
 	template <typename U>
     void fill_diag( const U& u){
     	fill_diag__dispatch( typename traits::is_trivial<RTYPE>::type(), u ) ;	
@@ -79,6 +106,13 @@
     	return res ;
     }
     
+    inline Proxy operator[]( int i ){
+    	return static_cast< Vector<RTYPE>* >( this )->operator[]( i ) ;
+    }
+    inline Proxy operator()( int i, int j ){
+    	return static_cast< Vector<RTYPE>* >( this )->operator()( i, j ) ;
+    }
+    
 private:
 	virtual void update(){
 		RCPP_DEBUG( "%s::update", DEMANGLE(Matrix) ) ;

Added: pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/vector/MatrixBase.h	2010-07-04 08:05:29 UTC (rev 1775)
@@ -0,0 +1,144 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// MatrixBase.h: Rcpp R/C++ interface class library -- 
+//
+// 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__vector__MatrixBase_h
+#define Rcpp__vector__MatrixBase_h
+
+namespace Rcpp{
+	
+/** a base class for vectors, modelled after the CRTP */
+template <int RTYPE, bool na, typename MATRIX>
+class MatrixBase : public traits::expands_to_logical__impl<RTYPE> {
+public:
+	struct r_type : traits::integral_constant<int,RTYPE>{} ;
+	struct can_have_na : traits::integral_constant<bool,na>{} ;
+	typedef typename traits::storage_type<RTYPE>::type stored_type ;
+	
+	MATRIX& get_ref(){
+		return static_cast<MATRIX&>(*this) ;
+	}
+
+	inline stored_type operator[]( int i) const { 
+		return static_cast<const MATRIX*>(this)->operator[](i) ;
+	}
+	
+	inline stored_type operator()( int i, int j){
+		return static_cast<const MATRIX*>(this)->operator()(i, j) ;
+	}
+	
+	inline int size() const { return static_cast<const MATRIX&>(*this).size() ; }
+	inline int nrow() const { return static_cast<const MATRIX&>(*this).nrow() ; }
+	inline int ncol() const { return static_cast<const MATRIX&>(*this).ncol() ; }
+	
+	class iterator {
+	public:
+		typedef stored_type reference ;
+		typedef stored_type* pointer ;
+		typedef int difference_type ;
+		typedef stored_type value_type;
+		typedef std::random_access_iterator_tag iterator_category ;
+
+		iterator( const MatrixBase& object_, int index_ ) : object(object_), index(index_){} 
+		iterator( const iterator& other) : object(other.object), index(other.index){};
+		
+		inline iterator& operator++(){
+			index++ ;
+			return *this ;
+		}
+		inline iterator& operator++(int){
+			index++;
+			return *this ;
+		}
+		
+		inline iterator& operator--(){
+			index-- ;
+			return *this ;
+		}
+		inline iterator& operator--(int){
+			index--; 
+			return *this ;
+		}
+		                    
+		inline iterator operator+(difference_type n) const {
+			return iterator( object, index+n ) ;
+		}
+		inline iterator operator-(difference_type n) const {
+			return iterator( object, index-n ) ;
+		}
+		
+		inline iterator& operator+=(difference_type n) {
+			index += n ;
+			return *this ;
+		}
+		inline iterator& operator-=(difference_type n) {
+			index -= n; 
+			return *this ;
+		}
+
+		inline reference operator*() {
+			//  TODO: it might be better to call object( i, j )
+			//        as in many cases the sugar expression 
+			//        is faster with two indexes
+			return object[index] ;
+		}
+		inline pointer operator->(){
+			return &object[index] ;
+		}
+		
+		inline bool operator==( const iterator& y) const {
+			return ( index == y.index ) ;
+		}
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rcpp -r 1775


More information about the Rcpp-commits mailing list