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