blob: 506acae14f00b6174eab83a4e1f5070a8b0becc7 [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
Mathias Agopianbdf73c72012-08-09 19:39:15 -070024#include <cutils/log.h>
25
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026#include <utils/VectorImpl.h>
27#include <utils/TypeHelpers.h>
28
29// ---------------------------------------------------------------------------
30
31namespace android {
32
Mathias Agopian320a2b42011-06-28 19:09:31 -070033template <typename TYPE>
34class SortedVector;
35
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080036/*!
37 * The main templated vector class ensuring type safety
38 * while making use of VectorImpl.
39 * This is the class users want to use.
40 */
41
42template <class TYPE>
43class Vector : private VectorImpl
44{
45public:
46 typedef TYPE value_type;
47
48 /*!
49 * Constructors and destructors
50 */
51
52 Vector();
53 Vector(const Vector<TYPE>& rhs);
Mathias Agopian320a2b42011-06-28 19:09:31 -070054 explicit Vector(const SortedVector<TYPE>& rhs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080055 virtual ~Vector();
56
57 /*! copy operator */
58 const Vector<TYPE>& operator = (const Vector<TYPE>& rhs) const;
59 Vector<TYPE>& operator = (const Vector<TYPE>& rhs);
60
Mathias Agopian320a2b42011-06-28 19:09:31 -070061 const Vector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const;
62 Vector<TYPE>& operator = (const SortedVector<TYPE>& rhs);
63
64 /*
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065 * empty the vector
66 */
67
68 inline void clear() { VectorImpl::clear(); }
69
70 /*!
71 * vector stats
72 */
73
74 //! returns number of items in the vector
75 inline size_t size() const { return VectorImpl::size(); }
Mathias Agopiane6bee122012-06-29 14:12:52 -070076 //! returns whether or not the vector is empty
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080077 inline bool isEmpty() const { return VectorImpl::isEmpty(); }
78 //! returns how many items can be stored without reallocating the backing store
79 inline size_t capacity() const { return VectorImpl::capacity(); }
Mathias Agopiane6bee122012-06-29 14:12:52 -070080 //! sets the capacity. capacity can never be reduced less than size()
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080081 inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); }
82
83 /*!
84 * C-style array access
85 */
86
87 //! read-only C-style access
88 inline const TYPE* array() const;
89 //! read-write C-style access
90 TYPE* editArray();
91
92 /*!
93 * accessors
94 */
95
96 //! read-only access to an item at a given index
97 inline const TYPE& operator [] (size_t index) const;
98 //! alternate name for operator []
99 inline const TYPE& itemAt(size_t index) const;
100 //! stack-usage of the vector. returns the top of the stack (last element)
101 const TYPE& top() const;
102 //! same as operator [], but allows to access the vector backward (from the end) with a negative index
103 const TYPE& mirrorItemAt(ssize_t index) const;
104
105 /*!
Mathias Agopiane6bee122012-06-29 14:12:52 -0700106 * modifying the array
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800107 */
108
109 //! copy-on write support, grants write access to an item
110 TYPE& editItemAt(size_t index);
Mathias Agopiane6bee122012-06-29 14:12:52 -0700111 //! grants right access to the top of the stack (last element)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800112 TYPE& editTop();
113
114 /*!
115 * append/insert another vector
116 */
117
118 //! insert another vector at a given index
119 ssize_t insertVectorAt(const Vector<TYPE>& vector, size_t index);
120
121 //! append another vector at the end of this one
122 ssize_t appendVector(const Vector<TYPE>& vector);
123
124
Jeff Brown66db6892010-04-22 18:58:52 -0700125 //! insert an array at a given index
Jeff Brown9efaaa42010-06-16 01:53:36 -0700126 ssize_t insertArrayAt(const TYPE* array, size_t index, size_t length);
Jeff Brown66db6892010-04-22 18:58:52 -0700127
128 //! append an array at the end of this vector
Jeff Brown9efaaa42010-06-16 01:53:36 -0700129 ssize_t appendArray(const TYPE* array, size_t length);
Jeff Brown66db6892010-04-22 18:58:52 -0700130
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131 /*!
132 * add/insert/replace items
133 */
134
135 //! insert one or several items initialized with their default constructor
136 inline ssize_t insertAt(size_t index, size_t numItems = 1);
Jeff Brown9efaaa42010-06-16 01:53:36 -0700137 //! insert one or several items initialized from a prototype item
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138 ssize_t insertAt(const TYPE& prototype_item, size_t index, size_t numItems = 1);
139 //! pop the top of the stack (removes the last element). No-op if the stack's empty
140 inline void pop();
141 //! pushes an item initialized with its default constructor
142 inline void push();
143 //! pushes an item on the top of the stack
144 void push(const TYPE& item);
145 //! same as push() but returns the index the item was added at (or an error)
146 inline ssize_t add();
147 //! same as push() but returns the index the item was added at (or an error)
148 ssize_t add(const TYPE& item);
149 //! replace an item with a new one initialized with its default constructor
150 inline ssize_t replaceAt(size_t index);
151 //! replace an item with a new one
152 ssize_t replaceAt(const TYPE& item, size_t index);
153
154 /*!
155 * remove items
156 */
157
158 //! remove several items
159 inline ssize_t removeItemsAt(size_t index, size_t count = 1);
160 //! remove one item
161 inline ssize_t removeAt(size_t index) { return removeItemsAt(index); }
162
163 /*!
164 * sort (stable) the array
165 */
166
167 typedef int (*compar_t)(const TYPE* lhs, const TYPE* rhs);
168 typedef int (*compar_r_t)(const TYPE* lhs, const TYPE* rhs, void* state);
169
170 inline status_t sort(compar_t cmp);
171 inline status_t sort(compar_r_t cmp, void* state);
172
Mathias Agopian7c123372011-03-16 23:18:07 -0700173 // for debugging only
174 inline size_t getItemSize() const { return itemSize(); }
175
Mathias Agopianbc55d722011-04-25 15:28:17 -0700176
177 /*
178 * these inlines add some level of compatibility with STL. eventually
179 * we should probably turn things around.
180 */
181 typedef TYPE* iterator;
182 typedef TYPE const* const_iterator;
183
184 inline iterator begin() { return editArray(); }
185 inline iterator end() { return editArray() + size(); }
186 inline const_iterator begin() const { return array(); }
187 inline const_iterator end() const { return array() + size(); }
188 inline void reserve(size_t n) { setCapacity(n); }
189 inline bool empty() const{ return isEmpty(); }
Andreas Huberf03fd0a2012-02-29 15:47:17 -0800190 inline void push_back(const TYPE& item) { insertAt(item, size(), 1); }
191 inline void push_front(const TYPE& item) { insertAt(item, 0, 1); }
Mathias Agopianbc55d722011-04-25 15:28:17 -0700192 inline iterator erase(iterator pos) {
193 return begin() + removeItemsAt(pos-array());
194 }
195
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800196protected:
197 virtual void do_construct(void* storage, size_t num) const;
198 virtual void do_destroy(void* storage, size_t num) const;
199 virtual void do_copy(void* dest, const void* from, size_t num) const;
200 virtual void do_splat(void* dest, const void* item, size_t num) const;
201 virtual void do_move_forward(void* dest, const void* from, size_t num) const;
202 virtual void do_move_backward(void* dest, const void* from, size_t num) const;
203};
204
Jeff Brown9a0a76d2012-03-16 14:45:49 -0700205// Vector<T> can be trivially moved using memcpy() because moving does not
206// require any change to the underlying SharedBuffer contents or reference count.
207template<typename T> struct trait_trivial_move<Vector<T> > { enum { value = true }; };
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800208
209// ---------------------------------------------------------------------------
210// No user serviceable parts from here...
211// ---------------------------------------------------------------------------
212
213template<class TYPE> inline
214Vector<TYPE>::Vector()
215 : VectorImpl(sizeof(TYPE),
216 ((traits<TYPE>::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0)
217 |(traits<TYPE>::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0)
Mathias Agopiana33bd162009-06-22 01:17:46 -0700218 |(traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0))
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800219 )
220{
221}
222
223template<class TYPE> inline
224Vector<TYPE>::Vector(const Vector<TYPE>& rhs)
225 : VectorImpl(rhs) {
226}
227
228template<class TYPE> inline
Mathias Agopian320a2b42011-06-28 19:09:31 -0700229Vector<TYPE>::Vector(const SortedVector<TYPE>& rhs)
230 : VectorImpl(static_cast<const VectorImpl&>(rhs)) {
231}
232
233template<class TYPE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800234Vector<TYPE>::~Vector() {
235 finish_vector();
236}
237
238template<class TYPE> inline
239Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) {
240 VectorImpl::operator = (rhs);
241 return *this;
242}
243
244template<class TYPE> inline
245const Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) const {
Mathias Agopian320a2b42011-06-28 19:09:31 -0700246 VectorImpl::operator = (static_cast<const VectorImpl&>(rhs));
247 return *this;
248}
249
250template<class TYPE> inline
251Vector<TYPE>& Vector<TYPE>::operator = (const SortedVector<TYPE>& rhs) {
252 VectorImpl::operator = (static_cast<const VectorImpl&>(rhs));
253 return *this;
254}
255
256template<class TYPE> inline
257const Vector<TYPE>& Vector<TYPE>::operator = (const SortedVector<TYPE>& rhs) const {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800258 VectorImpl::operator = (rhs);
259 return *this;
260}
261
262template<class TYPE> inline
263const TYPE* Vector<TYPE>::array() const {
264 return static_cast<const TYPE *>(arrayImpl());
265}
266
267template<class TYPE> inline
268TYPE* Vector<TYPE>::editArray() {
269 return static_cast<TYPE *>(editArrayImpl());
270}
271
272
273template<class TYPE> inline
274const TYPE& Vector<TYPE>::operator[](size_t index) const {
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700275 LOG_FATAL_IF(index>=size(),
276 "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__,
277 int(index), int(size()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800278 return *(array() + index);
279}
280
281template<class TYPE> inline
282const TYPE& Vector<TYPE>::itemAt(size_t index) const {
283 return operator[](index);
284}
285
286template<class TYPE> inline
287const TYPE& Vector<TYPE>::mirrorItemAt(ssize_t index) const {
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700288 const size_t i = index>0 ? index : -index;
289 LOG_FATAL_IF(index>=size(),
290 "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__,
291 int(index), int(size()));
292 return *(array() + i);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800293}
294
295template<class TYPE> inline
296const TYPE& Vector<TYPE>::top() const {
297 return *(array() + size() - 1);
298}
299
300template<class TYPE> inline
301TYPE& Vector<TYPE>::editItemAt(size_t index) {
302 return *( static_cast<TYPE *>(editItemLocation(index)) );
303}
304
305template<class TYPE> inline
306TYPE& Vector<TYPE>::editTop() {
307 return *( static_cast<TYPE *>(editItemLocation(size()-1)) );
308}
309
310template<class TYPE> inline
311ssize_t Vector<TYPE>::insertVectorAt(const Vector<TYPE>& vector, size_t index) {
312 return VectorImpl::insertVectorAt(reinterpret_cast<const VectorImpl&>(vector), index);
313}
314
315template<class TYPE> inline
316ssize_t Vector<TYPE>::appendVector(const Vector<TYPE>& vector) {
317 return VectorImpl::appendVector(reinterpret_cast<const VectorImpl&>(vector));
318}
319
320template<class TYPE> inline
Jeff Brown9efaaa42010-06-16 01:53:36 -0700321ssize_t Vector<TYPE>::insertArrayAt(const TYPE* array, size_t index, size_t length) {
322 return VectorImpl::insertArrayAt(array, index, length);
Jeff Brown66db6892010-04-22 18:58:52 -0700323}
324
325template<class TYPE> inline
Jeff Brown9efaaa42010-06-16 01:53:36 -0700326ssize_t Vector<TYPE>::appendArray(const TYPE* array, size_t length) {
327 return VectorImpl::appendArray(array, length);
Jeff Brown66db6892010-04-22 18:58:52 -0700328}
329
330template<class TYPE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800331ssize_t Vector<TYPE>::insertAt(const TYPE& item, size_t index, size_t numItems) {
332 return VectorImpl::insertAt(&item, index, numItems);
333}
334
335template<class TYPE> inline
336void Vector<TYPE>::push(const TYPE& item) {
337 return VectorImpl::push(&item);
338}
339
340template<class TYPE> inline
341ssize_t Vector<TYPE>::add(const TYPE& item) {
342 return VectorImpl::add(&item);
343}
344
345template<class TYPE> inline
346ssize_t Vector<TYPE>::replaceAt(const TYPE& item, size_t index) {
347 return VectorImpl::replaceAt(&item, index);
348}
349
350template<class TYPE> inline
351ssize_t Vector<TYPE>::insertAt(size_t index, size_t numItems) {
352 return VectorImpl::insertAt(index, numItems);
353}
354
355template<class TYPE> inline
356void Vector<TYPE>::pop() {
357 VectorImpl::pop();
358}
359
360template<class TYPE> inline
361void Vector<TYPE>::push() {
362 VectorImpl::push();
363}
364
365template<class TYPE> inline
366ssize_t Vector<TYPE>::add() {
367 return VectorImpl::add();
368}
369
370template<class TYPE> inline
371ssize_t Vector<TYPE>::replaceAt(size_t index) {
372 return VectorImpl::replaceAt(index);
373}
374
375template<class TYPE> inline
376ssize_t Vector<TYPE>::removeItemsAt(size_t index, size_t count) {
377 return VectorImpl::removeItemsAt(index, count);
378}
379
380template<class TYPE> inline
381status_t Vector<TYPE>::sort(Vector<TYPE>::compar_t cmp) {
382 return VectorImpl::sort((VectorImpl::compar_t)cmp);
383}
384
385template<class TYPE> inline
386status_t Vector<TYPE>::sort(Vector<TYPE>::compar_r_t cmp, void* state) {
387 return VectorImpl::sort((VectorImpl::compar_r_t)cmp, state);
388}
389
390// ---------------------------------------------------------------------------
391
392template<class TYPE>
393void Vector<TYPE>::do_construct(void* storage, size_t num) const {
394 construct_type( reinterpret_cast<TYPE*>(storage), num );
395}
396
397template<class TYPE>
398void Vector<TYPE>::do_destroy(void* storage, size_t num) const {
399 destroy_type( reinterpret_cast<TYPE*>(storage), num );
400}
401
402template<class TYPE>
403void Vector<TYPE>::do_copy(void* dest, const void* from, size_t num) const {
404 copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
405}
406
407template<class TYPE>
408void Vector<TYPE>::do_splat(void* dest, const void* item, size_t num) const {
409 splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num );
410}
411
412template<class TYPE>
413void Vector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const {
414 move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
415}
416
417template<class TYPE>
418void Vector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const {
419 move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
420}
421
422}; // namespace android
423
424
425// ---------------------------------------------------------------------------
426
427#endif // ANDROID_VECTOR_H