Rcpp Version 0.12.14
na_omit.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // na_omit.h: Rcpp R/C++ interface class library -- na_omit
4 //
5 // Copyright (C) 2013 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__na_omit_h
23 #define Rcpp__sugar__na_omit_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28  template <int RTYPE, bool NA, typename T>
30  R_xlen_t n = x.size() ;
31  R_xlen_t n_out = n - sum( is_na(x) ) ;
32 
33  if( n_out == n ) return x ;
34 
35  Vector<RTYPE> out = no_init(n_out) ;
36  for( R_xlen_t i=0, j=0; i<n; i++){
37  if( Vector<RTYPE>::is_na( x[i] ) ) continue ;
38  out[j++] = x[i];
39  }
40  return out ;
41  }
42 
43  template <int RTYPE, bool NA, typename T>
45  R_xlen_t n = x.size() ;
46  R_xlen_t n_out = n - sum( is_na(x) ) ;
47 
48  if( n_out == n ) return x;
49 
50  Vector<RTYPE> out = no_init(n_out) ;
51 
52  bool has_name = x.attr("names") != R_NilValue ;
53  if( has_name ){
54  CharacterVector names = x.attr("names") ;
55  CharacterVector onames( n_out ) ;
56 
57  for( R_xlen_t i=0, j=0; i<n; i++){
58  if( Vector<RTYPE>::is_na( x[i] ) ) continue ;
59  onames[j] = names[i] ;
60  out[j++] = x[i];
61  }
62  out.attr("names") = onames ;
63  } else {
64  for( R_xlen_t i=0, j=0; i<n; i++){
65  if( Vector<RTYPE>::is_na( x[i] ) ) continue ;
66  out[j++] = x[i];
67  }
68  }
69  return out ;
70  }
71 
72 } // sugar
73 
74 template <int RTYPE, bool NA, typename T>
76  return sugar::na_omit_impl<RTYPE,NA,T>(
77  t.get_ref(),
79  ) ;
80 }
81 
82 } // Rcpp
83 #endif
84 
AttributeProxy attr(const std::string &name)
VECTOR & get_ref()
Definition: VectorBase.h:37
no_init_vector no_init(R_xlen_t size)
Definition: no_init.h:69
R_xlen_t size() const
Definition: Vector.h:274
sugar::Sum< INTSXP, NA, T > sum(const VectorBase< INTSXP, NA, T > &t)
Definition: sum.h:98
Vector< RTYPE > na_omit_impl(const T &x, Rcpp::traits::false_type)
Definition: na_omit.h:29
sugar::IsNa< RTYPE, NA, T > is_na(const Rcpp::VectorBase< RTYPE, NA, T > &t)
Definition: is_na.h:87
Rcpp API.
Definition: algo.h:28
Vector< RTYPE > na_omit(const VectorBase< RTYPE, NA, T > &t)
Definition: na_omit.h:75