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 & 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 +void SortUnique(function const & comp, vector & v) +{ + sort(v.begin(), v.end(), comp); + function 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 void EraseIf(vector & v, TFn && fn) {