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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 8 11:10:32 CEST 2010


Author: romain
Date: 2010-07-08 11:10:32 +0200 (Thu, 08 Jul 2010)
New Revision: 1831

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/tail.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
   pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
new sugar function: tail

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-07-08 09:02:34 UTC (rev 1830)
+++ pkg/Rcpp/inst/ChangeLog	2010-07-08 09:10:32 UTC (rev 1831)
@@ -1,6 +1,7 @@
 2010-07-08  Romain Francois <romain at r-enthusiasts.com>
 
 	* inst/include/Rcpp/sugar/functions/head.h: new sugar function : head
+	* inst/include/Rcpp/sugar/functions/tail.h: new sugar function : tail
 
 2010-07-07  Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-07-08 09:02:34 UTC (rev 1830)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-07-08 09:10:32 UTC (rev 1831)
@@ -43,6 +43,7 @@
 #include <Rcpp/sugar/functions/rep_each.h>
 #include <Rcpp/sugar/functions/rev.h>
 #include <Rcpp/sugar/functions/head.h>
+#include <Rcpp/sugar/functions/tail.h>
 
 #include <Rcpp/sugar/functions/Re.h>
 #include <Rcpp/sugar/functions/Im.h>

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

Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-07-08 09:02:34 UTC (rev 1830)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R	2010-07-08 09:10:32 UTC (rev 1831)
@@ -528,6 +528,16 @@
 						_["neg"] = head( xx, -5 )
 					) ;
 				'
+			), 
+			"runit_tail" = list( 
+				signature( x = "numeric" ), 
+				'
+					NumericVector xx(x) ;
+					return List::create( 
+						_["pos"] = tail( xx, 5 ), 
+						_["neg"] = tail( xx, -5 )
+					) ;
+				'
 			)
 			
 		)
@@ -1000,6 +1010,15 @@
 		list( pos = 1:5, neg = 1:95 )
 		)
 }
+     
+test.sugar.tail <- function(){
+	fx <- .rcpp.sugar$runit_tail
+	checkEquals( 
+		fx(1:100), 
+		list( pos = 96:100, neg = 6:100 )
+		)
+}
+     
 
 
 
@@ -1007,8 +1026,6 @@
 
 
 
-
-
 			
 test.sugar.outer <- function( ){
 	fx <- .rcpp.sugar$runit_outer



More information about the Rcpp-commits mailing list