Permalink
Browse files

C++17: DpBottomUpBase:: Simplify with constexpr if.

For the case that the subproblems are just a simple vector.
  • Loading branch information...
1 parent 221df60 commit 722895658324ce2ee0ef008af0a9b31b2f8903da @murraycu committed Jul 7, 2016
Showing with 25 additions and 51 deletions.
  1. +25 −27 murraycdp/dp_bottom_up_base.h
  2. +0 −24 murraycdp/utils/vector_of_vectors.h
@@ -102,23 +102,9 @@ class DpBottomUpBase : public DpBase<T_subproblem, T_value_types...> {
#if defined(MURRAYC_DP_DEBUG_OUTPUT)
std::cout << "i=" << std::setw(2) << i << ": ";
#endif
- if (depth == 1) {
- // Call it via a tuple,
- // to avoid a compilation error when depth is not 1:
- type_values values;
- std::get<0>(values) = i;
-
- const auto subproblem = std::experimental::apply(
- [this, level](T_value_types... the_values) {
- return this->calc_subproblem(level, the_values...);
- },
- values);
-
- std::experimental::apply(
- [this, subproblem](T_value_types... the_values) {
- return this->set_subproblem(subproblem, the_values...);
- },
- values);
+ if constexpr(depth == 1) {
+ const auto subproblem = this->calc_subproblem(level, i);
+ set_subproblem(subproblem, i);
} else {
type_subproblems& subproblems_i =
subproblems_.get_at_offset_from_start(i);
@@ -202,11 +188,17 @@ class DpBottomUpBase : public DpBase<T_subproblem, T_value_types...> {
const type_subproblems& subproblems_i =
subproblems_.get_at_offset_from_start(i);
- const auto values_without_i = tupleutils::tuple_cdr(values_tuple);
- constexpr auto tuple_size =
- std::tuple_size<decltype(values_without_i)>::value;
- subproblem = call_get_at_sub_vectors_with_tuple(
- subproblems_i, values_without_i, std::make_index_sequence<tuple_size>());
+ constexpr auto depth =
+ std::tuple_size<type_values>::value; // sizeof...(T_value_types);
+ if constexpr(depth == 1) {
+ subproblem = subproblems_i;
+ } else {
+ const auto values_without_i = tupleutils::tuple_cdr(values_tuple);
+ constexpr auto tuple_size =
+ std::tuple_size<decltype(values_without_i)>::value;
+ subproblem = call_get_at_sub_vectors_with_tuple(
+ subproblems_i, values_without_i, std::make_index_sequence<tuple_size>());
+ }
// std::cout << "get_cached_subproblem(): returning cache for i=" << i << ",
// j=" << j << std::endl;
@@ -224,11 +216,17 @@ class DpBottomUpBase : public DpBase<T_subproblem, T_value_types...> {
const auto i = std::get<0>(values_tuple);
type_subproblems& subproblems_i = subproblems_.get_at_offset_from_start(i);
- const auto values_without_i = tupleutils::tuple_cdr(values_tuple);
- constexpr auto tuple_size =
- std::tuple_size<decltype(values_without_i)>::value;
- call_get_at_sub_vectors_with_tuple(subproblems_i, values_without_i,
- std::make_index_sequence<tuple_size>()) = subproblem;
+ constexpr auto depth =
+ std::tuple_size<type_values>::value; // sizeof...(T_value_types);
+ if constexpr(depth == 1) {
+ subproblems_i = subproblem;
+ } else {
+ const auto values_without_i = tupleutils::tuple_cdr(values_tuple);
+ constexpr auto tuple_size =
+ std::tuple_size<decltype(values_without_i)>::value;
+ call_get_at_sub_vectors_with_tuple(subproblems_i, values_without_i,
+ std::make_index_sequence<tuple_size>()) = subproblem;
+ }
}
template <typename... T_sizes>
@@ -122,30 +122,6 @@ resize_vector_of_vectors(std::vector<std::vector<T>>& vector,
}
/**
- * This is useless, but it lets us call get_at_vector_of_vectors() on an
- * instance of vector_of_vectors<Something, 0>::type,
- * so we can write generic code.
- */
-template <class T_element>
-decltype(auto)
-get_at_vector_of_vectors(T_element& element) {
- return element;
-}
-
-template <class T_element>
-decltype(auto)
-get_at_vector_of_vectors(const T_element& element) {
- return element;
-}
-
-// TODO: Remove the const& and non-const& overloads in favour of this && overload?
-template <class T_element>
-decltype(auto)
-get_at_vector_of_vectors(T_element&& element) {
- return std::forward<T_element>(element);
-}
-
-/**
* Get an element from a vector.
*
* @tparam T_element the value_type of the innermost vector.

0 comments on commit 7228956

Please sign in to comment.