Qt 4.8
qpodvector_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
60 
61 template<class T, int Increment=1024>
62 class QPODVector
63 {
64 public:
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 *)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 
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 
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 
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;
168  T *m_data;
169 };
170 
172 
173 #endif
const T & at(int idx) const
Definition: qpodvector_p.h:69
T * q_check_ptr(T *p)
Definition: qglobal.h:1857
void insertBlank(int idx, int count)
Definition: qpodvector_p.h:108
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void clear()
Definition: qpodvector_p.h:77
QPODVector< T, Increment > & operator<<(const T &v)
Definition: qpodvector_p.h:162
void copyAndClear(QPODVector< T, Increment > &other)
Definition: qpodvector_p.h:152
void append(const T &v)
Definition: qpodvector_p.h:85
bool contains(const T &v)
Definition: qpodvector_p.h:144
QPODVector & operator=(const QPODVector &)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void prepend(const T &v)
Definition: qpodvector_p.h:81
void removeOne(const T &v)
Definition: qpodvector_p.h:126
int count() const
Definition: qpodvector_p.h:148
T & operator[](int idx)
Definition: qpodvector_p.h:73
void insert(int idx, const T &v)
Definition: qpodvector_p.h:89
void reserve(int count)
Definition: qpodvector_p.h:101
int find(const T &v)
Definition: qpodvector_p.h:137