hash.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2015 Google Inc. All rights reserved.
  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 FLATBUFFERS_HASH_H_
  17. #define FLATBUFFERS_HASH_H_
  18. #include <cstdint>
  19. #include <cstring>
  20. #include "flatbuffers/flatbuffers.h"
  21. namespace flatbuffers {
  22. template <typename T>
  23. struct FnvTraits {
  24. static const T kFnvPrime;
  25. static const T kOffsetBasis;
  26. };
  27. template <>
  28. struct FnvTraits<uint32_t> {
  29. static const uint32_t kFnvPrime = 0x01000193;
  30. static const uint32_t kOffsetBasis = 0x811C9DC5;
  31. };
  32. template <>
  33. struct FnvTraits<uint64_t> {
  34. static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
  35. static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
  36. };
  37. template <typename T>
  38. T HashFnv1(const char *input) {
  39. T hash = FnvTraits<T>::kOffsetBasis;
  40. for (const char *c = input; *c; ++c) {
  41. hash *= FnvTraits<T>::kFnvPrime;
  42. hash ^= static_cast<unsigned char>(*c);
  43. }
  44. return hash;
  45. }
  46. template <typename T>
  47. T HashFnv1a(const char *input) {
  48. T hash = FnvTraits<T>::kOffsetBasis;
  49. for (const char *c = input; *c; ++c) {
  50. hash ^= static_cast<unsigned char>(*c);
  51. hash *= FnvTraits<T>::kFnvPrime;
  52. }
  53. return hash;
  54. }
  55. template <typename T>
  56. struct NamedHashFunction {
  57. const char *name;
  58. typedef T (*HashFunction)(const char*);
  59. HashFunction function;
  60. };
  61. const NamedHashFunction<uint32_t> kHashFunctions32[] = {
  62. { "fnv1_32", HashFnv1<uint32_t> },
  63. { "fnv1a_32", HashFnv1a<uint32_t> },
  64. };
  65. const NamedHashFunction<uint64_t> kHashFunctions64[] = {
  66. { "fnv1_64", HashFnv1<uint64_t> },
  67. { "fnv1a_64", HashFnv1a<uint64_t> },
  68. };
  69. inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
  70. const char *name) {
  71. std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
  72. for (std::size_t i = 0; i < size; ++i) {
  73. if (std::strcmp(name, kHashFunctions32[i].name) == 0) {
  74. return kHashFunctions32[i].function;
  75. }
  76. }
  77. return nullptr;
  78. }
  79. inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(
  80. const char *name) {
  81. std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
  82. for (std::size_t i = 0; i < size; ++i) {
  83. if (std::strcmp(name, kHashFunctions64[i].name) == 0) {
  84. return kHashFunctions64[i].function;
  85. }
  86. }
  87. return nullptr;
  88. }
  89. } // namespace flatbuffers
  90. #endif // FLATBUFFERS_HASH_H_