All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
knapsack_unbounded_two_app.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_UNBOUNDED_TWO_APP_HPP
16 #define PAAL_KNAPSACK_UNBOUNDED_TWO_APP_HPP
17 
21 
22 #include <boost/iterator/counting_iterator.hpp>
23 #include <boost/iterator/filter_iterator.hpp>
24 
25 #include <type_traits>
26 #include <utility>
27 
28 namespace paal {
29 
30 namespace detail {
31 template <typename KnapsackData,
32  typename ObjectIter = typename KnapsackData::object_iter,
33  typename Size = typename KnapsackData::size,
34  typename Value = typename KnapsackData::value>
35 std::tuple<Value, Size,
36  std::pair<ObjectIter, unsigned>>
37 get_greedy_fill(KnapsackData knap_data, unbounded_tag) {
38 
39  auto density = knap_data.get_density();
40  auto most_dense_iter = max_element_functor(
41  knap_data.get_objects(), density).base();
42 
43  unsigned nr = knap_data.get_capacity() / knap_data.get_size(*most_dense_iter);
44  Value value_sum = Value(nr) * knap_data.get_value(*most_dense_iter);
45  Size size_sum = Size (nr) * knap_data.get_size (*most_dense_iter);
46  return std::make_tuple(value_sum, size_sum,
47  std::make_pair(most_dense_iter, nr));
48 }
49 
50 template <typename ObjectsIterAndNr, typename OutputIter>
51 void greedy_to_output(ObjectsIterAndNr most_dense_iter_and_nr, OutputIter & out,
52  unbounded_tag) {
53  auto nr = most_dense_iter_and_nr.second;
54  auto most_dense_iter = most_dense_iter_and_nr.first;
55  for (unsigned i = 0; i < nr; ++i) {
56  *out = *most_dense_iter;
57  ++out;
58  }
59 }
60 
61 }
62 
64 template <typename OutputIterator, typename Objects,
65  typename ObjectSizeFunctor, typename ObjectValueFunctor,
66  //this enable if assures that range can be permuted
67  typename std::enable_if<!detail::is_range_const<Objects>::value>::type * = nullptr>
68 
69 typename detail::knapsack_base<Objects, ObjectSizeFunctor,
70  ObjectValueFunctor>::return_type
72  Objects && objects,
73  typename detail::FunctorOnRangePValue<ObjectSizeFunctor, Objects> capacity,
74  OutputIterator out, ObjectValueFunctor value, ObjectSizeFunctor size) {
75  return detail::knapsack_general_two_app(
76  detail::make_knapsack_data(std::forward<Objects>(objects), capacity, size, value, out),
78 }
79 }
80 #endif // PAAL_KNAPSACK_UNBOUNDED_TWO_APP_HPP
auto max_element_functor(Range &&range, Functor f)
combination of boost::max_element and boost::adaptors::transformed
detail::knapsack_base< Objects, ObjectSizeFunctor, ObjectValueFunctor >::return_type knapsack_unbounded_two_app(Objects &&objects, typename detail::FunctorOnRangePValue< ObjectSizeFunctor, Objects > capacity, OutputIterator out, ObjectValueFunctor value, ObjectSizeFunctor size)
detail