1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QPODVECTOR_P_H
41#define QPODVECTOR_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/qglobal.h>
55#include <QDebug>
56
57QT_BEGIN_NAMESPACE
58
59template<class T, int Increment>
60class QPODVector
61{
62public:
63 QPODVector()
64 : m_count(0), m_capacity(0), m_data(nullptr) {}
65 ~QPODVector() { if (m_data) ::free(ptr: m_data); }
66
67 const T &at(int idx) const {
68 return m_data[idx];
69 }
70
71 T &operator[](int idx) {
72 return m_data[idx];
73 }
74
75 void clear() {
76 m_count = 0;
77 }
78
79 void prepend(const T &v) {
80 insert(idx: 0, v);
81 }
82
83 void append(const T &v) {
84 insert(idx: m_count, v);
85 }
86
87 void insert(int idx, const T &v) {
88 if (m_count == m_capacity) {
89 m_capacity += Increment;
90 m_data = (T *)realloc(ptr: static_cast<void *>(m_data), size: m_capacity * sizeof(T));
91 }
92 int moveCount = m_count - idx;
93 if (moveCount)
94 ::memmove(dest: static_cast<void *>(m_data + idx + 1), src: static_cast<const void *>(m_data + idx), n: moveCount * sizeof(T));
95 m_count++;
96 m_data[idx] = v;
97 }
98
99 void reserve(int count) {
100 if (count >= m_capacity) {
101 m_capacity = (count + (Increment-1)) & (0xFFFFFFFF - Increment + 1);
102 m_data = (T *)realloc(ptr: static_cast<void *>(m_data), size: m_capacity * sizeof(T));
103 }
104 }
105
106 void insertBlank(int idx, int count) {
107 int newSize = m_count + count;
108 reserve(count: newSize);
109 int moveCount = m_count - idx;
110 if (moveCount)
111 ::memmove(dest: static_cast<void *>(m_data + idx + count), src: static_cast<const void *>(m_data + idx),
112 n: moveCount * sizeof(T));
113 m_count = newSize;
114 }
115
116 void remove(int idx, int count = 1) {
117 int moveCount = m_count - (idx + count);
118 if (moveCount)
119 ::memmove(dest: static_cast<void *>(m_data + idx), src: static_cast<const void *>(m_data + idx + count),
120 n: moveCount * sizeof(T));
121 m_count -= count;
122 }
123
124 void removeOne(const T &v) {
125 int idx = 0;
126 while (idx < m_count) {
127 if (m_data[idx] == v) {
128 remove(idx);
129 return;
130 }
131 ++idx;
132 }
133 }
134
135 int find(const T &v) {
136 for (int idx = 0; idx < m_count; ++idx)
137 if (m_data[idx] == v)
138 return idx;
139 return -1;
140 }
141
142 bool contains(const T &v) {
143 return find(v) != -1;
144 }
145
146 int count() const {
147 return m_count;
148 }
149
150 void copyAndClear(QPODVector<T,Increment> &other) {
151 if (other.m_data) ::free(ptr: other.m_data);
152 other.m_count = m_count;
153 other.m_capacity = m_capacity;
154 other.m_data = m_data;
155 m_count = 0;
156 m_capacity = 0;
157 m_data = nullptr;
158 }
159
160 QPODVector<T,Increment> &operator<<(const T &v) { append(v); return *this; }
161private:
162 QPODVector(const QPODVector &);
163 QPODVector &operator=(const QPODVector &);
164 int m_count;
165 int m_capacity;
166 T *m_data;
167};
168
169QT_END_NAMESPACE
170
171#endif
172

source code of qtdeclarative/src/qml/qml/ftw/qpodvector_p.h