[Rcpp-commits] r1598 - in pkg/Rcpp/inst: include/Rcpp/sugar unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jun 18 13:41:05 CEST 2010
Author: romain
Date: 2010-06-18 13:41:05 +0200 (Fri, 18 Jun 2010)
New Revision: 1598
Added:
pkg/Rcpp/inst/include/Rcpp/sugar/not.h
pkg/Rcpp/inst/unitTests/runit.sugar.not.R
Modified:
pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
Log:
operator not(VectorBase)
Added: pkg/Rcpp/inst/include/Rcpp/sugar/not.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/not.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/not.h 2010-06-18 11:41:05 UTC (rev 1598)
@@ -0,0 +1,108 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// not.h: Rcpp R/C++ interface class library -- operator+
+//
+// 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__not_h
+#define Rcpp__sugar__not_h
+
+namespace Rcpp{
+namespace sugar{
+
+ template <int RTYPE,bool NA>
+ class not_ {
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ inline int apply( STORAGE x ) const {
+ return Rcpp::traits::is_na<RTYPE>(x) ? NA_LOGICAL : (x ? FALSE : TRUE) ;
+ }
+ } ;
+ template <int RTYPE>
+ class not_<RTYPE,false> {
+ public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ inline int apply( STORAGE x ) const {
+ return x ? FALSE : TRUE ;
+ }
+ } ;
+ template <bool NA>
+ class not_<REALSXP,NA>{
+ public:
+ inline int apply( double x ) const {
+ return Rcpp::traits::is_na<REALSXP>( x ) ? NA_LOGICAL : ( (x == 0) ? FALSE : TRUE ) ;
+ }
+ } ;
+ template <>
+ class not_<REALSXP,false>{
+ public:
+ inline int apply( double x ) const {
+ return ( x == 0.0 ? FALSE : TRUE ) ;
+ }
+ } ;
+ template <bool NA>
+ class not_<CPLXSXP,NA>{
+ public:
+ inline int apply( Rcomplex x ) const {
+ return Rcpp::traits::is_na<CPLXSXP>( x ) ? NA_LOGICAL : ( (x.r == 0.0 & x.i == 0.0 ) ? FALSE : TRUE ) ;
+ }
+ } ;
+ template <>
+ class not_<CPLXSXP,false>{
+ public:
+ inline int apply( Rcomplex x ) const {
+ return (x.r == 0.0 & x.i == 0.0 ) ? FALSE : TRUE ;
+ }
+ } ;
+
+
+
+ template <int RTYPE, bool _NA_, typename VEC_TYPE>
+ class Not_Vector : public Rcpp::VectorBase<LGLSXP,_NA_, Not_Vector<RTYPE,_NA_,VEC_TYPE> > {
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ typedef not_<RTYPE,_NA_> OPERATOR ;
+
+ Not_Vector( const VEC_TYPE& lhs_ ) :
+ lhs(lhs_), op() {}
+
+ inline STORAGE operator[]( int i ) const {
+ return op.apply( lhs[i] ) ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+
+ private:
+ const VEC_TYPE& lhs ;
+ OPERATOR op ;
+ } ;
+
+}
+}
+
+template <int RTYPE,bool _NA_, typename T>
+inline Rcpp::sugar::Not_Vector< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+operator!(
+ const Rcpp::VectorBase<RTYPE,_NA_,T>& x
+) {
+ return Rcpp::sugar::Not_Vector<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( x ) ;
+}
+
+
+#endif
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h 2010-06-18 11:05:42 UTC (rev 1597)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h 2010-06-18 11:41:05 UTC (rev 1598)
@@ -28,7 +28,7 @@
#include <Rcpp/sugar/is_na.h>
#include <Rcpp/sugar/seq_along.h>
-// operators
+// binary operators
#include <Rcpp/sugar/Comparator.h>
#include <Rcpp/sugar/Comparator_With_One_Value.h>
#include <Rcpp/sugar/logical_operators.h>
@@ -37,4 +37,7 @@
#include <Rcpp/sugar/times.h>
#include <Rcpp/sugar/divides.h>
+// unary operators
+#include <Rcpp/sugar/not.h>
+
#endif
Added: pkg/Rcpp/inst/unitTests/runit.sugar.not.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.not.R (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.not.R 2010-06-18 11:41:05 UTC (rev 1598)
@@ -0,0 +1,39 @@
+#!/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.any.equal.not <- function( ){
+
+ fx <- cxxfunction( signature( x = "numeric", y = "numeric" ), '
+
+ NumericVector xx(x) ;
+ NumericVector yy(y) ;
+
+ return any( !( xx == yy) ) ;
+
+ ', plugin = "Rcpp" )
+
+ checkTrue( ! fx( 1, 1 ) )
+ checkTrue( fx( 1:2, c(1,1) ) )
+ checkTrue( fx( 0, 1 ) )
+ checkTrue( fx( 1, 0 ) )
+ checkTrue( is.na( fx( NA, 1 ) ) )
+
+}
+
More information about the Rcpp-commits
mailing list