All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
knapsack_utils.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_UTILS_HPP
16 #define PAAL_KNAPSACK_UTILS_HPP
17 
20 #include "paal/utils/functors.hpp"
21 
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>
28 
29 namespace paal {
30 
37 template <typename Value, typename Size> struct density {
38 
40  density(Value value, Size size) : m_value(value), m_size(size) {}
41 
43  template <typename ObjectRef> double operator()(ObjectRef obj) const {
44  return double(m_value(obj)) / double(m_size(obj));
45  }
46 
47  private:
48  Value m_value;
49  Size m_size;
50 };
51 
62 template <typename Value, typename Size>
63 density<Value, Size> make_density(Value value, Size size) {
64  return density<Value, Size>(value, size);
65 }
66 
67 
68 namespace detail {
69 template <typename Functor, typename Range>
70 using FunctorOnRangePValue =
71  puretype(std::declval<Functor>()(*std::begin(std::declval<Range>())));
72 
73 // definition of basic types
74 template <typename Objects, typename ObjectSizeFunctor,
75  typename ObjectValueFunctor>
76 struct knapsack_base {
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;
81  // add_const is unfortunately needed by function_output_iterator
82  // TODO it supposed to be changed in boost...
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;
89 };
90 
91 
92 // various tags
97 
100 
101 struct zero_one_tag {};
102 struct unbounded_tag {};
103 
106 
107 template <typename GetSize, typename GetValue, typename Objects,
108  typename OutputIterator>
110  public:
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;
118 
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) {}
123 
124  size get_size(object_ref obj) const { return m_get_size(obj); }
125 
126  value get_value(object_ref obj) const { return m_get_value(obj); }
127 
128  GetSize get_size() const { return m_get_size; }
129 
130  GetValue get_value() const { return m_get_value; }
131 
132  objects get_objects() { return m_objects; }
133 
134  void out(object_ref obj) {
135  *m_out = obj;
136  ++m_out;
137  }
138 
139  size get_capacity() const { return m_capacity; }
140 
141  OutputIterator & get_output_iter() const { return m_out; }
142 
143  density<GetValue, GetSize> get_density() {
144  return make_density(m_get_value, m_get_size);
145  }
146 
147 
148  private:
149  Objects m_objects;
150  size m_capacity;
151  GetSize m_get_size;
152  GetValue m_get_value;
153  OutputIterator & m_out;
154 };
155 
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
164  };
165 }
166 
167 
168 }
169 
170 }
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()