Skip to content

Commit b0bfc65

Browse files
committed
Algorithm...first checkin
1 parent 5ba63e9 commit b0bfc65

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ inst/lib
33
src/*.o
44
src/*.so
55
src/*.dll
6+
src/symbols.rds
67
.Rhistory
78
.RData
89

inst/include/Rcpp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@
8080

8181
#include <Rcpp/platform/solaris.h>
8282
#include <Rcpp/api/meat/meat.h>
83+
84+
#include <Rcpp/algorithm.h>
8385
#endif

inst/include/Rcpp/algorithm.h

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#if __cplusplus >= 201103L
2+
# define RCPP_CONSTEXPR constexpr
3+
#else
4+
# define RCPP_CONSTEXPR const
5+
#endif
6+
7+
namespace Rcpp {
8+
namespace algorithm {
9+
10+
namespace helpers {
11+
template< typename T >
12+
struct rtype_helper {
13+
static RCPP_CONSTEXPR int RTYPE = REALSXP;
14+
static inline double NA() { return NA_REAL; }
15+
static inline RCPP_CONSTEXPR double ZERO() { return 0.0; }
16+
static inline RCPP_CONSTEXPR double ONE() { return 1.0; }
17+
};
18+
19+
template<>
20+
struct rtype_helper< double > {
21+
static RCPP_CONSTEXPR int RTYPE = REALSXP;
22+
static inline double NA() { return NA_REAL; }
23+
static inline RCPP_CONSTEXPR double ZERO() { return 0.0; }
24+
static inline RCPP_CONSTEXPR double ONE() { return 1.0; }
25+
};
26+
27+
template<>
28+
struct rtype_helper< int > {
29+
static RCPP_CONSTEXPR int RTYPE = INTSXP;
30+
static inline int NA() { return NA_INTEGER; }
31+
static inline RCPP_CONSTEXPR int ZERO() { return 0; }
32+
static inline RCPP_CONSTEXPR int ONE() { return 1; }
33+
};
34+
35+
template< typename T >
36+
struct rtype {
37+
static RCPP_CONSTEXPR int RTYPE =
38+
rtype_helper< typename traits::remove_const_and_reference< T >::type >::RTYPE;
39+
static inline T NA() { return rtype_helper< typename traits::remove_const_and_reference< T >::type >::NA(); }
40+
static inline RCPP_CONSTEXPR T ZERO() { return rtype_helper< typename traits::remove_const_and_reference< T >::type >::ZERO(); }
41+
static inline RCPP_CONSTEXPR T ONE() { return rtype_helper< typename traits::remove_const_and_reference< T >::type >::ONE(); }
42+
};
43+
44+
struct log {
45+
template< typename T >
46+
inline double operator()(T val) {
47+
if (!Vector< rtype< T >::RTYPE >::is_na(val)) {
48+
return std::log(val);
49+
}
50+
51+
return rtype< double >::NA();
52+
}
53+
};
54+
55+
struct exp {
56+
template< typename T >
57+
inline double operator()(T val) {
58+
if (!Vector< rtype< T >::RTYPE >::is_na(val)) {
59+
return std::exp(val);
60+
}
61+
62+
return rtype< double >::NA();
63+
}
64+
};
65+
66+
struct sqrt {
67+
template< typename T >
68+
inline double operator()(T val) {
69+
if (!Vector< rtype< T >::RTYPE >::is_na(val)) {
70+
return std::sqrt(val);
71+
}
72+
73+
return rtype< double >::NA();
74+
}
75+
};
76+
} // namespace helpers
77+
78+
template< typename InputIterator >
79+
typename std::iterator_traits< InputIterator >::value_type sum(InputIterator begin, InputIterator end) {
80+
81+
typedef typename std::iterator_traits< InputIterator >::value_type value_type;
82+
typedef typename helpers::rtype< value_type > rtype;
83+
84+
if (begin != end) {
85+
value_type start = rtype::ZERO();
86+
87+
while (begin != end) {
88+
if (!Vector< rtype::RTYPE >::is_na(*begin)) {
89+
start += *begin++;
90+
} else {
91+
return rtype::NA();
92+
}
93+
}
94+
95+
return start;
96+
}
97+
98+
return rtype::ZERO();
99+
}
100+
101+
template< typename InputIterator >
102+
typename std::iterator_traits< InputIterator >::value_type sum_nona(InputIterator begin, InputIterator end) {
103+
104+
typedef typename std::iterator_traits< InputIterator >::value_type value_type;
105+
typedef typename helpers::rtype< value_type > rtype;
106+
107+
if (begin != end) {
108+
value_type start = rtype::ZERO();
109+
110+
while (begin != end) {
111+
start += *begin++;
112+
}
113+
114+
return start;
115+
}
116+
117+
return rtype::ZERO();
118+
}
119+
120+
template< typename InputIterator >
121+
typename std::iterator_traits< InputIterator >::value_type prod(InputIterator begin, InputIterator end) {
122+
123+
typedef typename std::iterator_traits< InputIterator >::value_type value_type;
124+
typedef typename helpers::rtype< value_type > rtype;
125+
126+
if (begin != end) {
127+
value_type start = rtype::ONE();
128+
129+
while (begin != end) {
130+
if (!Vector< rtype::RTYPE >::is_na(*begin)) {
131+
start *= *begin++;
132+
} else {
133+
return rtype::NA();
134+
}
135+
}
136+
137+
return start;
138+
}
139+
140+
return rtype::ZERO();
141+
}
142+
143+
template< typename InputIterator >
144+
typename std::iterator_traits< InputIterator >::value_type prod_nona(InputIterator begin, InputIterator end) {
145+
146+
typedef typename std::iterator_traits< InputIterator >::value_type value_type;
147+
typedef typename helpers::rtype< value_type > rtype;
148+
149+
if (begin != end) {
150+
value_type start = rtype::ONE();
151+
152+
while (begin != end) {
153+
start *= *begin++;
154+
}
155+
156+
return start;
157+
}
158+
159+
return rtype::ZERO();
160+
}
161+
162+
template< typename InputIterator, typename OutputIterator >
163+
void log(InputIterator begin, InputIterator end, OutputIterator out) {
164+
std::transform(begin, end, out, helpers::log());
165+
}
166+
167+
template< typename InputIterator, typename OutputIterator >
168+
void exp(InputIterator begin, InputIterator end, OutputIterator out) {
169+
std::transform(begin, end, out, helpers::exp());
170+
}
171+
172+
template< typename InputIterator, typename OutputIterator >
173+
void sqrt(InputIterator begin, InputIterator end, OutputIterator out) {
174+
std::transform(begin, end, out, helpers::sqrt());
175+
}
176+
177+
} // namespace algorithm
178+
} // namespace Rcpp
179+
180+
#undef RCPP_CONSTEXPR

inst/unitTests/cpp/algorithm.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <Rcpp.h>
2+
3+
// [[Rcpp::export]]
4+
double sumTest(Rcpp::NumericVector v, int begin, int end) {
5+
return Rcpp::algorithm::sum(v.begin() + (begin - 1), v.begin() + end);
6+
}
7+
8+
// [[Rcpp::export]]
9+
double prodTest(Rcpp::NumericVector v, int begin, int end) {
10+
return Rcpp::algorithm::prod(v.begin() + (begin - 1), v.begin() + end);
11+
}
12+
13+
// [[Rcpp::export]]
14+
Rcpp::NumericVector logTest(Rcpp::NumericVector v) {
15+
Rcpp::NumericVector x = Rcpp::clone(v);
16+
Rcpp::algorithm::log(v.begin(), v.end(), x.begin());
17+
return x;
18+
}
19+
20+
// [[Rcpp::export]]
21+
Rcpp::NumericVector expTest(Rcpp::NumericVector v) {
22+
Rcpp::NumericVector x = Rcpp::clone(v);
23+
Rcpp::algorithm::exp(v.begin(), v.end(), x.begin());
24+
return x;
25+
}
26+
27+
// [[Rcpp::export]]
28+
Rcpp::NumericVector sqrtTest(Rcpp::NumericVector v) {
29+
Rcpp::NumericVector x = Rcpp::clone(v);
30+
Rcpp::algorithm::sqrt(v.begin(), v.end(), x.begin());
31+
return x;
32+
}

inst/unitTests/runit.algorithm.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/r -t
2+
#
3+
# Copyright (C) 2010 - 2015 Dirk Eddelbuettel and Romain Francois
4+
#
5+
# This file is part of Rcpp.
6+
#
7+
# Rcpp is free software: you can redistribute it and/or modify it
8+
# under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 2 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# Rcpp is distributed in the hope that it will be useful, but
13+
# WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
19+
20+
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
21+
22+
if (.runThisTest) {
23+
24+
.setUp <- Rcpp:::unitTestSetup("algorithms.cpp")
25+
26+
test.sum <- function() {
27+
v <- c(1.0, 2.0, 3.0, 4.0, 5.0)
28+
checkEquals(sum(v), sumTest(v, 1, 5))
29+
}
30+
31+
test.prod <- function() {
32+
v <- c(1.0, 2.0, 3.0, 4.0, 5.0)
33+
checkEquals(prod(v), prodTest(v, 1, 5))
34+
}
35+
36+
test.log <- function() {
37+
v <- c(1.0, 2.0, 3.0, 4.0, 5.0)
38+
checkEquals(log(v), logTest(v))
39+
}
40+
41+
test.exp <- function() {
42+
v <- c(1.0, 2.0, 3.0, 4.0, 5.0)
43+
checkEquals(exp(v), expTest(v))
44+
}
45+
46+
test.sqrt <- function() {
47+
v <- c(1.0, 2.0, 3.0, 4.0, 5.0)
48+
checkEquals(sqrt(v), sqrtTest(v))
49+
}
50+
}

0 commit comments

Comments
 (0)