tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_Hash.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11#pragma once
12
13//==============================================================================
14/*
15 These hash functions are from boost and so the boost license agreement and
16 attribution are included here:
17*/
18//==============================================================================
19/*
20 Boost Software License - Version 1.0 - August 17th, 2003
21
22 Permission is hereby granted, free of charge, to any person or organization
23 obtaining a copy of the software and accompanying documentation covered by
24 this license (the "Software") to use, reproduce, display, distribute,
25 execute, and transmit the Software, and to prepare derivative works of the
26 Software, and to permit third-parties to whom the Software is furnished to
27 do so, all subject to the following:
28
29 The copyright notices in the Software and this entire statement, including
30 the above license grant, this restriction and the following disclaimer,
31 must be included in all copies of the Software, in whole or in part, and
32 all derivative works of the Software, unless such copies or derivative
33 works are solely in the form of machine-executable object code generated by
34 a source language processor.
35
36 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
39 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
40 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
41 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
42 DEALINGS IN THE SOFTWARE.
43*/
44//==============================================================================
45
46#include <cstddef>
47#include <functional>
48
49namespace tracktion { inline namespace core
50{
51
52//==============================================================================
53//==============================================================================
55template<typename T>
56void hash_combine (size_t& seed, const T& v)
57{
58 static_assert (! std::is_pointer_v<T>, "Using a pointer here is almost certainly incorrect as it will change on each run");
59 seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
60}
61
63template<typename It>
64[[ nodiscard ]] std::size_t hash_range (It first, It last)
65{
66 size_t seed = 0;
67
68 for (; first != last; ++first)
69 hash_combine (seed, *first);
70
71 return seed;
72}
73
75template<typename It>
76void hash_range (std::size_t& seed, It first, It last)
77{
78 for (; first != last; ++first)
79 hash_combine (seed, *first);
80}
81
83template<typename Container>
84[[ nodiscard ]] std::size_t hash_range (const Container& container)
85{
86 return hash_range (container.begin(), container.end());
87}
88
90template<typename T>
91[[ nodiscard ]] size_t hash (size_t seed, const T& v)
92{
93 hash_combine (seed, v);
94 return seed;
95}
96
97}}
T is_pointer_v
void hash_combine(size_t &seed, const T &v)
Hashes a type with a given seed, modifying the seed.
std::size_t hash_range(It first, It last)
Hashes a range with a default seed and returns the new hash value.
size_t hash(size_t seed, const T &v)
Hashes a type with a given seed and returns the new hash value.