Rcpp Version 0.9.10
nona.h
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
00002 //
00003 // nona.h: Rcpp R/C++ interface class library -- noNA handling
00004 //
00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
00006 //
00007 // This file is part of Rcpp.
00008 //
00009 // Rcpp is free software: you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 2 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // Rcpp is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
00021 
00022 #ifndef RCPP_SUGAR_NONA_NONA_H
00023 #define RCPP_SUGAR_NONA_NONA_H
00024 
00025 namespace Rcpp{
00026 namespace sugar {     
00027 
00028     template <int RTYPE, bool NA, typename VECTOR>
00029     class Nona : public Rcpp::VectorBase<RTYPE,false, Nona<RTYPE,NA,VECTOR> > {
00030     public:
00031         typedef typename Rcpp::VectorBase<RTYPE,NA,VECTOR> SUGAR_TYPE ;
00032         typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
00033         
00034         Nona( const SUGAR_TYPE& expr) : data(expr.get_ref()){} 
00035         
00036         inline int size() const { return data.size() ; }
00037         inline STORAGE operator[](int i) const { return data[i] ; }
00038         
00039     private:
00040         const VECTOR& data ;    
00041     } ;
00042     
00043     // specialization when the expression is actually a vector expression
00044     template <int RTYPE, bool NA>
00045     class Nona< RTYPE,NA,Rcpp::Vector<RTYPE> > : public Rcpp::VectorBase<RTYPE,false, Nona<RTYPE,NA,Rcpp::Vector<RTYPE> > > {
00046     public:
00047         typedef typename Rcpp::VectorBase<RTYPE,NA, Rcpp::Vector<RTYPE> > SUGAR_TYPE ;
00048         typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
00049         typedef typename Rcpp::Vector<RTYPE>::iterator iterator ;
00050         
00051         Nona( const SUGAR_TYPE& expr) : data(expr.get_ref().begin()), n(expr.size()){} 
00052         
00053         inline int size() const { return n ; }
00054         inline STORAGE operator[](int i) const { return data[i] ; }
00055         
00056     private:
00057         iterator data ;
00058         int n ;
00059     } ;
00060     
00061     template <typename T>
00062     class NonaPrimitive {
00063     public:
00064         NonaPrimitive( T t) : x(t){}
00065         inline operator T(){ return x ; }
00066         
00067     private:
00068         T x ;
00069     } ;
00070     
00071 }
00072 
00073 template <int RTYPE, bool NA, typename VECTOR>
00074 inline sugar::Nona<RTYPE,NA,VECTOR> noNA( const Rcpp::VectorBase<RTYPE,NA,VECTOR>& vec ){
00075     return sugar::Nona<RTYPE,NA,VECTOR>( vec ) ;
00076 }
00077 
00078 inline sugar::NonaPrimitive<double> noNA( double x ){
00079     return sugar::NonaPrimitive<double>( x ) ; 
00080 }
00081 inline sugar::NonaPrimitive<int> noNA( int x ){
00082     return sugar::NonaPrimitive<int>( x ) ; 
00083 }
00084 
00085 
00086 }
00087 
00088 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines