Skip to content

Commit 3f88fc3

Browse files
authored
Merge pull request #136 from ThibHlln/add-rank-member
add rank member to rtensor and rarray
2 parents 9fedb68 + 4d7bdc9 commit 3f88fc3

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

include/xtensor-r/rarray.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ namespace xt
106106

107107
constexpr static int SXP = Rcpp::traits::r_sexptype_traits<T>::rtype;
108108

109+
constexpr static std::size_t rank = SIZE_MAX;
110+
109111
rarray() = default;
110112
rarray(SEXP exp);
111113

include/xtensor-r/rtensor.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ namespace xt
9393

9494
constexpr static int SXP = Rcpp::traits::r_sexptype_traits<T>::rtype;
9595

96+
constexpr static std::size_t rank = N;
97+
9698
rtensor();
9799
rtensor(nested_initializer_list_t<T, N> t);
98100

test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ set(XTENSOR_R_TESTS
100100
test_roptional.cpp
101101
test_rtensor.cpp
102102
test_rvectorize.cpp
103+
test_sfinae.cpp
103104
)
104105

105106
add_executable(test_xtensor_r ${XTENSOR_R_TESTS} ${XTENSOR_R_HEADERS})

test/test_sfinae.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/***************************************************************************
2+
* Copyright (c) Wolf Vollprecht, Johan Mabille and Sylvain Corlay *
3+
* Copyright (c) QuantStack *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
****************************************************************************/
9+
10+
#include <limits>
11+
12+
#include "gtest/gtest.h"
13+
#include "xtensor-r/rtensor.hpp"
14+
#include "xtensor-r/rarray.hpp"
15+
#include "xtensor/xarray.hpp"
16+
#include "xtensor/xtensor.hpp"
17+
18+
namespace xt
19+
{
20+
template <class E, std::enable_if_t<!xt::has_fixed_rank_t<E>::value, int> = 0>
21+
inline bool sfinae_has_fixed_rank(E&&)
22+
{
23+
return false;
24+
}
25+
26+
template <class E, std::enable_if_t<xt::has_fixed_rank_t<E>::value, int> = 0>
27+
inline bool sfinae_has_fixed_rank(E&&)
28+
{
29+
return true;
30+
}
31+
32+
TEST(sfinae, fixed_rank)
33+
{
34+
xt::rarray<int> a = {{9, 9, 9}, {9, 9, 9}};
35+
xt::rtensor<int, 1> b = {9, 9};
36+
xt::rtensor<int, 2> c = {{9, 9}, {9, 9}};
37+
38+
EXPECT_TRUE(sfinae_has_fixed_rank(a) == false);
39+
EXPECT_TRUE(sfinae_has_fixed_rank(b) == true);
40+
EXPECT_TRUE(sfinae_has_fixed_rank(c) == true);
41+
}
42+
43+
TEST(sfinae, get_rank)
44+
{
45+
xt::rtensor<double, 1> A = xt::zeros<double>({2});
46+
xt::rtensor<double, 2> B = xt::zeros<double>({2, 2});
47+
xt::rarray<double> C = xt::zeros<double>({2, 2});
48+
49+
EXPECT_TRUE(xt::get_rank<decltype(A)>::value == 1ul);
50+
EXPECT_TRUE(xt::get_rank<decltype(B)>::value == 2ul);
51+
EXPECT_TRUE(xt::get_rank<decltype(C)>::value == SIZE_MAX);
52+
}
53+
}

0 commit comments

Comments
 (0)