e1acdc02aa5a2e70560a8b3454aff101f215214a
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qpodvector_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
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:
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