1#ifndef TATAMI_STATS__UTILS_HPP
2#define TATAMI_STATS__UTILS_HPP
27template<
typename Group_>
30 return static_cast<size_t>(*std::max_element(group, group + n)) + 1;
48template<
typename Group_,
typename Size_>
51 std::vector<Size_> group_sizes(ngroups);
52 for (Size_ r = 0; r < n; ++r) {
53 ++(group_sizes[group[r]]);
77template<
typename Output_>
88 template<
typename Index_>
89 LocalOutputBuffer(
size_t thread, Index_ start, Index_ length, Output_* output, Output_ fill) : my_output(output + start), use_local(thread > 0), my_buffer(use_local ? length : 0, fill) {
92 std::fill_n(my_output, length, fill);
105 template<
typename Index_>
119 return (use_local ? my_buffer.data() : my_output);
128 return (use_local ? my_buffer.data() : my_output);
137 std::copy(my_buffer.begin(), my_buffer.end(), my_output);
142 Output_* my_output = NULL;
143 bool use_local =
false;
144 std::vector<Output_> my_buffer;
158template<
typename Output_,
class GetOutput_>
170 template<
typename Index_>
171 LocalOutputBuffers(
size_t thread,
size_t number, Index_ start, Index_ length, GetOutput_ outfun, Output_ fill) :
174 my_use_local(thread > 0),
175 my_getter(std::move(outfun))
178 for (
size_t i = 0; i < my_number; ++i) {
180 std::fill_n(my_getter(i) + my_start, length, fill);
183 my_buffers.reserve(my_number);
184 for (
size_t i = 0; i < my_number; ++i) {
185 my_buffers.emplace_back(length, fill);
200 template<
typename Index_>
224 return (my_use_local ? my_buffers[i].
data() : my_getter(i) + my_start);
233 const Output_*
data(
size_t i)
const {
234 return (my_use_local ? my_buffers[i].
data() : my_getter(i) + my_start);
243 for (
size_t i = 0; i < my_number; ++i) {
244 const auto& current = my_buffers[i];
245 std::copy(current.begin(), current.end(), my_getter(i) + my_start);
251 size_t my_number = 0;
253 bool my_use_local =
true;
254 std::vector<std::vector<Output_> > my_buffers;
255 GetOutput_ my_getter;
263template<
typename Value_,
class If_,
class Else_>
264void nanable_ifelse(
bool skip_nan, If_ iffun, Else_ elsefun) {
265 if constexpr(std::numeric_limits<Value_>::has_quiet_NaN) {
274template<
typename Value_,
class If_,
class Else_>
275auto nanable_ifelse_with_value(
bool skip_nan, If_ iffun, Else_ elsefun) {
276 if constexpr(std::numeric_limits<Value_>::has_quiet_NaN) {
Local output buffer for running calculations.
Definition utils.hpp:78
LocalOutputBuffer(size_t thread, Index_ start, Index_ length, Output_ *output, Output_ fill)
Definition utils.hpp:89
void transfer()
Definition utils.hpp:135
Output_ * data()
Definition utils.hpp:118
LocalOutputBuffer()=default
const Output_ * data() const
Definition utils.hpp:127
LocalOutputBuffer(size_t thread, Index_ start, Index_ length, Output_ *output)
Definition utils.hpp:106
Local output buffers for running calculations.
Definition utils.hpp:159
void transfer()
Definition utils.hpp:241
const Output_ * data(size_t i) const
Definition utils.hpp:233
LocalOutputBuffers(size_t thread, size_t number, Index_ start, Index_ length, GetOutput_ outfun)
Definition utils.hpp:201
size_t size() const
Definition utils.hpp:213
LocalOutputBuffers()=default
LocalOutputBuffers(size_t thread, size_t number, Index_ start, Index_ length, GetOutput_ outfun, Output_ fill)
Definition utils.hpp:171
Output_ * data(size_t i)
Definition utils.hpp:223
Functions to compute statistics from a tatami::Matrix.
Definition counts.hpp:18
size_t total_groups(const Group_ *group, size_t n)
Definition utils.hpp:28
std::vector< Size_ > tabulate_groups(const Group_ *group, Size_ n)
Definition utils.hpp:49