Skip to content

Commit ee78ef9

Browse files
committed
Merge pull request #204 from RcppCore/feature/logical-vector-from-bool
Feature/logical vector from bool
2 parents 5784940 + 16fd62b commit ee78ef9

File tree

7 files changed

+88
-2
lines changed

7 files changed

+88
-2
lines changed

ChangeLog

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@
8787
* inst/unitTests/testRcppClass/man/Rcpp_class_examples.Rd: Similar
8888
update to yesterday's update for testRcppModule
8989

90+
2014-09-14 Kevin Ushey <kevinushey@gmail.com>
91+
92+
* inst/include/Rcpp/vector/Vector.h: Alternate strategy using SFINAE
93+
* inst/include/Rcpp/traits/traits.h: Idem
94+
* inst/include/Rcpp/traits/enable_if.h: Idem
95+
96+
2014-09-13 Kevin Ushey <kevinushey@gmail.com>
97+
98+
* inst/include/Rcpp/vector/Vector.h: Allow logical vectors to be created
99+
from bools
100+
* inst/unitTests/runit.Vector.R: Idem
101+
* inst/unitTests/cpp/Vector.cpp: Idem
102+
90103
2014-09-13 Dirk Eddelbuettel <edd@debian.org>
91104

92105
* inst/skeleton/Rcpp_modules_examples.Rd: Added to document modules examples
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef RCPP_TRAITS_ENABLE_IF_H
2+
#define RCPP_TRAITS_ENABLE_IF_H
3+
4+
namespace Rcpp {
5+
namespace traits {
6+
7+
template <bool B, typename T = void>
8+
struct enable_if {};
9+
10+
template <typename T>
11+
struct enable_if<true, T> {
12+
typedef T type;
13+
};
14+
15+
}
16+
}
17+
18+
#endif

inst/include/Rcpp/traits/is_bool.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef Rcpp__traits__is_bool__h
2+
#define Rcpp__traits__is_bool__h
3+
4+
namespace Rcpp{ namespace traits {
5+
6+
template<typename>
7+
struct is_bool
8+
: public false_type { };
9+
10+
template<>
11+
struct is_bool<bool>
12+
: public true_type { };
13+
14+
template<>
15+
struct is_bool<const bool>
16+
: public true_type { };
17+
18+
template<>
19+
struct is_bool<volatile bool>
20+
: public true_type { };
21+
22+
}}
23+
24+
#endif

inst/include/Rcpp/traits/traits.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct int2type { enum { value = I }; };
3737

3838
#include <Rcpp/traits/integral_constant.h>
3939
#include <Rcpp/traits/same_type.h>
40+
#include <Rcpp/traits/enable_if.h>
4041
#include <Rcpp/traits/is_wide_string.h>
4142
#include <Rcpp/traits/char_type.h>
4243
#include <Rcpp/traits/named_object.h>
@@ -59,6 +60,7 @@ struct int2type { enum { value = I }; };
5960
#include <Rcpp/traits/is_finite.h>
6061
#include <Rcpp/traits/is_infinite.h>
6162
#include <Rcpp/traits/is_nan.h>
63+
#include <Rcpp/traits/is_bool.h>
6264
#include <Rcpp/traits/if_.h>
6365
#include <Rcpp/traits/get_na.h>
6466
#include <Rcpp/traits/is_trivial.h>

inst/include/Rcpp/vector/Vector.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ class Vector :
8989
fill( u ) ;
9090
}
9191

92-
// constructor for CharacterVector()
92+
// constructor for CharacterVector()
9393
Vector( const std::string& st ){
9494
RCPP_DEBUG_2( "Vector<%d>( const std::string& = %s )", RTYPE, st.c_str() )
9595
Storage::set__( internal::vector_from_string<RTYPE>(st) ) ;
9696
}
9797

98-
// constructor for CharacterVector()
98+
// constructor for CharacterVector()
9999
Vector( const char* st ) {
100100
RCPP_DEBUG_2( "Vector<%d>( const char* = %s )", RTYPE, st )
101101
Storage::set__(internal::vector_from_string<RTYPE>(st) ) ;
@@ -119,6 +119,16 @@ class Vector :
119119
}
120120
}
121121

122+
// Enable construction from bool for LogicalVectors
123+
// SFINAE only work for template. Add template class T and then restict T to
124+
// bool.
125+
template <typename T>
126+
Vector(T value,
127+
typename Rcpp::traits::enable_if<traits::is_bool<T>::value && RTYPE == LGLSXP, void>::type* = 0) {
128+
Storage::set__(Rf_allocVector(RTYPE, 1));
129+
fill(value);
130+
}
131+
122132
template <typename U>
123133
Vector( const Dimension& dims, const U& u) {
124134
RCPP_DEBUG_2( "Vector<%d>( const Dimension& (%d), const U& )", RTYPE, dims.size() )

inst/unitTests/cpp/Vector.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,3 +750,14 @@ List list_sexp_assign(SEXP x) {
750750
L = x;
751751
return L;
752752
}
753+
754+
// [[Rcpp::export]]
755+
bool logical_vector_from_bool() {
756+
return true;
757+
}
758+
759+
// [[Rcpp::export]]
760+
LogicalVector logical_vector_from_bool_assign() {
761+
LogicalVector result = true;
762+
return result;
763+
}

inst/unitTests/runit.Vector.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,14 @@ if (.runThisTest) {
665665
other <- list_sexp_assign(l)
666666
checkIdentical(l, other)
667667
}
668+
669+
test.logical.vector.from.bool <- function() {
670+
checkIdentical(logical_vector_from_bool(), TRUE)
671+
}
672+
673+
test.logical.vector.from.bool.assign <- function() {
674+
checkIdentical(logical_vector_from_bool_assign(), TRUE)
675+
}
668676

669677
}
670678

0 commit comments

Comments
 (0)