Rcpp Version 0.12.12
sign.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // sign.h: Rcpp R/C++ interface class library -- sign
4 //
5 // Copyright (C) 2010 - 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__sign_h
23 #define Rcpp__sugar__sign_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <bool NA, int RTYPE>
29 class sign__impl{
30 public:
32  static inline int get( STORAGE x){
33  return Rcpp::traits::is_na<RTYPE>(x) ? NA_INTEGER : ( x > 0 ? 1 : (x==0 ? 0 : -1) ) ;
34  }
35 } ;
36 
37 template <int RTYPE>
38 class sign__impl<false,RTYPE>{
39 public:
41  static inline int get( STORAGE x){
42  return ( x > 0 ? 1 : (x==0 ? 0 : -1) ) ;
43  }
44 } ;
45 
46 
47 template <int RTYPE, bool NA, typename T>
48 class Sign : public Rcpp::VectorBase< INTSXP,NA, Sign<RTYPE,NA,T> > {
49 public:
52  typedef int r_import_type ;
53 
54  Sign( const VEC_TYPE& object_ ) : object(object_){}
55 
56  inline int operator[]( R_xlen_t i ) const {
57  return get(i) ;
58  }
59  inline R_xlen_t size() const { return object.size() ; }
60 
61  operator SEXP() const { return wrap( *this ); }
62  inline int get(R_xlen_t i) const { return sign__impl<NA,RTYPE>::get( object[i] ); }
63 private:
64  const VEC_TYPE& object ;
65 } ;
66 
67 } // sugar
68 
69 template <bool NA, typename T>
71  return sugar::Sign<INTSXP,NA,T>( t ) ;
72 }
73 
74 template <bool NA, typename T>
76  return sugar::Sign<REALSXP,NA,T>( t ) ;
77 }
78 
79 
80 } // Rcpp
81 #endif
82 
int r_import_type
Definition: sign.h:52
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: sign.h:31
Sign(const VEC_TYPE &object_)
Definition: sign.h:54
R_xlen_t size() const
Definition: sign.h:59
static int get(STORAGE x)
Definition: sign.h:32
const VEC_TYPE & object
Definition: sign.h:64
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: sign.h:40
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition: sign.h:50
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: sign.h:51
int operator[](R_xlen_t i) const
Definition: sign.h:56
SEXP wrap(const Date &date)
Definition: Date.h:38
Rcpp API.
Definition: algo.h:28
sugar::Sign< INTSXP, NA, T > sign(const VectorBase< INTSXP, NA, T > &t)
Definition: sign.h:70