// Copyright (c) rAthena Dev Teams - Licensed under GNU GPL // For more information, see LICENCE in the main folder #ifndef UTILILITIES_HPP #define UTILILITIES_HPP #include #include #include #include #include "cbasetypes.hpp" // Class used to perform time measurement class cScopeTimer { struct sPimpl; //this is to avoid long compilation time std::unique_ptr aPimpl; cScopeTimer(); }; int levenshtein( const std::string &s1, const std::string &s2 ); namespace rathena { namespace util { template bool map_exists( std::map& map, K key ){ return map.find( key ) != map.end(); } /** * Find a key-value pair and return the key value as a reference * @param map: Map to search through * @param key: Key wanted * @return Key value on success or nullptr on failure */ template V* map_find( std::map& map, K key ){ auto it = map.find( key ); if( it != map.end() ){ return &it->second; }else{ return nullptr; } } /** * Get a key-value pair and return the key value * @param map: Map to search through * @param key: Key wanted * @param defaultValue: Value returned if key doesn't exist * @return Key value on success or defaultValue on failure */ template V map_get(std::map& map, K key, V defaultValue) { auto it = map.find(key); if (it != map.end()) return it->second; else return defaultValue; } /** * Find a key-value pair and return the key value as a reference * @param map: Unordered Map to search through * @param key: Key wanted * @return Key value on success or nullptr on failure */ template V* umap_find(std::unordered_map& map, K key) { auto it = map.find(key); if (it != map.end()) return &it->second; else return nullptr; } /** * Get a key-value pair and return the key value * @param map: Unordered Map to search through * @param key: Key wanted * @param defaultValue: Value returned if key doesn't exist * @return Key value on success or defaultValue on failure */ template V umap_get(std::unordered_map& map, K key, V defaultValue) { auto it = map.find(key); if (it != map.end()) return it->second; else return defaultValue; } } } #endif /* UTILILITIES_HPP */