All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
knapsack_common.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_KNAPSACK_COMMON_HPP
16 #define PAAL_KNAPSACK_COMMON_HPP
17 
19 
20 namespace paal {
21 namespace detail {
22 
23 template <typename KnapsackData, typename Is_0_1_Tag,
24  typename RetrieveSolution = retrieve_solution_tag,
25  typename Value = typename KnapsackData::value,
26  typename Size = typename KnapsackData::size>
27 typename KnapsackData::return_type knapsack_check_integrality(KnapsackData knap_data, Is_0_1_Tag is_0_1_Tag,
28  RetrieveSolution retrieve_solutionTag =
29  RetrieveSolution{}) {
30 
31  return knapsack(std::move(knap_data), is_0_1_Tag,
32  detail::GetIntegralTag<Size, Value>{},
33  retrieve_solutionTag);
34 }
35 
36 // this overloads is for nonintegral SizeType and ValueType
37 // this case is invalid and allwas asserts!
38 template <typename KnapsackData,
39  typename IntegralTag, // always equals non_integral_value_and_size_tag
40  typename RetrieveSolution, typename Is_0_1_Tag,
41  typename = typename std::enable_if<std::is_same<
42  non_integral_value_and_size_tag, IntegralTag>::value>::type>
43 
44 typename KnapsackData::return_type knapsack(KnapsackData, Is_0_1_Tag is_0_1_Tag, IntegralTag,
45  RetrieveSolution retrieve_solution) {
46  // trick to avoid checking assert on template definition parse
47  static_assert(
48  std::is_same<IntegralTag, non_integral_value_and_size_tag>::value,
49  "At least one of the value or size must return integral value");
50 }
51 
56 template <typename KnapsackData, typename Is_0_1_Tag, typename RetrieveSolution>
57 typename KnapsackData::return_type knapsack(KnapsackData knap_data, Is_0_1_Tag is_0_1_Tag,
58  integral_value_and_size_tag, RetrieveSolution retrieve_solutionTag) {
59  if (get_value_bound(knap_data, is_0_1_Tag, upper_tag{}) >
60  knap_data.get_capacity()) {
61  return knapsack(std::move(knap_data), is_0_1_Tag, integral_size_tag{},
62  retrieve_solutionTag);
63  } else {
64  return knapsack(std::move(knap_data), is_0_1_Tag, integral_value_tag{},
65  retrieve_solutionTag);
66  }
67 }
68 
69 }
70 }
71 #endif // PAAL_KNAPSACK_COMMON_HPP