]> git.sesse.net Git - plocate/commitdiff
Generalize the sort+unique+erase pattern into unique_sort().
authorSteinar H. Gunderson <steinar+git@gunderson.no>
Sat, 10 Oct 2020 08:35:41 +0000 (10:35 +0200)
committerSteinar H. Gunderson <steinar+git@gunderson.no>
Sat, 10 Oct 2020 08:35:41 +0000 (10:35 +0200)
plocate.cpp
unique_sort.h [new file with mode: 0644]

index a908d30e0f4d67521d0691cc74fb3d8a1db1d5c9..d53c41c5ede0fb9f30f01dda384e9fbbdca8b585 100644 (file)
@@ -1,5 +1,6 @@
 #include "db.h"
 #include "io_uring_engine.h"
+#include "unique_sort.h"
 
 #include <algorithm>
 #include <chrono>
@@ -401,11 +402,7 @@ void do_search_file(const vector<string> &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<Trigram, size_t> &a, const pair<Trigram, size_t> &b) {
                     return a.first.num_docids < b.first.num_docids;
diff --git a/unique_sort.h b/unique_sort.h
new file mode 100644 (file)
index 0000000..6f80f0e
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef _UNIQUE_SORT_H
+#define _UNIQUE_SORT_H 1
+
+#include <algorithm>
+
+template<class Container, class LessThan = std::less<typename Container::value_type>, class EqualTo = std::equal_to<typename Container::value_type>>
+void unique_sort(Container *c, const LessThan &lt = 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)