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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 23 22:41:39 CEST 2010


Author: romain
Date: 2010-06-23 22:41:39 +0200 (Wed, 23 Jun 2010)
New Revision: 1693

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/Range.h
   pkg/Rcpp/inst/unitTests/runit.sugar.Range.R
Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h
   pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
Log:
Range for updating a slice of a vector

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-06-23 20:15:53 UTC (rev 1692)
+++ pkg/Rcpp/TODO	2010-06-23 20:41:39 UTC (rev 1693)
@@ -45,15 +45,17 @@
 	
 Syntactic sugar
 
-    o   duplicated, unique, count, sum, rep, head, tail
+    o   duplicated, unique, count, sum, rep, head, tail, sqrt
     
+    o	for complex vectors: Re, Im, Mod, Arg, Conj
+    
     o	min, max with specialization of the binary operators, so that we can do 
     	things like this lazily: 
     	
     	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 that
+    	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)
@@ -65,11 +67,6 @@
     
     o	for character vectors: nchar, grepl, sub, gsub
     
-    o	some way to lazily extract subsets of vectors so that we could do: 
-    
-    	IntegerVector xx(20) ;
-    	xx[ range(0,9) ] = seq_len(10 ) ;
-    
 	o	Compound operators: ++,--,+=, -=, ...
 	
 Testing

Added: pkg/Rcpp/inst/include/Rcpp/sugar/Range.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/Range.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/Range.h	2010-06-23 20:41:39 UTC (rev 1693)
@@ -0,0 +1,50 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Range.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_SUGAR_RANGE_H
+#define RCPP_SUGAR_RANGE_H
+
+namespace Rcpp{
+
+	class Range : public VectorBase<INTSXP,false, Range >{
+	public:
+		Range( int start_, int end_ ) throw(std::range_error) : start(start_), end(end_){
+			if( start_ > end_ ){
+				throw std::range_error( "upper value must be greater than lower value" ) ;
+			}
+		}
+		
+		inline int size() const{
+			return end - start + 1;
+		}
+		
+		inline int operator[]( int i) const {
+			return start + i ;
+		}
+		
+	private:
+		int start ;
+		int end ;
+	} ;
+	
+} 
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h	2010-06-23 20:15:53 UTC (rev 1692)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h	2010-06-23 20:41:39 UTC (rev 1693)
@@ -28,4 +28,6 @@
 // abstractions
 #include <Rcpp/sugar/logical/logical.h>
 
+#include <Rcpp/sugar/Range.h>
+
 #endif

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-06-23 20:15:53 UTC (rev 1692)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h	2010-06-23 20:41:39 UTC (rev 1693)
@@ -664,6 +664,42 @@
 	
 	traits::r_vector_cache<RTYPE> cache ;
 
+public:
+	
+	class RangeIndexer {
+	public:
+		
+			// TODO: check if the indexer is valid
+		RangeIndexer( Vector& vec_, const Range& range_) : 
+			vec(vec_), range(range_) {}
+		
+			// TODO: size exceptions
+		template <bool NA, typename T>	
+		RangeIndexer& operator=( const VectorBase<RTYPE,NA,T>& x){
+			int n = size() ;
+			for( int i=0; i<n; i++){
+				vec[ range[i] ] = x[i] ;
+			}
+			return *this ;
+		}
+			
+		inline Proxy operator[]( int i ){
+			return vec[ range[i] ] ;
+		}
+		
+		inline int size(){
+			return range.size() ;
+		}
+		
+	private:
+		Vector& vec ;
+		const Range& range ;
+	} ;
+	
+	inline RangeIndexer operator[]( const Range& range ){
+		return RangeIndexer( const_cast<Vector&>(*this), range );
+	}
+	
 } ; /* Vector */
 
 #endif

Added: pkg/Rcpp/inst/unitTests/runit.sugar.Range.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.Range.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.Range.R	2010-06-23 20:41:39 UTC (rev 1693)
@@ -0,0 +1,31 @@
+#!/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.Range <- function( ){
+
+	fx <- cxxfunction( signature(  ), '
+	
+		NumericVector xx(8) ;
+		xx[ Range(0,3) ] = exp( seq_len(4) ) ;
+		xx[ Range(4,7) ] = exp( - seq_len(4) ) ;
+		return xx ;
+	', plugin = "Rcpp" )
+	
+	checkEquals( fx() , c( exp(seq_len(4)), exp(-seq_len(4))  ) )
+}



More information about the Rcpp-commits mailing list