Rcpp Version 1.0.9
min.h
Go to the documentation of this file.
1 
2 // Min.h: Rcpp R/C++ interface class library -- min
3 //
4 // Copyright (C) 2012 - 2018 Dirk Eddelbuettel and Romain Francois
5 //
6 // This file is part of Rcpp.
7 //
8 // Rcpp is free software: you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // Rcpp is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20 
21 #ifndef Rcpp__sugar__min_h
22 #define Rcpp__sugar__min_h
23 
24 namespace Rcpp{
25 namespace sugar{
26 
27  template <int RTYPE, bool NA, typename T>
28  class Min {
29  public:
31 
32  Min( const T& obj_) : obj(obj_) {}
33 
34  operator STORAGE() const {
35  R_xlen_t n = obj.size();
36  if (n == 0) return(static_cast<STORAGE>(R_PosInf));
37 
38  STORAGE min, current ;
39  min = obj[0] ;
40  if( Rcpp::traits::is_na<RTYPE>( min ) ) return min ;
41 
42  for( R_xlen_t i=1; i<n; i++){
43  current = obj[i] ;
44  if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
45  if( current < min ) min = current ;
46  }
47  return min ;
48  }
49 
50  const T& obj ;
51  } ;
52 
53  // version for NA = false
54  template <int RTYPE, typename T>
55  class Min<RTYPE,false,T> {
56  public:
58 
59  Min( const T& obj_) : obj(obj_) {}
60 
61  operator STORAGE() const {
62  R_xlen_t n = obj.size();
63  if (n == 0) return(static_cast<STORAGE>(R_PosInf));
64 
65  STORAGE min, current ;
66  min = obj[0] ;
67  for( R_xlen_t i=1; i<n; i++){
68  current = obj[i] ;
69  if( current < min ) min = current ;
70  }
71  return min ;
72  }
73 
74  const T& obj ;
75  } ;
76 
77 
78 } // sugar
79 
80 
81 template <int RTYPE, bool NA, typename T>
83  return sugar::Min<RTYPE,NA,T>(x.get_ref()) ;
84 }
85 
86 } // Rcpp
87 
88 #endif
VECTOR & get_ref()
Definition: VectorBase.h:37
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: min.h:57
const T & obj
Definition: min.h:50
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: min.h:30
Min(const T &obj_)
Definition: min.h:32
Rcpp API.
Definition: algo.h:28
sugar::Min< RTYPE, NA, T > min(const VectorBase< RTYPE, NA, T > &x)
Definition: min.h:82