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