79 inline int size() const { return siz; } |
83 inline int size() const { return siz; } |
80 inline Type *data() const { return buffer; } |
84 inline Type *data() const { return buffer; } |
81 |
85 |
82 inline Type &at(int i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } |
86 inline Type &at(int i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } |
83 inline const Type &at(int i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } |
87 inline const Type &at(int i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; } |
|
88 inline Type &last() { Q_ASSERT(!isEmpty()); return buffer[siz-1]; } |
84 inline const Type &last() const { Q_ASSERT(!isEmpty()); return buffer[siz-1]; } |
89 inline const Type &last() const { Q_ASSERT(!isEmpty()); return buffer[siz-1]; } |
|
90 inline Type &first() { Q_ASSERT(!isEmpty()); return buffer[0]; } |
85 inline const Type &first() const { Q_ASSERT(!isEmpty()); return buffer[0]; } |
91 inline const Type &first() const { Q_ASSERT(!isEmpty()); return buffer[0]; } |
86 |
92 |
87 inline void add(const Type &t) { |
93 inline void add(const Type &t) { |
88 reserve(siz + 1); |
94 reserve(siz + 1); |
89 buffer[siz] = t; |
95 buffer[siz] = t; |
90 ++siz; |
96 ++siz; |
|
97 } |
|
98 |
|
99 inline void pop_back() { |
|
100 Q_ASSERT(siz > 0); |
|
101 --siz; |
91 } |
102 } |
92 |
103 |
93 inline void resize(int size) { |
104 inline void resize(int size) { |
94 reserve(size); |
105 reserve(size); |
95 siz = size; |
106 siz = size; |
96 } |
107 } |
97 |
108 |
98 inline void reserve(int size) { |
109 inline void reserve(int size) { |
99 if (size > capacity) { |
110 if (size > capacity) { |
|
111 if (capacity == 0) |
|
112 capacity = 1; |
100 while (capacity < size) |
113 while (capacity < size) |
101 capacity *= 2; |
114 capacity *= 2; |
102 buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type)); |
115 buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type)); |
103 } |
116 } |
104 } |
117 } |
105 |
118 |
106 inline void shrink(int size) { |
119 inline void shrink(int size) { |
107 capacity = size; |
120 capacity = size; |
108 buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type)); |
121 if (size) |
|
122 buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type)); |
|
123 else { |
|
124 qFree(buffer); |
|
125 buffer = 0; |
|
126 } |
109 } |
127 } |
110 |
128 |
111 inline void swap(QDataBuffer<Type> &other) { |
129 inline void swap(QDataBuffer<Type> &other) { |
112 qSwap(capacity, other.capacity); |
130 qSwap(capacity, other.capacity); |
113 qSwap(siz, other.siz); |
131 qSwap(siz, other.siz); |