You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hash_util.hxx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2023 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RSPAMD_HASH_UTIL_HXX
  17. #define RSPAMD_HASH_UTIL_HXX
  18. #pragma once
  19. #include <string_view>
  20. #include <string>
  21. #include "contrib/ankerl/unordered_dense.h"
  22. namespace rspamd {
  23. /*
  24. * Transparent smart pointers hashing
  25. */
  26. template<typename T>
  27. struct smart_ptr_equal {
  28. using is_transparent = void; /* We want to find values in a set of shared_ptr by reference */
  29. auto operator()(const std::shared_ptr<T> &a, const std::shared_ptr<T> &b) const
  30. {
  31. return (*a) == (*b);
  32. }
  33. auto operator()(const std::shared_ptr<T> &a, const T &b) const
  34. {
  35. return (*a) == b;
  36. }
  37. auto operator()(const T &a, const std::shared_ptr<T> &b) const
  38. {
  39. return a == (*b);
  40. }
  41. auto operator()(const std::unique_ptr<T> &a, const std::unique_ptr<T> &b) const
  42. {
  43. return (*a) == (*b);
  44. }
  45. auto operator()(const std::unique_ptr<T> &a, const T &b) const
  46. {
  47. return (*a) == b;
  48. }
  49. auto operator()(const T &a, const std::unique_ptr<T> &b) const
  50. {
  51. return a == (*b);
  52. }
  53. };
  54. template<typename T>
  55. struct smart_ptr_hash {
  56. using is_transparent = void; /* We want to find values in a set of shared_ptr by reference */
  57. using is_avalanching = void;
  58. auto operator()(const std::shared_ptr<T> &a) const
  59. {
  60. return std::hash<T>()(*a);
  61. }
  62. auto operator()(const std::unique_ptr<T> &a) const
  63. {
  64. return std::hash<T>()(*a);
  65. }
  66. auto operator()(const T &a) const
  67. {
  68. return std::hash<T>()(a);
  69. }
  70. };
  71. /* Enable lookup by string view */
  72. struct smart_str_equal {
  73. using is_transparent = void;
  74. auto operator()(const std::string &a, const std::string &b) const
  75. {
  76. return a == b;
  77. }
  78. auto operator()(const std::string_view &a, const std::string &b) const
  79. {
  80. return a == b;
  81. }
  82. auto operator()(const std::string &a, const std::string_view &b) const
  83. {
  84. return a == b;
  85. }
  86. };
  87. struct smart_str_hash {
  88. using is_transparent = void;
  89. using is_avalanching = void;
  90. auto operator()(const std::string &a) const
  91. {
  92. return ankerl::unordered_dense::hash<std::string>()(a);
  93. }
  94. auto operator()(const std::string_view &a) const
  95. {
  96. return ankerl::unordered_dense::hash<std::string_view>()(a);
  97. }
  98. };
  99. }// namespace rspamd
  100. #endif//RSPAMD_HASH_UTIL_HXX