Skip to content

Commit 5590cc2

Browse files
committed
Enhance xmasked_view support for output streaming
1 parent 92c873e commit 5590cc2

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

include/xtensor/io/xio.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <sstream>
1818
#include <string>
1919

20+
#include "xtl/xmasked_value_meta.hpp"
21+
2022
#include "../core/xexpression.hpp"
2123
#include "../core/xmath.hpp"
2224
#include "../views/xstrided_view.hpp"
@@ -615,7 +617,8 @@ namespace xt
615617
struct printer<
616618
T,
617619
std::enable_if_t<
618-
!xtl::is_fundamental<typename T::value_type>::value && !xtl::is_complex<typename T::value_type>::value>>
620+
!xtl::is_fundamental<typename T::value_type>::value
621+
&& !xtl::is_complex<typename T::value_type>::value>>
619622
{
620623
using const_reference = typename T::const_reference;
621624
using value_type = std::decay_t<typename T::value_type>;
@@ -646,7 +649,14 @@ namespace xt
646649
void update(const_reference val)
647650
{
648651
std::stringstream buf;
649-
buf << val;
652+
if constexpr (xtl::is_xmasked_value<value_type>::value)
653+
{
654+
buf << +val;
655+
}
656+
else
657+
{
658+
buf << val;
659+
}
650660
std::string s = buf.str();
651661
if (int(s.size()) > m_width)
652662
{

test/test_xmasked_view.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* The full license is in the file LICENSE, distributed with this software. *
88
****************************************************************************/
99

10+
#include <sstream>
11+
1012
#include "xtensor/io/xio.hpp"
1113
#include "xtensor/optional/xoptional_assembly.hpp"
1214
#include "xtensor/views/xmasked_view.hpp"
@@ -210,6 +212,21 @@ namespace xt
210212
EXPECT_EQ(data, expected2);
211213
}
212214

215+
TEST(xmasked_view, non_optional_data_stream)
216+
{
217+
const xarray<double> data = {{1., 5., 3.}, {4., 5., 6.}};
218+
const xarray<bool> mask = {{true, false, false}, {false, true, false}};
219+
220+
const auto masked_data = masked_view(data, mask);
221+
222+
std::stringstream out;
223+
out << masked_data;
224+
225+
const std::string expected = "{{ 1, masked, masked},\n"
226+
" {masked, 5, masked}}";
227+
EXPECT_EQ(out.str(), expected);
228+
}
229+
213230
TEST(xmasked_view, assign)
214231
{
215232
xarray<double> data = {{1., -2., 3.}, {4., 5., -6.}, {7., 8., -9.}};

0 commit comments

Comments
 (0)