1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QPODVECTOR_P_H
43 #define QPODVECTOR_P_H
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.
56 #include <QtCore/qglobal.h>
61 template<class T, int Increment=1024>
66 : m_count(0), m_capacity(0), m_data(0) {}
67 ~QPODVector() { if (m_data) ::free(m_data); }
69 const T &at(int idx) const {
73 T &operator[](int idx) {
81 void prepend(const T &v) {
85 void append(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 *)realloc(m_data, m_capacity * sizeof(T));
94 int moveCount = m_count - idx;
96 ::memmove(m_data + idx + 1, m_data + idx, moveCount * sizeof(T));
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));
108 void insertBlank(int idx, int count) {
109 int newSize = m_count + count;
111 int moveCount = m_count - idx;
113 ::memmove(m_data + idx + count, m_data + idx,
114 moveCount * sizeof(T));
118 void remove(int idx, int count = 1) {
119 int moveCount = m_count - (idx + count);
121 ::memmove(m_data + idx, m_data + idx + count,
122 moveCount * sizeof(T));
126 void removeOne(const T &v) {
128 while (idx < m_count) {
129 if (m_data[idx] == v) {
137 int find(const T &v) {
138 for (int idx = 0; idx < m_count; ++idx)
139 if (m_data[idx] == v)
144 bool contains(const T &v) {
145 return find(v) != -1;
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;
162 QPODVector<T,Increment> &operator<<(const T &v) { append(v); return *this; }
164 QPODVector(const QPODVector &);
165 QPODVector &operator=(const QPODVector &);