Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qalgorithms.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QALGORITHMS_H
6#define QALGORITHMS_H
7
8#if 0
9#pragma qt_class(QtAlgorithms)
10#endif
11
12#include <QtCore/qglobal.h>
13#include <QtCore/q20bit.h>
14#include <QtCore/q20functional.h>
15#include <type_traits>
16
17#define QT_HAS_CONSTEXPR_BITOPS
18
19QT_BEGIN_NAMESPACE
20
21template <typename ForwardIterator>
22Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
23{
24 while (begin != end) {
25 delete *begin;
26 ++begin;
27 }
28}
29
30template <typename Container>
31inline void qDeleteAll(const Container &c)
32{
33 qDeleteAll(c.begin(), c.end());
34}
35
36
37Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(quint32 v) noexcept
38{
39 return q20::popcount(v);
40}
41
42Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(quint8 v) noexcept
43{
44 return q20::popcount(v);
45}
46
47Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(quint16 v) noexcept
48{
49 return q20::popcount(v);
50}
51
52Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(quint64 v) noexcept
53{
54 return q20::popcount(v);
55}
56
57Q_DECL_CONST_FUNCTION constexpr inline uint qPopulationCount(long unsigned int v) noexcept
58{
59 return q20::popcount(v);
60}
61
62constexpr inline uint qCountTrailingZeroBits(quint32 v) noexcept
63{
64 return q20::countr_zero(v);
65}
66
67constexpr inline uint qCountTrailingZeroBits(quint8 v) noexcept
68{
69 return q20::countr_zero(v);
70}
71
72constexpr inline uint qCountTrailingZeroBits(quint16 v) noexcept
73{
74 return q20::countr_zero(v);
75}
76
77constexpr inline uint qCountTrailingZeroBits(quint64 v) noexcept
78{
79 return q20::countr_zero(v);
80}
81
82constexpr inline uint qCountTrailingZeroBits(unsigned long v) noexcept
83{
84 return q20::countr_zero(v);
85}
86
87constexpr inline uint qCountLeadingZeroBits(quint32 v) noexcept
88{
89 return q20::countl_zero(v);
90}
91
92constexpr inline uint qCountLeadingZeroBits(quint8 v) noexcept
93{
94 return q20::countl_zero(v);
95}
96
97constexpr inline uint qCountLeadingZeroBits(quint16 v) noexcept
98{
99 return q20::countl_zero(v);
100}
101
102constexpr inline uint qCountLeadingZeroBits(quint64 v) noexcept
103{
104 return q20::countl_zero(v);
105}
106
107constexpr inline uint qCountLeadingZeroBits(unsigned long v) noexcept
108{
109 return q20::countl_zero(v);
110}
111
112template <typename InputIterator, typename Result, typename Separator = Result,
113 typename Projection = q20::identity>
114Result qJoin(InputIterator first, InputIterator last, Result init, const Separator &separator = {},
115 Projection p = {})
116{
117 if (first != last) {
118 init += std::invoke(p, *first);
119 ++first;
120 }
121
122 while (first != last) {
123 init += separator;
124 init += std::invoke(p, *first);
125 ++first;
126 }
127
128 return init;
129}
130
131namespace QtPrivate {
132
133template <typename T>
134constexpr
137{
138 // Integral -> int version of std::log2():
139 Q_ASSERT(x > 0); // Q_PRE
140 // C++20: return std::bit_width(x) - 1
141 return int(sizeof(T) * 8 - 1 - qCountLeadingZeroBits(x));
142}
143
144} // namespace QtPrivate
145
146QT_END_NAMESPACE
147
148#endif // QALGORITHMS_H
static constexpr T fromSpecial(T source)
Definition qendian.h:319
static constexpr T toSpecial(T source)
Definition qendian.h:318
static constexpr T fromSpecial(T source)
Definition qendian.h:311
static constexpr T toSpecial(T source)
Definition qendian.h:310
bool operator!=(QSpecialInteger< S > i) const
Definition qendian.h:261
static constexpr QSpecialInteger max()
Definition qendian.h:300
QSpecialInteger & operator>>=(T i)
Definition qendian.h:269
constexpr QSpecialInteger(T i)
Definition qendian.h:255
QSpecialInteger & operator++()
Definition qendian.h:283
bool operator==(QSpecialInteger< S > i) const
Definition qendian.h:260
QSpecialInteger & operator=(T i)
Definition qendian.h:257
operator T() const
Definition qendian.h:258
QSpecialInteger operator++(int)
Definition qendian.h:287
QSpecialInteger & operator&=(T i)
Definition qendian.h:279
QSpecialInteger & operator+=(T i)
Definition qendian.h:263
QSpecialInteger & operator|=(T i)
Definition qendian.h:277
QSpecialInteger & operator--()
Definition qendian.h:285
QSpecialInteger & operator*=(T i)
Definition qendian.h:267
QSpecialInteger operator--(int)
Definition qendian.h:293
QSpecialInteger & operator^=(T i)
Definition qendian.h:281
QSpecialInteger()=default
QSpecialInteger & operator%=(T i)
Definition qendian.h:275
QSpecialInteger & operator/=(T i)
Definition qendian.h:273
static constexpr QSpecialInteger min()
Definition qendian.h:302
QSpecialInteger & operator-=(T i)
Definition qendian.h:265
Combined button and popup list for selecting options.
constexpr uint qCountLeadingZeroBits(unsigned long v) noexcept
void qDeleteAll(const Container &c)
Definition qalgorithms.h:31
constexpr uint qCountTrailingZeroBits(quint32 v) noexcept
Definition qalgorithms.h:62
constexpr uint qCountTrailingZeroBits(unsigned long v) noexcept
Definition qalgorithms.h:82
constexpr uint qCountLeadingZeroBits(quint32 v) noexcept
Definition qalgorithms.h:87
Result qJoin(InputIterator first, InputIterator last, Result init, const Separator &separator={}, Projection p={})
#define __has_builtin(x)
static Q_ALWAYS_INLINE void * bswapLoop(const uchar *src, size_t n, uchar *dst) noexcept
Definition qendian.cpp:843
void * qbswap< 2 >(const void *source, qsizetype n, void *dest) noexcept
Definition qendian.cpp:864
QBEInteger< quint64 > quint64_be
Definition qendian.h:404
float qbswap(float source)
Definition qendian.h:140
void qToLittleEndian(T src, void *dest)
Definition qendian.h:182
void qToLittleEndian(const void *source, qsizetype count, void *dest)
Definition qendian.h:187
QLEInteger< qint64 > qint64_le
Definition qendian.h:394
void qToBigEndian(T src, void *dest)
Definition qendian.h:180
QBEInteger< qint32 > qint32_be
Definition qendian.h:400
QLEInteger< qint32 > qint32_le
Definition qendian.h:393
void qFromLittleEndian(const void *source, qsizetype count, void *dest)
Definition qendian.h:191
void qbswap(const T src, void *dest)
Definition qendian.h:156
QSpecialInteger< QLittleEndianStorageType< T > > QLEInteger
Constructs a QLEInteger with the given value.
Definition qendian.h:379
QBEInteger< qint64 > qint64_be
Definition qendian.h:401
QLEInteger< quint32 > quint32_le
Definition qendian.h:396
QLEInteger< quint64 > quint64_le
Definition qendian.h:397
QSpecialInteger< QBigEndianStorageType< T > > QBEInteger
Constructs a QBEInteger with the given value.
Definition qendian.h:382
constexpr quint64 qbswap_helper(quint64 source)
Definition qendian.h:62
qfloat16 qbswap(qfloat16 source)
Definition qendian.h:135
void * qbswap(const void *source, qsizetype count, void *dest) noexcept
QBEInteger< quint32 > quint32_be
Definition qendian.h:403
constexpr T qFromLittleEndian(T source)
Definition qendian.h:178
constexpr T qToBigEndian(T source)
Definition qendian.h:172
QLEInteger< quint16 > quint16_le
Definition qendian.h:395
T qFromLittleEndian(const void *src)
Definition qendian.h:224
QLEInteger< qint16 > qint16_le
Definition qendian.h:392
constexpr T qFromBigEndian(T source)
Definition qendian.h:174
QBEInteger< qint16 > qint16_be
Definition qendian.h:399
void qToBigEndian(const void *source, qsizetype count, void *dest)
Definition qendian.h:185
QBEInteger< quint16 > quint16_be
Definition qendian.h:402
T qFromBigEndian(const void *src)
Definition qendian.h:238
double qbswap(double source)
Definition qendian.h:145
void qFromBigEndian(const void *source, qsizetype count, void *dest)
Definition qendian.h:189
constexpr T qToLittleEndian(T source)
Definition qendian.h:176
Float qbswapFloatHelper(Float source)
Definition qendian.h:127