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
qv4util_p.h
Go to the documentation of this file.
1// Copyright (C) 2019 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
4
5#ifndef QV4UTIL_H
6#define QV4UTIL_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/QBitArray>
20#include <QtCore/private/qglobal_p.h>
21#include <algorithm>
22#include <vector>
23
24QT_BEGIN_NAMESPACE
25
26namespace QV4 {
27
28#if !defined(BROKEN_STD_VECTOR_BOOL_OR_BROKEN_STD_FIND)
29// Sanity:
31{
32 std::vector<bool> bits;
33
34public:
35 BitVector(int size = 0, bool value = false)
36 : bits(size, value)
37 {}
38
39 void clear()
40 { bits = std::vector<bool>(bits.size(), false); }
41
42 void reserve(int size)
43 { bits.reserve(size); }
44
45 int size() const
46 {
47 Q_ASSERT(bits.size() < INT_MAX);
48 return static_cast<int>(bits.size());
49 }
50
51 void resize(int newSize)
52 { bits.resize(newSize); }
53
54 void resize(int newSize, bool newValue)
55 { bits.resize(newSize, newValue); }
56
57 void assign(int newSize, bool value)
58 { bits.assign(newSize, value); }
59
60 int findNext(int start, bool value, bool wrapAround) const
61 {
62 // The ++operator of std::vector<bool>::iterator in libc++ has a bug when using it on an
63 // iterator pointing to the last element. It will not be set to ::end(), but beyond
64 // that. (It will be set to the first multiple of the native word size that is bigger
65 // than size().)
66 //
67 // See http://llvm.org/bugs/show_bug.cgi?id=19663
68 //
69 // The work-around is to calculate the distance, and compare it to the size() to see if it's
70 // beyond the end, or take the minimum of the distance and the size.
71
72 size_t pos = std::distance(bits.begin(),
73 std::find(bits.begin() + start, bits.end(), value));
74 if (wrapAround && pos >= static_cast<size_t>(size()))
75 pos = std::distance(bits.begin(),
76 std::find(bits.begin(), bits.begin() + start, value));
77
78 pos = qMin(pos, static_cast<size_t>(size()));
79
80 Q_ASSERT(pos <= static_cast<size_t>(size()));
81 Q_ASSERT(pos < INT_MAX);
82
83 return static_cast<int>(pos);
84 }
85
86 bool at(int idx) const
87 { return bits.at(idx); }
88
89 void setBit(int idx)
90 { bits[idx] = true; }
91
92 void clearBit(int idx)
93 { bits[idx] = false; }
94};
95#else // Insanity:
96class BitVector
97{
99
100public:
101 BitVector(int size = 0, bool value = false)
102 : bits(size, value)
103 {}
104
105 void clear()
106 { bits = QBitArray(bits.size(), false); }
107
108 void reserve(int size)
109 { Q_UNUSED(size); }
110
111 int size() const
112 { return bits.size(); }
113
114 void resize(int newSize)
115 { bits.resize(newSize); }
116
117 void resize(int newSize, bool newValue)
118 {
119 int oldSize = bits.size();
122 }
123
124 void assign(int newSize, bool value)
125 {
127 bits.fill(value);
128 }
129
130 int findNext(int start, bool value, bool wrapAround) const
131 {
132 for (int i = start, ei = size(); i < ei; ++i) {
133 if (at(i) == value)
134 return i;
135 }
136
137 if (wrapAround) {
138 for (int i = 0, ei = start; i < ei; ++i) {
139 if (at(i) == value)
140 return i;
141 }
142 }
143
144 return size();
145 }
146
147 bool at(int idx) const
148 { return bits.at(idx); }
149
150 void setBit(int idx)
151 { bits[idx] = true; }
152
153 void clearBit(int idx)
154 { bits[idx] = false; }
155};
156#endif
157
158}
159
160QT_END_NAMESPACE
161
162#endif // QV4UTIL_H
void reserve(int size)
Definition qv4util_p.h:42
void assign(int newSize, bool value)
Definition qv4util_p.h:57
void resize(int newSize)
Definition qv4util_p.h:51
void resize(int newSize, bool newValue)
Definition qv4util_p.h:54
int size() const
Definition qv4util_p.h:45
void setBit(int idx)
Definition qv4util_p.h:89
void clearBit(int idx)
Definition qv4util_p.h:92
bool at(int idx) const
Definition qv4util_p.h:86
BitVector(int size=0, bool value=false)
Definition qv4util_p.h:35
int findNext(int start, bool value, bool wrapAround) const
Definition qv4util_p.h:60
Definition qjsvalue.h:24