diff options
author | Vladimir Byko-Ianko <v.bykoianko@corp.mail.ru> | 2016-05-25 18:31:31 +0300 |
---|---|---|
committer | Vladimir Byko-Ianko <v.bykoianko@corp.mail.ru> | 2016-05-27 14:17:13 +0300 |
commit | bb46721ccdca16e94de33de872aa180a8ad741d4 (patch) | |
tree | 0921a737e6ae9965ceee907deb214f03168b8271 /base | |
parent | 566024bb40cf86d241c84f234fc2a9989b035d88 (diff) |
Implementation SortUnique with predicate.
Diffstat (limited to 'base')
-rw-r--r-- | base/stl_helpers.hpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/base/stl_helpers.hpp b/base/stl_helpers.hpp index 832f53b829..db5cf870f0 100644 --- a/base/stl_helpers.hpp +++ b/base/stl_helpers.hpp @@ -1,6 +1,7 @@ #pragma once #include "std/algorithm.hpp" +#include "std/functional.hpp" #include "std/vector.hpp" namespace my @@ -85,6 +86,21 @@ void SortUnique(vector<T> & v) v.erase(unique(v.begin(), v.end()), v.end()); } +// Sorts and removes duplicate entries from |v| according to |comp|. +// Note. If several entries are equivalent according to |comp| an arbitrary entry of them +// is left in |v| after a call of this method. +// Note. |comp| should implement operator<. It means the expression +// !comp(t1, t2) && !comp(t2, t1) should return true iff t1 is equivalent to t2. +// It's necessary for std::unique. +template <typename T> +void SortUnique(function<bool(T const &, T const &)> const & comp, vector<T> & v) +{ + sort(v.begin(), v.end(), comp); + function<bool(T const &, T const &)> const pred = + [&comp](T const &t1, T const &t2) { return !comp(t1, t2) && !comp(t2, t1); }; + v.erase(unique(v.begin(), v.end(), pred), v.end()); +} + template <typename T, class TFn> void EraseIf(vector<T> & v, TFn && fn) { |