Skip to content

Commit 49cc25c

Browse files
committed
Add support of 1D expression in xcsv
1 parent 3f986cf commit 49cc25c

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

include/xtensor/io/xcsv.hpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#ifndef XTENSOR_CSV_HPP
1111
#define XTENSOR_CSV_HPP
1212

13-
#include <exception>
1413
#include <istream>
1514
#include <iterator>
1615
#include <sstream>
@@ -211,30 +210,40 @@ namespace xt
211210
{
212211
using size_type = typename E::size_type;
213212
const E& ex = e.derived_cast();
214-
if (ex.dimension() != 2)
213+
if (ex.dimension() == 1)
215214
{
216-
XTENSOR_THROW(std::runtime_error, "Only 2-D expressions can be serialized to CSV");
217-
}
218-
size_type nbrows = ex.shape()[0], nbcols = ex.shape()[1];
219-
auto st = ex.stepper_begin(ex.shape());
220-
for (size_type r = 0; r != nbrows; ++r)
221-
{
222-
for (size_type c = 0; c != nbcols; ++c)
215+
const size_type n = ex.shape()[0];
216+
for (size_type i = 0; i != n; ++i)
223217
{
224-
stream << *st;
225-
if (c != nbcols - 1)
218+
stream << ex(i);
219+
if (i != n - 1)
226220
{
227-
st.step(1);
228221
stream << ',';
229222
}
230-
else
223+
}
224+
stream << std::endl;
225+
}
226+
else if (ex.dimension() == 2)
227+
{
228+
const size_type nbrows = ex.shape()[0];
229+
const size_type nbcols = ex.shape()[1];
230+
for (size_type r = 0; r != nbrows; ++r)
231+
{
232+
for (size_type c = 0; c != nbcols; ++c)
231233
{
232-
st.reset(1);
233-
st.step(0);
234-
stream << std::endl;
234+
stream << ex(r, c);
235+
if (c != nbcols - 1)
236+
{
237+
stream << ',';
238+
}
235239
}
240+
stream << std::endl;
236241
}
237242
}
243+
else
244+
{
245+
XTENSOR_THROW(std::runtime_error, "Only 1-D and 2-D expressions can be serialized to CSV");
246+
}
238247
}
239248

240249
struct xcsv_config

test/test_xcsv.cpp

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

10-
#include <iostream>
1110
#include <sstream>
1211

13-
#include "xtensor/core/xmath.hpp"
1412
#include "xtensor/io/xcsv.hpp"
1513
#include "xtensor/io/xio.hpp"
1614

1715
#include "test_common_macros.hpp"
1816

1917
namespace xt
2018
{
19+
TEST(xcsv, load_1D)
20+
{
21+
const std::string source = "1, 2, 3, 4";
22+
23+
std::stringstream source_stream(source);
24+
25+
const xtensor<int, 2> res = load_csv<int>(source_stream);
26+
27+
const xtensor<int, 2> exp{{1, 2, 3, 4}};
28+
29+
ASSERT_TRUE(all(equal(res, exp)));
30+
}
31+
2132
TEST(xcsv, load_double)
2233
{
2334
std::string source = "1.0, 2.0, 3.0, 4.0\n"
@@ -49,6 +60,16 @@ namespace xt
4960
ASSERT_TRUE(all(equal(res, exp)));
5061
}
5162

63+
TEST(xcsv, dump_1D)
64+
{
65+
xtensor<double, 1> data{{1.0, 2.0, 3.0, 4.0}};
66+
67+
std::stringstream res;
68+
69+
dump_csv(res, data);
70+
ASSERT_EQ("1,2,3,4\n", res.str());
71+
}
72+
5273
TEST(xcsv, dump_double)
5374
{
5475
xtensor<double, 2> data{{1.0, 2.0, 3.0, 4.0}, {10.0, 12.0, 15.0, 18.0}};

0 commit comments

Comments
 (0)