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