OR-Tools  9.6
hash.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #include "ortools/base/hash.h"
15 
16 namespace operations_research {
17 
18 // This is a copy of the code from https://github.com/ztanml/fast-hash
19 // We include it here for build simplicity.
20 //
21 // We have made 2 modification:
22 // - replace the #define with an inline method to fix compilation on windows.
23 // - rename mix into mix_internal and hide inside a unnamed namespace.
24 
25 namespace {
26 // Compression function for Merkle-Damgard construction.
27 // This function is generated using the framework provided.
28 inline uint64_t mix_internal(uint64_t h) {
29  h ^= h >> 23;
30  h *= 0x2127599bf4325c37ULL;
31  h ^= h >> 47;
32  return h;
33 }
34 } // namespace
35 
36 uint64_t fasthash64(const void* buf, size_t len, uint64_t seed) {
37  const uint64_t m = 0x880355f21e6d1965ULL;
38  const uint64_t* pos = (const uint64_t*)buf;
39  const uint64_t* end = pos + (len / 8);
40  const unsigned char* pos2;
41  uint64_t h = seed ^ (len * m);
42  uint64_t v;
43 
44  while (pos != end) {
45  v = *pos++;
46  h ^= mix_internal(v);
47  h *= m;
48  }
49 
50  pos2 = (const unsigned char*)pos;
51  v = 0;
52 
53  switch (len & 7) {
54  case 7:
55  v ^= (uint64_t)pos2[6] << 48;
56  case 6:
57  v ^= (uint64_t)pos2[5] << 40;
58  case 5:
59  v ^= (uint64_t)pos2[4] << 32;
60  case 4:
61  v ^= (uint64_t)pos2[3] << 24;
62  case 3:
63  v ^= (uint64_t)pos2[2] << 16;
64  case 2:
65  v ^= (uint64_t)pos2[1] << 8;
66  case 1:
67  v ^= (uint64_t)pos2[0];
68  h ^= mix_internal(v);
69  h *= m;
70  }
71 
72  return mix_internal(h);
73 }
74 
75 } // namespace operations_research
Collection of objects used to extend the Constraint Solver library.
uint64_t fasthash64(const void *buf, size_t len, uint64_t seed)
Definition: hash.cc:36
std::optional< int64_t > end