Skip to content
Snippets Groups Projects
Commit b6975bb2 authored by Raphael Grimm's avatar Raphael Grimm
Browse files

Allow get_keys / get_values to work with all map like containers

parent 6550981f
No related branches found
No related tags found
No related merge requests found
......@@ -6,14 +6,15 @@
namespace simox
{
/// Get the keys of `map` in a vector.
template <typename K, typename V>
std::vector<K> get_keys(const std::map<K, V>& map)
template <class K, class V, template<class...> class Template = std::map, class...Ts>
std::vector<K> get_keys(const Template<K, V, Ts...>& map)
{
std::vector<K> keys;
keys.reserve(map.size());
if constexpr(std::is_same_v<std::map<K, V, Ts...>, Template<K, V, Ts...>>)
{
keys.reserve(map.size());
}
for (const auto& [k, v] : map)
{
keys.push_back(k);
......@@ -23,8 +24,8 @@ namespace simox
/// Get the keys of `map` in a set.
template <typename K, typename V>
std::set<K> get_keys_set(const std::map<K, V>& map)
template <class K, class V, template<class...> class Template = std::map, class...Ts>
std::set<K> get_keys_set(const Template<K, V, Ts...>& map)
{
std::set<K> keys;
for (const auto& [k, v] : map)
......@@ -36,8 +37,8 @@ namespace simox
/// Get the values of `map`.
template <typename K, typename V>
std::vector<V> get_values(const std::map<K, V>& map)
template <class K, class V, template<class...> class Template = std::map, class...Ts>
std::vector<V> get_values(const Template<K, V, Ts...>& map)
{
std::vector<V> values;
values.reserve(map.size());
......@@ -50,8 +51,8 @@ namespace simox
/// Get the results of applying `unary_func` to the values of `map`.
template <typename R, typename K, typename V>
std::vector<V> get_values(const std::map<K, V>& map, std::function<R(const V&)> unary_func)
template <class R, class K, class V, template<class...> class Template = std::map, class...Ts>
std::vector<V> get_values(const Template<K, V, Ts...>& map, std::function<R(const V&)> unary_func)
{
std::vector<V> values;
values.reserve(map.size());
......@@ -61,6 +62,4 @@ namespace simox
}
return values;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment