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 | |
60 | QT_BEGIN_NAMESPACE |
61 | |
62 | class QBitArray; |
63 | class QByteArray; |
64 | class QString; |
65 | class QStringRef; |
66 | class QLatin1String; |
67 | |
68 | Q_CORE_EXPORT int qGlobalQHashSeed(); |
69 | Q_CORE_EXPORT void qSetGlobalQHashSeed(int newSeed); |
70 | |
71 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHashBits(const void *p, size_t size, uint seed = 0) noexcept; |
72 | |
73 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(char key, uint seed = 0) noexcept { return uint(key) ^ seed; } |
74 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(uchar key, uint seed = 0) noexcept { return uint(key) ^ seed; } |
75 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(signed char key, uint seed = 0) noexcept { return uint(key) ^ seed; } |
76 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(ushort key, uint seed = 0) noexcept { return uint(key) ^ seed; } |
77 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(short key, uint seed = 0) noexcept { return uint(key) ^ seed; } |
78 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(uint key, uint seed = 0) noexcept { return key ^ seed; } |
79 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(int key, uint seed = 0) noexcept { return uint(key) ^ seed; } |
80 | Q_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 | } |
86 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(long key, uint seed = 0) noexcept { return qHash(key: ulong(key), seed); } |
87 | Q_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 | } |
91 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(qint64 key, uint seed = 0) noexcept { return qHash(key: quint64(key), seed); } |
92 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(float key, uint seed = 0) noexcept; |
93 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(double key, uint seed = 0) noexcept; |
94 | #if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC) |
95 | Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(long double key, uint seed = 0) noexcept; |
96 | #endif |
97 | Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(const QChar key, uint seed = 0) noexcept { return qHash(key: key.unicode(), seed); } |
98 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QByteArray &key, uint seed = 0) noexcept; |
99 | #if QT_STRINGVIEW_LEVEL < 2 |
100 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QString &key, uint seed = 0) noexcept; |
101 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QStringRef &key, uint seed = 0) noexcept; |
102 | #endif |
103 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QStringView key, uint seed = 0) noexcept; |
104 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QBitArray &key, uint seed = 0) noexcept; |
105 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QLatin1String key, uint seed = 0) noexcept; |
106 | Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) noexcept; |
107 | |
108 | Q_DECL_CONST_FUNCTION inline uint qHash(std::nullptr_t, uint seed = 0) noexcept |
109 | { |
110 | return qHash(key: reinterpret_cast<quintptr>(nullptr), seed); |
111 | } |
112 | |
113 | template <class T> inline uint qHash(const T *key, uint seed = 0) noexcept |
114 | { |
115 | return qHash(key: reinterpret_cast<quintptr>(key), seed); |
116 | } |
117 | template<typename T> inline uint qHash(const T &t, uint seed) |
118 | noexcept(noexcept(qHash(t))) |
119 | { return qHash(t) ^ seed; } |
120 | |
121 | namespace QtPrivate { |
122 | |
123 | struct 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 | |
131 | struct 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 | |
145 | template <typename InputIterator> |
146 | inline 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 | |
152 | template <typename InputIterator> |
153 | inline 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 | |
159 | template <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 | |
167 | template <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 | |
204 | QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QString) |
205 | QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QStringRef) |
206 | QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_VALUE(QStringView) |
207 | QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_VALUE(QLatin1String) |
208 | QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QByteArray) |
209 | QT_SPECIALIZE_STD_HASH_TO_CALL_QHASH_BY_CREF(QBitArray) |
210 | |
211 | QT_END_NAMESPACE |
212 | |
213 | #if defined(Q_CC_MSVC) |
214 | #pragma warning( pop ) |
215 | #endif |
216 | |
217 | #endif // QHASHFUNCTIONS_H |
218 | |