Rcpp Version 0.12.12
unary_minus.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // unary_minus.h: Rcpp R/C++ interface class library -- unary operator-
4 //
5 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__sugar__unary_minus_h
23 #define Rcpp__sugar__unary_minus_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28  template <int RTYPE>
31  enum{ value = RTYPE } ;
32  } ;
33  template <>
34  struct unary_minus_result_type<LGLSXP>{
36  enum{ value = INTSXP } ;
37  } ;
38 
39 
40  template <int RTYPE,bool NA>
41  class unary_minus {
42  public:
45  inline RESULT apply( STORAGE x ) const {
46  return Rcpp::traits::is_na<RTYPE>(x) ? x : ( -x ) ;
47  }
48  } ;
49  template <int RTYPE>
50  class unary_minus<RTYPE,false> {
51  public:
54  inline RESULT apply( STORAGE x ) const {
55  return -x ;
56  }
57  } ;
58  template <bool NA>
59  class unary_minus<CPLXSXP,NA>{
60  public:
61  inline Rcomplex apply( Rcomplex x ) const {
62  if (Rcpp::traits::is_na<CPLXSXP>( x ) ) return x;
63 
64  Rcomplex cx ;
65  cx.r = -x.r;
66  cx.i = -x.i ;
67  return cx ;
68  }
69  } ;
70  template <>
71  class unary_minus<CPLXSXP,false>{
72  public:
73  inline Rcomplex apply( Rcomplex x ) const {
74  Rcomplex cx ;
75  cx.r = -x.r;
76  cx.i = -x.i ;
77  return cx ;
78  }
79  } ;
80 
81 
82  template <int RTYPE, bool NA, typename T>
84  unary_minus_result_type<RTYPE>::value ,
85  NA,
86  UnaryMinus_Vector< unary_minus_result_type<RTYPE>::value ,NA,T>
87  > {
88  public:
93 
94  UnaryMinus_Vector( const VEC_TYPE& lhs_ ) :
95  lhs(lhs_), op() {}
96 
97  inline RESULT operator[]( R_xlen_t i ) const {
98  return op.apply( lhs[i] ) ;
99  }
100 
101  inline R_xlen_t size() const { return lhs.size() ; }
102 
103  private:
104  const VEC_TYPE& lhs ;
105  OPERATOR op ;
106  } ;
107 
108 }
109 }
110 
111 template <int RTYPE,bool NA, typename T>
115 ) {
117 }
118 
119 
120 #endif
RESULT apply(STORAGE x) const
Definition: unary_minus.h:54
Rcomplex apply(Rcomplex x) const
Definition: unary_minus.h:61
double operator-(const Date &d1, const Date &d2)
Definition: Date.h:166
bool is_na< CPLXSXP >(Rcomplex x)
Definition: is_na.h:46
unary_minus_result_type< RTYPE >::type RESULT
Definition: unary_minus.h:44
Rcomplex apply(Rcomplex x) const
Definition: unary_minus.h:73
traits::storage_type< RTYPE >::type STORAGE
Definition: unary_minus.h:43
unary_minus_result_type< RTYPE >::type RESULT
Definition: unary_minus.h:91
traits::storage_type< RTYPE >::type type
Definition: unary_minus.h:30
RESULT operator[](R_xlen_t i) const
Definition: unary_minus.h:97
static Na_Proxy NA
Definition: Na_Proxy.h:52
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: unary_minus.h:52
unary_minus_result_type< RTYPE >::type RESULT
Definition: unary_minus.h:53
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition: unary_minus.h:89
Rcpp API.
Definition: algo.h:28
traits::storage_type< RTYPE >::type STORAGE
Definition: unary_minus.h:90
traits::storage_type< INTSXP >::type type
Definition: unary_minus.h:35
RESULT apply(STORAGE x) const
Definition: unary_minus.h:45
UnaryMinus_Vector(const VEC_TYPE &lhs_)
Definition: unary_minus.h:94
unary_minus< RTYPE, NA > OPERATOR
Definition: unary_minus.h:92