utilities.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #ifndef UTILILITIES_HPP
  4. #define UTILILITIES_HPP
  5. #include <algorithm>
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <unordered_map>
  10. #include <vector>
  11. #include "cbasetypes.hpp"
  12. #include "random.hpp"
  13. // Class used to perform time measurement
  14. class cScopeTimer {
  15. struct sPimpl; //this is to avoid long compilation time
  16. std::unique_ptr<sPimpl> aPimpl;
  17. cScopeTimer();
  18. };
  19. int levenshtein( const std::string &s1, const std::string &s2 );
  20. namespace rathena {
  21. namespace util {
  22. template <typename K, typename V> bool map_exists( std::map<K,V>& map, K key ){
  23. return map.find( key ) != map.end();
  24. }
  25. /**
  26. * Find a key-value pair and return the key value as a reference
  27. * @param map: Map to search through
  28. * @param key: Key wanted
  29. * @return Key value on success or nullptr on failure
  30. */
  31. template <typename K, typename V> V* map_find( std::map<K,V>& map, K key ){
  32. auto it = map.find( key );
  33. if( it != map.end() ){
  34. return &it->second;
  35. }else{
  36. return nullptr;
  37. }
  38. }
  39. /**
  40. * Find a key-value pair and return the key value as a reference
  41. * @param map: Map to search through
  42. * @param key: Key wanted
  43. * @return Key value on success or nullptr on failure
  44. */
  45. template <typename K, typename V> std::shared_ptr<V> map_find( std::map<K,std::shared_ptr<V>>& map, K key ){
  46. auto it = map.find( key );
  47. if( it != map.end() ){
  48. return it->second;
  49. }else{
  50. return nullptr;
  51. }
  52. }
  53. /**
  54. * Get a key-value pair and return the key value
  55. * @param map: Map to search through
  56. * @param key: Key wanted
  57. * @param defaultValue: Value returned if key doesn't exist
  58. * @return Key value on success or defaultValue on failure
  59. */
  60. template <typename K, typename V> V map_get(std::map<K, V>& map, K key, V defaultValue) {
  61. auto it = map.find(key);
  62. if (it != map.end())
  63. return it->second;
  64. else
  65. return defaultValue;
  66. }
  67. /**
  68. * Find a key-value pair and return the key value as a reference
  69. * @param map: Unordered Map to search through
  70. * @param key: Key wanted
  71. * @return Key value on success or nullptr on failure
  72. */
  73. template <typename K, typename V> V* umap_find(std::unordered_map<K, V>& map, K key) {
  74. auto it = map.find(key);
  75. if (it != map.end())
  76. return &it->second;
  77. else
  78. return nullptr;
  79. }
  80. /**
  81. * Find a key-value pair and return the key value as a reference
  82. * @param map: Unordered Map to search through
  83. * @param key: Key wanted
  84. * @return Key value on success or nullptr on failure
  85. */
  86. template <typename K, typename V> std::shared_ptr<V> umap_find(std::unordered_map<K, std::shared_ptr<V>>& map, K key) {
  87. auto it = map.find(key);
  88. if (it != map.end())
  89. return it->second;
  90. else
  91. return nullptr;
  92. }
  93. /**
  94. * Get a key-value pair and return the key value
  95. * @param map: Unordered Map to search through
  96. * @param key: Key wanted
  97. * @param defaultValue: Value returned if key doesn't exist
  98. * @return Key value on success or defaultValue on failure
  99. */
  100. template <typename K, typename V> V umap_get(std::unordered_map<K, V>& map, K key, V defaultValue) {
  101. auto it = map.find(key);
  102. if (it != map.end())
  103. return it->second;
  104. else
  105. return defaultValue;
  106. }
  107. /**
  108. * Get a random value from the given map
  109. * @param map: Unordered Map to search through
  110. * @return A random value by reference
  111. */
  112. template <typename K, typename V> V& umap_random( std::unordered_map<K, V>& map ){
  113. auto it = map.begin();
  114. std::advance( it, rnd_value( 0, map.size() - 1 ) );
  115. return it->second;
  116. }
  117. /**
  118. * Get an iterator element
  119. * @param vec: Vector to search through
  120. * @param value: Value wanted
  121. * @return Key value iterator on success or vector end iterator on failure
  122. */
  123. template <typename K, typename V> typename std::vector<K>::iterator vector_get(std::vector<K> &vec, V key) {
  124. return std::find(vec.begin(), vec.end(), key);
  125. }
  126. /**
  127. * Determine if a value exists in the vector
  128. * @param vec: Vector to search through
  129. * @param value: Value wanted
  130. * @return True on success or false on failure
  131. */
  132. template <typename K, typename V> bool vector_exists(std::vector<K> &vec, V value) {
  133. auto it = std::find(vec.begin(), vec.end(), value);
  134. if (it != vec.end())
  135. return true;
  136. else
  137. return false;
  138. }
  139. /**
  140. * Erase an index value from a vector
  141. * @param vector: Vector to erase value from
  142. * @param index: Index value to remove
  143. */
  144. template <typename K> void erase_at(std::vector<K>& vector, size_t index) {
  145. if (vector.size() == 1) {
  146. vector.clear();
  147. vector.shrink_to_fit();
  148. } else
  149. vector.erase(vector.begin() + index);
  150. }
  151. bool safe_addition( int64 a, int64 b, int64& result );
  152. bool safe_substraction( int64 a, int64 b, int64& result );
  153. bool safe_multiplication( int64 a, int64 b, int64& result );
  154. }
  155. }
  156. #endif /* UTILILITIES_HPP */