From: Steinar H. Gunderson Date: Sat, 10 Oct 2020 08:35:41 +0000 (+0200) Subject: Generalize the sort+unique+erase pattern into unique_sort(). X-Git-Tag: 1.0.0~19 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=9048228b9a64c9a86d8ef9264d4d6198fe70827b;hp=1dd9495198bb877e0d5d332def4d3b218fc353ba;p=plocate Generalize the sort+unique+erase pattern into unique_sort(). --- diff --git a/plocate.cpp b/plocate.cpp index a908d30..d53c41c 100644 --- a/plocate.cpp +++ b/plocate.cpp @@ -1,5 +1,6 @@ #include "db.h" #include "io_uring_engine.h" +#include "unique_sort.h" #include #include @@ -401,11 +402,7 @@ void do_search_file(const vector &needles, const char *filename) } return; } - sort(trigrams.begin(), trigrams.end()); - { - auto last = unique(trigrams.begin(), trigrams.end()); - trigrams.erase(last, trigrams.end()); - } + unique_sort(&trigrams); sort(trigrams.begin(), trigrams.end(), [&](const pair &a, const pair &b) { return a.first.num_docids < b.first.num_docids; diff --git a/unique_sort.h b/unique_sort.h new file mode 100644 index 0000000..6f80f0e --- /dev/null +++ b/unique_sort.h @@ -0,0 +1,14 @@ +#ifndef _UNIQUE_SORT_H +#define _UNIQUE_SORT_H 1 + +#include + +template, class EqualTo = std::equal_to> +void unique_sort(Container *c, const LessThan < = LessThan(), const EqualTo &eq = EqualTo()) +{ + sort(c->begin(), c->end(), lt); + auto new_end = unique(c->begin(), c->end(), eq); + c->erase(new_end, c->end()); +} + +#endif // !defined(_UNIQUE_SORT_H)