2017-04-01 16:09:34 +08:00
|
|
|
#pragma once
|
|
|
|
|
2017-07-13 08:40:41 +08:00
|
|
|
#include <map>
|
2017-04-01 16:09:34 +08:00
|
|
|
#include <utility>
|
2017-04-28 08:56:06 +08:00
|
|
|
#include <vector>
|
2017-04-01 16:09:34 +08:00
|
|
|
|
|
|
|
namespace vcpkg::Util
|
|
|
|
{
|
|
|
|
template<class Cont, class Func>
|
2017-08-22 08:16:14 +08:00
|
|
|
using FmapOut = decltype(std::declval<Func>()(*begin(std::declval<Cont>())));
|
2017-04-01 16:09:34 +08:00
|
|
|
|
|
|
|
template<class Cont, class Func, class Out = FmapOut<Cont, Func>>
|
2017-08-22 08:16:14 +08:00
|
|
|
std::vector<Out> fmap(Cont&& xs, Func&& f)
|
2017-04-01 16:09:34 +08:00
|
|
|
{
|
2017-08-22 08:16:14 +08:00
|
|
|
using O = decltype(f(*begin(xs)));
|
2017-04-01 16:09:34 +08:00
|
|
|
|
|
|
|
std::vector<O> ret;
|
|
|
|
ret.reserve(xs.size());
|
|
|
|
|
|
|
|
for (auto&& x : xs)
|
|
|
|
ret.push_back(f(x));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2017-04-13 13:12:37 +08:00
|
|
|
|
2017-04-09 11:19:35 +08:00
|
|
|
template<class Container, class Pred>
|
|
|
|
void unstable_keep_if(Container& cont, Pred pred)
|
|
|
|
{
|
|
|
|
cont.erase(std::partition(cont.begin(), cont.end(), pred), cont.end());
|
|
|
|
}
|
2017-04-13 13:57:23 +08:00
|
|
|
|
2017-04-13 13:12:37 +08:00
|
|
|
template<class Container, class Pred>
|
2017-04-14 06:37:24 +08:00
|
|
|
void erase_remove_if(Container& cont, Pred pred)
|
2017-04-13 13:12:37 +08:00
|
|
|
{
|
|
|
|
cont.erase(std::remove_if(cont.begin(), cont.end(), pred), cont.end());
|
|
|
|
}
|
2017-04-14 08:53:09 +08:00
|
|
|
|
2017-06-07 07:08:36 +08:00
|
|
|
template<class Container, class V>
|
|
|
|
auto find(const Container& cont, V&& v)
|
|
|
|
{
|
|
|
|
return std::find(cont.cbegin(), cont.cend(), v);
|
|
|
|
}
|
|
|
|
|
2017-04-14 08:53:09 +08:00
|
|
|
template<class Container, class Pred>
|
|
|
|
auto find_if(const Container& cont, Pred pred)
|
|
|
|
{
|
|
|
|
return std::find_if(cont.cbegin(), cont.cend(), pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Container, class Pred>
|
|
|
|
auto find_if_not(const Container& cont, Pred pred)
|
|
|
|
{
|
|
|
|
return std::find_if_not(cont.cbegin(), cont.cend(), pred);
|
|
|
|
}
|
2017-04-15 05:31:17 +08:00
|
|
|
|
|
|
|
template<class K, class V, class Container, class Func>
|
2017-04-28 08:56:06 +08:00
|
|
|
void group_by(const Container& cont, std::map<K, std::vector<const V*>>* output, Func f)
|
2017-04-15 05:31:17 +08:00
|
|
|
{
|
|
|
|
for (const V& element : cont)
|
|
|
|
{
|
|
|
|
K key = f(element);
|
|
|
|
(*output)[key].push_back(&element);
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 16:09:34 +08:00
|
|
|
}
|