Rcpp Version 1.1.2
Loading...
Searching...
No Matches
max.h
Go to the documentation of this file.
1
2// max.h: Rcpp R/C++ interface class library -- max
3//
4// Copyright (C) 2012 - 2025 Dirk Eddelbuettel and Romain Francois
5// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
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
25namespace Rcpp{
26namespace 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 R_xlen_t n = obj.size();
37 if (n == 0) {
38 if (RTYPE != REALSXP)
39 Rcpp::stop("missing argument to max");
40 return(static_cast<STORAGE>(R_NegInf));
41 }
42
43 STORAGE max, current ;
44 max = obj[0] ;
45 if( Rcpp::traits::is_na<RTYPE>( max ) ) return max ;
46 for( R_xlen_t i=1; i<n; i++){
47 current = obj[i] ;
48 if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
49 if( current > max ) max = current ;
50 }
51 return max ;
52 }
53
54 private:
55 const T& obj ;
56 } ;
57
58 // version for NA = false
59 template <int RTYPE, typename T>
60 class Max<RTYPE,false,T> {
61 public:
63
64 Max( const T& obj_) : obj(obj_) {}
65
66 operator STORAGE() const {
67 R_xlen_t n = obj.size();
68 if (n == 0) {
69 if (RTYPE != REALSXP)
70 Rcpp::stop("missing argument to max");
71 return(static_cast<STORAGE>(R_NegInf));
72 }
73
74 STORAGE max, current ;
75 max = obj[0] ;
76 for( R_xlen_t i=1; i<n; i++){
77 current = obj[i] ;
78 if( current > max ) max = current ;
79 }
80 return max ;
81 }
82
83 private:
84 const T& obj ;
85 } ;
86
87
88} // sugar
89
90template <int RTYPE, bool NA, typename T>
94
95} // Rcpp
96
97#endif
VECTOR & get_ref()
Definition VectorBase.h:37
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition max.h:62
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:55
bool is_na(typename storage_type< RTYPE >::type)
Definition is_na.h:32
Rcpp API.
Definition algo.h:28
void NORET stop(const std::string &message)
Definition exceptions.h:119
sugar::Max< RTYPE, NA, T > max(const VectorBase< RTYPE, NA, T > &x)
Definition max.h:91