Skip to content

Commit 3c6ddc1

Browse files
committed
Merge remote-tracking branch 'RcppCore/master' into unit-test-rdist
# Conflicts: # ChangeLog
2 parents db68cba + 75c3f6d commit 3c6ddc1

File tree

7 files changed

+216
-41
lines changed

7 files changed

+216
-41
lines changed

ChangeLog

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
* inst/unitTests/cpp/rmath.cpp: Added unit test functions for Rmath
44
random number generators.
55
* inst/unitTests/runit.rmath.R: idem
6+
* inst/include/Rcpp/Environment.h: Added get() & find() that accept
7+
a symbol
8+
* inst/include/Rcpp.h: Modified header load order so that Symbol.h
9+
is now placed before Environment.h
610

711
2016-07-21 Dirk Eddelbuettel <edd@debian.org>
812

@@ -16,7 +20,7 @@
1620

1721
* DESCRIPTION: Release 0.12.6
1822
* inst/NEWS.Rd: Release 0.12.6
19-
* vignettes/Rcpp.bib: Release 0.12.6, RProtoBuf updates
23+
* vignettes/Rcpp.bib: Release 0.12.6, RProtoBuf updates
2024
* inst/include/Rcpp/config.h: Release 0.12.6
2125

2226
* README.md: Updated counts for dependents and tests

inst/NEWS.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
\itemize{
1010
\item The \code{NORET} macro is now defined if it was not already defined
1111
by R (Kevin fixing issue \ghit{512}).
12+
\item Environment functions get() & find() now accept a Symbol
13+
(James Joseph Balamuta in \ghpr{513} addressing issue \ghit{326}).
1214
}
1315
\item Changes in Rcpp unit tests
1416
\itemize{

inst/include/Rcpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <Rcpp/clone.h>
3434
#include <Rcpp/grow.h>
3535
#include <Rcpp/Dimension.h>
36+
37+
#include <Rcpp/Symbol.h>
3638
#include <Rcpp/Environment.h>
3739

3840
#include <Rcpp/Vector.h>
@@ -42,7 +44,6 @@
4244
#include <Rcpp/Promise.h>
4345

4446
#include <Rcpp/XPtr.h>
45-
#include <Rcpp/Symbol.h>
4647
#include <Rcpp/DottedPairImpl.h>
4748
#include <Rcpp/Function.h>
4849
#include <Rcpp/Language.h>

inst/include/Rcpp/Environment.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,27 @@ namespace Rcpp{
115115
}
116116
return res ;
117117
}
118+
119+
/**
120+
* Get an object from the environment
121+
*
122+
* @param name symbol name to call
123+
*
124+
* @return a SEXP (possibly R_NilValue)
125+
*/
126+
SEXP get(Symbol name) const {
127+
SEXP env = Storage::get__() ;
128+
SEXP res = Rf_findVarInFrame( env, name ) ;
129+
130+
if( res == R_UnboundValue ) return R_NilValue ;
131+
132+
/* We need to evaluate if it is a promise */
133+
if( TYPEOF(res) == PROMSXP){
134+
res = Rf_eval( res, env ) ;
135+
}
136+
return res ;
137+
}
138+
118139

119140
/**
120141
* Get an object from the environment or one of its
@@ -136,6 +157,29 @@ namespace Rcpp{
136157
}
137158
return res ;
138159
}
160+
161+
/**
162+
* Get an object from the environment or one of its
163+
* parents
164+
*
165+
* @param name symbol name to call
166+
*/
167+
SEXP find(Symbol name) const{
168+
SEXP env = Storage::get__() ;
169+
SEXP res = Rf_findVar( name, env ) ;
170+
171+
if( res == R_UnboundValue ) {
172+
// Pass on the const char* to the RCPP_EXCEPTION_CLASS's
173+
// const std::string& requirement
174+
throw binding_not_found(name.c_str()) ;
175+
}
176+
177+
/* We need to evaluate if it is a promise */
178+
if( TYPEOF(res) == PROMSXP){
179+
res = Rf_eval( res, env ) ;
180+
}
181+
return res ;
182+
}
139183

