Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qv4identifierhash.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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#include <private/qv4identifierhash_p.h>
5#include <private/qv4identifiertable_p.h>
6#include <private/qv4string_p.h>
7#include <private/qv4identifierhashdata_p.h>
8#include <private/qprimefornumbits_p.h>
9
11
12namespace QV4 {
13
19
21{
22 if (!d || d->refCount.loadAcquire() == 1)
23 return;
24 IdentifierHashData *newData = new IdentifierHashData(d);
25 if (d && !d->refCount.deref())
26 delete d;
27 d = newData;
28}
29
30inline
31IdentifierHashEntry *IdentifierHash::addEntry(PropertyKey identifier)
32{
33 Q_ASSERT(identifier.isStringOrSymbol());
34
35 // fill up to max 50%
36 bool grow = (d->alloc <= d->size*2);
37
38 if (grow) {
39 ++d->numBits;
40 int newAlloc = qPrimeForNumBits(d->numBits);
41 IdentifierHashEntry *newEntries = (IdentifierHashEntry *)malloc(newAlloc * sizeof(IdentifierHashEntry));
42 memset(newEntries, 0, newAlloc*sizeof(IdentifierHashEntry));
43 for (int i = 0; i < d->alloc; ++i) {
44 const IdentifierHashEntry &e = d->entries[i];
45 if (!e.identifier.isValid())
46 continue;
47 uint idx = e.identifier.id() % newAlloc;
48 while (newEntries[idx].identifier.isValid()) {
49 ++idx;
50 idx %= newAlloc;
51 }
52 newEntries[idx] = e;
53 }
54 free(d->entries);
55 d->entries = newEntries;
56 d->alloc = newAlloc;
57 }
58
59 uint idx = identifier.id() % d->alloc;
60 while (d->entries[idx].identifier.isValid()) {
61 Q_ASSERT(d->entries[idx].identifier != identifier);
62 ++idx;
63 idx %= d->alloc;
64 }
65 d->entries[idx].identifier = identifier;
66 ++d->size;
67 return d->entries + idx;
68}
69
70inline
71const IdentifierHashEntry *IdentifierHash::lookup(PropertyKey identifier) const
72{
73 if (!d || !identifier.isStringOrSymbol())
74 return nullptr;
75 Q_ASSERT(d->entries);
76
77 uint idx = identifier.id() % d->alloc;
78 while (1) {
79 if (!d->entries[idx].identifier.isValid())
80 return nullptr;
81 if (d->entries[idx].identifier == identifier)
82 return d->entries + idx;
83 ++idx;
84 idx %= d->alloc;
85 }
86}
87
88inline
89const IdentifierHashEntry *IdentifierHash::lookup(const QString &str) const
90{
91 if (!d)
92 return nullptr;
93
95 return lookup(id);
96}
97
98inline
99const IdentifierHashEntry *IdentifierHash::lookup(String *str) const
100{
101 if (!d)
102 return nullptr;
103 PropertyKey id = d->identifierTable->asPropertyKey(str);
104 if (id.isValid())
105 return lookup(id);
106 return lookup(str->toQString());
107}
108
109inline
110const PropertyKey IdentifierHash::toIdentifier(const QString &str) const
111{
112 Q_ASSERT(d);
114}
115
116inline
117const PropertyKey IdentifierHash::toIdentifier(Heap::String *str) const
118{
119 Q_ASSERT(d);
121}
122
124{
125 IdentifierHashEntry *e = d->entries;
126 IdentifierHashEntry *end = e + d->alloc;
127 while (e < end) {
128 if (e->identifier.isValid() && e->value == value)
129 return e->identifier.toQString();
130 ++e;
131 }
132 return QString();
133}
134
136{
137 d = other.d;
138 if (d)
139 d->refCount.ref();
140}
141
143{
144 if (d && !d->refCount.deref())
145 delete d;
146}
147
149{
150 if (other.d)
151 other.d->refCount.ref();
152 if (d && !d->refCount.deref())
153 delete d;
154 d = other.d;
155 return *this;
156}
157
159{
160 return d ? d->size : 0;
161}
162
164{
165 IdentifierHashEntry *e = addEntry(toIdentifier(str));
166 e->value = value;
167}
168
170{
171 IdentifierHashEntry *e = addEntry(toIdentifier(str));
172 e->value = value;
173}
174
176{
177 const IdentifierHashEntry *e = lookup(str);
178 return e ? e->value : -1;
179}
180
182{
183 const IdentifierHashEntry *e = lookup(str);
184 return e ? e->value : -1;
185}
186
187
188}
189
static AVMetadataIdentifier toIdentifier(QMediaMetaData::Key key, AVMetadataKeySpace keySpace)
bool deref() noexcept
T loadAcquire() const noexcept
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString str
[2]
Combined button and popup list for selecting options.
static int grow(QLayoutStruct &ls, int delta)
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLuint GLuint end
QT_BEGIN_NAMESPACE int qPrimeForNumBits(int numBits)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int uint
Definition qtypes.h:34
QSharedPointer< T > other(t)
[5]
QJSEngine engine
[0]
IdentifierHashEntry * entries
QString findId(int value) const
IdentifierHash()=default
void add(const QString &str, int value)
int value(const QString &str) const
IdentifierHash & operator=(const IdentifierHash &other)
PropertyKey asPropertyKey(const Heap::String *str)
bool isStringOrSymbol() const
bool isValid() const
Q_QML_EXPORT QString toQString() const
quint64 id() const