Skip to content

Commit

Permalink
Adding a cast operator to allow frame<T> -> series<T>
Browse files Browse the repository at this point in the history
Requires a check to make sure that for frame<Ts...> sizeof...(Ts) is 1
  • Loading branch information
tedmiddleton committed Apr 30, 2023
1 parent d8aefda commit 8dc1fea
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions mainframe/frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,20 @@ class frame
bool
operator==(const frame<Ts...>& other) const;

/// Cast to series<> if the frame has only one column
///
/// This is a convenience operator for the common case of a single-column
/// frame. It allows you to write
///
/// void takes_a_series(const series<double>& s);
///
/// frame_row<year_month, int, double> fr1;
/// fr1.push_back(2022_y/11, 1, 3.14);
/// takes_a_series(fr1[_2]);
///
template<size_t NumColumns = sizeof...(Ts)>
operator std::enable_if_t<NumColumns == 1, series<typename pack_element<0, Ts...>::type>>() const;

/// Dereference the first row of a single-column frame
///
/// This is a convenience operator for the common case of a single-column
Expand Down
7 changes: 7 additions & 0 deletions mainframe/impl/frame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ frame<Ts...>::operator==(const frame<Ts...>& other) const
return eq_impl<0>(other);
}

template<typename... Ts>
template<size_t NumColumns>
frame<Ts...>::operator std::enable_if_t<NumColumns == 1, series<typename pack_element<0, Ts...>::type>>() const
{
return std::get<0>(m_columns);
}

template<typename... Ts>
template<size_t Num>
typename std::enable_if<Num == 1, typename detail::pack_element<0, Ts...>::type>::type
Expand Down
25 changes: 25 additions & 0 deletions tests/mainframe_test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3525,6 +3525,31 @@ TEST_CASE("operator*", "[frame]")
}
}

TEST_CASE("operator()", "[frame]")
{
SECTION("legitimate")
{
auto func = [](const series<year_month_day>& s) {
REQUIRE(s.size() == 5);
REQUIRE(s.name() == "date");
REQUIRE(s[0] == 2022_y / January / 1);
REQUIRE(s[1] == 2022_y / January / 2);
REQUIRE(s[2] == 2022_y / January / 3);
REQUIRE(s[3] == 2022_y / January / 4);
REQUIRE(s[4] == 2022_y / January / 5);
};

frame<double, year_month_day, double> f1;
f1.set_column_names("real", "date", "imag");
f1.push_back( 8.9, 2022_y / January / 1, 10.2);
f1.push_back(10.2, 2022_y / January / 2, 10.2);
f1.push_back(11.1, 2022_y / January / 3, 7.7);
f1.push_back(12.2, 2022_y / January / 4, 10.2);
f1.push_back(13.3, 2022_y / January / 5, 10.2);

func(f1[_1]);
}
}

//template<typename Func, typename Arg>
//struct fnobj;
Expand Down

0 comments on commit 8dc1fea

Please sign in to comment.