Rcpp Version 0.12.12
which_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 // which_max.h: Rcpp R/C++ interface class library -- which.max
4 //
5 // Copyright (C) 2012 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__which_max_h
23 #define Rcpp__sugar__which_max_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, bool NA, typename T>
29 class WhichMax {
30 public:
33  WhichMax(const VEC_TYPE& obj_ ) : obj(obj_){}
34 
35  R_xlen_t get() const {
36  STORAGE current = obj[0] ;
37  STORAGE min = current ;
38  R_xlen_t index = 0 ;
39  if( Rcpp::traits::is_na<RTYPE>(current) ) return NA_INTEGER ;
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 NA_INTEGER ;
44  if( current > min ){
45  min = current ;
46  index = i ;
47  }
48  }
49  return index ;
50  }
51 
52 private:
53  const VEC_TYPE& obj ;
54 
55 } ;
56 
57 template <int RTYPE, typename T>
58 class WhichMax<RTYPE,false,T> {
59 public:
62  WhichMax(const VEC_TYPE& obj_ ) : obj(obj_){}
63 
64  R_xlen_t get() const {
65  STORAGE current = obj[0] ;
66  STORAGE min = current ;
67  R_xlen_t index = 0 ;
68  R_xlen_t n = obj.size() ;
69  for( R_xlen_t i=1; i<n; i++){
70  current = obj[i] ;
71  if( current > min ){
72  min = current ;
73  index = i ;
74  }
75  }
76  return index ;
77  }
78 
79 private:
80  const VEC_TYPE& obj ;
81 
82 } ;
83 
84 
85 } // sugar
86 
87 
88 
89 template <int RTYPE, bool NA, typename T>
90 R_xlen_t which_max( const VectorBase<RTYPE,NA,T>& t ){
91  return sugar::WhichMax<RTYPE,NA,T>(t).get() ;
92 }
93 
94 } // Rcpp
95 #endif
96 
Rcpp::VectorBase< RTYPE, false, T > VEC_TYPE
Definition: which_max.h:60
R_xlen_t size() const
Definition: VectorBase.h:49
const VEC_TYPE & obj
Definition: which_max.h:53
sugar::Min< RTYPE, NA, T > min(const VectorBase< RTYPE, NA, T > &x)
Definition: min.h:80
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition: which_max.h:31
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: which_max.h:32
R_xlen_t which_max(const VectorBase< RTYPE, NA, T > &t)
Definition: which_max.h:90
WhichMax(const VEC_TYPE &obj_)
Definition: which_max.h:33
Rcpp API.
Definition: algo.h:28
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: which_max.h:61