tatami_stats
Matrix statistics for tatami
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef TATAMI_STATS__UTILS_HPP
2#define TATAMI_STATS__UTILS_HPP
3
4#include <vector>
5#include <algorithm>
6
13namespace tatami_stats {
14
27template<typename Group_>
28size_t total_groups(const Group_* group, size_t n) {
29 if (n) {
30 return static_cast<size_t>(*std::max_element(group, group + n)) + 1;
31 } else {
32 return 0;
33 }
34}
35
48template<typename Group_, typename Size_>
49std::vector<Size_> tabulate_groups(const Group_* group, Size_ n) {
50 auto ngroups = total_groups(group, n);
51 std::vector<Size_> group_sizes(ngroups);
52 for (Size_ r = 0; r < n; ++r) {
53 ++(group_sizes[group[r]]);
54 }
55 return group_sizes;
56}
57
71template<typename Output_>
73public:
82 template<typename Index_>
83 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) {
84 if (!use_local) {
85 // Setting to zero to match the initial behavior of 'my_buffer' when 'use_local = true'.
86 std::fill_n(my_output, length, fill);
87 }
88 }
89
99 template<typename Index_>
100 LocalOutputBuffer(size_t thread, Index_ start, Index_ length, Output_* output) : LocalOutputBuffer(thread, start, length, output, 0) {}
101
105 LocalOutputBuffer() = default;
106
111 Output_* data() {
112 return (use_local ? my_buffer.data() : my_output);
113 }
114
119 const Output_* data() const {
120 return (use_local ? my_buffer.data() : my_output);
121 }
122
126 void transfer() {
127 if (use_local) {
128 std::copy(my_buffer.begin(), my_buffer.end(), my_output);
129 }
130 }
131
132private:
133 Output_* my_output = NULL;
134 bool use_local = false;
135 std::vector<Output_> my_buffer;
136};
137
141namespace internal {
142
143template<typename Value_, class If_, class Else_>
144void nanable_ifelse(bool skip_nan, If_ iffun, Else_ elsefun) {
145 if constexpr(std::numeric_limits<Value_>::has_quiet_NaN) {
146 if (skip_nan) {
147 iffun();
148 return;
149 }
150 }
151 elsefun();
152}
153
154template<typename Value_, class If_, class Else_>
155auto nanable_ifelse_with_value(bool skip_nan, If_ iffun, Else_ elsefun) {
156 if constexpr(std::numeric_limits<Value_>::has_quiet_NaN) {
157 if (skip_nan) {
158 return iffun();
159 }
160 }
161 return elsefun();
162}
163
164}
169}
170
171#endif
Local output buffer for running calculations.
Definition utils.hpp:72
LocalOutputBuffer(size_t thread, Index_ start, Index_ length, Output_ *output, Output_ fill)
Definition utils.hpp:83
void transfer()
Definition utils.hpp:126
Output_ * data()
Definition utils.hpp:111
const Output_ * data() const
Definition utils.hpp:119
LocalOutputBuffer(size_t thread, Index_ start, Index_ length, Output_ *output)
Definition utils.hpp:100
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