Skip to content

Commit b78688a

Browse files
committed
Use integer fill for integer storages (avoid a conversion to double)
1 parent 8d43c39 commit b78688a

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

include/bh_python/fill.hpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ using arg_t = variant::variant<c_array_t<double>,
101101
int,
102102
c_array_t<std::string>,
103103
std::string>;
104-
105-
using weight_t = variant::variant<variant::monostate, double, c_array_t<double>>;
104+
template <class T>
105+
using weight_t = variant::variant<variant::monostate, T, c_array_t<T>>;
106106

107107
inline auto get_vargs(const vector_axis_variant& axes, const py::args& args) {
108108
if(args.size() != axes.size())
@@ -131,25 +131,26 @@ inline auto get_vargs(const vector_axis_variant& axes, const py::args& args) {
131131
return vargs;
132132
}
133133

134-
inline auto get_weight(py::kwargs& kwargs) {
134+
template <class T>
135+
auto get_weight(py::kwargs& kwargs) {
135136
// default constructed as monostate to indicate absence of weight
136-
variant::variant<variant::monostate, double, c_array_t<double>> weight;
137+
weight_t<T> weight;
137138
auto w = optional_arg(kwargs, "weight");
138139
if(!w.is_none()) {
139-
if(is_value<double>(w))
140-
weight = py::cast<double>(w);
140+
if(is_value<T>(w))
141+
weight = py::cast<T>(w);
141142
else
142-
weight = py::cast<c_array_t<double>>(w);
143+
weight = py::cast<c_array_t<T>>(w);
143144
}
144145
return weight;
145146
}
146147

147148
// for accumulators that accept a weight
148-
template <class Histogram, class VArgs>
149+
template <class weight_type, class Histogram, class VArgs>
149150
void fill_impl(bh::detail::accumulator_traits_holder<true>,
150151
Histogram& h,
151152
const VArgs& vargs,
152-
const weight_t& weight,
153+
const weight_t<weight_type>& weight,
153154
py::kwargs& kwargs) {
154155
none_only_arg(kwargs, "sample");
155156
finalize_args(kwargs);
@@ -163,11 +164,11 @@ void fill_impl(bh::detail::accumulator_traits_holder<true>,
163164
}
164165

165166
// for accumulators that accept a weight and a double
166-
template <class Histogram, class VArgs>
167+
template <class weight_type, class Histogram, class VArgs>
167168
void fill_impl(bh::detail::accumulator_traits_holder<true, const double&>,
168169
Histogram& h,
169170
const VArgs& vargs,
170-
const weight_t& weight,
171+
const weight_t<weight_type>& weight,
171172
py::kwargs& kwargs) {
172173
auto s = required_arg(kwargs, "sample");
173174
finalize_args(kwargs);
@@ -192,10 +193,14 @@ void fill_impl(bh::detail::accumulator_traits_holder<true, const double&>,
192193
template <class Histogram>
193194
Histogram& fill(Histogram& self, py::args args, py::kwargs kwargs) {
194195
using value_type = typename Histogram::value_type;
195-
detail::fill_impl(bh::detail::accumulator_traits<value_type>{},
196-
self,
197-
detail::get_vargs(bh::unsafe_access::axes(self), args),
198-
detail::get_weight(kwargs),
199-
kwargs);
196+
using weight_type
197+
= boost::mp11::mp_if_c<std::is_integral<value_type>::value, int, double>;
198+
199+
detail::fill_impl<weight_type>(
200+
bh::detail::accumulator_traits<value_type>{},
201+
self,
202+
detail::get_vargs(bh::unsafe_access::axes(self), args),
203+
detail::get_weight<weight_type>(kwargs),
204+
kwargs);
200205
return self;
201206
}

0 commit comments

Comments
 (0)