43 lines
1.4 KiB
C++
43 lines
1.4 KiB
C++
// Copyright (c) 2023 PaddlePaddle Authors. 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.
|
|
|
|
#pragma once
|
|
|
|
inline void HashCombine(std::size_t* seed) {}
|
|
|
|
// combine hash value
|
|
// https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x
|
|
template <typename T, typename... Rest>
|
|
inline void HashCombine(std::size_t* seed, const T& v, Rest... rest) {
|
|
std::hash<T> hasher;
|
|
*seed ^= hasher(v) + 0x9e3779b9 + (*seed << 6) + (*seed >> 2);
|
|
*seed *= 0x00000100000001B3;
|
|
HashCombine(seed, rest...);
|
|
}
|
|
|
|
// custom specialization of std::hash can be injected in namespace std
|
|
// ref: https://en.cppreference.com/w/cpp/utility/hash
|
|
namespace std {
|
|
template <typename T>
|
|
struct hash<std::vector<T>> {
|
|
std::size_t operator()(std::vector<T> const& vec) const noexcept {
|
|
std::size_t seed = 0xcbf29ce484222325;
|
|
for (auto val : vec) {
|
|
HashCombine(&seed, val);
|
|
}
|
|
return seed;
|
|
}
|
|
};
|
|
} // namespace std
|