/* * Copyright 2014 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 FRUIT_HASH_CODES_DEFN_H #define FRUIT_HASH_CODES_DEFN_H #include namespace fruit { namespace impl { template inline std::size_t hashTupleElement(const Tuple& x) { const auto& value = std::get(x); using T = typename std::remove_const::type>::type; return std::hash()(value); } template struct HashTupleHelper; template struct HashTupleHelper, last_index> { std::size_t operator()(const std::tuple<>&) { return 0; } }; template struct HashTupleHelper, 1> { std::size_t operator()(const std::tuple& x) { return hashTupleElement, 0>(x); } }; template struct HashTupleHelper, last_index> { std::size_t operator()(const std::tuple& x) { return combineHashes(hashTupleElement, last_index - 1>(x), HashTupleHelper, last_index - 1>()(x)); } }; template inline std::size_t hashTuple(const std::tuple& x) { return HashTupleHelper, sizeof...(Args)>()(x); } inline std::size_t combineHashes(std::size_t h1, std::size_t h2) { h1 ^= h2 + 0x9e3779b9 + (h1 << 6) + (h1 >> 2); return h1; } } // namespace impl } // namespace fruit #endif // FRUIT_HASH_CODES_DEFN_H