1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QHASHFUNCTIONS_H
42#define QHASHFUNCTIONS_H
43
44#include <QtCore/qstring.h>
45#include <QtCore/qpair.h>
46
47#include <numeric> // for std::accumulate
48#include <functional> // for std::hash
49
50#if 0
51#pragma qt_class(QHashFunctions)
52#endif
53
54#if defined(Q_CC_MSVC)
55#pragma warning( push )
56#pragma warning( disable : 4311 ) // disable pointer truncation warning
57#pragma warning( disable : 4127 ) // conditional expression is constant
58#endif
59
60QT_BEGIN_NAMESPACE
61
62class QBitArray;
63class QByteArray;
64class QString;
65class QStringRef;
66class QLatin1String;
67
68Q_CORE_EXPORT int qGlobalQHashSeed();
69Q_CORE_EXPORT void qSetGlobalQHashSeed(int newSeed);
70
71Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHashBits(const void *p, size_t size, uint seed = 0) noexcept;
72
73Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(char key, uint seed = 0) noexcept { return uint(key) ^ seed; }
74Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(uchar key, uint seed = 0) noexcept { return uint(key) ^ seed; }
75Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(signed char key, uint seed = 0) noexcept { return uint(key) ^ seed; }
76Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(ushort key, uint seed = 0) noexcept { return uint(key) ^ seed; }
77Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(short key, uint seed = 0) noexcept { return uint(key) ^ seed; }
78Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(uint key, uint seed = 0) noexcept { return key ^ seed; }
79Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(int key, uint seed = 0) noexcept { return uint(key) ^ seed; }
80Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(ulong key, uint seed = 0) noexcept
81{
82 return (sizeof(ulong) > sizeof(uint))
83 ? (uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed)
84 : (uint(key & (~0U)) ^ seed);
85}
86Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(long key, uint seed = 0) noexcept { return qHash(ulong(key), seed); }
87Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(quint64 key, uint seed = 0) noexcept
88{
89 return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed;
90}
91Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(qint64 key, uint seed = 0) noexcept { return qHash(quint64(key), seed); }
92Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(float key, uint seed = 0) noexcept;
93Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(double key, uint seed = 0) noexcept;
94#if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC)
95Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(long double key, uint seed = 0) noexcept;
96#endif
97Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(const QChar key, uint seed = 0) noexcept { return qHash(key.unicode(), seed); }
98Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QByteArray &key, uint seed = 0) noexcept;
99#if QT_STRINGVIEW_LEVEL < 2
100Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QString &key, uint seed = 0) noexcept;
101Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QStringRef &key, uint seed = 0) noexcept;
102#endif
103Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QStringView key, uint seed = 0) noexcept;
104Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QBitArray &key, uint seed = 0) noexcept;
105Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QLatin1String key, uint seed = 0) noexcept;
106Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) noexcept;
107
108Q_DECL_CONST_FUNCTION inline uint qHash(std::nullptr_t, uint seed = 0) noexcept
109{
110 return qHash(reinterpret_cast<quintptr>(nullptr), seed);
111}
112
113template <class T> inline uint qHash(const T *key, uint seed = 0) noexcept
114{
115 return qHash(reinterpret_cast<quintptr>(key), seed);
116}
117template<typename T> inline uint qHash(const T &t, uint seed)
118 noexcept(noexcept(qHash(t)))
119{ return qHash(t) ^ seed; }
120
121namespace QtPrivate {
122
123struct QHashCombine {
124 typedef uint result_type;
125 template <typename T>
126 Q_DECL_CONSTEXPR result_type operator()(uint seed, const T &t) const noexcept(noexcept(qHash(t)))
127 // combiner taken from N3876 / boost::hash_combine
128 { return seed ^ (qHash(t) + 0x9e3779b9 + (seed << 6) + (seed >> 2)) ; }
129};
130
131struct QHashCombineCommutative {
132 // QHashCombine is a good hash combiner, but is not commutative,
133 // ie. it depends on the order of the input elements. That is
134 // usually what we want: {0,1,3} should hash differently than
135 // {1,3,0}. Except when it isn't (e.g. for QSet and
136 // QHash). Therefore, provide a commutative combiner, too.
137 typedef uint result_type;
138 template <typename T>
139 Q_DECL_CONSTEXPR result_type operator()(uint seed, const T &t) const noexcept(noexcept(qHash(t)))
140 { return seed + qHash(t); } // don't use xor!
141};
142
143} // namespace QtPrivate
144
145template <typename InputIterator>
146inline uint qHashRange(InputIterator first, InputIterator last, uint seed = 0)
147 noexcept(noexcept(qHash(*first))) // assume iterator operations don't throw
148{
149 return std::accumulate(first, last, seed, QtPrivate::QHashCombine());
150}
151
152template <typename InputIterator>
153inline uint qHashRangeCommutative(InputIterator first, InputIterator last, uint seed = 0)
154 noexcept(noexcept(qHash(*first))) // assume iterator operations don't throw
155{
156 return std::accumulate(first, last, seed, QtPrivate::QHashCombineCommutative());
157}
158
159template <typename T1, typename T2> inline uint qHash(const QPair<T1, T2> &key, uint seed = 0)
160 noexcept(noexcept(qHash(key.first, seed)) && noexcept(qHash(key.second, seed)))
161{
162 uint h1 = qHash(key.first, seed);
163 uint h2 = qHash(key.second, seed);
164 return ((h1 << 16) | (h1 >> 16)) ^ h2 ^ seed;
165}
166
167template <typename T1, typename T2> inline uint qHash(const std::pair<T1, T2> &key, uint seed = 0)
168 noexcept(noexcept(qHash(key.first, seed)) && noexcept(qHash(key.second, seed)))
169{
170 QtPrivate::QHashCombine hash;
171 seed = hash(seed, key.first);
172 seed = hash(seed, key.second);
173 return seed;
174}
175
176#define QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH(Class, Arguments) \
177 QT_BEGIN_INCLUDE_NAMESPACE \
178 namespace std { \
179 template <> \
180 struct hash< QT_PREPEND_NAMESPACE(Class) > { \
181 using argument_type = QT_PREPEND_NAMESPACE(Class); \
182 using result_type = size_t; \
183 size_t operator()(Arguments s) const \
184 noexcept(noexcept(QT_PREPEND_NAMESPACE(qHash)(s))) \
185 { \
186 /* this seeds qHash with the result of */ \
187 /* std::hash applied to an int, to reap */ \
188 /* any protection against predictable hash */ \
189 /* values the std implementation may provide */ \
190 return QT_PREPEND_NAMESPACE(qHash)(s, \
191 QT_PREPEND_NAMESPACE(qHash)( \
192 std::hash<int>{}(0))); \
193 } \
194 }; \
195 } \
196 QT_END_INCLUDE_NAMESPACE \
197 /*end*/
198
199#define QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(Class) \
200 QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH(Class, const argument_type &)
201#define QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_VALUE(Class) \
202 QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH(Class, argument_type)
203
204QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QString)
205QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QStringRef)
206QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_VALUE(QStringView)
207QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_VALUE(QLatin1String)
208QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QByteArray)
209QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QBitArray)
210
211QT_END_NAMESPACE
212
213#if defined(Q_CC_MSVC)
214#pragma warning( pop )
215#endif
216
217#endif // QHASHFUNCTIONS_H
218