Line |
Branch |
Exec |
Source |
1 |
|
|
// Copyright (c) 2021-2025 ChilliBits. All rights reserved. |
2 |
|
|
|
3 |
|
|
#include "CustomHashFunctions.h" |
4 |
|
|
|
5 |
|
|
#include <numeric> |
6 |
|
|
|
7 |
|
|
namespace std { |
8 |
|
|
|
9 |
|
4831247 |
size_t hash<spice::compiler::TypeChainElement>::operator()(const spice::compiler::TypeChainElement &tce) const noexcept { |
10 |
|
|
// Hasher for QualTypeList |
11 |
|
330657 |
constexpr auto pred = [](const size_t acc, const spice::compiler::QualType &val) { |
12 |
|
|
// Combine the previous hash value with the current element's hash, adjusted by a prime number to reduce collisions |
13 |
|
330657 |
return acc * 31 + std::hash<spice::compiler::QualType>{}(val); |
14 |
|
|
}; |
15 |
|
|
// Hash all fields |
16 |
|
4831247 |
const size_t hashSuperType = std::hash<spice::compiler::SuperType>{}(tce.superType); |
17 |
|
4831247 |
const size_t hashSubType = std::hash<std::string>{}(tce.subType) << 1; |
18 |
|
4831247 |
const size_t hashTypeId = std::hash<uint64_t>{}(tce.typeId) << 2; |
19 |
|
4831247 |
const size_t hashData = std::hash<spice::compiler::Scope *>{}(tce.data.bodyScope) << 3; |
20 |
|
4831247 |
const size_t hashTemplateTypes = accumulate(tce.templateTypes.begin(), tce.templateTypes.end(), 0u, pred) << 4; |
21 |
|
4831247 |
const size_t hashParamTypes = accumulate(tce.paramTypes.begin(), tce.paramTypes.end(), 0u, pred) << 5; |
22 |
|
4831247 |
return hashSuperType ^ hashSubType ^ hashTypeId ^ hashData ^ hashTemplateTypes ^ hashParamTypes; |
23 |
|
|
} |
24 |
|
|
|
25 |
|
4802800 |
size_t hash<spice::compiler::Type>::operator()(const spice::compiler::Type &t) const noexcept { |
26 |
|
4831247 |
const auto pred = [](const size_t acc, const spice::compiler::TypeChainElement &val) { |
27 |
|
|
// Combine the previous hash value with the current element's hash, adjusted by a prime number to reduce collisions |
28 |
|
4831247 |
return acc * 31 + std::hash<spice::compiler::TypeChainElement>{}(val); |
29 |
|
|
}; |
30 |
|
4802800 |
return accumulate(t.typeChain.begin(), t.typeChain.end(), 0u, pred); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
515450 |
size_t hash<spice::compiler::TypeQualifiers>::operator()(const spice::compiler::TypeQualifiers &qualifiers) const noexcept { |
34 |
|
515450 |
const size_t hashConst = std::hash<bool>{}(qualifiers.isConst); |
35 |
|
515450 |
const size_t hashSigned = std::hash<bool>{}(qualifiers.isSigned) << 1; |
36 |
|
515450 |
const size_t hashUnsigned = std::hash<bool>{}(qualifiers.isUnsigned) << 2; |
37 |
|
515450 |
const size_t hashHeap = std::hash<bool>{}(qualifiers.isHeap) << 3; |
38 |
|
515450 |
const size_t hashPublic = std::hash<bool>{}(qualifiers.isPublic) << 4; |
39 |
|
515450 |
const size_t hashInline = std::hash<bool>{}(qualifiers.isInline) << 5; |
40 |
|
515450 |
const size_t hashComposition = std::hash<bool>{}(qualifiers.isComposition) << 6; |
41 |
|
515450 |
return hashConst ^ hashSigned ^ hashUnsigned ^ hashHeap ^ hashPublic ^ hashInline ^ hashComposition; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
515450 |
size_t hash<spice::compiler::QualType>::operator()(const spice::compiler::QualType &qualType) const noexcept { |
45 |
|
515450 |
const size_t hashType = std::hash<const spice::compiler::Type *>{}(qualType.getType()); |
46 |
|
515450 |
spice::compiler::TypeQualifiers qualifiers = qualType.getQualifiers(); |
47 |
|
515450 |
qualifiers.isPublic = false; // Ignore the public qualifier for hashing |
48 |
|
515450 |
const size_t hashQualifiers = std::hash<spice::compiler::TypeQualifiers>{}(qualifiers) << 1; |
49 |
|
515450 |
return hashType ^ hashQualifiers; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
} // namespace std |
53 |
|
|
|