1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtDeclarative 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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
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:
QPODVector()65     QPODVector()
66     : m_count(0), m_capacity(0), m_data(0) {}
~QPODVector()67     ~QPODVector() { if (m_data) ::free(m_data); }
68 
at(int idx)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 
clear()77     void clear() {
78         m_count = 0;
79     }
80 
prepend(const T & v)81     void prepend(const T &v) {
82         insert(0, v);
83     }
84 
append(const T & v)85     void append(const T &v) {
86         insert(m_count, v);
87     }
88 
insert(int idx,const T & v)89     void insert(int idx, const T &v) {
90         if (m_count == m_capacity) {
91             m_capacity += Increment;
92             m_data = (T *)q_check_ptr(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 
reserve(int count)101     void reserve(int count) {
102         if (count >= m_capacity) {
103             m_capacity = (count + (Increment-1)) & (0xFFFFFFFF - Increment + 1);
104             m_data = (T *)q_check_ptr(realloc(m_data, m_capacity * sizeof(T)));
105         }
106     }
107 
insertBlank(int idx,int count)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 
removeOne(const T & v)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 
find(const T & v)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 
contains(const T & v)144     bool contains(const T &v) {
145         return find(v) != -1;
146     }
147 
count()148     int count() const {
149         return m_count;
150     }
151 
copyAndClear(QPODVector<T,Increment> & other)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
174