Rcpp Version 1.0.9
nona.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
2 //
3 // nona.h: Rcpp R/C++ interface class library -- noNA handling
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_NONA_NONA_H
23 #define RCPP_SUGAR_NONA_NONA_H
24 
25 namespace Rcpp{
26 namespace sugar {
27 
28  template <int RTYPE, bool NA, typename VECTOR>
29  class Nona : public Rcpp::VectorBase<RTYPE,false, Nona<RTYPE,NA,VECTOR> > {
30  public:
33 
34  Nona( const SUGAR_TYPE& expr) : data(expr.get_ref()){}
35 
36  inline R_xlen_t size() const { return data.size() ; }
37  inline STORAGE operator[](R_xlen_t i) const { return data[i] ; }
38 
39  private:
40  const VECTOR& data ;
41  } ;
42 
43  // specialization when the expression is actually a vector expression
44  template <int RTYPE, bool NA>
45  class Nona< RTYPE,NA,Rcpp::Vector<RTYPE> > : public Rcpp::VectorBase<RTYPE,false, Nona<RTYPE,NA,Rcpp::Vector<RTYPE> > > {
46  public:
50 
51  Nona( const SUGAR_TYPE& expr) : data(expr.get_ref().begin()), n(expr.size()){}
52 
53  inline R_xlen_t size() const { return n ; }
54  inline STORAGE operator[](R_xlen_t i) const { return data[i] ; }
55 
56  private:
58  R_xlen_t n ;
59  } ;
60 
61  template <typename T>
62  class NonaPrimitive {
63  public:
64  NonaPrimitive( T t) : x(t){}
65  inline operator T(){ return x ; }
66 
67  private:
68  T x ;
69  } ;
70 
71 }
72 
73 template <int RTYPE, bool NA, typename VECTOR>
75  return sugar::Nona<RTYPE,NA,VECTOR>( vec ) ;
76 }
77 
79  return sugar::NonaPrimitive<double>( x ) ;
80 }
82  return sugar::NonaPrimitive<int>( x ) ;
83 }
84 
85 
86 }
87 
88 #endif
traits::r_vector_const_iterator< RTYPE, StoragePolicy >::type const_iterator
Definition: Vector.h:47
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: nona.h:48
Rcpp::Vector< RTYPE >::const_iterator iterator
Definition: nona.h:49
Rcpp::VectorBase< RTYPE, NA, Rcpp::Vector< RTYPE > > SUGAR_TYPE
Definition: nona.h:47
R_xlen_t size() const
Definition: nona.h:36
STORAGE operator[](R_xlen_t i) const
Definition: nona.h:37
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: nona.h:32
const VECTOR & data
Definition: nona.h:40
Rcpp::VectorBase< RTYPE, NA, VECTOR > SUGAR_TYPE
Definition: nona.h:31
Nona(const SUGAR_TYPE &expr)
Definition: nona.h:34
Rcpp API.
Definition: algo.h:28
sugar::Nona< RTYPE, NA, VECTOR > noNA(const Rcpp::VectorBase< RTYPE, NA, VECTOR > &vec)
Definition: nona.h:74
static Na_Proxy NA
Definition: Na_Proxy.h:52