Rcpp Version 1.1.2
Loading...
Searching...
No Matches
clamp.h
Go to the documentation of this file.
1
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2
//
3
// clamp.h: Rcpp R/C++ interface class library -- clamp
4
//
5
// Copyright (C) 2012 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__clamp_h
23
#define Rcpp__sugar__clamp_h
24
25
namespace
Rcpp
{
26
namespace
sugar
{
27
28
template
<
int
RTYPE,
bool
NA>
29
struct
clamp_operator
{
30
typedef
typename
Rcpp::traits::storage_type<RTYPE>::type
STORAGE
;
31
32
clamp_operator
(
STORAGE
lhs_,
STORAGE
rhs_ ) :
lhs
(lhs_),
rhs
(rhs_){}
33
34
inline
STORAGE
operator()
(
STORAGE
x)
const
{
35
return
x <
lhs
?
lhs
: (x >
rhs
?
rhs
: x ) ;
36
}
37
STORAGE
lhs
,
rhs
;
38
} ;
39
// need to write this special version
40
template
<>
41
struct
clamp_operator
<REALSXP,true> {
42
clamp_operator
(
double
lhs_,
double
rhs_ ) :
lhs
(lhs_),
rhs
(rhs_){}
43
44
inline
double
operator()
(
double
x)
const
{
45
if
(
Rcpp::traits::is_na<REALSXP>
(x) )
return
x ;
46
return
x <
lhs
?
lhs
: (x >
rhs
?
rhs
: x ) ;
47
}
48
double
lhs
,
rhs
;
49
} ;
50
51
52
53
template
<
54
int
RTYPE,
55
bool
NA
,
typename
T
56
>
57
class
Clamp_Primitive_Vector_Primitive
:
public
VectorBase
<
58
RTYPE ,
59
NA ,
60
Clamp_Primitive_Vector_Primitive<RTYPE,NA,T>
61
> {
62
public
:
63
typedef
typename
Rcpp::traits::storage_type<RTYPE>::type
STORAGE
;
64
typedef
clamp_operator<RTYPE,NA>
OPERATOR
;
65
66
Clamp_Primitive_Vector_Primitive
(
STORAGE
lhs_,
const
T& vec_,
STORAGE
rhs_) :
vec
(vec_),
op
(lhs_,rhs_) {}
67
68
inline
STORAGE
operator[]
( R_xlen_t i )
const
{
69
return
op
(
vec
[i] ) ;
70
}
71
inline
R_xlen_t
size
()
const
{
return
vec
.size() ; }
72
73
private
:
74
const
T&
vec
;
75
OPERATOR
op
;
76
} ;
77
78
79
80
}
// sugar
81
82
template
<
int
RTYPE,
bool
NA,
typename
T>
83
inline
sugar::Clamp_Primitive_Vector_Primitive<RTYPE,NA,T>
84
clamp
(
85
typename
Rcpp::traits::storage_type<RTYPE>::type
lhs,
86
const
Rcpp::VectorBase<RTYPE,NA,T>
& vec,
87
typename
Rcpp::traits::storage_type<RTYPE>::type
rhs
88
){
89
return
sugar::Clamp_Primitive_Vector_Primitive<RTYPE,NA,T>
( lhs, vec.
get_ref
(), rhs ) ;
90
}
91
92
93
}
// Rcpp
94
95
#endif
Rcpp::VectorBase
Definition
VectorBase.h:29
Rcpp::VectorBase::get_ref
VECTOR & get_ref()
Definition
VectorBase.h:37
Rcpp::sugar::Clamp_Primitive_Vector_Primitive
Definition
clamp.h:61
Rcpp::sugar::Clamp_Primitive_Vector_Primitive::vec
const T & vec
Definition
clamp.h:74
Rcpp::sugar::Clamp_Primitive_Vector_Primitive::Clamp_Primitive_Vector_Primitive
Clamp_Primitive_Vector_Primitive(STORAGE lhs_, const T &vec_, STORAGE rhs_)
Definition
clamp.h:66
Rcpp::sugar::Clamp_Primitive_Vector_Primitive::STORAGE
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition
clamp.h:63
Rcpp::sugar::Clamp_Primitive_Vector_Primitive::size
R_xlen_t size() const
Definition
clamp.h:71
Rcpp::sugar::Clamp_Primitive_Vector_Primitive::operator[]
STORAGE operator[](R_xlen_t i) const
Definition
clamp.h:68
Rcpp::sugar::Clamp_Primitive_Vector_Primitive::op
OPERATOR op
Definition
clamp.h:75
Rcpp::sugar::Clamp_Primitive_Vector_Primitive::OPERATOR
clamp_operator< RTYPE, NA > OPERATOR
Definition
clamp.h:64
Rcpp::sugar
Definition
IndexHash.h:43
Rcpp::traits::is_na
bool is_na(typename storage_type< RTYPE >::type)
Definition
is_na.h:32
Rcpp
Rcpp API.
Definition
algo.h:28
Rcpp::clamp
sugar::Clamp_Primitive_Vector_Primitive< RTYPE, NA, T > clamp(typename Rcpp::traits::storage_type< RTYPE >::type lhs, const Rcpp::VectorBase< RTYPE, NA, T > &vec, typename Rcpp::traits::storage_type< RTYPE >::type rhs)
Definition
clamp.h:84
Rcpp::NA
static Na_Proxy NA
Definition
Na_Proxy.h:52
Rcpp::sugar::clamp_operator< REALSXP, true >::lhs
double lhs
Definition
clamp.h:48
Rcpp::sugar::clamp_operator< REALSXP, true >::clamp_operator
clamp_operator(double lhs_, double rhs_)
Definition
clamp.h:42
Rcpp::sugar::clamp_operator< REALSXP, true >::operator()
double operator()(double x) const
Definition
clamp.h:44
Rcpp::sugar::clamp_operator< REALSXP, true >::rhs
double rhs
Definition
clamp.h:48
Rcpp::sugar::clamp_operator
Definition
clamp.h:29
Rcpp::sugar::clamp_operator::operator()
STORAGE operator()(STORAGE x) const
Definition
clamp.h:34
Rcpp::sugar::clamp_operator::lhs
STORAGE lhs
Definition
clamp.h:37
Rcpp::sugar::clamp_operator::clamp_operator
clamp_operator(STORAGE lhs_, STORAGE rhs_)
Definition
clamp.h:32
Rcpp::sugar::clamp_operator::STORAGE
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition
clamp.h:30
Rcpp::sugar::clamp_operator::rhs
STORAGE rhs
Definition
clamp.h:37
Rcpp::traits::storage_type::type
SEXP type
Definition
storage_type.h:36
inst
include
Rcpp
sugar
functions
clamp.h
Generated on
for Rcpp Version 1.1.2 by
1.15.0