utilities.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "utilities.hpp"
  4. #include <algorithm>
  5. #include <chrono>
  6. #include <iostream>
  7. #include <numeric> //iota
  8. #include <string>
  9. struct cScopeTimer::sPimpl {
  10. std::chrono::steady_clock::time_point start;
  11. std::chrono::steady_clock::time_point end;
  12. sPimpl()
  13. {
  14. start = std::chrono::steady_clock::now();
  15. }
  16. ~sPimpl(){
  17. end = std::chrono::steady_clock::now();
  18. std::chrono::microseconds diff = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  19. std::cout << " took=" << diff.count() << "ms !\n";
  20. }
  21. };
  22. cScopeTimer::cScopeTimer()
  23. : aPimpl(new sPimpl())
  24. {}
  25. /**
  26. * Calculates the Levenshtein distance of two strings.
  27. * @author http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C.2B.2B
  28. * comparison test was done here http://cpp.sh/2o7w
  29. */
  30. int levenshtein(const std::string &s1, const std::string &s2)
  31. {
  32. // To change the type this function manipulates and returns, change
  33. // the return type and the types of the two variables below.
  34. int s1len = static_cast<int>(s1.size());
  35. int s2len = static_cast<int>(s2.size());
  36. auto column_start = (decltype(s1len))1;
  37. auto column = new decltype(s1len)[s1len + 1];
  38. std::iota(column + column_start, column + s1len + 1, column_start);
  39. for (auto x = column_start; x <= s2len; x++) {
  40. column[0] = x;
  41. auto last_diagonal = x - column_start;
  42. for (auto y = column_start; y <= s1len; y++) {
  43. auto old_diagonal = column[y];
  44. auto possibilities = {
  45. column[y] + 1,
  46. column[y - 1] + 1,
  47. last_diagonal + (s1[y - 1] == s2[x - 1]? 0 : 1)
  48. };
  49. column[y] = std::min(possibilities);
  50. last_diagonal = old_diagonal;
  51. }
  52. }
  53. auto result = column[s1len];
  54. delete[] column;
  55. return result;
  56. }
  57. bool rathena::util::safe_addition( int64 a, int64 b, int64& result ){
  58. #if defined(__GNUC__) || defined(__clang__)
  59. return __builtin_add_overflow( a, b, &result );
  60. #else
  61. bool overflow = false;
  62. if( b < 0 ){
  63. if( a < ( INT64_MIN - b ) ){
  64. overflow = true;
  65. }
  66. }else{
  67. if( a > ( INT64_MAX - b ) ){
  68. overflow = true;
  69. }
  70. }
  71. result = a + b;
  72. return overflow;
  73. #endif
  74. }
  75. bool rathena::util::safe_substraction( int64 a, int64 b, int64& result ){
  76. #if defined(__GNUC__) || defined(__clang__)
  77. return __builtin_sub_overflow( a, b, &result );
  78. #else
  79. bool overflow = false;
  80. if( b < 0 ){
  81. if( a > ( INT64_MAX + b ) ){
  82. overflow = true;
  83. }
  84. }else{
  85. if( a < ( INT64_MIN + b ) ){
  86. overflow = true;
  87. }
  88. }
  89. result = a - b;
  90. return overflow;
  91. #endif
  92. }
  93. bool rathena::util::safe_multiplication( int64 a, int64 b, int64& result ){
  94. #if defined(__GNUC__) || defined(__clang__)
  95. return __builtin_mul_overflow( a, b, &result );
  96. #else
  97. result = a * b;
  98. if( a > 0 ){
  99. if( b > 0 ){
  100. return result < 0;
  101. }else if( b < 0 ){
  102. return result > 0;
  103. }
  104. }else if( a < 0 ){
  105. if( b > 0 ){
  106. return result > 0;
  107. }else if( b < 0 ){
  108. return result < 0;
  109. }
  110. }
  111. return false;
  112. #endif
  113. }