[Rcpp-commits] r1533 - in pkg/Rcpp/inst: include/Rcpp/operators include/Rcpp/sugar unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jun 14 15:13:32 CEST 2010
Author: romain
Date: 2010-06-14 15:13:32 +0200 (Mon, 14 Jun 2010)
New Revision: 1533
Added:
pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h
pkg/Rcpp/inst/include/Rcpp/sugar/logical_operators.h
Removed:
pkg/Rcpp/inst/include/Rcpp/operators/VectorOperators.h
Modified:
pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h
pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
more sugar binary operators
Deleted: pkg/Rcpp/inst/include/Rcpp/operators/VectorOperators.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/operators/VectorOperators.h 2010-06-14 11:05:49 UTC (rev 1532)
+++ pkg/Rcpp/inst/include/Rcpp/operators/VectorOperators.h 2010-06-14 13:13:32 UTC (rev 1533)
@@ -1,138 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
-//
-// VectorOperators.h: Rcpp R/C++ interface class library -- vector operators
-//
-// 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__operators_VectorOperators_h
-#define Rcpp__operators_VectorOperators_h
-
-namespace Rcpp{
-
-template <int RTYPE>
-class LessThan {
-public:
- typedef Vector<RTYPE> VEC ;
- typedef typename traits::storage_type<RTYPE>::type STORAGE ;
-
- LessThan( const VEC& lhs_, const VEC& rhs_) :
- lhs(lhs_), rhs(rhs_){}
-
- inline int operator[]( int i ) const {
- return ( traits::is_na<RTYPE>(lhs[i]) || traits::is_na<RTYPE>(rhs[i]) ) ?
- NA_LOGICAL : ( lhs[i] < rhs[i] ) ;
- }
-
- inline int size() const { return lhs.size() ; }
-
- class iterator {
- public:
- typedef int difference_type ;
- typedef STORAGE storage_type ;
- typedef int value_type ;
- typedef const int* pointer ;
- typedef int reference ;
- typedef std::random_access_iterator_tag iterator_category ;
-
- iterator( const LessThan& comp_, int index_ = 0 ) :
- comp(comp_), index(index_) {}
-
- inline difference_type operator-( const iterator& other ){
- return index - other.index ;
- }
- inline bool operator==( const iterator& other ){
- return index == other.index ;
- }
- inline bool operator<( const iterator& other ){
- return index < other.index ;
- }
- inline bool operator>( const iterator& other ){
- return index > other.index ;
- }
- inline bool operator<=( const iterator& other ){
- return index <= other.index ;
- }
- inline bool operator>=( const iterator& other ){
- return index >= other.index ;
- }
- inline bool operator!=( const iterator& other ){
- return index != other.index ;
- }
-
- inline reference operator*(){
- return comp[index] ;
- }
- inline pointer operator->(){
- return &comp[index] ;
- }
-
- inline iterator& operator++(){
- index++;
- return *this ;
- }
- inline iterator& operator++(int){
- index++ ;
- return *this ;
- }
- inline iterator& operator--(){
- index-- ;
- return *this ;
- }
- inline iterator& operator--(int){
- index-- ;
- return *this ;
- }
- inline iterator& operator+=(difference_type n) {
- index += n ;
- return *this ;
- }
- inline iterator& operator-=(difference_type n) {
- index -= n ;
- return *this ;
- }
-
- inline iterator operator+(difference_type n) const {
- return iterator( comp, index + n ) ;
- }
- inline iterator operator-(difference_type n) const {
- return iterator( comp, index - n ) ;
- }
-
- private:
- const LessThan& comp ;
- int index ;
- } ;
-
- inline iterator begin() const { return iterator(*this, 0 ) ; }
- inline iterator end() const { return iterator(*this, lhs.size() ) ; }
-
-private:
- const VEC& lhs ;
- const VEC& rhs ;
-
-} ;
-
-}
-
-template <int RTYPE>
-inline Rcpp::LessThan<RTYPE> operator<( const Rcpp::Vector<RTYPE>& lhs , const Rcpp::Vector<RTYPE>& rhs ){
- return Rcpp::LessThan<RTYPE>( lhs, rhs ) ;
-}
-
-
-#endif
Added: pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h 2010-06-14 13:13:32 UTC (rev 1533)
@@ -0,0 +1,130 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// LogicalResult.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__LogicalResult_h
+#define Rcpp__sugar__LogicalResult_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <typename T>
+class LogicalResult {
+public:
+
+ LogicalResult() {} ;
+
+ inline int operator[]( int i){
+ return static_cast<T*>(this)->operator[]( i ) ;
+ }
+
+ inline int size(){
+ return static_cast<T&>(*this).size() ;
+ }
+
+public:
+
+ class iterator {
+ public:
+ typedef int difference_type ;
+ typedef int value_type ;
+ typedef const int* pointer ;
+ typedef int reference ;
+ typedef std::random_access_iterator_tag iterator_category ;
+
+ iterator( const LogicalResult& comp_, int index_ = 0 ) :
+ comp(comp_), index(index_) {}
+
+ inline difference_type operator-( const iterator& other ){
+ return index - other.index ;
+ }
+ inline bool operator==( const iterator& other ){
+ return index == other.index ;
+ }
+ inline bool operator<( const iterator& other ){
+ return index < other.index ;
+ }
+ inline bool operator>( const iterator& other ){
+ return index > other.index ;
+ }
+ inline bool operator<=( const iterator& other ){
+ return index <= other.index ;
+ }
+ inline bool operator>=( const iterator& other ){
+ return index >= other.index ;
+ }
+ inline bool operator!=( const iterator& other ){
+ return index != other.index ;
+ }
+
+ inline reference operator*(){
+ return comp[index] ;
+ }
+ inline pointer operator->(){
+ return &comp[index] ;
+ }
+
+ inline iterator& operator++(){
+ index++;
+ return *this ;
+ }
+ inline iterator& operator++(int){
+ index++ ;
+ return *this ;
+ }
+ inline iterator& operator--(){
+ index-- ;
+ return *this ;
+ }
+ inline iterator& operator--(int){
+ index-- ;
+ return *this ;
+ }
+ inline iterator& operator+=(difference_type n) {
+ index += n ;
+ return *this ;
+ }
+ inline iterator& operator-=(difference_type n) {
+ index -= n ;
+ return *this ;
+ }
+
+ inline iterator operator+(difference_type n) const {
+ return iterator( comp, index + n ) ;
+ }
+ inline iterator operator-(difference_type n) const {
+ return iterator( comp, index - n ) ;
+ }
+
+ private:
+ const LogicalResult& comp ;
+ int index ;
+ } ;
+
+ inline iterator begin() const { return iterator(*this, 0 ) ; }
+ inline iterator end() const { return iterator(*this, size() ) ; }
+
+
+} ;
+
+}
+}
+
+#endif
Added: pkg/Rcpp/inst/include/Rcpp/sugar/logical_operators.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/logical_operators.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/logical_operators.h 2010-06-14 13:13:32 UTC (rev 1533)
@@ -0,0 +1,138 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// LessThan.h: Rcpp R/C++ interface class library -- vector operators
+//
+// 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__logical_operators_h
+#define Rcpp__sugar__logical_operators_h
+
+namespace Rcpp{
+namespace sugar{
+
+#undef RCPP_OP
+#define RCPP_OP(NAME,OP) \
+template <int RTYPE> \
+class NAME { \
+public: \
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ; \
+ inline bool compare_one( STORAGE lhs, STORAGE rhs) const { \
+ return lhs OP rhs ; \
+ } \
+} ;
+RCPP_OP(less,<)
+RCPP_OP(greater,>)
+RCPP_OP(less_or_equal,<=)
+RCPP_OP(greater_or_equal,>=)
+RCPP_OP(equal,==)
+RCPP_OP(not_equal,!=)
+#undef RCPP_OP
+
+
+template <int RTYPE, typename T>
+class r_binary_op : public T {
+public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+
+ r_binary_op(){}
+
+ inline int compare( STORAGE lhs, STORAGE rhs) const {
+ return ( traits::is_na<RTYPE>(lhs) || traits::is_na<RTYPE>(rhs) ) ?
+ NA_LOGICAL : static_cast<int>( compare_one( lhs, rhs ) ) ;
+ }
+
+} ;
+
+
+template <int RTYPE, typename Operator>
+class Comparator : public LogicalResult< Comparator<RTYPE,Operator> > {
+public:
+ typedef Vector<RTYPE> VEC ;
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ typedef r_binary_op<RTYPE,Operator> R_OPERATOR ;
+
+ Comparator( const VEC& lhs_, const VEC& rhs_) :
+ op(), lhs(lhs_), rhs(rhs_){}
+
+ inline int operator[]( int i ) const {
+ return op.compare( lhs[i], rhs[i] ) ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+private:
+ R_OPERATOR op ;
+ const VEC& lhs ;
+ const VEC& rhs ;
+
+} ;
+
+
+} // sugar
+} // Rcpp
+
+
+template <int RTYPE>
+inline Rcpp::sugar::Comparator< RTYPE , Rcpp::sugar::less<RTYPE> >
+operator<( const Rcpp::Vector<RTYPE>& lhs , const Rcpp::Vector<RTYPE>& rhs ){
+ return Rcpp::sugar::Comparator<RTYPE, Rcpp::sugar::less<RTYPE> >(
+ lhs, rhs
+ ) ;
+}
+
+template <int RTYPE>
+inline Rcpp::sugar::Comparator< RTYPE , Rcpp::sugar::greater<RTYPE> >
+operator>( const Rcpp::Vector<RTYPE>& lhs , const Rcpp::Vector<RTYPE>& rhs ){
+ return Rcpp::sugar::Comparator<RTYPE, Rcpp::sugar::greater<RTYPE> >(
+ lhs, rhs
+ ) ;
+}
+
+template <int RTYPE>
+inline Rcpp::sugar::Comparator< RTYPE , Rcpp::sugar::less_or_equal<RTYPE> >
+operator<=( const Rcpp::Vector<RTYPE>& lhs , const Rcpp::Vector<RTYPE>& rhs ){
+ return Rcpp::sugar::Comparator<RTYPE, Rcpp::sugar::less_or_equal<RTYPE> >(
+ lhs, rhs
+ ) ;
+}
+
+template <int RTYPE>
+inline Rcpp::sugar::Comparator< RTYPE , Rcpp::sugar::greater_or_equal<RTYPE> >
+operator>=( const Rcpp::Vector<RTYPE>& lhs , const Rcpp::Vector<RTYPE>& rhs ){
+ return Rcpp::sugar::Comparator<RTYPE, Rcpp::sugar::greater_or_equal<RTYPE> >(
+ lhs, rhs
+ ) ;
+}
+
+template <int RTYPE>
+inline Rcpp::sugar::Comparator< RTYPE , Rcpp::sugar::equal<RTYPE> >
+operator==( const Rcpp::Vector<RTYPE>& lhs , const Rcpp::Vector<RTYPE>& rhs ){
+ return Rcpp::sugar::Comparator<RTYPE, Rcpp::sugar::equal<RTYPE> >(
+ lhs, rhs
+ ) ;
+}
+
+template <int RTYPE>
+inline Rcpp::sugar::Comparator< RTYPE , Rcpp::sugar::not_equal<RTYPE> >
+operator!=( const Rcpp::Vector<RTYPE>& lhs , const Rcpp::Vector<RTYPE>& rhs ){
+ return Rcpp::sugar::Comparator<RTYPE, Rcpp::sugar::not_equal<RTYPE> >(
+ lhs, rhs
+ ) ;
+}
+
+#endif
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h 2010-06-14 11:05:49 UTC (rev 1532)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h 2010-06-14 13:13:32 UTC (rev 1533)
@@ -24,5 +24,6 @@
// implementations
#include <Rcpp/sugar/any.h>
+#include <Rcpp/sugar/logical_operators.h>
#endif
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h 2010-06-14 11:05:49 UTC (rev 1532)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar_forward.h 2010-06-14 13:13:32 UTC (rev 1533)
@@ -27,5 +27,6 @@
// abstractions
#include <Rcpp/sugar/SingleLogicalResult.h>
+#include <Rcpp/sugar/LogicalResult.h>
#endif
Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-06-14 11:05:49 UTC (rev 1532)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-06-14 13:13:32 UTC (rev 1533)
@@ -17,9 +17,8 @@
# 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.less <- function( ){
-test.any <- function( ){
-
fx <- cxxfunction( signature( x = "numeric", y = "numeric" ), '
NumericVector xx(x) ;
@@ -30,8 +29,104 @@
', plugin = "Rcpp" )
checkTrue( ! fx( 1, 0 ) )
- checkTrue( ! fx( 1:10, 2:11 ) )
+ checkTrue( fx( 1:10, 2:11 ) )
checkTrue( fx( 0, 1 ) )
checkTrue( is.na( fx( NA, 1 ) ) )
}
+
+test.sugar.any.greater <- function( ){
+
+ fx <- cxxfunction( signature( x = "numeric", y = "numeric" ), '
+
+ NumericVector xx(x) ;
+ NumericVector yy(y) ;
+
+ return any( xx > yy ) ;
+
+ ', plugin = "Rcpp" )
+
+ checkTrue( fx( 1, 0 ) )
+ checkTrue( fx( 2:11, 1:10 ) )
+ checkTrue( ! fx( 0, 1 ) )
+ checkTrue( is.na( fx( NA, 1 ) ) )
+
+}
+
+test.sugar.any.less.or.equal <- 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 ) ) )
+
+}
+
+test.sugar.any.greater.or.equal <- 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 ) ) )
+
+}
+
+
+test.sugar.any.equal <- 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 ) ) )
+
+}
+
+test.sugar.any.not.equal <- 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