1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the porting application of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
32
#include <smallobject.h>
46
inline List(pool *_pool) : p(_pool), d(0) { d = malloc(16); d->size = 0; d->alloc = 16; }
48
inline List(const List &v) : d(0) { operator=(v); }
49
inline ~List() { d = 0; }
50
List &operator=(const List &v);
52
bool operator==(const List &v) const;
53
inline bool operator!=(const List &v) const { return !(*this == v); }
55
inline int size() const { return d->size; }
56
inline bool isEmpty() const { return d->size == 0; }
58
inline int capacity() const { return d->alloc; }
59
void reserve(int alloc);
61
inline T* data() { return d->array; }
62
inline const T* data() const { return d->array; }
65
const T &at(int i) const;
67
const T &operator[](int i) const;
68
void append(const T &t);
69
void prepend(const T &t);
70
void insert(int i, const T &t);
71
void insert(int i, int n, const T &t);
72
void replace(int i, const T &t);
74
void remove(int i, int n);
76
List &fill(const T &t, int size = -1);
78
int indexOf(const T &t, int from = 0) const;
79
int lastIndexOf(const T &t, int from = -1) const;
80
bool contains(const T &t) const;
81
int count(const T &t) const;
85
typedef const T* const_iterator;
86
inline iterator begin() { return d->array; }
87
inline const_iterator begin() const { return d->array; }
88
inline iterator end() { return d->array + d->size; }
89
inline const_iterator end() const { return d->array + d->size; }
90
iterator insert(iterator before, int n, const T &x);
91
inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); }
92
iterator erase(iterator begin, iterator end);
93
inline iterator erase(iterator pos) { return erase(pos, pos+1); }
96
inline int count() const { return d->size; }
97
inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
98
inline const T& first() const { Q_ASSERT(!isEmpty()); return *begin(); }
99
inline T& last() { Q_ASSERT(!isEmpty()); return *(end()-1); }
100
inline const T& last() const { Q_ASSERT(!isEmpty()); return *(end()-1); }
102
T value(int i) const;
103
T value(int i, const T &defaultValue) const;
106
typedef T value_type;
107
typedef value_type* pointer;
108
typedef const value_type* const_pointer;
109
typedef value_type& reference;
110
typedef const value_type& const_reference;
112
typedef ptrdiff_t difference_type;
114
typedef int difference_type;
116
typedef iterator Iterator;
117
typedef const_iterator ConstIterator;
118
typedef int size_type;
119
inline void push_back(const T &t) { append(t); }
120
inline void push_front(const T &t) { prepend(t); }
121
void pop_back() { Q_ASSERT(!isEmpty()); erase(end()-1); }
122
void pop_front() { Q_ASSERT(!isEmpty()); erase(begin()); }
123
inline bool empty() const
124
{ return d->size == 0; }
125
inline T& front() { return first(); }
126
inline const_reference front() const { return first(); }
127
inline reference back() { return last(); }
128
inline const_reference back() const { return last(); }
131
List &operator+=(const List &l);
132
inline List operator+(const List &l) const
133
{ List n = *this; n += l; return n; }
134
inline void operator+=(const T &t)
136
inline List &operator<< (const T &t)
137
{ append(t); return *this; }
140
Data *malloc(int alloc);
143
template <typename T>
144
inline void List<T>::clear()
147
template <typename T>
148
inline const T &List<T>::at(int i) const
149
{ Q_ASSERT_X(i >= 0 && i < d->size, "List<T>::at", "index out of range");
150
return d->array[i]; }
151
template <typename T>
152
inline const T &List<T>::operator[](int i) const
153
{ Q_ASSERT_X(i >= 0 && i < d->size, "List<T>::operator[]", "index out of range");
154
return d->array[i]; }
155
template <typename T>
156
inline T &List<T>::operator[](int i)
157
{ Q_ASSERT_X(i >= 0 && i < d->size, "List<T>::operator[]", "index out of range");
158
return d->array[i]; }
159
template <typename T>
160
inline void List<T>::insert(int i, const T &t)
161
{ Q_ASSERT_X(i >= 0 && i <= d->size, "List<T>::insert", "index out of range");
162
insert(begin()+i, 1, t); }
163
template <typename T>
164
inline void List<T>::insert(int i, int n, const T &t)
165
{ Q_ASSERT_X(i >= 0 && i <= d->size, "List<T>::insert", "index out of range");
166
insert(begin() + i, n, t); }
167
template <typename T>
168
inline void List<T>::remove(int i, int n)
169
{ Q_ASSERT_X(i >= 0 && i + n <= d->size, "List<T>::remove", "index out of range");
170
erase(begin() + i, begin() + i + n); }
171
template <typename T>
172
inline void List<T>::remove(int i)
173
{ Q_ASSERT_X(i >= 0 && i < d->size, "List<T>::remove", "index out of range");
174
erase(begin() + i, begin() + i + 1); }
175
template <typename T>
176
inline void List<T>::prepend(const T &t)
177
{ insert(begin(), 1, t); }
178
template <typename T>
179
inline void List<T>::replace(int i, const T &t)
180
{ Q_ASSERT_X(i >= 0 && i < d->size, "List<T>::replace", "index out of range");
183
template <typename T>
184
List<T> &List<T>::operator=(const List<T> &v)
187
d = malloc(v.d->alloc);
188
memcpy(d, v.d, sizeof(Data) + (v.d->size - 1) * sizeof(T));
192
template <typename T>
193
inline typename List<T>::Data *List<T>::malloc(int alloc)
195
return static_cast<Data *>(p->allocate(sizeof(Data) + (alloc - 1) * sizeof(T)));
198
template <typename T>
199
void List<T>::reserve(int alloc)
201
if (alloc <= d->alloc)
204
d = static_cast<Data *>(p->reallocate(d, sizeof(Data) + d->alloc * sizeof(T),
205
sizeof(Data) + (alloc - 1) * sizeof(T)));
210
T List<T>::value(int i) const
212
if(i < 0 || i >= d->size) {
218
T List<T>::value(int i, const T& defaultValue) const
220
return ((i < 0 || i >= d->size) ? defaultValue : d->array[i]);
223
template <typename T>
224
void List<T>::append(const T &t)
226
reserve(d->size + 1);
227
d->array[d->size++] = t;
231
template <typename T>
232
typename List<T>::iterator List<T>::insert(iterator before, size_type n, const T& t)
234
int p = before - d->array;
236
reserve(d->size + n);
239
memmove(i, b, (d->size-p)*sizeof(T));
247
template <typename T>
248
typename List<T>::iterator List<T>::erase(iterator begin, iterator end)
250
int f = begin - d->array;
251
int l = end - d->array;
253
memmove(d->array + f, d->array + l, (d->size-l)*sizeof(T));
258
template <typename T>
259
bool List<T>::operator==(const List<T> &v) const
261
if (d->size != v.d->size)
265
T* j = v.d->array + d->size;
272
template <typename T>
273
List<T> &List<T>::fill(const T &t, int size)
275
resize(size < 0 ? d->size : size);
277
T* i = d->array + d->size;
285
template <typename T>
286
List<T> &List<T>::operator+=(const List &l)
288
int newSize = d->size + l.d->size;
291
T *w = d->array + newSize;
292
T *i = l.d->array + l.d->size;
300
template <typename T>
301
int List<T>::indexOf(const T &t, int from) const
304
from = qMax(from + d->size, 0);
305
if (from < d->size) {
306
T* n = d->array + from - 1;
307
T* e = d->array + d->size;
315
template <typename T>
316
int List<T>::lastIndexOf(const T &t, int from) const
320
else if (from >= d->size)
324
T* n = d->array + from + 1;
333
template <typename T>
334
bool List<T>::contains(const T &t) const
337
T* i = d->array + d->size;
344
template <typename T>
345
int List<T>::count(const T &t) const
349
T* i = d->array + d->size;