Skip to content

Commit 1ad9019

Browse files
committed
improve multcomp_pmt
1 parent 7610d42 commit 1ad9019

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

inst/include/pmt/impl_multcomp_pmt.hpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,31 @@ RObject impl_multcomp_pmt(
55
const NumericVector data,
66
IntegerVector group,
77
T&& statistic_func,
8-
const double n_permu)
8+
const double n_permu
9+
)
910
{
1011
Stat<progress> statistic_container;
1112

12-
int k = *(group.end() - 1);
13+
int k = *std::prev(group.end());
1314

1415
auto statistic_closure = statistic_func(data, group);
15-
auto multcomp_update = [&statistic_container, &statistic_closure, data, group, k]() {
16-
auto pairwise_closure = statistic_closure(data, group);
17-
18-
for (int i = 1; i < k - 1; i++) {
16+
auto multcomp_update = [&statistic_container, &statistic_closure, data, group, pairs = [k]() {
17+
std::vector<std::pair<int, int>> pairs;
18+
pairs.reserve(C(k, 2));
19+
for (int i = 1; i < k; i++) {
1920
for (int j = i + 1; j <= k; j++) {
20-
statistic_container << pairwise_closure(i, j);
21+
pairs.emplace_back(i, j);
2122
}
2223
}
24+
return pairs;
25+
}()]() {
26+
auto pairwise_closure = statistic_closure(data, group);
2327

24-
return statistic_container << pairwise_closure(k - 1, k);
28+
auto it = pairs.begin();
29+
do {
30+
statistic_container << pairwise_closure(it->first, it->second);
31+
} while (++it != std::prev(pairs.end()));
32+
return statistic_container << pairwise_closure(it->first, it->second);
2533
};
2634

2735
statistic_container.allocate(C(k, 2), n_permu != 0 ? n_permu : n_permutation(group));

0 commit comments

Comments
 (0)