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