summaryrefslogtreecommitdiffstats
path: root/src/foundation/Qt3DSIndexableLinkedList.h
blob: ff36b42afee9819ec810cac4cc605106667bd125 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/****************************************************************************
**
** Copyright (C) 2008-2012 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QT3DS_FOUNDATION_INDEXABLE_LINKED_LIST_H
#define QT3DS_FOUNDATION_INDEXABLE_LINKED_LIST_H
#include "foundation/Qt3DSPool.h"

namespace qt3ds {
namespace foundation {
    // Set of helper functions for manipulation of lists that are
    // somewhat indexable but also amenable to using a pool structure.
    template <typename TNodeType, typename TObjType, QT3DSU32 TObjCount>
    struct IndexableLinkedList
    {
        typedef TNodeType SNode;

        typedef Pool<SNode, ForwardingAllocator> TPoolType;

        static TObjType &Create(SNode *&inInitialNode, QT3DSU32 &ioCurrentCount, TPoolType &ioPool)
        {
            QT3DSU32 idx = ioCurrentCount;
            ++ioCurrentCount;
            QT3DSU32 numGroups = (ioCurrentCount + TObjCount - 1) / TObjCount;
            QT3DSU32 localIdx = idx % TObjCount;

            SNode *theCurrentNode = inInitialNode;
            for (QT3DSU32 idx = 0, end = numGroups; idx < end; ++idx) {
                if (idx == 0) {
                    if (theCurrentNode == NULL)
                        inInitialNode = ioPool.construct(__FILE__, __LINE__);

                    theCurrentNode = inInitialNode;
                } else {
                    if (theCurrentNode->m_NextNode == NULL)
                        theCurrentNode->m_NextNode = ioPool.construct(__FILE__, __LINE__);
                    theCurrentNode = theCurrentNode->m_NextNode;
                }
            }
            return theCurrentNode->m_Data[localIdx];
        }

        static void CreateAll(SNode *&inInitialNode, QT3DSU32 inCount, TPoolType &ioPool)
        {
            QT3DSU32 numGroups = (inCount + TObjCount - 1) / TObjCount;
            SNode *lastNode = NULL;
            for (QT3DSU32 idx = 0, end = numGroups; idx < end; ++idx) {
                SNode *nextNode = ioPool.construct(__FILE__, __LINE__);
                if (idx == 0)
                    inInitialNode = nextNode;
                else
                    lastNode->m_NextNode = nextNode;
                lastNode = nextNode;
            }
        }

        static void DeleteList(SNode *&inInitialNode, TPoolType &ioPool)
        {
            SNode *theNode = inInitialNode;
            inInitialNode = NULL;
            while (theNode) {
                SNode *theNext = theNode->m_NextNode;
                theNode->m_NextNode = NULL;
                ioPool.deallocate(theNode);
                theNode = theNext;
            }
        }

        // Performs no checking, so you need to have already allocated what you need.
        static const TObjType &GetObjAtIdx(const SNode *inInitialNode, QT3DSU32 inIdx)
        {
            QT3DSU32 groupIdx = inIdx / TObjCount;
            QT3DSU32 localIdx = inIdx % TObjCount;
            for (QT3DSU32 idx = 0; idx < groupIdx; ++idx)
                inInitialNode = inInitialNode->m_NextNode;
            return inInitialNode->m_Data[inIdx];
        }

        static TObjType &GetObjAtIdx(SNode *inInitialNode, QT3DSU32 inIdx)
        {
            QT3DSU32 groupIdx = inIdx / TObjCount;
            QT3DSU32 localIdx = inIdx % TObjCount;
            for (QT3DSU32 idx = 0; idx < groupIdx; ++idx)
                inInitialNode = inInitialNode->m_NextNode;
            return inInitialNode->m_Data[localIdx];
        }

        struct SIteratorType
        {
            QT3DSU32 m_Idx;
            QT3DSU32 m_Count;
            SNode *m_Node;

            SIteratorType()
                : m_Idx(0)
                , m_Count(0)
                , m_Node(NULL)
            {
            }
            SIteratorType(const SIteratorType &iter)
                : m_Idx(iter.m_Idx)
                , m_Count(iter.m_Count)
                , m_Node(iter.m_Node)
            {
            }
            SIteratorType(SNode *inNode, QT3DSU32 inCount, QT3DSU32 inIdx)
                : m_Idx(inIdx)
                , m_Count(inCount)
                , m_Node(inNode)
            {
            }

            bool operator==(const SIteratorType &iter) const { return m_Idx == iter.m_Idx; }

            bool operator!=(const SIteratorType &iter) const { return m_Idx != iter.m_Idx; }

            TObjType &operator*()
            {
                QT3DSU32 localIdx = m_Idx % TObjCount;
                return m_Node->m_Data[localIdx];
            }

            SIteratorType &operator++()
            {
                ++m_Idx;
                if ((m_Idx % TObjCount) == 0)
                    m_Node = m_Node->m_NextNode;

                return *this;
            }
        };

        struct SConstIteratorType
        {
            QT3DSU32 m_Idx;
            QT3DSU32 m_Count;
            const SNode *m_Node;

            SConstIteratorType()
                : m_Idx(0)
                , m_Count(0)
                , m_Node(NULL)
            {
            }
            SConstIteratorType(const SIteratorType &iter)
                : m_Idx(iter.m_Idx)
                , m_Count(iter.m_Count)
                , m_Node(iter.m_Node)
            {
            }
            SConstIteratorType(const SConstIteratorType &iter)
                : m_Idx(iter.m_Idx)
                , m_Count(iter.m_Count)
                , m_Node(iter.m_Node)
            {
            }
            SConstIteratorType(const SNode *inNode, QT3DSU32 inCount, QT3DSU32 inIdx)
                : m_Idx(inIdx)
                , m_Count(inCount)
                , m_Node(inNode)
            {
            }

            bool operator==(const SConstIteratorType &iter) const { return m_Idx == iter.m_Idx; }

            bool operator!=(const SConstIteratorType &iter) const { return m_Idx != iter.m_Idx; }

            const TObjType &operator*()
            {
                QT3DSU32 localIdx = m_Idx % TObjCount;
                return m_Node->m_Data[localIdx];
            }

            SConstIteratorType &operator++()
            {
                ++m_Idx;
                if ((m_Idx % TObjCount) == 0)
                    m_Node = m_Node->m_NextNode;

                return *this;
            }
        };

        typedef SIteratorType iterator;
        typedef SConstIteratorType const_iterator;

        static iterator begin(SNode *inInitialNode, QT3DSU32 inItemCount)
        {
            return SIteratorType(inInitialNode, inItemCount, 0);
        }

        static iterator end(SNode * /*inInitialNode*/, QT3DSU32 inItemCount)
        {
            return SIteratorType(NULL, inItemCount, inItemCount);
        }

        static const_iterator begin(const SNode *inInitialNode, QT3DSU32 inItemCount)
        {
            return SConstIteratorType(inInitialNode, inItemCount, 0);
        }

        static const_iterator end(const SNode * /*inInitialNode*/, QT3DSU32 inItemCount)
        {
            return SConstIteratorType(NULL, inItemCount, inItemCount);
        }

        struct SNodeIteratorType
        {
            SNode *m_Node;

            SNodeIteratorType()
                : m_Node(NULL)
            {
            }
            SNodeIteratorType(const SNodeIteratorType &iter)
                : m_Node(iter.m_Node)
            {
            }
            SNodeIteratorType(SNode *inNode)
                : m_Node(inNode)
            {
            }

            bool operator==(const SNodeIteratorType &iter) const { return m_Node == iter.m_Node; }

            bool operator!=(const SNodeIteratorType &iter) const { return m_Node != iter.m_Node; }

            TNodeType &operator*() { return *m_Node; }

            SNodeIteratorType &operator++()
            {
                if (m_Node)
                    m_Node = m_Node->m_NextNode;

                return *this;
            }
        };

        typedef SNodeIteratorType node_iterator;
        static node_iterator node_begin(SNode *inFirstNode) { return node_iterator(inFirstNode); }
        static node_iterator node_end(SNode * /*inFirstNode*/) { return node_iterator(NULL); }

        // Iterates through the number of nodes required for a given count of objects
        struct SCountIteratorType
        {
            QT3DSU32 m_Idx;
            QT3DSU32 m_Count;

            SCountIteratorType()
                : m_Idx(0)
                , m_Count(0)
            {
            }
            SCountIteratorType(const SCountIteratorType &iter)
                : m_Idx(iter.m_Idx)
                , m_Count(iter.m_Count)
            {
            }
            SCountIteratorType(QT3DSU32 idx, QT3DSU32 count)
                : m_Idx(idx)
                , m_Count(count)
            {
            }

            bool operator==(const SCountIteratorType &iter) const { return m_Idx == iter.m_Idx; }

            bool operator!=(const SCountIteratorType &iter) const { return m_Idx != iter.m_Idx; }

            SCountIteratorType &operator++()
            {
                m_Idx = NVMin(m_Idx + TObjCount, m_Count);
                return *this;
            }
        };

        typedef SCountIteratorType count_iterator;
        static count_iterator count_begin(QT3DSU32 count) { return SCountIteratorType(0, count); }
        static count_iterator count_end(QT3DSU32 count) { return SCountIteratorType(count, count); }
    };
}
}

#endif