Skip to content

Commit 2bb30bc

Browse files
committed
Replace repeated sorts with one std::ranges::sort using a projection
Instead of sorting several times in a row using different orderings, we can define a single projection that defines the desired final order.
1 parent fb0a8f0 commit 2bb30bc

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/report_generator.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,31 @@ struct order_by_major_section {
6363
lwg::section_map& section_db;
6464
};
6565

66+
// Create a LessThanComparable object that defines an ordering based on date,
67+
// with newer dates first.
68+
auto ordered_date(lwg::issue const & issue) {
69+
std::chrono::sys_days date(issue.mod_date);
70+
return -date.time_since_epoch().count();
71+
}
72+
73+
// Create a LessThanComparable object that defines an ordering that depends on
74+
// the section number (e.g. 23.5.1) first and then on the section stable tag.
75+
// Using both is not redundant, because we use section 99 for all sections of some TS's.
76+
// Including the tag in the order gives a total order for sections in those TS's,
77+
// e.g., {99,[arrays.ts::dynarray]} < {99,[arrays.ts::dynarraconstructible_from.cons]}.
78+
auto ordered_section(lwg::section_map & section_db, lwg::issue const & issue) {
79+
assert(!issue.tags.empty());
80+
return std::tie(section_db[issue.tags.front()], issue.tags.front());
81+
}
82+
6683
struct order_by_section {
6784
explicit order_by_section(lwg::section_map &sections)
6885
: section_db(sections)
6986
{
7087
}
7188

7289
auto operator()(lwg::issue const & x, lwg::issue const & y) const -> bool {
73-
assert(!x.tags.empty());
74-
assert(!y.tags.empty());
75-
// This sorts by the section number (e.g. 23.5.1) then by the section stable tag.
76-
// This is not redundant, because for e.g. Arrays TS the entire paper has section num 99,
77-
// so including the tag orders [arrays.ts::dynarray] before [arrays.ts::dynarray.cons].
78-
return std::tie(section_db[x.tags.front()], x.tags.front()) < std::tie(section_db[y.tags.front()], y.tags.front());
90+
return ordered_section(section_db, x) < ordered_section(section_db, y);
7991
}
8092

8193
private:
@@ -696,10 +708,10 @@ sorted by priority.</p>
696708

697709

698710
void report_generator::make_sort_by_status(std::vector<issue>& issues, fs::path const & filename) {
699-
sort(issues.begin(), issues.end(), order_by_issue_number{});
700-
stable_sort(issues.begin(), issues.end(), [](issue const & x, issue const & y) { return x.mod_date > y.mod_date; } );
701-
stable_sort(issues.begin(), issues.end(), order_by_section{section_db});
702-
stable_sort(issues.begin(), issues.end(), order_by_status{});
711+
auto proj = [this](const auto& i) {
712+
return std::make_tuple(lwg::get_status_priority(i.stat), ordered_section(section_db, i), ordered_date(i), i.num);
713+
};
714+
std::ranges::sort(issues.begin(), issues.end(), {}, proj);
703715

704716
std::ofstream out{filename};
705717
if (!out)
@@ -733,10 +745,10 @@ This document is the Index by Status and Section for the <a href="lwg-active.htm
733745

734746

735747
void report_generator::make_sort_by_status_mod_date(std::vector<issue> & issues, fs::path const & filename) {
736-
sort(issues.begin(), issues.end(), order_by_issue_number{});
737-
stable_sort(issues.begin(), issues.end(), order_by_section{section_db});
738-
stable_sort(issues.begin(), issues.end(), [](issue const & x, issue const & y) { return x.mod_date > y.mod_date; } );
739-
stable_sort(issues.begin(), issues.end(), order_by_status{});
748+
auto proj = [this](const auto& i) {
749+
return std::make_tuple(lwg::get_status_priority(i.stat), ordered_date(i), ordered_section(section_db, i), i.num);
750+
};
751+
std::ranges::sort(issues.begin(), issues.end(), {}, proj);
740752

741753
std::ofstream out{filename};
742754
if (!out)
@@ -769,9 +781,11 @@ This document is the Index by Status and Date for the <a href="lwg-active.html">
769781

770782

771783
void report_generator::make_sort_by_section(std::vector<issue>& issues, fs::path const & filename, bool active_only) {
772-
sort(issues.begin(), issues.end(), order_by_issue_number{});
773-
stable_sort(issues.begin(), issues.end(), [](issue const & x, issue const & y) { return x.mod_date > y.mod_date; } );
774-
stable_sort(issues.begin(), issues.end(), order_by_status{});
784+
auto proj = [this](const auto& i) {
785+
return std::make_tuple(lwg::get_status_priority(i.stat), ordered_date(i), i.num);
786+
};
787+
std::ranges::sort(issues.begin(), issues.end(), {}, proj);
788+
775789
auto b = issues.begin();
776790
auto e = issues.end();
777791
if(active_only) {

0 commit comments

Comments
 (0)