17 #ifndef PAAL_KNAPSACK_GREEDY_HPP
18 #define PAAL_KNAPSACK_GREEDY_HPP
23 #include <boost/iterator/counting_iterator.hpp>
24 #include <boost/range/algorithm/remove_if.hpp>
33 template <
typename MaxValueType,
typename SizeType>
36 : m_max_value(maxValue) {}
38 template <
typename Iterator,
typename Comparator>
39 Iterator operator()(Iterator begin, Iterator end, Comparator compare) {
42 for (
auto iter = end - 1; iter != begin; --iter) {
43 if (*iter && compareOpt(m_max_value, *iter)) {
52 MaxValueType m_max_value;
58 template <
typename ValueType>
60 template <
typename Iterator,
typename Comparator>
61 Iterator operator()(Iterator begin, Iterator end, Comparator compare) {
66 template <
typename KnapsackData,
68 typename Value =
typename KnapsackData::value,
69 typename Size =
typename KnapsackData::size>
70 typename KnapsackData::return_type knapsack_general_two_app(
73 using ObjectRef =
typename KnapsackData::object_ref;
75 static_assert(std::is_arithmetic<Value>::value &&
76 std::is_arithmetic<Size>::value,
77 "Size type and Value type must be arithmetic types");
78 auto capacity = knapsack_data.get_capacity();
80 auto bad_size = [=](ObjectRef o){
return knapsack_data.get_size(o) > capacity;};
82 auto objects = boost::remove_if<boost::return_begin_found>(knapsack_data.get_objects(), bad_size);
84 if (boost::empty(objects)) {
85 return std::pair<Value, Size>();
89 auto greedyFill = get_greedy_fill(
92 knapsack_data.get_size(),
93 knapsack_data.get_value(),
94 knapsack_data.get_output_iter()), is_0_1_Tag);
101 if (*largest > std::get<0>(greedyFill)) {
102 knapsack_data.out(*largest.base());
103 return std::make_pair(*largest, knapsack_data.get_size(*largest.base()));
105 greedy_to_output(std::get<2>(greedyFill), knapsack_data.get_output_iter(), is_0_1_Tag);
106 return std::make_pair(std::get<0>(greedyFill), std::get<1>(greedyFill));
110 template <
typename Range>
112 using ref =
typename boost::range_reference<Range>::type;
113 static const bool value = std::is_const<ref>::value ||
114 !std::is_reference<ref>::value;
118 #endif // PAAL_KNAPSACK_GREEDY_HPP
auto max_element_functor(Range &&range, Functor f)
combination of boost::max_element and boost::adaptors::transformed
less_pointees_t< Comparator > make_less_pointees_t(Comparator compare)
make function for less_pointees_t