aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/ftw/qlinkedstringhash_p.h
blob: 0086fa468c2362a3f89cc69368b4a4241f9bd1d8 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#ifndef QLINKEDSTRINGHASH_P_H
#define QLINKEDSTRINGHASH_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <private/qstringhash_p.h>

QT_BEGIN_NAMESPACE

template<class T>
class QLinkedStringHash : private QStringHash<T>
{
public:
    using typename QStringHash<T>::Node;
    using typename QStringHash<T>::NewedNode;
    using typename QStringHash<T>::ReservedNodePool;
    using typename QStringHash<T>::mapped_type;

    using ConstIteratorData = QStringHashData::IteratorData<const QLinkedStringHash>;
    using ConstIterator = typename QStringHash<T>::template Iterator<ConstIteratorData, const T>;

    void linkAndReserve(const QLinkedStringHash<T> &other, int additionalReserve)
    {
        clear();

        if (other.count()) {
            data.size = other.data.size;
            data.rehashToSize(other.count() + additionalReserve);

            if (data.numBuckets == other.data.numBuckets) {
                nodePool = new ReservedNodePool;
                nodePool->count = additionalReserve;
                nodePool->used = 0;
                nodePool->nodes = new Node[additionalReserve];

                for (int ii = 0; ii < data.numBuckets; ++ii)
                    data.buckets[ii] = (Node *)other.data.buckets[ii];

                link = &other;
                return;
            }

            data.size = 0;
        }

        data.numBits = other.data.numBits;
        reserve(other.count() + additionalReserve);
        copy(other);
    }

    inline bool isLinked() const
    {
        return link != 0;
    }

    void clear()
    {
        QStringHash<T>::clear();
        link = nullptr;
    }

    template<typename K>
    void insert(const K &key, const T &value)
    {
        // If this is a linked hash, we can't rely on owning the node, so we always
        // create a new one.
        Node *n = link ? nullptr : QStringHash<T>::findNode(key);
        if (n)
            n->value = value;
        else
            QStringHash<T>::createNode(key, value);
    }

    template<typename K>
    inline ConstIterator find(const K &key) const
    {
        return iterator(QStringHash<T>::findNode(key));
    }

    ConstIterator begin() const
    {
        return ConstIterator(
                QStringHash<T>::template iterateFirst<const QLinkedStringHash<T>,
                                                      ConstIteratorData>(this));
    }

    ConstIterator end() const { return ConstIterator(); }

    inline T *value(const ConstIterator &iter) { return value(iter.node()->key()); }

    using QStringHash<T>::value;
    using QStringHash<T>::reserve;
    using QStringHash<T>::copy;

protected:
    friend QStringHash<T>;
    using QStringHash<T>::data;
    using QStringHash<T>::nodePool;

    using QStringHash<T>::createNode;

    inline ConstIteratorData iterateFirst() const
    {
        const ConstIteratorData rv
                = QStringHash<T>::template iterateFirst<const QLinkedStringHash<T>,
                                                        ConstIteratorData>(this);
        return (rv.n == nullptr && link) ? link->iterateFirst() : rv;
    }

    static inline ConstIteratorData iterateNext(const ConstIteratorData &d)
    {
        const QLinkedStringHash<T> *self = d.p;
        const ConstIteratorData rv = QStringHash<T>::iterateNext(d);
        return (rv.n == nullptr && self->link) ? self->link->iterateFirst() : rv;
    }

    inline ConstIterator iterator(Node *n) const
    {
        if (!n)
            return ConstIterator();

        const QLinkedStringHash<T> *container = this;

        if (link) {
            // This node could be in the linked hash
            if ((n >= nodePool->nodes) && (n < (nodePool->nodes + nodePool->used))) {
                // The node is in this hash
            } else if ((n >= link->nodePool->nodes)
                       && (n < (link->nodePool->nodes + link->nodePool->used))) {
                // The node is in the linked hash
                container = link;
            } else {
                const NewedNode *ln = link->newedNodes;
                while (ln) {
                    if (ln == n) {
                        // This node is in the linked hash's newed list
                        container = link;
                        break;
                    }
                    ln = ln->nextNewed;
                }
            }
        }


        ConstIteratorData rv;
        rv.n = n;
        rv.p = container;
        return ConstIterator(rv);
    }

    const QLinkedStringHash<T> *link = nullptr;
};

template<class T>
class QLinkedStringMultiHash : public QLinkedStringHash<T>
{
public:
    using ConstIterator = typename QLinkedStringHash<T>::ConstIterator;

    template<typename K>
    inline void insert(const K &key, const T &value)
    {
        // Always create a new node
        QLinkedStringHash<T>::createNode(key, value);
    }

    inline void insert(const ConstIterator &iter)
    {
        // Always create a new node
        QLinkedStringHash<T>::createNode(iter.key(), iter.value());
    }

    inline ConstIterator findNext(const ConstIterator &iter) const
    {
        if (auto *node = iter.node()) {
            QHashedString key(node->key());
            while ((node = static_cast<typename QLinkedStringHash<T>::Node *>(*node->next))) {
                if (node->equals(key))
                    return QLinkedStringHash<T>::iterator(node);
            }
        }

        return ConstIterator();
    }
};

QT_END_NAMESPACE

#endif // QLINKEDSTRINGHASH_P_H