/* * Copyright 2015 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef FLATBUFFERS_HASH_H_ #define FLATBUFFERS_HASH_H_ #include #include #include "flatbuffers/flatbuffers.h" namespace flatbuffers { template struct FnvTraits { static const T kFnvPrime; static const T kOffsetBasis; }; template <> struct FnvTraits { static const uint32_t kFnvPrime = 0x01000193; static const uint32_t kOffsetBasis = 0x811C9DC5; }; template <> struct FnvTraits { static const uint64_t kFnvPrime = 0x00000100000001b3ULL; static const uint64_t kOffsetBasis = 0xcbf29ce484222645ULL; }; template T HashFnv1(const char *input) { T hash = FnvTraits::kOffsetBasis; for (const char *c = input; *c; ++c) { hash *= FnvTraits::kFnvPrime; hash ^= static_cast(*c); } return hash; } template T HashFnv1a(const char *input) { T hash = FnvTraits::kOffsetBasis; for (const char *c = input; *c; ++c) { hash ^= static_cast(*c); hash *= FnvTraits::kFnvPrime; } return hash; } template struct NamedHashFunction { const char *name; typedef T (*HashFunction)(const char*); HashFunction function; }; const NamedHashFunction kHashFunctions32[] = { { "fnv1_32", HashFnv1 }, { "fnv1a_32", HashFnv1a }, }; const NamedHashFunction kHashFunctions64[] = { { "fnv1_64", HashFnv1 }, { "fnv1a_64", HashFnv1a }, }; inline NamedHashFunction::HashFunction FindHashFunction32( const char *name) { std::size_t size = sizeof(kHashFunctions32) / sizeof(kHashFunctions32[0]); for (std::size_t i = 0; i < size; ++i) { if (std::strcmp(name, kHashFunctions32[i].name) == 0) { return kHashFunctions32[i].function; } } return nullptr; } inline NamedHashFunction::HashFunction FindHashFunction64( const char *name) { std::size_t size = sizeof(kHashFunctions64) / sizeof(kHashFunctions64[0]); for (std::size_t i = 0; i < size; ++i) { if (std::strcmp(name, kHashFunctions64[i].name) == 0) { return kHashFunctions64[i].function; } } return nullptr; } } // namespace flatbuffers #endif // FLATBUFFERS_HASH_H_