Files
ortools-clone/ortools/base/hash.h

87 lines
1.8 KiB
C
Raw Permalink Normal View History

2025-01-10 11:35:44 +01:00
// Copyright 2010-2025 Google LLC
2010-09-15 12:42:33 +00:00
// 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.
2025-11-05 11:34:49 +01:00
#ifndef ORTOOLS_BASE_HASH_H_
#define ORTOOLS_BASE_HASH_H_
2010-09-15 12:42:33 +00:00
2015-02-04 09:53:26 +00:00
#include <array>
2023-08-25 03:51:16 +02:00
#include <cstdint>
#include <string>
#include <utility>
2010-09-15 12:42:33 +00:00
// In SWIG mode, we don't want anything besides these top-level includes.
2014-04-04 09:11:49 +00:00
#if !defined(SWIG)
2010-09-15 12:42:33 +00:00
namespace operations_research {
uint64_t fasthash64(const void* buf, size_t len, uint64_t seed);
2010-09-15 12:42:33 +00:00
2013-01-09 11:09:38 +00:00
// 64 bit version.
2021-04-01 12:13:35 +02:00
static inline void mix(uint64_t& a, uint64_t& b, uint64_t& c) { // NOLINT
2014-01-08 12:01:58 +00:00
a -= b;
a -= c;
a ^= (c >> 43);
b -= c;
b -= a;
b ^= (a << 9);
c -= a;
c -= b;
c ^= (b >> 8);
a -= b;
a -= c;
a ^= (c >> 38);
b -= c;
b -= a;
b ^= (a << 23);
c -= a;
c -= b;
c ^= (b >> 5);
a -= b;
a -= c;
a ^= (c >> 35);
b -= c;
b -= a;
b ^= (a << 49);
c -= a;
c -= b;
c ^= (b >> 11);
a -= b;
a -= c;
a ^= (c >> 12);
b -= c;
b -= a;
b ^= (a << 18);
c -= a;
c -= b;
c ^= (b >> 22);
2010-09-15 12:42:33 +00:00
}
2020-10-22 23:36:58 +02:00
} // namespace operations_research
2010-09-15 12:42:33 +00:00
2020-10-22 23:36:58 +02:00
#endif // SWIG
2018-07-12 17:50:11 -07:00
namespace util_hash {
2021-04-01 12:13:35 +02:00
inline uint64_t Hash(uint64_t num, uint64_t c) {
uint64_t b = uint64_t{0xe08c1d668b756f82}; // More of the golden ratio.
2018-07-12 17:50:11 -07:00
operations_research::mix(num, b, c);
return c;
}
2021-04-01 12:13:35 +02:00
inline uint64_t Hash(uint64_t a, uint64_t b, uint64_t c) {
operations_research::mix(a, b, c);
return c;
}
2020-10-22 23:36:58 +02:00
} // namespace util_hash
2025-11-05 11:34:49 +01:00
#endif // ORTOOLS_BASE_HASH_H_