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
49#if 0
50#pragma qt_class(QHashFunctions)
51#endif
52
53#if defined(Q_CC_MSVC)
54#pragma warning( push )
55#pragma warning( disable : 4311 ) // disable pointer truncation warning
56#pragma warning( disable : 4127 ) // conditional expression is constant
57#endif
58
59QT_BEGIN_NAMESPACE
60
61class QBitArray;
62class QByteArray;
63class QString;
64class QStringRef;
65class QLatin1String;
66
67Q_CORE_EXPORT int qGlobalQHashSeed();
68Q_CORE_EXPORT void qSetGlobalQHashSeed(int newSeed);
69
70Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHashBits(const void *p, size_t size, uint seed = 0) Q_DECL_NOTHROW;
71
72Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
73Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(uchar key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
74Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(signed char key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
75Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(ushort key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
76Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(short key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
77Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(uint key, uint seed = 0) Q_DECL_NOTHROW { return key ^ seed; }
78Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(int key, uint seed = 0) Q_DECL_NOTHROW { return uint(key) ^ seed; }
79Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(ulong key, uint seed = 0) Q_DECL_NOTHROW
80{
81 return (sizeof(ulong) > sizeof(uint))
82 ? (uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed)
83 : (uint(key & (~0U)) ^ seed);
84}
85Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(long key, uint seed = 0) Q_DECL_NOTHROW { return qHash(ulong(key), seed); }
86Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(quint64 key, uint seed = 0) Q_DECL_NOTHROW
87{
88 return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)) ^ seed;
89}
90Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(qint64 key, uint seed = 0) Q_DECL_NOTHROW { return qHash(quint64(key), seed); }
91Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(float key, uint seed = 0) Q_DECL_NOTHROW;
92Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(double key, uint seed = 0) Q_DECL_NOTHROW;
93#if !defined(Q_OS_DARWIN) || defined(Q_CLANG_QDOC)
94Q_CORE_EXPORT Q_DECL_CONST_FUNCTION uint qHash(long double key, uint seed = 0) Q_DECL_NOTHROW;
95#endif
96Q_DECL_CONST_FUNCTION Q_DECL_CONSTEXPR inline uint qHash(const QChar key, uint seed = 0) Q_DECL_NOTHROW { return qHash(key.unicode(), seed); }
97Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QByteArray &key, uint seed = 0) Q_DECL_NOTHROW;
98#if QT_STRINGVIEW_LEVEL < 2
99Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QString &key, uint seed = 0) Q_DECL_NOTHROW;
100Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QStringRef &key, uint seed = 0) Q_DECL_NOTHROW;
101#endif
102Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QStringView key, uint seed = 0) Q_DECL_NOTHROW;
103Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(const QBitArray &key, uint seed = 0) Q_DECL_NOTHROW;
104Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qHash(QLatin1String key, uint seed = 0) Q_DECL_NOTHROW;
105Q_CORE_EXPORT Q_DECL_PURE_FUNCTION uint qt_hash(QStringView key, uint chained = 0) Q_DECL_NOTHROW;
106
107Q_DECL_CONST_FUNCTION inline uint qHash(std::nullptr_t, uint seed = 0) Q_DECL_NOTHROW
108{
109 return qHash(reinterpret_cast<quintptr>(nullptr), seed);
110}
111
112template <class T> inline uint qHash(const T *key, uint seed = 0) Q_DECL_NOTHROW
113{
114 return qHash(reinterpret_cast<quintptr>(key), seed);
115}
116template<typename T> inline uint qHash(const T &t, uint seed)
117 Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(t)))
118{ return qHash(t) ^ seed; }
119
120namespace QtPrivate {
121
122struct QHashCombine {
123 typedef uint result_type;
124 template <typename T>
125 Q_DECL_CONSTEXPR result_type operator()(uint seed, const T &t) const Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(t)))
126 // combiner taken from N3876 / boost::hash_combine
127 { return seed ^ (qHash(t) + 0x9e3779b9 + (seed << 6) + (seed >> 2)) ; }
128};
129
130struct QHashCombineCommutative {
131 // QHashCombine is a good hash combiner, but is not commutative,
132 // ie. it depends on the order of the input elements. That is
133 // usually what we want: {0,1,3} should hash differently than
134 // {1,3,0}. Except when it isn't (e.g. for QSet and
135 // QHash). Therefore, provide a commutative combiner, too.
136 typedef uint result_type;
137 template <typename T>
138 Q_DECL_CONSTEXPR result_type operator()(uint seed, const T &t) const Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(t)))
139 { return seed + qHash(t); } // don't use xor!
140};
141
142} // namespace QtPrivate
143
144template <typename InputIterator>
145inline uint qHashRange(InputIterator first, InputIterator last, uint seed = 0)
146 Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(*first))) // assume iterator operations don't throw
147{
148 return std::accumulate(first, last, seed, QtPrivate::QHashCombine());
149}
150
151template <typename InputIterator>
152inline uint qHashRangeCommutative(InputIterator first, InputIterator last, uint seed = 0)
153 Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(*first))) // assume iterator operations don't throw
154{
155 return std::accumulate(first, last, seed, QtPrivate::QHashCombineCommutative());
156}
157
158template <typename T1, typename T2> inline uint qHash(const QPair<T1, T2> &key, uint seed = 0)
159 Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(key.first, seed)) && noexcept(qHash(key.second, seed)))
160{
161 uint h1 = qHash(key.first, seed);
162 uint h2 = qHash(key.second, seed);
163 return ((h1 << 16) | (h1 >> 16)) ^ h2 ^ seed;
164}
165
166template <typename T1, typename T2> inline uint qHash(const std::pair<T1, T2> &key, uint seed = 0)
167 Q_DECL_NOEXCEPT_EXPR(noexcept(qHash(key.first, seed)) && noexcept(qHash(key.second, seed)))
168{
169 QtPrivate::QHashCombine hash;
170 seed = hash(seed, key.first);
171 seed = hash(seed, key.second);
172 return seed;
173}
174
175QT_END_NAMESPACE
176
177#if defined(Q_CC_MSVC)
178#pragma warning( pop )
179#endif
180
181#endif // QHASHFUNCTIONS_H
182