Skip to content

Commit 961722d

Browse files
authored
Enhance xmasked_view support for output streaming (#2899)
# Checklist - [x] The title and commit message(s) are descriptive. - [x] Small commits made to fix your PR have been squashed to avoid history pollution. - [x] Tests have been added for new features or bug fixes. - [x] API of new functions and classes are documented. # Description Fix `xt::masked_view` pretty-printing when the underlying data is non-optional. Related to: #2495 The generic non-fundamental printer was streaming masked proxy values directly, which could lose the correct masked/value state during formatting. This change keeps the existing generic printer path but handles `xtl::xmasked_value` with a compile-time branch and streams a stabilized value via unary `+`. ## Changes - include `xtl/xmasked_value_meta.hpp` in `xio.hpp` - update the generic non-fundamental printer to use `if constexpr` - stream masked values with `buf << +val` - add a regression test for `masked_view` stream output on plain `xarray` data --------- Co-authored-by: Alexis Placet <2400067+Alex-PLACET@users.noreply.github.com>
1 parent 8adbd1c commit 961722d

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

include/xtensor/io/xio.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "../core/xexpression.hpp"
2121
#include "../core/xmath.hpp"
2222
#include "../views/xstrided_view.hpp"
23+
#include "xtl/xmasked_value_meta.hpp"
2324

2425
namespace xt
2526
{
@@ -646,7 +647,14 @@ namespace xt
646647
void update(const_reference val)
647648
{
648649
std::stringstream buf;
649-
buf << val;
650+
if constexpr (xtl::is_xmasked_value<value_type>::value)
651+
{
652+
buf << +val;
653+
}
654+
else
655+
{
656+
buf << val;
657+
}
650658
std::string s = buf.str();
651659
if (int(s.size()) > m_width)
652660
{

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)