The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame^] | 24 | #include <cutils/log.h> |
| 25 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <utils/VectorImpl.h> |
| 27 | #include <utils/TypeHelpers.h> |
| 28 | |
| 29 | // --------------------------------------------------------------------------- |
| 30 | |
| 31 | namespace android { |
| 32 | |
Mathias Agopian | 320a2b4 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 33 | template <typename TYPE> |
| 34 | class SortedVector; |
| 35 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | /*! |
| 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 | |
| 42 | template <class TYPE> |
| 43 | class Vector : private VectorImpl |
| 44 | { |
| 45 | public: |
| 46 | typedef TYPE value_type; |
| 47 | |
| 48 | /*! |
| 49 | * Constructors and destructors |
| 50 | */ |
| 51 | |
| 52 | Vector(); |
| 53 | Vector(const Vector<TYPE>& rhs); |
Mathias Agopian | 320a2b4 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 54 | explicit Vector(const SortedVector<TYPE>& rhs); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | 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 Agopian | 320a2b4 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 61 | const Vector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const; |
| 62 | Vector<TYPE>& operator = (const SortedVector<TYPE>& rhs); |
| 63 | |
| 64 | /* |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | * 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 Agopian | e6bee12 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 76 | //! returns whether or not the vector is empty |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | 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 Agopian | e6bee12 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 80 | //! sets the capacity. capacity can never be reduced less than size() |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | 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 Agopian | e6bee12 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 106 | * modifying the array |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | */ |
| 108 | |
| 109 | //! copy-on write support, grants write access to an item |
| 110 | TYPE& editItemAt(size_t index); |
Mathias Agopian | e6bee12 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 111 | //! grants right access to the top of the stack (last element) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | 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 Brown | 66db689 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 125 | //! insert an array at a given index |
Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 126 | ssize_t insertArrayAt(const TYPE* array, size_t index, size_t length); |
Jeff Brown | 66db689 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 127 | |
| 128 | //! append an array at the end of this vector |
Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 129 | ssize_t appendArray(const TYPE* array, size_t length); |
Jeff Brown | 66db689 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 130 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | /*! |
| 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 Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 137 | //! insert one or several items initialized from a prototype item |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | 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 Agopian | 7c12337 | 2011-03-16 23:18:07 -0700 | [diff] [blame] | 173 | // for debugging only |
| 174 | inline size_t getItemSize() const { return itemSize(); } |
| 175 | |
Mathias Agopian | bc55d72 | 2011-04-25 15:28:17 -0700 | [diff] [blame] | 176 | |
| 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 Huber | f03fd0a | 2012-02-29 15:47:17 -0800 | [diff] [blame] | 190 | inline void push_back(const TYPE& item) { insertAt(item, size(), 1); } |
| 191 | inline void push_front(const TYPE& item) { insertAt(item, 0, 1); } |
Mathias Agopian | bc55d72 | 2011-04-25 15:28:17 -0700 | [diff] [blame] | 192 | inline iterator erase(iterator pos) { |
| 193 | return begin() + removeItemsAt(pos-array()); |
| 194 | } |
| 195 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | protected: |
| 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 Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 205 | // Vector<T> can be trivially moved using memcpy() because moving does not |
| 206 | // require any change to the underlying SharedBuffer contents or reference count. |
| 207 | template<typename T> struct trait_trivial_move<Vector<T> > { enum { value = true }; }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
| 209 | // --------------------------------------------------------------------------- |
| 210 | // No user serviceable parts from here... |
| 211 | // --------------------------------------------------------------------------- |
| 212 | |
| 213 | template<class TYPE> inline |
| 214 | Vector<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 Agopian | a33bd16 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 218 | |(traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0)) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | ) |
| 220 | { |
| 221 | } |
| 222 | |
| 223 | template<class TYPE> inline |
| 224 | Vector<TYPE>::Vector(const Vector<TYPE>& rhs) |
| 225 | : VectorImpl(rhs) { |
| 226 | } |
| 227 | |
| 228 | template<class TYPE> inline |
Mathias Agopian | 320a2b4 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 229 | Vector<TYPE>::Vector(const SortedVector<TYPE>& rhs) |
| 230 | : VectorImpl(static_cast<const VectorImpl&>(rhs)) { |
| 231 | } |
| 232 | |
| 233 | template<class TYPE> inline |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | Vector<TYPE>::~Vector() { |
| 235 | finish_vector(); |
| 236 | } |
| 237 | |
| 238 | template<class TYPE> inline |
| 239 | Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) { |
| 240 | VectorImpl::operator = (rhs); |
| 241 | return *this; |
| 242 | } |
| 243 | |
| 244 | template<class TYPE> inline |
| 245 | const Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) const { |
Mathias Agopian | 320a2b4 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 246 | VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)); |
| 247 | return *this; |
| 248 | } |
| 249 | |
| 250 | template<class TYPE> inline |
| 251 | Vector<TYPE>& Vector<TYPE>::operator = (const SortedVector<TYPE>& rhs) { |
| 252 | VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)); |
| 253 | return *this; |
| 254 | } |
| 255 | |
| 256 | template<class TYPE> inline |
| 257 | const Vector<TYPE>& Vector<TYPE>::operator = (const SortedVector<TYPE>& rhs) const { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | VectorImpl::operator = (rhs); |
| 259 | return *this; |
| 260 | } |
| 261 | |
| 262 | template<class TYPE> inline |
| 263 | const TYPE* Vector<TYPE>::array() const { |
| 264 | return static_cast<const TYPE *>(arrayImpl()); |
| 265 | } |
| 266 | |
| 267 | template<class TYPE> inline |
| 268 | TYPE* Vector<TYPE>::editArray() { |
| 269 | return static_cast<TYPE *>(editArrayImpl()); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | template<class TYPE> inline |
| 274 | const TYPE& Vector<TYPE>::operator[](size_t index) const { |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame^] | 275 | LOG_FATAL_IF(index>=size(), |
| 276 | "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__, |
| 277 | int(index), int(size())); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | return *(array() + index); |
| 279 | } |
| 280 | |
| 281 | template<class TYPE> inline |
| 282 | const TYPE& Vector<TYPE>::itemAt(size_t index) const { |
| 283 | return operator[](index); |
| 284 | } |
| 285 | |
| 286 | template<class TYPE> inline |
| 287 | const TYPE& Vector<TYPE>::mirrorItemAt(ssize_t index) const { |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame^] | 288 | 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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | template<class TYPE> inline |
| 296 | const TYPE& Vector<TYPE>::top() const { |
| 297 | return *(array() + size() - 1); |
| 298 | } |
| 299 | |
| 300 | template<class TYPE> inline |
| 301 | TYPE& Vector<TYPE>::editItemAt(size_t index) { |
| 302 | return *( static_cast<TYPE *>(editItemLocation(index)) ); |
| 303 | } |
| 304 | |
| 305 | template<class TYPE> inline |
| 306 | TYPE& Vector<TYPE>::editTop() { |
| 307 | return *( static_cast<TYPE *>(editItemLocation(size()-1)) ); |
| 308 | } |
| 309 | |
| 310 | template<class TYPE> inline |
| 311 | ssize_t Vector<TYPE>::insertVectorAt(const Vector<TYPE>& vector, size_t index) { |
| 312 | return VectorImpl::insertVectorAt(reinterpret_cast<const VectorImpl&>(vector), index); |
| 313 | } |
| 314 | |
| 315 | template<class TYPE> inline |
| 316 | ssize_t Vector<TYPE>::appendVector(const Vector<TYPE>& vector) { |
| 317 | return VectorImpl::appendVector(reinterpret_cast<const VectorImpl&>(vector)); |
| 318 | } |
| 319 | |
| 320 | template<class TYPE> inline |
Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 321 | ssize_t Vector<TYPE>::insertArrayAt(const TYPE* array, size_t index, size_t length) { |
| 322 | return VectorImpl::insertArrayAt(array, index, length); |
Jeff Brown | 66db689 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | template<class TYPE> inline |
Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 326 | ssize_t Vector<TYPE>::appendArray(const TYPE* array, size_t length) { |
| 327 | return VectorImpl::appendArray(array, length); |
Jeff Brown | 66db689 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | template<class TYPE> inline |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | ssize_t Vector<TYPE>::insertAt(const TYPE& item, size_t index, size_t numItems) { |
| 332 | return VectorImpl::insertAt(&item, index, numItems); |
| 333 | } |
| 334 | |
| 335 | template<class TYPE> inline |
| 336 | void Vector<TYPE>::push(const TYPE& item) { |
| 337 | return VectorImpl::push(&item); |
| 338 | } |
| 339 | |
| 340 | template<class TYPE> inline |
| 341 | ssize_t Vector<TYPE>::add(const TYPE& item) { |
| 342 | return VectorImpl::add(&item); |
| 343 | } |
| 344 | |
| 345 | template<class TYPE> inline |
| 346 | ssize_t Vector<TYPE>::replaceAt(const TYPE& item, size_t index) { |
| 347 | return VectorImpl::replaceAt(&item, index); |
| 348 | } |
| 349 | |
| 350 | template<class TYPE> inline |
| 351 | ssize_t Vector<TYPE>::insertAt(size_t index, size_t numItems) { |
| 352 | return VectorImpl::insertAt(index, numItems); |
| 353 | } |
| 354 | |
| 355 | template<class TYPE> inline |
| 356 | void Vector<TYPE>::pop() { |
| 357 | VectorImpl::pop(); |
| 358 | } |
| 359 | |
| 360 | template<class TYPE> inline |
| 361 | void Vector<TYPE>::push() { |
| 362 | VectorImpl::push(); |
| 363 | } |
| 364 | |
| 365 | template<class TYPE> inline |
| 366 | ssize_t Vector<TYPE>::add() { |
| 367 | return VectorImpl::add(); |
| 368 | } |
| 369 | |
| 370 | template<class TYPE> inline |
| 371 | ssize_t Vector<TYPE>::replaceAt(size_t index) { |
| 372 | return VectorImpl::replaceAt(index); |
| 373 | } |
| 374 | |
| 375 | template<class TYPE> inline |
| 376 | ssize_t Vector<TYPE>::removeItemsAt(size_t index, size_t count) { |
| 377 | return VectorImpl::removeItemsAt(index, count); |
| 378 | } |
| 379 | |
| 380 | template<class TYPE> inline |
| 381 | status_t Vector<TYPE>::sort(Vector<TYPE>::compar_t cmp) { |
| 382 | return VectorImpl::sort((VectorImpl::compar_t)cmp); |
| 383 | } |
| 384 | |
| 385 | template<class TYPE> inline |
| 386 | status_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 | |
| 392 | template<class TYPE> |
| 393 | void Vector<TYPE>::do_construct(void* storage, size_t num) const { |
| 394 | construct_type( reinterpret_cast<TYPE*>(storage), num ); |
| 395 | } |
| 396 | |
| 397 | template<class TYPE> |
| 398 | void Vector<TYPE>::do_destroy(void* storage, size_t num) const { |
| 399 | destroy_type( reinterpret_cast<TYPE*>(storage), num ); |
| 400 | } |
| 401 | |
| 402 | template<class TYPE> |
| 403 | void 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 | |
| 407 | template<class TYPE> |
| 408 | void 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 | |
| 412 | template<class TYPE> |
| 413 | void 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 | |
| 417 | template<class TYPE> |
| 418 | void 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 |