Skip to content

add rank member to rtensor and rarray #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/xtensor-r/rarray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ namespace xt

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

constexpr static std::size_t rank = SIZE_MAX;

rarray() = default;
rarray(SEXP exp);

Expand Down
2 changes: 2 additions & 0 deletions include/xtensor-r/rtensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ namespace xt

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

constexpr static std::size_t rank = N;

rtensor();
rtensor(nested_initializer_list_t<T, N> t);

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ set(XTENSOR_R_TESTS
test_roptional.cpp
test_rtensor.cpp
test_rvectorize.cpp
test_sfinae.cpp
)

add_executable(test_xtensor_r ${XTENSOR_R_TESTS} ${XTENSOR_R_HEADERS})
Expand Down
53 changes: 53 additions & 0 deletions test/test_sfinae.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/***************************************************************************
* Copyright (c) Wolf Vollprecht, Johan Mabille and Sylvain Corlay *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#include <limits>

#include "gtest/gtest.h"
#include "xtensor-r/rtensor.hpp"
#include "xtensor-r/rarray.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xtensor.hpp"

namespace xt
{
template <class E, std::enable_if_t<!xt::has_fixed_rank_t<E>::value, int> = 0>
inline bool sfinae_has_fixed_rank(E&&)
{
return false;
}

template <class E, std::enable_if_t<xt::has_fixed_rank_t<E>::value, int> = 0>
inline bool sfinae_has_fixed_rank(E&&)
{
return true;
}

TEST(sfinae, fixed_rank)
{
xt::rarray<int> a = {{9, 9, 9}, {9, 9, 9}};
xt::rtensor<int, 1> b = {9, 9};
xt::rtensor<int, 2> c = {{9, 9}, {9, 9}};

EXPECT_TRUE(sfinae_has_fixed_rank(a) == false);
EXPECT_TRUE(sfinae_has_fixed_rank(b) == true);
EXPECT_TRUE(sfinae_has_fixed_rank(c) == true);
}

TEST(sfinae, get_rank)
{
xt::rtensor<double, 1> A = xt::zeros<double>({2});
xt::rtensor<double, 2> B = xt::zeros<double>({2, 2});
xt::rarray<double> C = xt::zeros<double>({2, 2});

EXPECT_TRUE(xt::get_rank<decltype(A)>::value == 1ul);
EXPECT_TRUE(xt::get_rank<decltype(B)>::value == 2ul);
EXPECT_TRUE(xt::get_rank<decltype(C)>::value == SIZE_MAX);
}
}