Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:30:26

0001 /*
0002  * Copyright 2015 Google Inc. All rights reserved.
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *     http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  */
0016 
0017 #ifndef FLATBUFFERS_HASH_H_
0018 #define FLATBUFFERS_HASH_H_
0019 
0020 #include <cstdint>
0021 #include <cstring>
0022 
0023 #include "flatbuffers/flatbuffers.h"
0024 
0025 namespace flatbuffers {
0026 
0027 template<typename T> struct FnvTraits {
0028   static const T kFnvPrime;
0029   static const T kOffsetBasis;
0030 };
0031 
0032 template<> struct FnvTraits<uint32_t> {
0033   static const uint32_t kFnvPrime = 0x01000193;
0034   static const uint32_t kOffsetBasis = 0x811C9DC5;
0035 };
0036 
0037 template<> struct FnvTraits<uint64_t> {
0038   static const uint64_t kFnvPrime = 0x00000100000001b3ULL;
0039   static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL;
0040 };
0041 
0042 template<typename T> T HashFnv1(const char *input) {
0043   T hash = FnvTraits<T>::kOffsetBasis;
0044   for (const char *c = input; *c; ++c) {
0045     hash *= FnvTraits<T>::kFnvPrime;
0046     hash ^= static_cast<unsigned char>(*c);
0047   }
0048   return hash;
0049 }
0050 
0051 template<typename T> T HashFnv1a(const char *input) {
0052   T hash = FnvTraits<T>::kOffsetBasis;
0053   for (const char *c = input; *c; ++c) {
0054     hash ^= static_cast<unsigned char>(*c);
0055     hash *= FnvTraits<T>::kFnvPrime;
0056   }
0057   return hash;
0058 }
0059 
0060 template<> inline uint16_t HashFnv1<uint16_t>(const char *input) {
0061   uint32_t hash = HashFnv1<uint32_t>(input);
0062   return (hash >> 16) ^ (hash & 0xffff);
0063 }
0064 
0065 template<> inline uint16_t HashFnv1a<uint16_t>(const char *input) {
0066   uint32_t hash = HashFnv1a<uint32_t>(input);
0067   return (hash >> 16) ^ (hash & 0xffff);
0068 }
0069 
0070 template<typename T> struct NamedHashFunction {
0071   const char *name;
0072 
0073   typedef T (*HashFunction)(const char *);
0074   HashFunction function;
0075 };
0076 
0077 const NamedHashFunction<uint16_t> kHashFunctions16[] = {
0078   { "fnv1_16", HashFnv1<uint16_t> },
0079   { "fnv1a_16", HashFnv1a<uint16_t> },
0080 };
0081 
0082 const NamedHashFunction<uint32_t> kHashFunctions32[] = {
0083   { "fnv1_32", HashFnv1<uint32_t> },
0084   { "fnv1a_32", HashFnv1a<uint32_t> },
0085 };
0086 
0087 const NamedHashFunction<uint64_t> kHashFunctions64[] = {
0088   { "fnv1_64", HashFnv1<uint64_t> },
0089   { "fnv1a_64", HashFnv1a<uint64_t> },
0090 };
0091 
0092 inline NamedHashFunction<uint16_t>::HashFunction FindHashFunction16(
0093     const char *name) {
0094   std::size_t size = sizeof(kHashFunctions16) / sizeof(kHashFunctions16[0]);
0095   for (std::size_t i = 0; i < size; ++i) {
0096     if (std::strcmp(name, kHashFunctions16[i].name) == 0) {
0097       return kHashFunctions16[i].function;
0098     }
0099   }
0100   return nullptr;
0101 }
0102 
0103 inline NamedHashFunction<uint32_t>::HashFunction FindHashFunction32(
0104     const char *name) {
0105   std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]);
0106   for (std::size_t i = 0; i < size; ++i) {
0107     if (std::strcmp(name, kHashFunctions32[i].name) == 0) {
0108       return kHashFunctions32[i].function;
0109     }
0110   }
0111   return nullptr;
0112 }
0113 
0114 inline NamedHashFunction<uint64_t>::HashFunction FindHashFunction64(
0115     const char *name) {
0116   std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]);
0117   for (std::size_t i = 0; i < size; ++i) {
0118     if (std::strcmp(name, kHashFunctions64[i].name) == 0) {
0119       return kHashFunctions64[i].function;
0120     }
0121   }
0122   return nullptr;
0123 }
0124 
0125 }  // namespace flatbuffers
0126 
0127 #endif  // FLATBUFFERS_HASH_H_