[Rcpp-commits] r1830 - in pkg/Rcpp: . inst inst/include/Rcpp/sugar/functions inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 8 11:02:35 CEST 2010


Author: romain
Date: 2010-07-08 11:02:34 +0200 (Thu, 08 Jul 2010)
New Revision: 1830

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/head.h
Modified:
   pkg/Rcpp/NEWS
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
+ sugar : head

Modified: pkg/Rcpp/NEWS
===================================================================
--- pkg/Rcpp/NEWS	2010-07-07 18:52:35 UTC (rev 1829)
+++ pkg/Rcpp/NEWS	2010-07-08 09:02:34 UTC (rev 1830)
@@ -1,6 +1,6 @@
 0.8.4   (under development)
 
-	o	new sugar functions: rep, rep_len, rep_each, rev
+	o	new sugar functions: rep, rep_len, rep_each, rev, head, tail
 	
     o	sugar has been extended to matrices: The Matrix class now extends the 
     	Matrix_Base template that implements CRTP. Currently sugar functions 

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-07-07 18:52:35 UTC (rev 1829)
+++ pkg/Rcpp/TODO	2010-07-08 09:02:34 UTC (rev 1830)
@@ -45,7 +45,7 @@
         
 Syntactic sugar
 
-    o   duplicated, unique, count, sum, head, tail, sqrt, log, log10, ln
+    o   duplicated, unique, count, sum, sqrt, log, log10, ln
     
     o	operator% 
     

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-07-07 18:52:35 UTC (rev 1829)
+++ pkg/Rcpp/inst/ChangeLog	2010-07-08 09:02:34 UTC (rev 1830)
@@ -1,3 +1,7 @@
+2010-07-08  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/include/Rcpp/sugar/functions/head.h: new sugar function : head
+
 2010-07-07  Romain Francois <romain at r-enthusiasts.com>
 
 	* inst/include/Rcpp/sugar/functions/rep.h: version of rep that takes a

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-07-07 18:52:35 UTC (rev 1829)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-07-08 09:02:34 UTC (rev 1830)
@@ -42,6 +42,7 @@
 #include <Rcpp/sugar/functions/rep_len.h>
 #include <Rcpp/sugar/functions/rep_each.h>
 #include <Rcpp/sugar/functions/rev.h>
+#include <Rcpp/sugar/functions/head.h>
 
 #include <Rcpp/sugar/functions/Re.h>
 #include <Rcpp/sugar/functions/Im.h>

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/head.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/head.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/head.h	2010-07-08 09:02:34 UTC (rev 1830)
@@ -0,0 +1,62 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// head.h: Rcpp R/C++ interface class library -- head
+//
+// 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__head_h
+#define Rcpp__sugar__head_h
+
+namespace Rcpp{
+namespace sugar{
+	
+template <int RTYPE, bool NA, typename T>
+class Head : public Rcpp::VectorBase< RTYPE ,NA, Head<RTYPE,NA,T> > {
+public:
+	typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	
+	Head( const VEC_TYPE& object_, int n_ ) : object(object_), n(n_) {
+		if( n < 0 ){
+			n = object.size() + n ;
+		}
+	}
+	
+	inline STORAGE operator[]( int i ) const {
+		return object[ i ] ;
+	}
+	inline int size() const { return n; }
+	         
+private:
+	const VEC_TYPE& object ;
+	int n ;
+} ;
+	
+} // sugar
+
+template <int RTYPE,bool NA, typename T>
+inline sugar::Head<RTYPE,NA,T> head( 
+	const VectorBase<RTYPE,NA,T>& t, 
+	int n 
+	){
+	return sugar::Head<RTYPE,NA,T>( t, n ) ;
+}
+
+} // Rcpp
+#endif
+

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-07-07 18:52:35 UTC (rev 1829)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-07-08 09:02:34 UTC (rev 1830)
@@ -518,6 +518,16 @@
 					_["col"] = col( xx )
 					) ;
 				'	
+			), 
+			"runit_head" = list( 
+				signature( x = "numeric" ), 
+				'
+					NumericVector xx(x) ;
+					return List::create( 
+						_["pos"] = head( xx, 5 ), 
+						_["neg"] = head( xx, -5 )
+					) ;
+				'
 			)
 			
 		)
@@ -983,6 +993,13 @@
 	checkEquals( fx(1:10), rev( 1:10 * 1:10 ) )
 }
 
+test.sugar.head <- function(){
+	fx <- .rcpp.sugar$runit_head
+	checkEquals( 
+		fx(1:100), 
+		list( pos = 1:5, neg = 1:95 )
+		)
+}
 
 
 



More information about the Rcpp-commits mailing list