~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/tools/qvarlengtharray.h

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#ifndef QVARLENGTHARRAY_H
 
30
#define QVARLENGTHARRAY_H
 
31
 
 
32
#include <QtCore/qglobal.h>
 
33
 
 
34
template<class T, int Prealloc = 256>
 
35
class QVarLengthArray
 
36
{
 
37
public:
 
38
    inline explicit QVarLengthArray(int size = 0);
 
39
 
 
40
    inline QVarLengthArray(const QVarLengthArray &other)
 
41
        : a(Prealloc), s(0), ptr(reinterpret_cast<T *>(array))
 
42
    {
 
43
        append(other.constData(), other.size());
 
44
    }
 
45
 
 
46
    inline ~QVarLengthArray() {
 
47
        if (QTypeInfo<T>::isComplex) {
 
48
            T *i = ptr + s;
 
49
            while (i-- != ptr)
 
50
                i->~T();
 
51
        }
 
52
        if (ptr != reinterpret_cast<T *>(array))
 
53
            qFree(ptr);
 
54
    }
 
55
    inline QVarLengthArray &operator=(const QVarLengthArray &other)
 
56
    {
 
57
        if (this != &other) {
 
58
            clear();
 
59
            append(other.constData(), other.size());
 
60
        }
 
61
        return *this;
 
62
    }
 
63
 
 
64
    inline int size() const { return s; }
 
65
    inline int count() const { return s; }
 
66
    inline bool isEmpty() const { return (s == 0); }
 
67
    inline void resize(int size);
 
68
    inline void clear() { resize(0); }
 
69
 
 
70
    inline int capacity() const { return a; }
 
71
    inline void reserve(int size);
 
72
 
 
73
    inline T &operator[](int idx) {
 
74
        Q_ASSERT(idx >= 0 && idx < s);
 
75
        return ptr[idx];
 
76
    }
 
77
    inline const T &operator[](int idx) const {
 
78
        Q_ASSERT(idx >= 0 && idx < s);
 
79
        return ptr[idx];
 
80
    }
 
81
 
 
82
    inline void append(const T &t) {
 
83
        const int idx = s;
 
84
        resize(idx + 1);
 
85
        ptr[idx] = t;
 
86
    }
 
87
    void append(const T *buf, int size);
 
88
 
 
89
    inline T *data() { return ptr; }
 
90
    inline const T *data() const { return ptr; }
 
91
    inline const T * constData() const { return ptr; }
 
92
 
 
93
private:
 
94
    void realloc(int size, int alloc);
 
95
 
 
96
    int a;
 
97
    int s;
 
98
    unsigned char array[Prealloc * sizeof(T)];
 
99
    T *ptr;
 
100
};
 
101
 
 
102
template <class T, int Prealloc>
 
103
Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(int asize)
 
104
    : s(asize) {
 
105
    if (s > Prealloc) {
 
106
        ptr = reinterpret_cast<T *>(qMalloc(s * sizeof(T)));
 
107
        a = s;
 
108
    } else {
 
109
        ptr = reinterpret_cast<T *>(array);
 
110
        a = Prealloc;
 
111
    }
 
112
    if (QTypeInfo<T>::isComplex) {
 
113
        T *i = ptr + s;
 
114
        while (i != ptr)
 
115
            new (--i) T;
 
116
    }
 
117
}
 
118
 
 
119
template <class T, int Prealloc>
 
120
Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::resize(int asize)
 
121
{ realloc(asize, qMax(asize, a)); }
 
122
 
 
123
template <class T, int Prealloc>
 
124
Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize)
 
125
{ if (asize > a) realloc(s, asize); }
 
126
 
 
127
template <class T, int Prealloc>
 
128
Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int asize)
 
129
{
 
130
    Q_ASSERT(abuf);
 
131
    if (asize <= 0)
 
132
        return;
 
133
 
 
134
    const int idx = s;
 
135
    resize(idx + asize);
 
136
 
 
137
    if (QTypeInfo<T>::isComplex) {
 
138
        T *i = ptr + idx;
 
139
        T *j = i + asize;
 
140
        while (i < j)
 
141
            new (i++) T(*abuf++);
 
142
    } else {
 
143
        qMemCopy(&ptr[idx], abuf, asize * sizeof(T));
 
144
    }
 
145
}
 
146
 
 
147
template <class T, int Prealloc>
 
148
Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
 
149
{
 
150
    Q_ASSERT(aalloc >= asize);
 
151
    T *oldPtr = ptr;
 
152
    int osize = s;
 
153
    s = asize;
 
154
 
 
155
    if (aalloc != a) {
 
156
        ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
 
157
        a = aalloc;
 
158
 
 
159
        if (QTypeInfo<T>::isStatic) {
 
160
            T *i = ptr + osize;
 
161
            T *j = oldPtr + osize;
 
162
            while (i != ptr) {
 
163
                new (--i) T(*--j);
 
164
                j->~T();
 
165
            }
 
166
        } else {
 
167
            qMemCopy(ptr, oldPtr, osize * sizeof(T));
 
168
        }
 
169
    }
 
170
 
 
171
    if (QTypeInfo<T>::isComplex) {
 
172
        if (asize < osize) {
 
173
            T *i = oldPtr + osize;
 
174
            T *j = oldPtr + asize;
 
175
            while (i-- != j)
 
176
                i->~T();
 
177
        } else {
 
178
            T *i = ptr + asize;
 
179
            T *j = ptr + osize;
 
180
            while (i != j)
 
181
                new (--i) T;
 
182
        }
 
183
    }
 
184
 
 
185
    if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
 
186
        qFree(oldPtr);
 
187
}
 
188
 
 
189
#endif // QVARLENGTHARRAY_H