xtensor: Select rows with specific string column values

106 Views Asked by At

I am adapting a method to find rows with specific integer values (xtensor: Select rows with specific column values). The adapatation finds string values, rather than integer values, in the first column of a 2D xtensor.

I get a compile error when trying to use xt::equal() as shown below. The compile error is "binary '+': 'std::basic_string<char,std::char_traits,std::allocator>' does not define this operator or a conversion to a type acceptable to the predefined operator in file xtype_traits.hpp"

I don't understand why the compiler rejects this line due to a std::string issue when n1 is a simple list of integers: {1, 0, 0, 0, 0}

#include <vector>
#include <iostream>
#include <string>
#include <numeric>
#include <limits>
#undef max 
#undef min
#define NOMINMAX
#include <xtensor/xtensor.hpp>
#include <xtensor/xview.hpp>
#include <xtensor/xio.hpp>

xt::xtensor <std::string, 2> xtAudit =
{ {"Cell1", "A1", "B1"},
 {"Cell2", "A2", "B2"},
 {"Cell3", "A3", "B3"},
 {"Cell4", "A4", "B4"},
 {"Cell5", "A5", "B5"} };

std::string strCell = "Cell1"; 
auto test1 = xt::equal(xt::view(xtAudit, xt::all(), xt::keep(0)), strCell);
std::cout << test1 << std::endl;

auto n1 = xt::sum(test1, 1);
std::cout << n1 << std::endl;

auto idx1 = xt::flatten_indices(xt::argwhere(xt::equal(n1, 1))); // compile error here with xt::equal()
std::cout << idx1 << std::endl;

auto b1 = xt::view(xtAudit, xt::keep(idx1), xt::all());
std::cout << b1 << std::endl;

The outputs up to the compile error are (I got these by commenting out the lines that caused compile errors and those beyond):

{{true}, {false}, {false}, {false}, {false}}

{1, 0, 0, 0, 0}
1

There are 1 best solutions below

0
Tom de Geus On

To me it looks like there might be some bug. I don't see why test1 should have any memory of it being based on an equality operator between std::string. The bug seems to be related to the laziness of the operations. To work around, you can use

auto n1 = xt::eval(xt::sum(test1, 1));

I would propose that you file a bug-report with xtensor.