#pragma once #include #include "Result.hpp" #include #include #include #include namespace geode::map_utils { /** * Returns true if the map contains * a value matching `containFunc`. * @param map Map to check * @param containFunc Predicate run against each value in map; * return true if the item matches what is * beeing looked for, and false if not. * @returns True if value matching `containFunc` was found, * false if not. * @author HJfod */ template bool contains( std::unordered_map const& map, std::function containFunc ) { for (auto const& [_, r] : map) { if (containFunc(r)) return true; } return false; } /** * Get the first item in a map that * matches `selectFunc`. * @param map Map to check * @param selectFunc Predicate run against each value in map; * return true if the item matches what is * beeing looked for, and false if not. * @returns The value matching `selectFunc` if one was found, * otherwise the default value for type R or `nullptr` if R is * a pointer. * @author HJfod */ template R select( std::unordered_map const& map, std::function selectFunc ) { for (auto const& [_, r] : map) { if (selectFunc(r)) return r; } if (std::is_pointer::value) { return nullptr; } return R(); } /** * Get all items in a map that match `selectFunc`. * @param map Map to check * @param selectFunc Predicate run against each value in map; * return true if the item matches what is * beeing looked for, and false if not. * @returns Vector of all values that matched. * @author HJfod */ template std::vector selectAll( std::unordered_map const& map, std::function selectFunc ) { std::vector res; for (auto const& [_, r] : map) { if (selectFunc(r)) { res.push_back(r); } } return res; } /** * Get all values in a map. * @param map Map to get values from * @returns Vector of all values. * @author HJfod */ template std::vector getValues(std::unordered_map const& map) { std::vector res; for (auto const& [_, r] : map) { res.push_back(r); } return res; } /** * Get all keys in a map. * @param map Map to get keys from * @returns Vector of all keys. * @author HJfod */ template std::vector getKeys(std::unordered_map const& map) { std::vector res; for (auto const& [t, _] : map) { res.push_back(t); } return res; } /** * Transform an unordered_map into * another unordered_map of a different * type. * @param map Map to convert * @param remapFunc Function that converts * key-value pairs from the first map to * the second * @returns New map */ template std::unordered_map remap( std::unordered_map const& map, std::function(std::pair)> remapFunc ) { std::unordered_map res; for (auto const& [t, v] : map) { res.insert(remapFunc({ t, v })); } return res; } }