1 /*
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
30 namespace android {
31
32 template <typename TYPE>
33 class SortedVector;
34
35 /*!
36 * The main templated vector class ensuring type safety
37 * while making use of VectorImpl.
38 * This is the class users want to use.
39 */
40
41 template <class TYPE>
42 class Vector : private VectorImpl
43 {
44 public:
45 typedef TYPE value_type;
46
47 /*!
48 * Constructors and destructors
49 */
50
51 Vector();
52 Vector(const Vector<TYPE>& rhs);
53 explicit Vector(const SortedVector<TYPE>& rhs);
54 virtual ~Vector();
55
56 /*! copy operator */
57 const Vector<TYPE>& operator = (const Vector<TYPE>& rhs) const;
58 Vector<TYPE>& operator = (const Vector<TYPE>& rhs);
59
60 const Vector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const;
61 Vector<TYPE>& operator = (const SortedVector<TYPE>& rhs);
62
63 /*
64 * empty the vector
65 */
66
clear()67 inline void clear() { VectorImpl::clear(); }
68
69 /*!
70 * vector stats
71 */
72
73 //! returns number of items in the vector
size()74 inline size_t size() const { return VectorImpl::size(); }
75 //! returns wether or not the vector is empty
isEmpty()76 inline bool isEmpty() const { return VectorImpl::isEmpty(); }
77 //! returns how many items can be stored without reallocating the backing store
capacity()78 inline size_t capacity() const { return VectorImpl::capacity(); }
79 //! setst the capacity. capacity can never be reduced less than size()
setCapacity(size_t size)80 inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); }
81
82 /*!
83 * C-style array access
84 */
85
86 //! read-only C-style access
87 inline const TYPE* array() const;
88 //! read-write C-style access
89 TYPE* editArray();
90
91 /*!
92 * accessors
93 */
94
95 //! read-only access to an item at a given index
96 inline const TYPE& operator [] (size_t index) const;
97 //! alternate name for operator []
98 inline const TYPE& itemAt(size_t index) const;
99 //! stack-usage of the vector. returns the top of the stack (last element)
100 const TYPE& top() const;
101 //! same as operator [], but allows to access the vector backward (from the end) with a negative index
102 const TYPE& mirrorItemAt(ssize_t index) const;
103
104 /*!
105 * modifing the array
106 */
107
108 //! copy-on write support, grants write access to an item
109 TYPE& editItemAt(size_t index);
110 //! grants right acces to the top of the stack (last element)
111 TYPE& editTop();
112
113 /*!
114 * append/insert another vector
115 */
116
117 //! insert another vector at a given index
118 ssize_t insertVectorAt(const Vector<TYPE>& vector, size_t index);
119
120 //! append another vector at the end of this one
121 ssize_t appendVector(const Vector<TYPE>& vector);
122
123
124 //! insert an array at a given index
125 ssize_t insertArrayAt(const TYPE* array, size_t index, size_t length);
126
127 //! append an array at the end of this vector
128 ssize_t appendArray(const TYPE* array, size_t length);
129
130 /*!
131 * add/insert/replace items
132 */
133
134 //! insert one or several items initialized with their default constructor
135 inline ssize_t insertAt(size_t index, size_t numItems = 1);
136 //! insert one or several items initialized from a prototype item
137 ssize_t insertAt(const TYPE& prototype_item, size_t index, size_t numItems = 1);
138 //! pop the top of the stack (removes the last element). No-op if the stack's empty
139 inline void pop();
140 //! pushes an item initialized with its default constructor
141 inline void push();
142 //! pushes an item on the top of the stack
143 void push(const TYPE& item);
144 //! same as push() but returns the index the item was added at (or an error)
145 inline ssize_t add();
146 //! same as push() but returns the index the item was added at (or an error)
147 ssize_t add(const TYPE& item);
148 //! replace an item with a new one initialized with its default constructor
149 inline ssize_t replaceAt(size_t index);
150 //! replace an item with a new one
151 ssize_t replaceAt(const TYPE& item, size_t index);
152
153 /*!
154 * remove items
155 */
156
157 //! remove several items
158 inline ssize_t removeItemsAt(size_t index, size_t count = 1);
159 //! remove one item
removeAt(size_t index)160 inline ssize_t removeAt(size_t index) { return removeItemsAt(index); }
161
162 /*!
163 * sort (stable) the array
164 */
165
166 typedef int (*compar_t)(const TYPE* lhs, const TYPE* rhs);
167 typedef int (*compar_r_t)(const TYPE* lhs, const TYPE* rhs, void* state);
168
169 inline status_t sort(compar_t cmp);
170 inline status_t sort(compar_r_t cmp, void* state);
171
172 // for debugging only
getItemSize()173 inline size_t getItemSize() const { return itemSize(); }
174
175
176 /*
177 * these inlines add some level of compatibility with STL. eventually
178 * we should probably turn things around.
179 */
180 typedef TYPE* iterator;
181 typedef TYPE const* const_iterator;
182
begin()183 inline iterator begin() { return editArray(); }
end()184 inline iterator end() { return editArray() + size(); }
begin()185 inline const_iterator begin() const { return array(); }
end()186 inline const_iterator end() const { return array() + size(); }
reserve(size_t n)187 inline void reserve(size_t n) { setCapacity(n); }
empty()188 inline bool empty() const{ return isEmpty(); }
push_back(const TYPE & item)189 inline void push_back(const TYPE& item) { insertAt(item, size()); }
push_front(const TYPE & item)190 inline void push_front(const TYPE& item) { insertAt(item, 0); }
erase(iterator pos)191 inline iterator erase(iterator pos) {
192 return begin() + removeItemsAt(pos-array());
193 }
194
195 protected:
196 virtual void do_construct(void* storage, size_t num) const;
197 virtual void do_destroy(void* storage, size_t num) const;
198 virtual void do_copy(void* dest, const void* from, size_t num) const;
199 virtual void do_splat(void* dest, const void* item, size_t num) const;
200 virtual void do_move_forward(void* dest, const void* from, size_t num) const;
201 virtual void do_move_backward(void* dest, const void* from, size_t num) const;
202 };
203
204
205 // ---------------------------------------------------------------------------
206 // No user serviceable parts from here...
207 // ---------------------------------------------------------------------------
208
209 template<class TYPE> inline
Vector()210 Vector<TYPE>::Vector()
211 : VectorImpl(sizeof(TYPE),
212 ((traits<TYPE>::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0)
213 |(traits<TYPE>::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0)
214 |(traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0))
215 )
216 {
217 }
218
219 template<class TYPE> inline
Vector(const Vector<TYPE> & rhs)220 Vector<TYPE>::Vector(const Vector<TYPE>& rhs)
221 : VectorImpl(rhs) {
222 }
223
224 template<class TYPE> inline
Vector(const SortedVector<TYPE> & rhs)225 Vector<TYPE>::Vector(const SortedVector<TYPE>& rhs)
226 : VectorImpl(static_cast<const VectorImpl&>(rhs)) {
227 }
228
229 template<class TYPE> inline
~Vector()230 Vector<TYPE>::~Vector() {
231 finish_vector();
232 }
233
234 template<class TYPE> inline
235 Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) {
236 VectorImpl::operator = (rhs);
237 return *this;
238 }
239
240 template<class TYPE> inline
241 const Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) const {
242 VectorImpl::operator = (static_cast<const VectorImpl&>(rhs));
243 return *this;
244 }
245
246 template<class TYPE> inline
247 Vector<TYPE>& Vector<TYPE>::operator = (const SortedVector<TYPE>& rhs) {
248 VectorImpl::operator = (static_cast<const VectorImpl&>(rhs));
249 return *this;
250 }
251
252 template<class TYPE> inline
253 const Vector<TYPE>& Vector<TYPE>::operator = (const SortedVector<TYPE>& rhs) const {
254 VectorImpl::operator = (rhs);
255 return *this;
256 }
257
258 template<class TYPE> inline
array()259 const TYPE* Vector<TYPE>::array() const {
260 return static_cast<const TYPE *>(arrayImpl());
261 }
262
263 template<class TYPE> inline
editArray()264 TYPE* Vector<TYPE>::editArray() {
265 return static_cast<TYPE *>(editArrayImpl());
266 }
267
268
269 template<class TYPE> inline
270 const TYPE& Vector<TYPE>::operator[](size_t index) const {
271 LOG_FATAL_IF( index>=size(),
272 "itemAt: index %d is past size %d", (int)index, (int)size() );
273 return *(array() + index);
274 }
275
276 template<class TYPE> inline
itemAt(size_t index)277 const TYPE& Vector<TYPE>::itemAt(size_t index) const {
278 return operator[](index);
279 }
280
281 template<class TYPE> inline
mirrorItemAt(ssize_t index)282 const TYPE& Vector<TYPE>::mirrorItemAt(ssize_t index) const {
283 LOG_FATAL_IF( (index>0 ? index : -index)>=size(),
284 "mirrorItemAt: index %d is past size %d",
285 (int)index, (int)size() );
286 return *(array() + ((index<0) ? (size()-index) : index));
287 }
288
289 template<class TYPE> inline
top()290 const TYPE& Vector<TYPE>::top() const {
291 return *(array() + size() - 1);
292 }
293
294 template<class TYPE> inline
editItemAt(size_t index)295 TYPE& Vector<TYPE>::editItemAt(size_t index) {
296 return *( static_cast<TYPE *>(editItemLocation(index)) );
297 }
298
299 template<class TYPE> inline
editTop()300 TYPE& Vector<TYPE>::editTop() {
301 return *( static_cast<TYPE *>(editItemLocation(size()-1)) );
302 }
303
304 template<class TYPE> inline
insertVectorAt(const Vector<TYPE> & vector,size_t index)305 ssize_t Vector<TYPE>::insertVectorAt(const Vector<TYPE>& vector, size_t index) {
306 return VectorImpl::insertVectorAt(reinterpret_cast<const VectorImpl&>(vector), index);
307 }
308
309 template<class TYPE> inline
appendVector(const Vector<TYPE> & vector)310 ssize_t Vector<TYPE>::appendVector(const Vector<TYPE>& vector) {
311 return VectorImpl::appendVector(reinterpret_cast<const VectorImpl&>(vector));
312 }
313
314 template<class TYPE> inline
insertArrayAt(const TYPE * array,size_t index,size_t length)315 ssize_t Vector<TYPE>::insertArrayAt(const TYPE* array, size_t index, size_t length) {
316 return VectorImpl::insertArrayAt(array, index, length);
317 }
318
319 template<class TYPE> inline
appendArray(const TYPE * array,size_t length)320 ssize_t Vector<TYPE>::appendArray(const TYPE* array, size_t length) {
321 return VectorImpl::appendArray(array, length);
322 }
323
324 template<class TYPE> inline
insertAt(const TYPE & item,size_t index,size_t numItems)325 ssize_t Vector<TYPE>::insertAt(const TYPE& item, size_t index, size_t numItems) {
326 return VectorImpl::insertAt(&item, index, numItems);
327 }
328
329 template<class TYPE> inline
push(const TYPE & item)330 void Vector<TYPE>::push(const TYPE& item) {
331 return VectorImpl::push(&item);
332 }
333
334 template<class TYPE> inline
add(const TYPE & item)335 ssize_t Vector<TYPE>::add(const TYPE& item) {
336 return VectorImpl::add(&item);
337 }
338
339 template<class TYPE> inline
replaceAt(const TYPE & item,size_t index)340 ssize_t Vector<TYPE>::replaceAt(const TYPE& item, size_t index) {
341 return VectorImpl::replaceAt(&item, index);
342 }
343
344 template<class TYPE> inline
insertAt(size_t index,size_t numItems)345 ssize_t Vector<TYPE>::insertAt(size_t index, size_t numItems) {
346 return VectorImpl::insertAt(index, numItems);
347 }
348
349 template<class TYPE> inline
pop()350 void Vector<TYPE>::pop() {
351 VectorImpl::pop();
352 }
353
354 template<class TYPE> inline
push()355 void Vector<TYPE>::push() {
356 VectorImpl::push();
357 }
358
359 template<class TYPE> inline
add()360 ssize_t Vector<TYPE>::add() {
361 return VectorImpl::add();
362 }
363
364 template<class TYPE> inline
replaceAt(size_t index)365 ssize_t Vector<TYPE>::replaceAt(size_t index) {
366 return VectorImpl::replaceAt(index);
367 }
368
369 template<class TYPE> inline
removeItemsAt(size_t index,size_t count)370 ssize_t Vector<TYPE>::removeItemsAt(size_t index, size_t count) {
371 return VectorImpl::removeItemsAt(index, count);
372 }
373
374 template<class TYPE> inline
sort(Vector<TYPE>::compar_t cmp)375 status_t Vector<TYPE>::sort(Vector<TYPE>::compar_t cmp) {
376 return VectorImpl::sort((VectorImpl::compar_t)cmp);
377 }
378
379 template<class TYPE> inline
sort(Vector<TYPE>::compar_r_t cmp,void * state)380 status_t Vector<TYPE>::sort(Vector<TYPE>::compar_r_t cmp, void* state) {
381 return VectorImpl::sort((VectorImpl::compar_r_t)cmp, state);
382 }
383
384 // ---------------------------------------------------------------------------
385
386 template<class TYPE>
do_construct(void * storage,size_t num)387 void Vector<TYPE>::do_construct(void* storage, size_t num) const {
388 construct_type( reinterpret_cast<TYPE*>(storage), num );
389 }
390
391 template<class TYPE>
do_destroy(void * storage,size_t num)392 void Vector<TYPE>::do_destroy(void* storage, size_t num) const {
393 destroy_type( reinterpret_cast<TYPE*>(storage), num );
394 }
395
396 template<class TYPE>
do_copy(void * dest,const void * from,size_t num)397 void Vector<TYPE>::do_copy(void* dest, const void* from, size_t num) const {
398 copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
399 }
400
401 template<class TYPE>
do_splat(void * dest,const void * item,size_t num)402 void Vector<TYPE>::do_splat(void* dest, const void* item, size_t num) const {
403 splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num );
404 }
405
406 template<class TYPE>
do_move_forward(void * dest,const void * from,size_t num)407 void Vector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const {
408 move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
409 }
410
411 template<class TYPE>
do_move_backward(void * dest,const void * from,size_t num)412 void Vector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const {
413 move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
414 }
415
416 }; // namespace android
417
418
419 // ---------------------------------------------------------------------------
420
421 #endif // ANDROID_VECTOR_H
422