Rcpp Version 0.10.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 int size() const { return data.size() ; }
37  inline STORAGE operator[](int 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:
49  typedef typename Rcpp::Vector<RTYPE>::iterator iterator ;
50 
51  Nona( const SUGAR_TYPE& expr) : data(expr.get_ref().begin()), n(expr.size()){}
52 
53  inline int size() const { return n ; }
54  inline STORAGE operator[](int i) const { return data[i] ; }
55 
56  private:
58  int 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