140184
/**
141185
* Indicates if an object called name exists in the

inst/unitTests/cpp/rmath.cpp

Lines changed: 108 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ NumericVector runit_qnorm( double x, double a, double b ){
4444

4545
// [[Rcpp::export]]
4646
NumericVector runit_rnorm( double a, double b ){
47-
return NumericVector::create(R::rnorm(a, b), R::rnorm(a, b));
47+
NumericVector o(5);
48+
49+
for(int i = 0; i < o.size(); i++)
50+
o[i] = R::rnorm(a, b);
51+
52+
return o;
4853
}
4954

5055
// ------------------- Uniform Distribution
@@ -68,7 +73,12 @@ NumericVector runit_qunif( double x, double a, double b ){
6873

6974
// [[Rcpp::export]]
7075
NumericVector runit_runif( double a, double b ){
71-
return NumericVector::create(R::runif(a, b), R::runif(a, b));
76+
NumericVector o(5);
77+
78+
for(int i = 0; i < o.size(); i++)
79+
o[i] = R::runif(a, b);
80+
81+
return o;
7282
}
7383

7484

@@ -93,7 +103,12 @@ NumericVector runit_qgamma( double x, double a, double b ){
93103

94104
// [[Rcpp::export]]
95105
NumericVector runit_rgamma( double a, double b ){
96-
return NumericVector::create(R::rgamma(a, b), R::rgamma(a, b));
106+
NumericVector o(5);
107+
108+
for(int i = 0; i < o.size(); i++)
109+
o[i] = R::rgamma(a, b);
110+
111+
return o;
97112
}
98113

99114

@@ -118,7 +133,12 @@ NumericVector runit_qbeta( double x, double a, double b ){
118133

119134
// [[Rcpp::export]]
120135
NumericVector runit_rbeta( double a, double b ){
121-
return NumericVector::create(R::rbeta(a, b), R::rbeta(a, b));
136+
NumericVector o(5);
137+
138+
for(int i = 0; i < o.size(); i++)
139+
o[i] = R::rbeta(a, b);
140+
141+
return o;
122142
}
123143

124144

@@ -143,7 +163,12 @@ NumericVector runit_qlnorm( double x, double a, double b ){
143163

144164
// [[Rcpp::export]]
145165
NumericVector runit_rlnorm( double a, double b ){
146-
return NumericVector::create(R::rlnorm(a, b), R::rlnorm(a, b));
166+
NumericVector o(5);
167+
168+
for(int i = 0; i < o.size(); i++)
169+
o[i] = R::rlnorm(a, b);
170+
171+
return o;
147172
}
148173

149174
// ------------------- Chi-Squared Distribution
@@ -167,7 +192,12 @@ NumericVector runit_qchisq( double x, double a ){
167192

168193
// [[Rcpp::export]]
169194
NumericVector runit_rchisq( double a ){
170-
return NumericVector::create(R::rchisq(a), R::rchisq(a));
195+
NumericVector o(5);
196+
197+
for(int i = 0; i < o.size(); i++)
198+
o[i] = R::rchisq(a);
199+
200+
return o;
171201
}
172202

173203
// ------------------- Non-central Chi-Squared Distribution
@@ -210,7 +240,12 @@ NumericVector runit_qf( double x, double a, double b ){
210240

211241
// [[Rcpp::export]]
212242
NumericVector runit_rf( double a, double b ){
213-
return NumericVector::create(R::rf(a, b), R::rf(a, b));
243+
NumericVector o(5);
244+
245+
for(int i = 0; i < o.size(); i++)
246+
o[i] = R::rf(a, b);
247+
248+
return o;
214249
}
215250

216251
// ------------------- Student t Distribution
@@ -234,7 +269,12 @@ NumericVector runit_qt( double x, double a ){
234269

235270
// [[Rcpp::export]]
236271
NumericVector runit_rt( double a ){
237-
return NumericVector::create(R::rt(a), R::rt(a));
272+
NumericVector o(5);
273+
274+
for(int i = 0; i < o.size(); i++)
275+
o[i] = R::rt(a);
276+
277+
return o;
238278
}
239279

240280
// ------------------- Binomial Distribution
@@ -258,7 +298,12 @@ NumericVector runit_qbinom( double x, double a, double b ){
258298

259299
// [[Rcpp::export]]
260300
NumericVector runit_rbinom( double a, double b ){
261-
return NumericVector::create(R::rbinom(a, b), R::rbinom(a, b));
301+
NumericVector o(5);
302+
303+
for(int i = 0; i < o.size(); i++)
304+
o[i] = R::rbinom(a, b);
305+
306+
return o;
262307
}
263308

264309
// ------------------- Cauchy Distribution
@@ -282,7 +327,12 @@ NumericVector runit_qcauchy( double x, double a, double b ){
282327

283328
// [[Rcpp::export]]
284329
NumericVector runit_rcauchy( double a, double b ){
285-
return NumericVector::create(R::rcauchy(a, b), R::rcauchy(a, b));
330+
NumericVector o(5);
331+
332+
for(int i = 0; i < o.size(); i++)
333+
o[i] = R::rcauchy(a, b);
334+
335+
return o;
286336
}
287337

288338
// ------------------- Exponential Distribution
@@ -306,7 +356,12 @@ NumericVector runit_qexp( double x, double a ){
306356

307357
// [[Rcpp::export]]
308358
NumericVector runit_rexp( double a ){
309-
return NumericVector::create(R::rexp(a), R::rexp(a));
359+
NumericVector o(5);
360+
361+
for(int i = 0; i < o.size(); i++)
362+
o[i] = R::rexp(a);
363+
364+
return o;
310365
}
311366

312367
// ------------------- Geometric Distribution
@@ -330,7 +385,12 @@ NumericVector runit_qgeom( double x, double a ){
330385

331386
// [[Rcpp::export]]
332387
NumericVector runit_rgeom( double a ){
333-
return NumericVector::create(R::rgeom(a), R::rgeom(a));
388+
NumericVector o(5);
389+
390+
for(int i = 0; i < o.size(); i++)
391+
o[i] = R::rgeom(a);
392+
393+
return o;
334394
}
335395

336396
// ------------------- Hypergeometric Distribution
@@ -354,7 +414,12 @@ NumericVector runit_qhyper( double x, double a, double b, double c ){
354414

355415
// [[Rcpp::export]]
356416
NumericVector runit_rhyper( double a, double b, double c ){
357-
return NumericVector::create(R::rhyper(a, b, c), R::rhyper(a, b, c));
417+
NumericVector o(5);
418+
419+
for(int i = 0; i < o.size(); i++)
420+
o[i] = R::rhyper(a, b, c);
421+
422+
return o;
358423
}
359424

360425
// ------------------- Negative Binomial Distribution
@@ -378,7 +443,12 @@ NumericVector runit_qnbinom( double x, double a, double b ){
378443

379444
// [[Rcpp::export]]
380445
NumericVector runit_rnbinom( double a, double b){
381-
return NumericVector::create(R::rnbinom(a, b), R::rnbinom(a, b));
446+
NumericVector o(5);
447+
448+
for(int i = 0; i < o.size(); i++)
449+
o[i] = R::rnbinom(a, b);
450+
451+
return o;
382452
}
383453

384454
// ------------------- Poisson Distribution
@@ -402,7 +472,12 @@ NumericVector runit_qpois( double x, double a ){
402472

403473
// [[Rcpp::export]]
404474
NumericVector runit_rpois( double a ){
405-
return NumericVector::create(R::rpois(a), R::rpois(a));
475+
NumericVector o(5);
476+
477+
for(int i = 0; i < o.size(); i++)
478+
o[i] = R::rpois(a);
479+
480+
return o;
406481
}
407482

408483
// ------------------- Weibull Distribution
@@ -426,7 +501,12 @@ NumericVector runit_qweibull( double x, double a, double b ){
426501

427502
// [[Rcpp::export]]
428503
NumericVector runit_rweibull( double a, double b ){
429-
return NumericVector::create(R::rweibull(a, b), R::rweibull(a, b));
504+
NumericVector o(5);
505+
506+
for(int i = 0; i < o.size(); i++)
507+
o[i] = R::rweibull(a, b);
508+
509+
return o;
430510
}
431511

432512
// ------------------- Logistic Distribution
@@ -450,7 +530,12 @@ NumericVector runit_qlogis( double x, double a, double b ){
450530

451531
// [[Rcpp::export]]
452532
NumericVector runit_rlogis( double a, double b ){
453-
return NumericVector::create(R::rlogis(a, b), R::rlogis(a, b));
533+
NumericVector o(5);
534+
535+
for(int i = 0; i < o.size(); i++)
536+
o[i] = R::rlogis(a, b);
537+
538+
return o;
454539
}
455540

456541
// ------------------- Non-central Beta Distribution
@@ -531,5 +616,10 @@ NumericVector runit_qwilcox( double x, double a, double b ){
531616

532617
// [[Rcpp::export]]
533618
NumericVector runit_rwilcox( double a, double b ){
534-
return NumericVector::create(R::rwilcox(a, b), R::rwilcox(a, b));
619+
NumericVector o(5);
620+
621+
for(int i = 0; i < o.size(); i++)
622+
o[i] = R::rwilcox(a, b);
623+
624+
return o;
535625
}

0 commit comments

Comments
 (0)