48template <
typename Sum_,
typename Detected_>
75template <
typename Sum_,
typename Detected_>
84 std::vector<std::vector<Sum_> >
sums;
101template<
bool sparse_,
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
102void compute_aggregate_by_row(
104 const Factor_* factor,
113 auto nsums = buffers.
sums.size();
114 auto tmp_sums = sanisizer::create<std::vector<Sum_> >(nsums);
115 auto ndetected = buffers.
detected.size();
116 auto tmp_detected = sanisizer::create<std::vector<Detected_> >(ndetected);
121 if constexpr(sparse_) {
128 for (Index_ x = s, end = s + l; x < end; ++x) {
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[factor[row.index[j]]] += row.value[j];
145 for (Index_ j = 0; j < NC; ++j) {
146 tmp_sums[factor[j]] += row[j];
151 for (
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[factor[row.index[j]]] += (row.value[j] > 0);
164 for (Index_ j = 0; j < NC; ++j) {
165 tmp_detected[factor[j]] += (row[j] > 0);
169 for (
decltype(ndetected) l = 0; l < ndetected; ++l) {
170 buffers.
detected[l][x] = tmp_detected[l];
177template<
bool sparse_,
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
178void compute_aggregate_by_column(
180 const Factor_* factor,
181 const AggregateAcrossCellsBuffers<Sum_, Detected_>& buffers,
182 const AggregateAcrossCellsOptions& options)
192 if constexpr(sparse_) {
199 auto num_sums = buffers.sums.size();
200 auto get_sum = [&](Index_ i) -> Sum_* {
return buffers.sums[i]; };
201 tatami_stats::LocalOutputBuffers<Sum_,
decltype(get_sum)> local_sums(t, num_sums, start, length, std::move(get_sum));
202 auto get_detected = [&](Index_ i) -> Detected_* {
return buffers.detected[i]; };
203 auto num_detected = buffers.detected.size();
204 tatami_stats::LocalOutputBuffers<Detected_,
decltype(get_detected)> local_detected(t, num_detected, start, length, std::move(get_detected));
206 for (Index_ x = 0; x < NC; ++x) {
207 auto current = factor[x];
209 if constexpr(sparse_) {
210 auto col = ext->fetch(vbuffer.data(), ibuffer.data());
212 auto cursum = local_sums.data(current);
213 for (Index_ i = 0; i < col.number; ++i) {
214 cursum[col.index[i] - start] += col.value[i];
218 auto curdetected = local_detected.data(current);
219 for (Index_ i = 0; i < col.number; ++i) {
220 curdetected[col.index[i] - start] += (col.value[i] > 0);
225 auto col = ext->fetch(vbuffer.data());
227 auto cursum = local_sums.data(current);
228 for (Index_ i = 0; i < length; ++i) {
233 auto curdetected = local_detected.data(current);
234 for (Index_ i = 0; i < length; ++i) {
235 curdetected[i] += (col[i] > 0);
241 local_sums.transfer();
242 local_detected.transfer();
243 }, p.
nrow(), options.num_threads);
270template<
typename Data_,
typename Index_,
typename Factor_,
typename Sum_,
typename Detected_>
273 const Factor_* factor,
279 internal::compute_aggregate_by_row<true>(input, factor, buffers, options);
281 internal::compute_aggregate_by_row<false>(input, factor, buffers, options);
285 internal::compute_aggregate_by_column<true>(input, factor, buffers, options);
287 internal::compute_aggregate_by_column<false>(input, factor, buffers, options);
309template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Data_,
typename Index_,
typename Factor_>
312 const Factor_* factor,
315 Index_ NR = input.
nrow();
316 Index_ NC = input.
ncol();
317 std::size_t nlevels = [&]{
319 return sanisizer::sum<std::size_t>(*std::max_element(factor, factor + NC), 1);
321 return static_cast<std::size_t
>(0);
330 sanisizer::cast<
decltype(output.
sums.size())>(nlevels),
332#ifdef SCRAN_AGGREGATE_TEST_INIT
333 , SCRAN_AGGREGATE_TEST_INIT
338 sanisizer::cast<
decltype(buffers.
sums.size())>(nlevels)
340 for (
decltype(nlevels) l = 0; l < nlevels; ++l) {
341 buffers.
sums[l] = output.
sums[l].data();
347 sanisizer::cast<
decltype(output.
detected.size())>(nlevels),
349#ifdef SCRAN_AGGREGATE_TEST_INIT
350 , SCRAN_AGGREGATE_TEST_INIT
355 sanisizer::cast<
decltype(buffers.
detected.size())>(nlevels)
357 for (
decltype(nlevels) l = 0; l < nlevels; ++l) {
void aggregate_across_cells(const tatami::Matrix< Data_, Index_ > &input, const Factor_ *factor, const AggregateAcrossCellsBuffers< Sum_, Detected_ > &buffers, const AggregateAcrossCellsOptions &options)
Definition aggregate_across_cells.hpp:271