_tbb_hash_compare_impl.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // must be included outside namespaces.
  14. #ifndef __TBB_tbb_hash_compare_impl_H
  15. #define __TBB_tbb_hash_compare_impl_H
  16. #include <string>
  17. namespace tbb {
  18. namespace interface5 {
  19. namespace internal {
  20. // Template class for hash compare
  21. template<typename Key, typename Hasher, typename Key_equality>
  22. class hash_compare
  23. {
  24. public:
  25. typedef Hasher hasher;
  26. typedef Key_equality key_equal;
  27. hash_compare() {}
  28. hash_compare(Hasher a_hasher) : my_hash_object(a_hasher) {}
  29. hash_compare(Hasher a_hasher, Key_equality a_keyeq) : my_hash_object(a_hasher), my_key_compare_object(a_keyeq) {}
  30. size_t operator()(const Key& key) const {
  31. return ((size_t)my_hash_object(key));
  32. }
  33. bool operator()(const Key& key1, const Key& key2) const {
  34. // TODO: get rid of the result invertion
  35. return (!my_key_compare_object(key1, key2));
  36. }
  37. Hasher my_hash_object; // The hash object
  38. Key_equality my_key_compare_object; // The equality comparator object
  39. };
  40. //! Hash multiplier
  41. static const size_t hash_multiplier = tbb::internal::select_size_t_constant<2654435769U, 11400714819323198485ULL>::value;
  42. } // namespace internal
  43. //! Hasher functions
  44. template<typename T>
  45. __TBB_DEPRECATED_MSG("tbb::tbb_hasher is deprecated, use std::hash") inline size_t tbb_hasher( const T& t ) {
  46. return static_cast<size_t>( t ) * internal::hash_multiplier;
  47. }
  48. template<typename P>
  49. __TBB_DEPRECATED_MSG("tbb::tbb_hasher is deprecated, use std::hash") inline size_t tbb_hasher( P* ptr ) {
  50. size_t const h = reinterpret_cast<size_t>( ptr );
  51. return (h >> 3) ^ h;
  52. }
  53. template<typename E, typename S, typename A>
  54. __TBB_DEPRECATED_MSG("tbb::tbb_hasher is deprecated, use std::hash") inline size_t tbb_hasher( const std::basic_string<E,S,A>& s ) {
  55. size_t h = 0;
  56. for( const E* c = s.c_str(); *c; ++c )
  57. h = static_cast<size_t>(*c) ^ (h * internal::hash_multiplier);
  58. return h;
  59. }
  60. template<typename F, typename S>
  61. __TBB_DEPRECATED_MSG("tbb::tbb_hasher is deprecated, use std::hash") inline size_t tbb_hasher( const std::pair<F,S>& p ) {
  62. return tbb_hasher(p.first) ^ tbb_hasher(p.second);
  63. }
  64. } // namespace interface5
  65. using interface5::tbb_hasher;
  66. // Template class for hash compare
  67. template<typename Key>
  68. class __TBB_DEPRECATED_MSG("tbb::tbb_hash is deprecated, use std::hash") tbb_hash
  69. {
  70. public:
  71. tbb_hash() {}
  72. size_t operator()(const Key& key) const
  73. {
  74. return tbb_hasher(key);
  75. }
  76. };
  77. //! hash_compare that is default argument for concurrent_hash_map
  78. template<typename Key>
  79. struct tbb_hash_compare {
  80. static size_t hash( const Key& a ) { return tbb_hasher(a); }
  81. static bool equal( const Key& a, const Key& b ) { return a == b; }
  82. };
  83. } // namespace tbb
  84. #endif /* __TBB_tbb_hash_compare_impl_H */