50template <
typename Sum_,
typename Detected_>
77template <
typename Sum_,
typename Detected_>
86 std::vector<std::vector<Sum_> >
sums;
103template<
bool sparse_,
typename Data_,
typename Index_,
typename Group_,
typename Sum_,
typename Detected_>
104void compute_aggregate_by_row(
106 const Group_*
const group,
115 const auto nsums = buffers.
sums.size();
116 auto tmp_sums = sanisizer::create<std::vector<Sum_> >(nsums);
117 const auto ndetected = buffers.
detected.size();
118 auto tmp_detected = sanisizer::create<std::vector<Detected_> >(ndetected);
120 const auto NC = p.
ncol();
123 if constexpr(sparse_) {
130 for (Index_ x = s, end = s + l; x < end; ++x) {
131 const auto row = [&]{
132 if constexpr(sparse_) {
133 return ext->fetch(vbuffer.data(), ibuffer.data());
135 return ext->fetch(vbuffer.data());
140 std::fill(tmp_sums.begin(), tmp_sums.end(), 0);
142 if constexpr(sparse_) {
143 for (Index_ j = 0; j < row.number; ++j) {
144 tmp_sums[group[row.index[j]]] += row.value[j];
147 for (Index_ j = 0; j < NC; ++j) {
148 tmp_sums[group[j]] += row[j];
153 for (
decltype(I(nsums)) l = 0; l < nsums; ++l) {
154 buffers.
sums[l][x] = tmp_sums[l];
159 std::fill(tmp_detected.begin(), tmp_detected.end(), 0);
161 if constexpr(sparse_) {
162 for (Index_ j = 0; j < row.number; ++j) {
163 tmp_detected[group[row.index[j]]] += (row.value[j] > 0);
166 for (Index_ j = 0; j < NC; ++j) {
167 tmp_detected[group[j]] += (row[j] > 0);
171 for (
decltype(I(ndetected)) l = 0; l < ndetected; ++l) {
172 buffers.
detected[l][x] = tmp_detected[l];
179template<
bool sparse_,
typename Data_,
typename Index_,
typename Group_,
typename Sum_,
typename Detected_>
180void compute_aggregate_by_column(
182 const Group_*
const group,
183 const AggregateAcrossCellsBuffers<Sum_, Detected_>& buffers,
184 const AggregateAcrossCellsOptions& options)
190 const auto NC = p.
ncol();
194 if constexpr(sparse_) {
201 const auto num_sums = buffers.sums.size();
202 auto get_sum = [&](Index_ i) -> Sum_* {
return buffers.sums[i]; };
203 tatami_stats::LocalOutputBuffers<Sum_,
decltype(I(get_sum))> local_sums(t, num_sums, start, length, std::move(get_sum));
205 const auto num_detected = buffers.detected.size();
206 auto get_detected = [&](Index_ i) -> Detected_* {
return buffers.detected[i]; };
207 tatami_stats::LocalOutputBuffers<Detected_,
decltype(I(get_detected))> local_detected(t, num_detected, start, length, std::move(get_detected));
209 for (Index_ x = 0; x < NC; ++x) {
210 const auto current = group[x];
212 if constexpr(sparse_) {
213 const auto col = ext->fetch(vbuffer.data(), ibuffer.data());
215 const auto cursum = local_sums.data(current);
216 for (Index_ i = 0; i < col.number; ++i) {
217 cursum[col.index[i] - start] += col.value[i];
221 const auto curdetected = local_detected.data(current);
222 for (Index_ i = 0; i < col.number; ++i) {
223 curdetected[col.index[i] - start] += (col.value[i] > 0);
228 const auto col = ext->fetch(vbuffer.data());
230 const auto cursum = local_sums.data(current);
231 for (Index_ i = 0; i < length; ++i) {
236 const auto curdetected = local_detected.data(current);
237 for (Index_ i = 0; i < length; ++i) {
238 curdetected[i] += (col[i] > 0);
244 local_sums.transfer();
245 local_detected.transfer();
246 }, p.
nrow(), options.num_threads);
272template<
typename Data_,
typename Index_,
typename Group_,
typename Sum_,
typename Detected_>
275 const Group_*
const group,
281 internal::compute_aggregate_by_row<true>(input, group, buffers, options);
283 internal::compute_aggregate_by_row<false>(input, group, buffers, options);
287 internal::compute_aggregate_by_column<true>(input, group, buffers, options);
289 internal::compute_aggregate_by_column<false>(input, group, buffers, options);
311template<
typename Sum_ =
double,
typename Detected_ =
int,
typename Data_,
typename Index_,
typename Group_>
314 const Group_*
const group,
317 const Index_ NR = input.
nrow();
318 const Index_ NC = input.
ncol();
319 const std::size_t ngroups = [&]{
321 return sanisizer::sum<std::size_t>(*std::max_element(group, group + NC), 1);
323 return static_cast<std::size_t
>(0);
331 sanisizer::resize(output.
sums, ngroups);
332 sanisizer::resize(buffers.
sums, ngroups);
333 for (
decltype(I(ngroups)) l = 0; l < ngroups; ++l) {
334 auto& cursum = output.
sums[l];
336#ifdef SCRAN_AGGREGATE_TEST_INIT
337 , SCRAN_AGGREGATE_TEST_INIT
340 buffers.
sums[l] = cursum.data();
345 sanisizer::resize(output.
detected, ngroups);
346 sanisizer::resize(buffers.
detected, ngroups);
347 for (
decltype(I(ngroups)) l = 0; l < ngroups; ++l) {
350#ifdef SCRAN_AGGREGATE_TEST_INIT
351 , SCRAN_AGGREGATE_TEST_INIT
354 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:273