Rcpp Version 0.12.12
min.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // Min.h: Rcpp R/C++ interface class library -- min
4 //
5 // Copyright (C) 2012 - 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__min_h
23 #define Rcpp__sugar__min_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28  template <int RTYPE, bool NA, typename T>
29  class Min {
30  public:
32 
33  Min( const T& obj_) : obj(obj_) {}
34 
35  operator STORAGE() const {
36  STORAGE min, current ;
37  min = obj[0] ;
38  if( Rcpp::traits::is_na<RTYPE>( min ) ) return min ;
39 
40  R_xlen_t n = obj.size() ;
41  for( R_xlen_t i=1; i<n; i++){
42  current = obj[i] ;
43  if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
44  if( current < min ) min = current ;
45  }
46  return min ;
47  }
48 
49  const T& obj ;
50  } ;
51 
52  // version for NA = false
53  template <int RTYPE, typename T>
54  class Min<RTYPE,false,T> {
55  public:
57 
58  Min( const T& obj_) : obj(obj_) {}
59 
60  operator STORAGE() const {
61  STORAGE min, current ;
62  min = obj[0] ;
63 
64  R_xlen_t n = obj.size() ;
65  for( R_xlen_t i=1; i<n; i++){
66  current = obj[i] ;
67  if( current < min ) min = current ;
68  }
69  return min ;
70  }
71 
72  const T& obj ;
73  } ;
74 
75 
76 } // sugar
77 
78 
79 template <int RTYPE, bool NA, typename T>
81  return sugar::Min<RTYPE,NA,T>(x.get_ref()) ;
82 }
83 
84 } // Rcpp
85 
86 #endif
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: min.h:56
VECTOR & get_ref()
Definition: VectorBase.h:37
sugar::Min< RTYPE, NA, T > min(const VectorBase< RTYPE, NA, T > &x)
Definition: min.h:80
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: min.h:31
Min(const T &obj_)
Definition: min.h:33
Rcpp API.
Definition: algo.h:28
const T & obj
Definition: min.h:49