Rcpp Version 1.1.2
Loading...
Searching...
No Matches
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 - 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__min_h
23#define Rcpp__sugar__min_h
24
25namespace Rcpp{
26namespace 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 R_xlen_t n = obj.size();
37 if (n == 0) {
38 if (RTYPE != REALSXP)
39 Rcpp::stop("missing argument to min");
40 return(static_cast<STORAGE>(R_PosInf));
41 }
42
43 STORAGE min, current ;
44 min = obj[0] ;
45 if( Rcpp::traits::is_na<RTYPE>( min ) ) return min ;
46
47 for( R_xlen_t i=1; i<n; i++){
48 current = obj[i] ;
49 if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
50 if( current < min ) min = current ;
51 }
52 return min ;
53 }
54
55 const T& obj ;
56 } ;
57
58 // version for NA = false
59 template <int RTYPE, typename T>
60 class Min<RTYPE,false,T> {
61 public:
63
64 Min( 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 min");
71 return(static_cast<STORAGE>(R_PosInf));
72 }
73
74 STORAGE min, current ;
75 min = obj[0] ;
76 for( R_xlen_t i=1; i<n; i++){
77 current = obj[i] ;
78 if( current < min ) min = current ;
79 }
80 return min ;
81 }
82
83 const T& obj ;
84 } ;
85
86
87} // sugar
88
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 min.h:62
const T & obj
Definition min.h:55
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition min.h:31
Min(const T &obj_)
Definition min.h:33
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::Min< RTYPE, NA, T > min(const VectorBase< RTYPE, NA, T > &x)
Definition min.h:91