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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 17 20:46:29 CEST 2010


Author: romain
Date: 2010-06-17 20:46:25 +0200 (Thu, 17 Jun 2010)
New Revision: 1577

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/seq_along.h
   pkg/Rcpp/inst/unitTests/runit.sugar.seqalong.R
Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
Log:
seq_along

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-06-17 17:34:46 UTC (rev 1576)
+++ pkg/Rcpp/TODO	2010-06-17 18:46:25 UTC (rev 1577)
@@ -41,7 +41,7 @@
 		
 Syntactic sugar
 
-    o   duplicated, unique, count, sum, seq_along, seq_len, rep
+    o   duplicated, unique, count, sum, seq_len, rep
     
     o	for matrices: row, col, lower_tri, upper_tri
 	

Added: pkg/Rcpp/inst/include/Rcpp/sugar/seq_along.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/seq_along.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/seq_along.h	2010-06-17 18:46:25 UTC (rev 1577)
@@ -0,0 +1,51 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// seq_along.h: Rcpp R/C++ interface class library -- any
+//
+// 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__seq_along_h
+#define Rcpp__sugar__seq_along_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <typename T>
+class SeqAlong : public VectorBase< INTSXP,false,SeqAlong<T> > {
+public:
+	SeqAlong( int len_ ) : len(len_){}
+	
+	inline int operator[]( int i ) const {
+		return 1 + i ;
+	}
+	inline int size() const { return len ; }
+	         
+private:
+	const int len ;
+} ;
+	
+} // sugar
+
+template <int RTYPE, bool _NA_, typename T>
+inline sugar::SeqAlong<T> seq_along( const Rcpp::VectorBase<RTYPE,_NA_,T>& t){
+	return sugar::SeqAlong<T>( t.size() ) ;
+}
+
+} // Rcpp
+#endif
+

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-06-17 17:34:46 UTC (rev 1576)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-06-17 18:46:25 UTC (rev 1577)
@@ -26,6 +26,7 @@
 #include <Rcpp/sugar/any.h>
 #include <Rcpp/sugar/all.h>
 #include <Rcpp/sugar/is_na.h>
+#include <Rcpp/sugar/seq_along.h>
 
 #include <Rcpp/sugar/Comparator.h>
 #include <Rcpp/sugar/Comparator_With_One_Value.h>

Added: pkg/Rcpp/inst/unitTests/runit.sugar.seqalong.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.seqalong.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.seqalong.R	2010-06-17 18:46:25 UTC (rev 1577)
@@ -0,0 +1,34 @@
+#!/usr/bin/r -t
+#
+# 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/>.
+
+test.sugar.seqlaong <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+	
+		NumericVector xx(x) ;
+		IntegerVector res = seq_along( xx );
+		
+		return res ;
+	
+	', plugin = "Rcpp" )
+	
+	
+	checkEquals( fx( rnorm(10)) , 1:10  )
+}
+



More information about the Rcpp-commits mailing list