Skip to content

Commit 0e97e34

Browse files
factor out min, max and range in their own file and classes
1 parent cf77796 commit 0e97e34

File tree

7 files changed

+272
-155
lines changed

7 files changed

+272
-155
lines changed

ChangeLog

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
so that it works even when we don't know the previous value
1414
* unitTests/runit.sugar.R :
1515
* unitTests/cpp/sugar.cpp :
16-
* TODO : 2 less items
16+
* include/Rcpp/sugar/functions/range.h : factored out of minmax.h
17+
* include/Rcpp/sugar/functions/min.h : factored out of minmax.h
18+
* include/Rcpp/sugar/functions/max.h : factored out of minmax.h
19+
* TODO : 3 less items
1720

1821
2013-09-17 JJ Allaire <jj@rstudio.org>
1922

TODO

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ Syntactic sugar
5050

5151
o operator/ needs to handle the case of division by 0
5252

53-
o min, max with specialization of the binary operators, so that we can do
54-
things like this lazily:
55-
56-
min( x ) < 4
57-
5853
o matrix functions : apply
5954

6055
o for character vectors: nchar, grepl, sub, gsub

inst/include/Rcpp/sugar/functions/functions.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
#include <Rcpp/sugar/functions/pmin.h>
4747
#include <Rcpp/sugar/functions/pmax.h>
4848
#include <Rcpp/sugar/functions/clamp.h>
49-
#include <Rcpp/sugar/functions/minmax.h>
49+
#include <Rcpp/sugar/functions/min.h>
50+
#include <Rcpp/sugar/functions/max.h>
51+
#include <Rcpp/sugar/functions/range.h>
5052
#include <Rcpp/sugar/functions/sign.h>
5153
#include <Rcpp/sugar/functions/diff.h>
5254
#include <Rcpp/sugar/functions/pow.h>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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:
31+
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
32+
33+
Max( const T& obj_) : obj(obj_) {}
34+
35+
operator STORAGE() {
36+
max_ = obj[0] ;
37+
if( Rcpp::traits::is_na<RTYPE>( max_ ) ) return max_ ;
38+
39+
int n = obj.size() ;
40+
for( int i=1; i<n; i++){
41+
current = obj[i] ;
42+
if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
43+
if( current > max_ ) max_ = current ;
44+
}
45+
return max_ ;
46+
}
47+
48+
private:
49+
const T& obj ;
50+
STORAGE min_, max_, current ;
51+
} ;
52+
53+
// version for NA = false
54+
template <int RTYPE, typename T>
55+
class Max<RTYPE,false,T> {
56+
public:
57+
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
58+
59+
Max( const T& obj_) : obj(obj_) {}
60+
61+
operator STORAGE() {
62+
max_ = obj[0] ;
63+
64+
int n = obj.size() ;
65+
for( int i=1; i<n; i++){
66+
current = obj[i] ;
67+
if( current > max_ ) max_ = current ;
68+
}
69+
return max_ ;
70+
}
71+
72+
private:
73+
const T& obj ;
74+
STORAGE max_, current ;
75+
} ;
76+
77+
78+
} // sugar
79+
80+
template <int RTYPE, bool NA, typename T>
81+
sugar::Max<RTYPE,NA,T> max( const VectorBase<RTYPE,NA,T>& x){
82+
return sugar::Max<RTYPE,NA,T>(x.get_ref()) ;
83+
}
84+
85+
} // Rcpp
86+
87+
#endif
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// Min.h: Rcpp R/C++ interface class library -- min
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__min_h
23+
#define Rcpp__sugar__min_h
24+
25+
namespace Rcpp{
26+
namespace sugar{
27+
28+
template <int RTYPE, bool NA, typename T>
29+
class Min {
30+
public:
31+
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
32+
33+
Min( const T& obj_) : obj(obj_) {}
34+
35+
operator STORAGE() {
36+
min_ = obj[0] ;
37+
if( Rcpp::traits::is_na<RTYPE>( min_ ) ) return min_ ;
38+
39+
int n = obj.size() ;
40+
for( int i=1; i<n; i++){
41+
current = obj[i] ;
42+
if( Rcpp::traits::is_na<RTYPE>( current ) ) return current;
43+
if( current < min_ ) min_ = current ;
44+
}
45+
return min_ ;
46+
}
47+
48+
const T& obj ;
49+
STORAGE min_, max_, current ;
50+
} ;
51+
52+
// version for NA = false
53+
template <int RTYPE, typename T>
54+
class Min<RTYPE,false,T> {
55+
public:
56+
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
57+
58+
Min( const T& obj_) : obj(obj_) {}
59+
60+
operator STORAGE() {
61+
min_ = obj[0] ;
62+
63+
int n = obj.size() ;
64+
for( int i=1; i<n; i++){
65+
current = obj[i] ;
66+
if( current < min_ ) min_ = current ;
67+
}
68+
return min_ ;
69+
}
70+
71+
const T& obj ;
72+
STORAGE min_, current ;
73+
} ;
74+
75+
76+
} // sugar
77+
78+
79+
template <int RTYPE, bool NA, typename T>
80+
sugar::Min<RTYPE,NA,T> min( const VectorBase<RTYPE,NA,T>& x){
81+
return sugar::Min<RTYPE,NA,T>(x.get_ref()) ;
82+
}
83+
84+
} // Rcpp
85+
86+
#endif

inst/include/Rcpp/sugar/functions/minmax.h

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)