blob: 90477b75dd7d1117662900bd3c75a44ba9c57329 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_VECTOR_H
18#define ANDROID_VECTOR_H
19
20#include <new>
21#include <stdint.h>
22#include <sys/types.h>
23
24#include <utils/Log.h>
25#include <utils/VectorImpl.h>
26#include <utils/TypeHelpers.h>
27
28// ---------------------------------------------------------------------------
29
30namespace android {
31
32/*!
33 * The main templated vector class ensuring type safety
34 * while making use of VectorImpl.
35 * This is the class users want to use.
36 */
37
38template <class TYPE>
39class Vector : private VectorImpl
40{
41public:
42 typedef TYPE value_type;
43
44 /*!
45 * Constructors and destructors
46 */
47
48 Vector();
49 Vector(const Vector<TYPE>& rhs);
50 virtual ~Vector();
51
52 /*! copy operator */
53 const Vector<TYPE>& operator = (const Vector<TYPE>& rhs) const;
54 Vector<TYPE>& operator = (const Vector<TYPE>& rhs);
55
56 /*
57 * empty the vector
58 */
59
60 inline void clear() { VectorImpl::clear(); }
61
62 /*!
63 * vector stats
64 */
65
66 //! returns number of items in the vector
67 inline size_t size() const { return VectorImpl::size(); }
68 //! returns wether or not the vector is empty
69 inline bool isEmpty() const { return VectorImpl::isEmpty(); }
70 //! returns how many items can be stored without reallocating the backing store
71 inline size_t capacity() const { return VectorImpl::capacity(); }
72 //! setst the capacity. capacity can never be reduced less than size()
73 inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); }
74
75 /*!
76 * C-style array access
77 */
78
79 //! read-only C-style access
80 inline const TYPE* array() const;
81 //! read-write C-style access
82 TYPE* editArray();
83
84 /*!
85 * accessors
86 */
87
88 //! read-only access to an item at a given index
89 inline const TYPE& operator [] (size_t index) const;
90 //! alternate name for operator []
91 inline const TYPE& itemAt(size_t index) const;
92 //! stack-usage of the vector. returns the top of the stack (last element)
93 const TYPE& top() const;
94 //! same as operator [], but allows to access the vector backward (from the end) with a negative index
95 const TYPE& mirrorItemAt(ssize_t index) const;
96
97 /*!
98 * modifing the array
99 */
100
101 //! copy-on write support, grants write access to an item
102 TYPE& editItemAt(size_t index);
103 //! grants right acces to the top of the stack (last element)
104 TYPE& editTop();
105
106 /*!
107 * append/insert another vector
108 */
109
110 //! insert another vector at a given index
111 ssize_t insertVectorAt(const Vector<TYPE>& vector, size_t index);
112
113 //! append another vector at the end of this one
114 ssize_t appendVector(const Vector<TYPE>& vector);
115
116
Jeff Brown66db6892010-04-22 18:58:52 -0700117 //! insert an array at a given index
Jeff Brown9efaaa42010-06-16 01:53:36 -0700118 ssize_t insertArrayAt(const TYPE* array, size_t index, size_t length);
Jeff Brown66db6892010-04-22 18:58:52 -0700119
120 //! append an array at the end of this vector
Jeff Brown9efaaa42010-06-16 01:53:36 -0700121 ssize_t appendArray(const TYPE* array, size_t length);
Jeff Brown66db6892010-04-22 18:58:52 -0700122
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800123 /*!
124 * add/insert/replace items
125 */
126
127 //! insert one or several items initialized with their default constructor
128 inline ssize_t insertAt(size_t index, size_t numItems = 1);
Jeff Brown9efaaa42010-06-16 01:53:36 -0700129 //! insert one or several items initialized from a prototype item
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800130 ssize_t insertAt(const TYPE& prototype_item, size_t index, size_t numItems = 1);
131 //! pop the top of the stack (removes the last element). No-op if the stack's empty
132 inline void pop();
133 //! pushes an item initialized with its default constructor
134 inline void push();
135 //! pushes an item on the top of the stack
136 void push(const TYPE& item);
137 //! same as push() but returns the index the item was added at (or an error)
138 inline ssize_t add();
139 //! same as push() but returns the index the item was added at (or an error)
140 ssize_t add(const TYPE& item);
141 //! replace an item with a new one initialized with its default constructor
142 inline ssize_t replaceAt(size_t index);
143 //! replace an item with a new one
144 ssize_t replaceAt(const TYPE& item, size_t index);
145
146 /*!
147 * remove items
148 */
149
150 //! remove several items
151 inline ssize_t removeItemsAt(size_t index, size_t count = 1);
152 //! remove one item
153 inline ssize_t removeAt(size_t index) { return removeItemsAt(index); }
154
155 /*!
156 * sort (stable) the array
157 */
158
159 typedef int (*compar_t)(const TYPE* lhs, const TYPE* rhs);
160 typedef int (*compar_r_t)(const TYPE* lhs, const TYPE* rhs, void* state);
161
162 inline status_t sort(compar_t cmp);
163 inline status_t sort(compar_r_t cmp, void* state);
164
Mathias Agopian7c123372011-03-16 23:18:07 -0700165 // for debugging only
166 inline size_t getItemSize() const { return itemSize(); }
167
Mathias Agopianbc55d722011-04-25 15:28:17 -0700168
169 /*
170 * these inlines add some level of compatibility with STL. eventually
171 * we should probably turn things around.
172 */
173 typedef TYPE* iterator;
174 typedef TYPE const* const_iterator;
175
176 inline iterator begin() { return editArray(); }
177 inline iterator end() { return editArray() + size(); }
178 inline const_iterator begin() const { return array(); }
179 inline const_iterator end() const { return array() + size(); }
180 inline void reserve(size_t n) { setCapacity(n); }
181 inline bool empty() const{ return isEmpty(); }
182 inline void push_back(const TYPE& item) { insertAt(size(), item); }
183 inline void push_front(const TYPE& item) { insertAt(0, item); }
184 inline iterator erase(iterator pos) {
185 return begin() + removeItemsAt(pos-array());
186 }
187
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800188protected:
189 virtual void do_construct(void* storage, size_t num) const;
190 virtual void do_destroy(void* storage, size_t num) const;
191 virtual void do_copy(void* dest, const void* from, size_t num) const;
192 virtual void do_splat(void* dest, const void* item, size_t num) const;
193 virtual void do_move_forward(void* dest, const void* from, size_t num) const;
194 virtual void do_move_backward(void* dest, const void* from, size_t num) const;
195};
196
197
198// ---------------------------------------------------------------------------
199// No user serviceable parts from here...
200// ---------------------------------------------------------------------------
201
202template<class TYPE> inline
203Vector<TYPE>::Vector()
204 : VectorImpl(sizeof(TYPE),
205 ((traits<TYPE>::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0)
206 |(traits<TYPE>::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0)
Mathias Agopiana33bd162009-06-22 01:17:46 -0700207 |(traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0))
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800208 )
209{
210}
211
212template<class TYPE> inline
213Vector<TYPE>::Vector(const Vector<TYPE>& rhs)
214 : VectorImpl(rhs) {
215}
216
217template<class TYPE> inline
218Vector<TYPE>::~Vector() {
219 finish_vector();
220}
221
222template<class TYPE> inline
223Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) {
224 VectorImpl::operator = (rhs);
225 return *this;
226}
227
228template<class TYPE> inline
229const Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) const {
230 VectorImpl::operator = (rhs);
231 return *this;
232}
233
234template<class TYPE> inline
235const TYPE* Vector<TYPE>::array() const {
236 return static_cast<const TYPE *>(arrayImpl());
237}
238
239template<class TYPE> inline
240TYPE* Vector<TYPE>::editArray() {
241 return static_cast<TYPE *>(editArrayImpl());
242}
243
244
245template<class TYPE> inline
246const TYPE& Vector<TYPE>::operator[](size_t index) const {
247 LOG_FATAL_IF( index>=size(),
248 "itemAt: index %d is past size %d", (int)index, (int)size() );
249 return *(array() + index);
250}
251
252template<class TYPE> inline
253const TYPE& Vector<TYPE>::itemAt(size_t index) const {
254 return operator[](index);
255}
256
257template<class TYPE> inline
258const TYPE& Vector<TYPE>::mirrorItemAt(ssize_t index) const {
259 LOG_FATAL_IF( (index>0 ? index : -index)>=size(),
260 "mirrorItemAt: index %d is past size %d",
261 (int)index, (int)size() );
262 return *(array() + ((index<0) ? (size()-index) : index));
263}
264
265template<class TYPE> inline
266const TYPE& Vector<TYPE>::top() const {
267 return *(array() + size() - 1);
268}
269
270template<class TYPE> inline
271TYPE& Vector<TYPE>::editItemAt(size_t index) {
272 return *( static_cast<TYPE *>(editItemLocation(index)) );
273}
274
275template<class TYPE> inline
276TYPE& Vector<TYPE>::editTop() {
277 return *( static_cast<TYPE *>(editItemLocation(size()-1)) );
278}
279
280template<class TYPE> inline
281ssize_t Vector<TYPE>::insertVectorAt(const Vector<TYPE>& vector, size_t index) {
282 return VectorImpl::insertVectorAt(reinterpret_cast<const VectorImpl&>(vector), index);
283}
284
285template<class TYPE> inline
286ssize_t Vector<TYPE>::appendVector(const Vector<TYPE>& vector) {
287 return VectorImpl::appendVector(reinterpret_cast<const VectorImpl&>(vector));
288}
289
290template<class TYPE> inline
Jeff Brown9efaaa42010-06-16 01:53:36 -0700291ssize_t Vector<TYPE>::insertArrayAt(const TYPE* array, size_t index, size_t length) {
292 return VectorImpl::insertArrayAt(array, index, length);
Jeff Brown66db6892010-04-22 18:58:52 -0700293}
294
295template<class TYPE> inline
Jeff Brown9efaaa42010-06-16 01:53:36 -0700296ssize_t Vector<TYPE>::appendArray(const TYPE* array, size_t length) {
297 return VectorImpl::appendArray(array, length);
Jeff Brown66db6892010-04-22 18:58:52 -0700298}
299
300template<class TYPE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800301ssize_t Vector<TYPE>::insertAt(const TYPE& item, size_t index, size_t numItems) {
302 return VectorImpl::insertAt(&item, index, numItems);
303}
304
305template<class TYPE> inline
306void Vector<TYPE>::push(const TYPE& item) {
307 return VectorImpl::push(&item);
308}
309
310template<class TYPE> inline
311ssize_t Vector<TYPE>::add(const TYPE& item) {
312 return VectorImpl::add(&item);
313}
314
315template<class TYPE> inline
316ssize_t Vector<TYPE>::replaceAt(const TYPE& item, size_t index) {
317 return VectorImpl::replaceAt(&item, index);
318}
319
320template<class TYPE> inline
321ssize_t Vector<TYPE>::insertAt(size_t index, size_t numItems) {
322 return VectorImpl::insertAt(index, numItems);
323}
324
325template<class TYPE> inline
326void Vector<TYPE>::pop() {
327 VectorImpl::pop();
328}
329
330template<class TYPE> inline
331void Vector<TYPE>::push() {
332 VectorImpl::push();
333}
334
335template<class TYPE> inline
336ssize_t Vector<TYPE>::add() {
337 return VectorImpl::add();
338}
339
340template<class TYPE> inline
341ssize_t Vector<TYPE>::replaceAt(size_t index) {
342 return VectorImpl::replaceAt(index);
343}
344
345template<class TYPE> inline
346ssize_t Vector<TYPE>::removeItemsAt(size_t index, size_t count) {
347 return VectorImpl::removeItemsAt(index, count);
348}
349
350template<class TYPE> inline
351status_t Vector<TYPE>::sort(Vector<TYPE>::compar_t cmp) {
352 return VectorImpl::sort((VectorImpl::compar_t)cmp);
353}
354
355template<class TYPE> inline
356status_t Vector<TYPE>::sort(Vector<TYPE>::compar_r_t cmp, void* state) {
357 return VectorImpl::sort((VectorImpl::compar_r_t)cmp, state);
358}
359
360// ---------------------------------------------------------------------------
361
362template<class TYPE>
363void Vector<TYPE>::do_construct(void* storage, size_t num) const {
364 construct_type( reinterpret_cast<TYPE*>(storage), num );
365}
366
367template<class TYPE>
368void Vector<TYPE>::do_destroy(void* storage, size_t num) const {
369 destroy_type( reinterpret_cast<TYPE*>(storage), num );
370}
371
372template<class TYPE>
373void Vector<TYPE>::do_copy(void* dest, const void* from, size_t num) const {
374 copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
375}
376
377template<class TYPE>
378void Vector<TYPE>::do_splat(void* dest, const void* item, size_t num) const {
379 splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num );
380}
381
382template<class TYPE>
383void Vector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const {
384 move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
385}
386
387template<class TYPE>
388void Vector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const {
389 move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
390}
391
392}; // namespace android
393
394
395// ---------------------------------------------------------------------------
396
397#endif // ANDROID_VECTOR_H