Skip to content

Commit 11abcd9

Browse files
author
Aperjump
committed
Change from_tuples interfaces
1 parent fb4e5b9 commit 11abcd9

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

include/boost/numeric/ublas/data_frame.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <string>
88
#include <unordered_map>
99
#include <vector>
10+
#include <initializer_list>
1011
#include <tuple>
1112
#include <typeinfo>
1213
namespace boost { namespace numeric { namespace ublas {
@@ -24,6 +25,10 @@ class data_frame {
2425
bool add_column(std::string col_name, std::vector<T> tmp_vec);
2526
template<class... Args>
2627
void from_tuples(const std::vector<std::tuple<Args...>>& t, const std::vector<std::string>& names);
28+
template<class... Args>
29+
void from_tuples(std::initializer_list<std::tuple<Args...>> t, const std::vector<std::string>& names);
30+
template<template<class...> class TypeLists, class... Args>
31+
void from_tuples(const std::vector<std::tuple<Args...>>& t, const std::vector<std::string>& names, TypeLists<Args...>);
2732
template<typename T>
2833
T& get(const std::string& col_name, size_t pos);
2934
template<typename T>
@@ -265,6 +270,11 @@ bool data_frame::init_column(const std::string& col_name, int size) {
265270
return true;
266271
}
267272
template<class... Args>
273+
void data_frame::from_tuples(std::initializer_list<std::tuple<Args...>> t, const std::vector<std::string>& names) {
274+
std::vector<std::tuple<Args...>> vec(t);
275+
from_tuples(vec, names);
276+
}
277+
template<class... Args>
268278
void data_frame::from_tuples(const std::vector<std::tuple<Args...>>& t, const std::vector<std::string>& names) {
269279
if (sizeof...(Args) != names.size()) return;
270280
cur_rows = t.size();
@@ -273,6 +283,10 @@ void data_frame::from_tuples(const std::vector<std::tuple<Args...>>& t, const st
273283
from_tuple(t[i], names, i);
274284
}
275285
}
286+
template<template<class...> class TypeLists, class... Args>
287+
void data_frame::from_tuples(const std::vector<std::tuple<Args...>>& t, const std::vector<std::string>& names, TypeLists<Args...>) {
288+
from_tuples<Args...>(t, names);
289+
}
276290
template<typename T>
277291
std::vector<int> data_frame::order(const std::string& col_name) {
278292
if (!col_names_map.count(col_name)) return {};

test/data_frame_test.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,39 @@ TEST(Data_frame, initialize_from_tuple) {
2626
df2.from_tuples(std::vector{std::make_tuple(1, 3.3, "hello"s),
2727
std::make_tuple(2, 2.2, "world"s),
2828
std::make_tuple(3, 1.1, "bili"s)},
29-
{"int_vec", "double_vec", "str_vec"});
29+
{"int_vec", "double_vec", "str_vec"});
3030
EXPECT_EQ(df2.get_cur_rows(), 3);
3131
EXPECT_EQ(df2.get_cur_cols(), 3);
32+
data_frame df3;
33+
df3.from_tuples<int, double, std::string>({{1, 3.3, "hello"s},
34+
{2, 2.2, "world"s},
35+
{3, 1.1, "bili"s}},
36+
{"int_vec", "double_vec", "str_vec"});
37+
EXPECT_EQ(df3.get_cur_rows(), 3);
38+
EXPECT_EQ(df3.get_cur_cols(), 3);
39+
data_frame df4;
40+
/**
41+
* df4.from_tuples({{0, 3.4, "hello"s}
42+
* {2, 2.2, "world"s},
43+
* {3, 1.1, "bili"s}},
44+
* {"int_vec", "double_vec", "str_vec"});
45+
*/
46+
std::tuple t{0, 3.4, "hello"s};
47+
df4.from_tuples({t,
48+
{2, 2.2, "world"s},
49+
{3, 1.1, "bilibili"s}},
50+
{"int_vec", "double_vec", "str_vec"});
51+
EXPECT_EQ(df4.get_cur_rows(), 3);
52+
EXPECT_EQ(df4.get_cur_cols(), 3);
53+
data_frame df5;
54+
using type_collection = type_list<int, double, std::string>::types;
55+
df5.from_tuples({{1, 3.3, "hello"s},
56+
{2, 2.2, "world"s},
57+
{3, 1.1, "bili"s}},
58+
{"int_vec", "double_vec", "str_vec"},
59+
type_collection{});
60+
EXPECT_EQ(df5.get_cur_rows(), 3);
61+
EXPECT_EQ(df5.get_cur_cols(), 3);
3262
}
3363
TEST(Data_frame, sort_with_single_column) {
3464
data_frame df2;
@@ -121,4 +151,4 @@ TEST(Data_frame, apply_with_index_test) {
121151
return t * 2;
122152
}, type_collection2{});
123153
df4.print_with_index({0, 1, 2},type_collection2{} );
124-
}
154+
}

0 commit comments

Comments
 (0)