1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- // 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 <map>
- #include <memory>
- #include <string>
- #include <unordered_map>
- #include "cbasetypes.hpp"
- // Class used to perform time measurement
- class cScopeTimer {
- struct sPimpl; //this is to avoid long compilation time
- std::unique_ptr<sPimpl> aPimpl;
-
- cScopeTimer();
- };
- int levenshtein( const std::string &s1, const std::string &s2 );
- namespace rathena {
- namespace util {
- template <typename K, typename V> bool map_exists( std::map<K,V>& 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 <typename K, typename V> V* map_find( std::map<K,V>& 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 <typename K, typename V> V map_get(std::map<K, V>& 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 <typename K, typename V> V* umap_find(std::unordered_map<K, V>& 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 <typename K, typename V> V umap_get(std::unordered_map<K, V>& map, K key, V defaultValue) {
- auto it = map.find(key);
- if (it != map.end())
- return it->second;
- else
- return defaultValue;
- }
- }
- }
- #endif /* UTILILITIES_HPP */
|