Rcpp Version 0.10.3
Main Page
Namespaces
Classes
Files
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerator
Friends
Macros
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>
29
struct
unary_minus_result_type
{
30
typedef
typename
traits::storage_type<RTYPE>::type
type
;
31
enum
{
value
= RTYPE } ;
32
} ;
33
template
<>
34
struct
unary_minus_result_type
<LGLSXP>{
35
typedef
traits::storage_type<INTSXP>::type
type
;
36
enum
{
value
= INTSXP } ;
37
} ;
38
39
40
template
<
int
RTYPE,
bool
NA>
41
class
unary_minus
{
42
public
:
43
typedef
typename
traits::storage_type<RTYPE>::type
STORAGE
;
44
typedef
typename
unary_minus_result_type<RTYPE>::type
RESULT
;
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
:
52
typedef
typename
Rcpp::traits::storage_type<RTYPE>::type
STORAGE
;
53
typedef
typename
unary_minus_result_type<RTYPE>::type
RESULT
;
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>
83
class
UnaryMinus_Vector
:
public
Rcpp::VectorBase
<
84
unary_minus_result_type<RTYPE>::value ,
85
NA,
86
UnaryMinus_Vector< unary_minus_result_type<RTYPE>::value ,NA,T>
87
> {
88
public
:
89
typedef
typename
Rcpp::VectorBase<RTYPE,NA,T>
VEC_TYPE
;
90
typedef
typename
traits::storage_type<RTYPE>::type
STORAGE
;
91
typedef
typename
unary_minus_result_type<RTYPE>::type
RESULT
;
92
typedef
unary_minus<RTYPE,NA>
OPERATOR
;
93
94
UnaryMinus_Vector
(
const
VEC_TYPE
& lhs_ ) :
95
lhs
(lhs_),
op
() {}
96
97
inline
RESULT
operator[]
(
int
i
)
const
{
98
return
op
.
apply
(
lhs
[i] ) ;
99
}
100
101
inline
int
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>
112
inline
Rcpp::sugar::UnaryMinus_Vector< RTYPE , NA , T >
113
operator-
(
114
const
Rcpp::VectorBase<RTYPE,NA,T>
&
x
115
) {
116
return
Rcpp::sugar::UnaryMinus_Vector<RTYPE,NA, T >
(
x
) ;
117
}
118
119
120
#endif
inst
include
Rcpp
sugar
operators
unary_minus.h
Generated on Sat Mar 23 2013 11:26:08 for Rcpp Version 0.10.3 by
1.8.1.2