50template <
typename Sum_,
typename Detected_>
77template <
typename Sum_,
typename Detected_>
86 std::vector<std::vector<Sum_> >
sums;
101template<
bool sparse_,
typename Data_,
typename Index_,
typename Group_,
typename Sum_,
typename Detected_>
102void aggregate_across_cells_by_row(
104 const Group_*
const group,
113 const auto nsums = buffers.
sums.size();
114 auto tmp_sums = sanisizer::create<std::vector<Sum_> >(nsums);
115 const auto ndetected = buffers.
detected.size();
116 auto tmp_detected = sanisizer::create<std::vector<Detected_> >(ndetected);
118 const auto NC = p.
ncol();
121 if constexpr(sparse_) {
128 for (Index_ x = s, end = s + l; x < end; ++x) {
129 const auto row = [&]{
130 if constexpr(sparse_) {
131 return ext->fetch(vbuffer.data(), ibuffer.data());
133 return ext->fetch(vbuffer.data());
138 std::fill(tmp_sums.begin(), tmp_sums.end(), 0);
140 if constexpr(sparse_) {
141 for (Index_ j = 0; j < row.number; ++j) {
142 tmp_sums[group[row.index[j]]] += row.value[j];
145 for (Index_ j = 0; j < NC; ++j) {
146 tmp_sums[group[j]] += row[j];
151 for (I<
decltype(nsums)> l = 0; l < nsums; ++l) {
152 buffers.
sums[l][x] = tmp_sums[l];
157 std::fill(tmp_detected.begin(), tmp_detected.end(), 0);
159 if constexpr(sparse_) {
160 for (Index_ j = 0; j < row.number; ++j) {
161 tmp_detected[group[row.index[j]]] += (row.value[j] > 0);
164 for (Index_ j = 0; j < NC; ++j) {
165 tmp_detected[group[j]] += (row[j] > 0);
169 for (I<
decltype(ndetected)> l = 0; l < ndetected; ++l) {
170 buffers.
detected[l][x] = tmp_detected[l];
177template<
bool sparse_,
typename Data_,
typename Index_,
typename Group_,
typename Sum_,
typename Detected_>
178void aggregate_across_cells_by_column(
180 const Group_*
const group,
181 const AggregateAcrossCellsBuffers<Sum_, Detected_>& buffers,
182 const AggregateAcrossCellsOptions& options)
188 const auto NC = p.
ncol();
192 if constexpr(sparse_) {
199 const auto num_sums = buffers.sums.size();
200 auto get_sum = [&](Index_ i) -> Sum_* {
return buffers.sums[i]; };
201 tatami_stats::LocalOutputBuffers<Sum_, I<
decltype(get_sum)>> local_sums(t, num_sums, start, length, std::move(get_sum));
203 const auto num_detected = buffers.detected.size();
204 auto get_detected = [&](Index_ i) -> Detected_* {
return buffers.detected[i]; };
205 tatami_stats::LocalOutputBuffers<Detected_, I<
decltype(get_detected)>> local_detected(t, num_detected, start, length, std::move(get_detected));
207 for (Index_ x = 0; x < NC; ++x) {
208 const auto current = group[x];
210 if constexpr(sparse_) {
211 const auto col = ext->fetch(vbuffer.data(), ibuffer.data());
213 const auto cursum = local_sums.data(current);
214 for (Index_ i = 0; i < col.number; ++i) {
215 cursum[col.index[i] - start] += col.value[i];
219 const auto curdetected = local_detected.data(current);
220 for (Index_ i = 0; i < col.number; ++i) {
221 curdetected[col.index[i] - start] += (col.value[i] > 0);
226 const auto col = ext->fetch(vbuffer.data());
228 const auto cursum = local_sums.data(current);
229 for (Index_ i = 0; i < length; ++i) {
234 const auto curdetected = local_detected.data(current);
235 for (Index_ i = 0; i < length; ++i) {
236 curdetected[i] += (col[i] > 0);
242 local_sums.transfer();
243 local_detected.transfer();
244 }, p.
nrow(), options.num_threads);
268template<
typename Data_,
typename Index_,
typename Group_,
typename Sum_,
typename Detected_>
271 const Group_*
const group,
277 aggregate_across_cells_by_row<true>(input, group, buffers, options);
279 aggregate_across_cells_by_row<false>(input, group, buffers, options);
283 aggregate_across_cells_by_column<true>(input, group, buffers, options);
285 aggregate_across_cells_by_column<false>(input, group, buffers, options);
307template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Data_,
typename Index_,
typename Group_>
310 const Group_*
const group,
313 const Index_ NR = input.
nrow();
314 const Index_ NC = input.
ncol();
315 const std::size_t ngroups = [&]{
317 return sanisizer::sum<std::size_t>(*std::max_element(group, group + NC), 1);
319 return static_cast<std::size_t
>(0);
327 sanisizer::resize(output.
sums, ngroups);
328 sanisizer::resize(buffers.
sums, ngroups);
329 for (I<
decltype(ngroups)> l = 0; l < ngroups; ++l) {
330 auto& cursum = output.
sums[l];
332#ifdef SCRAN_AGGREGATE_TEST_INIT
333 , SCRAN_AGGREGATE_TEST_INIT
336 buffers.
sums[l] = cursum.data();
341 sanisizer::resize(output.
detected, ngroups);
342 sanisizer::resize(buffers.
detected, ngroups);
343 for (I<
decltype(ngroups)> l = 0; l < ngroups; ++l) {
346#ifdef SCRAN_AGGREGATE_TEST_INIT
347 , SCRAN_AGGREGATE_TEST_INIT
350 buffers.
detected[l] = curdet.data();
void aggregate_across_cells(const tatami::Matrix< Data_, Index_ > &input, const Group_ *const group, const AggregateAcrossCellsBuffers< Sum_, Detected_ > &buffers, const AggregateAcrossCellsOptions &options)
Definition aggregate_across_cells.hpp:269