blob: a97b231525ed4ac412f1425810c23272ad7b2c28 [file] [log] [blame]
Alex Amesd5753212015-02-13 15:58:29 -08001/*
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
17#ifndef FLATBUFFERS_HASH_H_
18#define FLATBUFFERS_HASH_H_
19
20#include <cstdint>
21#include <cstring>
22
Chandra Penkeb63ebad2016-01-06 08:31:53 -080023#include "flatbuffers/flatbuffers.h"
24
Alex Amesd5753212015-02-13 15:58:29 -080025namespace flatbuffers {
26
Wouter van Oortmerssen89711c92017-12-21 10:54:28 -080027template<typename T> struct FnvTraits {
Alex Amesd5753212015-02-13 15:58:29 -080028 static const T kFnvPrime;
29 static const T kOffsetBasis;
30};
31
Wouter van Oortmerssen89711c92017-12-21 10:54:28 -080032template<> struct FnvTraits<uint32_t> {
Alex Amesd5753212015-02-13 15:58:29 -080033 static const uint32_t kFnvPrime = 0x01000193;
34 static const uint32_t kOffsetBasis = 0x811C9DC5;
35};
36
Wouter van Oortmerssen89711c92017-12-21 10:54:28 -080037template<> struct FnvTraits<uint64_t> {
Chandra Penkeb63ebad2016-01-06 08:31:53 -080038 static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
39 static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
Alex Amesd5753212015-02-13 15:58:29 -080040};
41
Christian Helmichec74f582018-03-03 02:13:55 +090042template<typename T> FLATBUFFERS_CONSTEXPR_CPP14 T HashFnv1(const char *input) {
Alex Amesd5753212015-02-13 15:58:29 -080043 T hash = FnvTraits<T>::kOffsetBasis;
44 for (const char *c = input; *c; ++c) {
45 hash *= FnvTraits<T>::kFnvPrime;
46 hash ^= static_cast<unsigned char>(*c);
47 }
48 return hash;
49}
50
Christian Helmichec74f582018-03-03 02:13:55 +090051template<typename T> FLATBUFFERS_CONSTEXPR_CPP14 T HashFnv1a(const char *input) {
Alex Amesd5753212015-02-13 15:58:29 -080052 T hash = FnvTraits<T>::kOffsetBasis;
53 for (const char *c = input; *c; ++c) {
54 hash ^= static_cast<unsigned char>(*c);
55 hash *= FnvTraits<T>::kFnvPrime;
56 }
57 return hash;
58}
59
Christian Helmichec74f582018-03-03 02:13:55 +090060template <> FLATBUFFERS_CONSTEXPR_CPP14 inline uint16_t HashFnv1<uint16_t>(const char *input) {
Christian Helmich6e2e9092018-02-24 03:29:37 +090061 uint32_t hash = HashFnv1<uint32_t>(input);
62 return (hash >> 16) ^ (hash & 0xffff);
63}
64
Christian Helmichec74f582018-03-03 02:13:55 +090065template <> FLATBUFFERS_CONSTEXPR_CPP14 inline uint16_t HashFnv1a<uint16_t>(const char *input) {
Christian Helmich6e2e9092018-02-24 03:29:37 +090066 uint32_t hash = HashFnv1a<uint32_t>(input);
67 return (hash >> 16) ^ (hash & 0xffff);
68}
69
70template <typename T> struct NamedHashFunction {
Alex Amesd5753212015-02-13 15:58:29 -080071 const char *name;
72
Wouter van Oortmerssen89711c92017-12-21 10:54:28 -080073 typedef T (*HashFunction)(const char *);
Alex Amesd5753212015-02-13 15:58:29 -080074 HashFunction function;
75};
76
Christian Helmich6e2e9092018-02-24 03:29:37 +090077const NamedHashFunction<uint16_t> kHashFunctions16[] = {
78 { "fnv1_16", HashFnv1<uint16_t> },
79 { "fnv1a_16", HashFnv1a<uint16_t> },
80};
81
Alex Amesd5753212015-02-13 15:58:29 -080082const NamedHashFunction<uint32_t> kHashFunctions32[] = {
Wouter van Oortmerssen89711c92017-12-21 10:54:28 -080083 { "fnv1_32", HashFnv1<uint32_t> },
Alex Amesd5753212015-02-13 15:58:29 -080084 { "fnv1a_32", HashFnv1a<uint32_t> },
85};
86
87const NamedHashFunction<uint64_t> kHashFunctions64[] = {
Wouter van Oortmerssen89711c92017-12-21 10:54:28 -080088 { "fnv1_64", HashFnv1<uint64_t> },
Alex Amesd5753212015-02-13 15:58:29 -080089 { "fnv1a_64", HashFnv1a<uint64_t> },
90};
91
Christian Helmich6e2e9092018-02-24 03:29:37 +090092inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(
93 const char *name) {
94 std::size_t size = sizeof(kHashFunctions16) / sizeof(kHashFunctions16[0]);
95 for (std::size_t i = 0; i < size; ++i) {
96 if (std::strcmp(name, kHashFunctions16[i].name) == 0) {
97 return kHashFunctions16[i].function;
98 }
99 }
100 return nullptr;
101}
102
Alex Amesd5753212015-02-13 15:58:29 -0800103inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
104 const char *name) {
105 std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
106 for (std::size_t i = 0; i < size; ++i) {
107 if (std::strcmp(name, kHashFunctions32[i].name) == 0) {
108 return kHashFunctions32[i].function;
109 }
110 }
111 return nullptr;
112}
113
114inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(
115 const char *name) {
116 std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
117 for (std::size_t i = 0; i < size; ++i) {
118 if (std::strcmp(name, kHashFunctions64[i].name) == 0) {
119 return kHashFunctions64[i].function;
120 }
121 }
122 return nullptr;
123}
124
125} // namespace flatbuffers
126
127#endif // FLATBUFFERS_HASH_H_