1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QALGORITHMS_H
#define QALGORITHMS_H
#if 0
#pragma qt_class(QtAlgorithms)
#endif
#include <QtCore/qglobal.h>
#include <QtCore/q20bit.h>
#include <QtCore/q20functional.h>
#include <type_traits>
#define QT_HAS_CONSTEXPR_BITOPS
QT_BEGIN_NAMESPACE
template <typename ForwardIterator>
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
{
while (begin != end) {
delete *begin;
++begin;
}
}
template <typename Container>
inline void qDeleteAll(const Container &c)
{
qDeleteAll(c.begin(), c.end());
}
Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(quint32 v) noexcept
{
return q20::popcount(v);
}
Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(quint8 v) noexcept
{
return q20::popcount(v);
}
Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(quint16 v) noexcept
{
return q20::popcount(v);
}
Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(quint64 v) noexcept
{
return q20::popcount(v);
}
Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(long unsigned int v) noexcept
{
return q20::popcount(v);
}
constexpr inline uint qCountTrailingZeroBits(quint32 v) noexcept
{
return q20::countr_zero(v);
}
constexpr inline uint qCountTrailingZeroBits(quint8 v) noexcept
{
return q20::countr_zero(v);
}
constexpr inline uint qCountTrailingZeroBits(quint16 v) noexcept
{
return q20::countr_zero(v);
}
constexpr inline uint qCountTrailingZeroBits(quint64 v) noexcept
{
return q20::countr_zero(v);
}
constexpr inline uint qCountTrailingZeroBits(unsigned long v) noexcept
{
return q20::countr_zero(v);
}
constexpr inline uint qCountLeadingZeroBits(quint32 v) noexcept
{
return q20::countl_zero(v);
}
constexpr inline uint qCountLeadingZeroBits(quint8 v) noexcept
{
return q20::countl_zero(v);
}
constexpr inline uint qCountLeadingZeroBits(quint16 v) noexcept
{
return q20::countl_zero(v);
}
constexpr inline uint qCountLeadingZeroBits(quint64 v) noexcept
{
return q20::countl_zero(v);
}
constexpr inline uint qCountLeadingZeroBits(unsigned long v) noexcept
{
return q20::countl_zero(v);
}
template <typename InputIterator, typename Result, typename Separator = Result,
typename Projection = q20::identity>
Result qJoin(InputIterator first, InputIterator last, Result init, const Separator &separator = {},
Projection p = {})
{
if (first != last) {
init += std::invoke(p, *first);
++first;
}
while (first != last) {
init += separator;
init += std::invoke(p, *first);
++first;
}
return init;
}
namespace QtPrivate {
template <typename T>
constexpr
std::enable_if_t<std::conjunction_v<std::is_integral<T>, std::is_unsigned<T>>, int>
log2i(T x)
{
// Integral -> int version of std::log2():
Q_ASSERT(x > 0); // Q_PRE
// C++20: return std::bit_width(x) - 1
return int(sizeof(T) * 8 - 1 - qCountLeadingZeroBits(x));
}
} // namespace QtPrivate
QT_END_NAMESPACE
#endif // QALGORITHMS_H
|