15 #ifndef PAAL_KNAPSACK_UTILS_HPP
16 #define PAAL_KNAPSACK_UTILS_HPP
22 #include <boost/iterator/filter_iterator.hpp>
23 #include <boost/optional.hpp>
24 #include <boost/range/adaptor/filtered.hpp>
25 #include <boost/range/algorithm/max_element.hpp>
26 #include <boost/range/algorithm/min_element.hpp>
27 #include <boost/range/numeric.hpp>
37 template <
typename Value,
typename Size>
struct density {
40 density(Value value, Size size) : m_value(value), m_size(size) {}
43 template <
typename ObjectRef>
double operator()(ObjectRef obj)
const {
44 return double(m_value(obj)) / double(m_size(obj));
62 template <
typename Value,
typename Size>
69 template <
typename Functor,
typename Range>
70 using FunctorOnRangePValue =
71 puretype(std::declval<Functor>()(*std::begin(std::declval<Range>())));
74 template <
typename Objects,
typename ObjectSizeFunctor,
75 typename ObjectValueFunctor>
77 typedef detail::FunctorOnRangePValue<ObjectSizeFunctor, Objects> SizeType;
78 typedef detail::FunctorOnRangePValue<ObjectValueFunctor, Objects> ValueType;
79 typedef puretype(*std::begin(std::declval<Objects>())) ObjectType;
80 using PureObjRef =
typename boost::range_reference<Objects>::type;
83 typedef typename std::conditional<
84 std::is_reference<PureObjRef>::value,
85 typename std::add_lvalue_reference<
typename std::add_const<
86 typename std::remove_reference<PureObjRef>::type>::type>::type,
87 typename std::add_const<PureObjRef>::type>::type ObjectRef;
88 typedef std::pair<ValueType, SizeType> return_type;
107 template <
typename GetSize,
typename GetValue,
typename Objects,
108 typename OutputIterator>
112 using size =
typename traits::SizeType;
113 using value =
typename traits::ValueType;
114 using objects = Objects;
115 using object_ref =
typename traits::ObjectRef;
116 using object_iter =
typename boost::range_iterator<Objects>::type;
117 using return_type =
typename traits::return_type;
119 knapsack_data(Objects objects, size capacity, GetSize get_size,
120 GetValue get_value, OutputIterator & out)
121 : m_objects(objects), m_capacity(capacity), m_get_size(get_size),
122 m_get_value(get_value), m_out(out) {}
124 size get_size(object_ref obj)
const {
return m_get_size(obj); }
126 value get_value(object_ref obj)
const {
return m_get_value(obj); }
128 GetSize get_size()
const {
return m_get_size; }
130 GetValue get_value()
const {
return m_get_value; }
132 objects get_objects() {
return m_objects; }
134 void out(object_ref obj) {
139 size get_capacity()
const {
return m_capacity; }
141 OutputIterator & get_output_iter()
const {
return m_out; }
152 GetValue m_get_value;
153 OutputIterator & m_out;
156 template <
typename GetSize,
typename GetValue,
typename Objects,
157 typename OutputIterator,
158 typename Size = FunctorOnRangePValue<GetSize, Objects>>
160 make_knapsack_data(Objects &&objects, Size capacity, GetSize get_size,
161 GetValue get_value, OutputIterator & out) {
163 std::forward<Objects>(objects), capacity, get_size, get_value, out
171 #endif // PAAL_KNAPSACK_UTILS_HPP
density< Value, Size > make_density(Value value, Size size)
make for density
density(Value value, Size size)
constructor
#define puretype(t)
for given expression returns its type with removed const and reference
This file contains set of simple useful functors or functor adapters.
density functor, for given value and size
double operator()(ObjectRef obj) const
operator()