Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
HashFunction.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* HashFunction.cc (C) 2000-2023 */
9/* */
10/* Hash function. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/HashFunction.h"
15#include "arcane/utils/String.h"
16
17#include <cstdint>
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28// MurmurHash2, 64-bit versions, by Austin Appleby
29
30namespace
31{
32 uint64_t _MurmurHash64A(const void* key, uint64_t len, unsigned int seed)
33 {
34 const uint64_t m = 0xc6a4a7935bd1e995;
35 const int r = 47;
36
37 uint64_t h = seed ^ (len * m);
38
39 const uint64_t* data = (const uint64_t*)key;
40 const uint64_t* end = data + (len / 8);
41
42 while (data != end) {
43 uint64_t k = *data++;
44
45 k *= m;
46 k ^= k >> r;
47 k *= m;
48
49 h ^= k;
50 h *= m;
51 }
52
53 const unsigned char* data2 = (const unsigned char*)data;
54
55 switch (len & 7) {
56 case 7:
57 h ^= uint64_t(data2[6]) << 48;
58 [[fallthrough]];
59 case 6:
60 h ^= uint64_t(data2[5]) << 40;
61 [[fallthrough]];
62 case 5:
63 h ^= uint64_t(data2[4]) << 32;
64 [[fallthrough]];
65 case 4:
66 h ^= uint64_t(data2[3]) << 24;
67 [[fallthrough]];
68 case 3:
69 h ^= uint64_t(data2[2]) << 16;
70 [[fallthrough]];
71 case 2:
72 h ^= uint64_t(data2[1]) << 8;
73 [[fallthrough]];
74 case 1:
75 h ^= uint64_t(data2[0]);
76 h *= m;
77 };
78
79 h ^= h >> r;
80 h *= m;
81 h ^= h >> r;
82
83 return h;
84 }
85} // namespace
86
87/*---------------------------------------------------------------------------*/
88/*---------------------------------------------------------------------------*/
89
92{
93 Span<const Byte> bytes(str.bytes());
94 return _MurmurHash64A(bytes.data(), bytes.size(), 0x424);
95}
96
97/*---------------------------------------------------------------------------*/
98/*---------------------------------------------------------------------------*/
99
100} // namespace Arcane
101
102/*---------------------------------------------------------------------------*/
103/*---------------------------------------------------------------------------*/
View of an array of elements of type T.
Definition Span.h:635
View of a UTF-8 character string.
Definition StringView.h:44
Integer len(const char *s)
Returns the length of the string s.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.