The Android Open Source Project | 9066cfe | 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 | |
| 24 | #include <utils/Log.h> |
| 25 | #include <utils/VectorImpl.h> |
| 26 | #include <utils/TypeHelpers.h> |
| 27 | |
| 28 | // --------------------------------------------------------------------------- |
| 29 | |
| 30 | namespace 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 | |
| 38 | template <class TYPE> |
| 39 | class Vector : private VectorImpl |
| 40 | { |
| 41 | public: |
| 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 | |
| 117 | /*! |
| 118 | * add/insert/replace items |
| 119 | */ |
| 120 | |
| 121 | //! insert one or several items initialized with their default constructor |
| 122 | inline ssize_t insertAt(size_t index, size_t numItems = 1); |
| 123 | //! insert on onr several items initialized from a prototype item |
| 124 | ssize_t insertAt(const TYPE& prototype_item, size_t index, size_t numItems = 1); |
| 125 | //! pop the top of the stack (removes the last element). No-op if the stack's empty |
| 126 | inline void pop(); |
| 127 | //! pushes an item initialized with its default constructor |
| 128 | inline void push(); |
| 129 | //! pushes an item on the top of the stack |
| 130 | void push(const TYPE& item); |
| 131 | //! same as push() but returns the index the item was added at (or an error) |
| 132 | inline ssize_t add(); |
| 133 | //! same as push() but returns the index the item was added at (or an error) |
| 134 | ssize_t add(const TYPE& item); |
| 135 | //! replace an item with a new one initialized with its default constructor |
| 136 | inline ssize_t replaceAt(size_t index); |
| 137 | //! replace an item with a new one |
| 138 | ssize_t replaceAt(const TYPE& item, size_t index); |
| 139 | |
| 140 | /*! |
| 141 | * remove items |
| 142 | */ |
| 143 | |
| 144 | //! remove several items |
| 145 | inline ssize_t removeItemsAt(size_t index, size_t count = 1); |
| 146 | //! remove one item |
| 147 | inline ssize_t removeAt(size_t index) { return removeItemsAt(index); } |
| 148 | |
| 149 | /*! |
| 150 | * sort (stable) the array |
| 151 | */ |
| 152 | |
| 153 | typedef int (*compar_t)(const TYPE* lhs, const TYPE* rhs); |
| 154 | typedef int (*compar_r_t)(const TYPE* lhs, const TYPE* rhs, void* state); |
| 155 | |
| 156 | inline status_t sort(compar_t cmp); |
| 157 | inline status_t sort(compar_r_t cmp, void* state); |
| 158 | |
| 159 | protected: |
| 160 | virtual void do_construct(void* storage, size_t num) const; |
| 161 | virtual void do_destroy(void* storage, size_t num) const; |
| 162 | virtual void do_copy(void* dest, const void* from, size_t num) const; |
| 163 | virtual void do_splat(void* dest, const void* item, size_t num) const; |
| 164 | virtual void do_move_forward(void* dest, const void* from, size_t num) const; |
| 165 | virtual void do_move_backward(void* dest, const void* from, size_t num) const; |
| 166 | }; |
| 167 | |
| 168 | |
| 169 | // --------------------------------------------------------------------------- |
| 170 | // No user serviceable parts from here... |
| 171 | // --------------------------------------------------------------------------- |
| 172 | |
| 173 | template<class TYPE> inline |
| 174 | Vector<TYPE>::Vector() |
| 175 | : VectorImpl(sizeof(TYPE), |
| 176 | ((traits<TYPE>::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0) |
| 177 | |(traits<TYPE>::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0) |
Mathias Agopian | ab44515 | 2009-06-22 01:17:46 -0700 | [diff] [blame] | 178 | |(traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0)) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | ) |
| 180 | { |
| 181 | } |
| 182 | |
| 183 | template<class TYPE> inline |
| 184 | Vector<TYPE>::Vector(const Vector<TYPE>& rhs) |
| 185 | : VectorImpl(rhs) { |
| 186 | } |
| 187 | |
| 188 | template<class TYPE> inline |
| 189 | Vector<TYPE>::~Vector() { |
| 190 | finish_vector(); |
| 191 | } |
| 192 | |
| 193 | template<class TYPE> inline |
| 194 | Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) { |
| 195 | VectorImpl::operator = (rhs); |
| 196 | return *this; |
| 197 | } |
| 198 | |
| 199 | template<class TYPE> inline |
| 200 | const Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) const { |
| 201 | VectorImpl::operator = (rhs); |
| 202 | return *this; |
| 203 | } |
| 204 | |
| 205 | template<class TYPE> inline |
| 206 | const TYPE* Vector<TYPE>::array() const { |
| 207 | return static_cast<const TYPE *>(arrayImpl()); |
| 208 | } |
| 209 | |
| 210 | template<class TYPE> inline |
| 211 | TYPE* Vector<TYPE>::editArray() { |
| 212 | return static_cast<TYPE *>(editArrayImpl()); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | template<class TYPE> inline |
| 217 | const TYPE& Vector<TYPE>::operator[](size_t index) const { |
| 218 | LOG_FATAL_IF( index>=size(), |
| 219 | "itemAt: index %d is past size %d", (int)index, (int)size() ); |
| 220 | return *(array() + index); |
| 221 | } |
| 222 | |
| 223 | template<class TYPE> inline |
| 224 | const TYPE& Vector<TYPE>::itemAt(size_t index) const { |
| 225 | return operator[](index); |
| 226 | } |
| 227 | |
| 228 | template<class TYPE> inline |
| 229 | const TYPE& Vector<TYPE>::mirrorItemAt(ssize_t index) const { |
| 230 | LOG_FATAL_IF( (index>0 ? index : -index)>=size(), |
| 231 | "mirrorItemAt: index %d is past size %d", |
| 232 | (int)index, (int)size() ); |
| 233 | return *(array() + ((index<0) ? (size()-index) : index)); |
| 234 | } |
| 235 | |
| 236 | template<class TYPE> inline |
| 237 | const TYPE& Vector<TYPE>::top() const { |
| 238 | return *(array() + size() - 1); |
| 239 | } |
| 240 | |
| 241 | template<class TYPE> inline |
| 242 | TYPE& Vector<TYPE>::editItemAt(size_t index) { |
| 243 | return *( static_cast<TYPE *>(editItemLocation(index)) ); |
| 244 | } |
| 245 | |
| 246 | template<class TYPE> inline |
| 247 | TYPE& Vector<TYPE>::editTop() { |
| 248 | return *( static_cast<TYPE *>(editItemLocation(size()-1)) ); |
| 249 | } |
| 250 | |
| 251 | template<class TYPE> inline |
| 252 | ssize_t Vector<TYPE>::insertVectorAt(const Vector<TYPE>& vector, size_t index) { |
| 253 | return VectorImpl::insertVectorAt(reinterpret_cast<const VectorImpl&>(vector), index); |
| 254 | } |
| 255 | |
| 256 | template<class TYPE> inline |
| 257 | ssize_t Vector<TYPE>::appendVector(const Vector<TYPE>& vector) { |
| 258 | return VectorImpl::appendVector(reinterpret_cast<const VectorImpl&>(vector)); |
| 259 | } |
| 260 | |
| 261 | template<class TYPE> inline |
| 262 | ssize_t Vector<TYPE>::insertAt(const TYPE& item, size_t index, size_t numItems) { |
| 263 | return VectorImpl::insertAt(&item, index, numItems); |
| 264 | } |
| 265 | |
| 266 | template<class TYPE> inline |
| 267 | void Vector<TYPE>::push(const TYPE& item) { |
| 268 | return VectorImpl::push(&item); |
| 269 | } |
| 270 | |
| 271 | template<class TYPE> inline |
| 272 | ssize_t Vector<TYPE>::add(const TYPE& item) { |
| 273 | return VectorImpl::add(&item); |
| 274 | } |
| 275 | |
| 276 | template<class TYPE> inline |
| 277 | ssize_t Vector<TYPE>::replaceAt(const TYPE& item, size_t index) { |
| 278 | return VectorImpl::replaceAt(&item, index); |
| 279 | } |
| 280 | |
| 281 | template<class TYPE> inline |
| 282 | ssize_t Vector<TYPE>::insertAt(size_t index, size_t numItems) { |
| 283 | return VectorImpl::insertAt(index, numItems); |
| 284 | } |
| 285 | |
| 286 | template<class TYPE> inline |
| 287 | void Vector<TYPE>::pop() { |
| 288 | VectorImpl::pop(); |
| 289 | } |
| 290 | |
| 291 | template<class TYPE> inline |
| 292 | void Vector<TYPE>::push() { |
| 293 | VectorImpl::push(); |
| 294 | } |
| 295 | |
| 296 | template<class TYPE> inline |
| 297 | ssize_t Vector<TYPE>::add() { |
| 298 | return VectorImpl::add(); |
| 299 | } |
| 300 | |
| 301 | template<class TYPE> inline |
| 302 | ssize_t Vector<TYPE>::replaceAt(size_t index) { |
| 303 | return VectorImpl::replaceAt(index); |
| 304 | } |
| 305 | |
| 306 | template<class TYPE> inline |
| 307 | ssize_t Vector<TYPE>::removeItemsAt(size_t index, size_t count) { |
| 308 | return VectorImpl::removeItemsAt(index, count); |
| 309 | } |
| 310 | |
| 311 | template<class TYPE> inline |
| 312 | status_t Vector<TYPE>::sort(Vector<TYPE>::compar_t cmp) { |
| 313 | return VectorImpl::sort((VectorImpl::compar_t)cmp); |
| 314 | } |
| 315 | |
| 316 | template<class TYPE> inline |
| 317 | status_t Vector<TYPE>::sort(Vector<TYPE>::compar_r_t cmp, void* state) { |
| 318 | return VectorImpl::sort((VectorImpl::compar_r_t)cmp, state); |
| 319 | } |
| 320 | |
| 321 | // --------------------------------------------------------------------------- |
| 322 | |
| 323 | template<class TYPE> |
| 324 | void Vector<TYPE>::do_construct(void* storage, size_t num) const { |
| 325 | construct_type( reinterpret_cast<TYPE*>(storage), num ); |
| 326 | } |
| 327 | |
| 328 | template<class TYPE> |
| 329 | void Vector<TYPE>::do_destroy(void* storage, size_t num) const { |
| 330 | destroy_type( reinterpret_cast<TYPE*>(storage), num ); |
| 331 | } |
| 332 | |
| 333 | template<class TYPE> |
| 334 | void Vector<TYPE>::do_copy(void* dest, const void* from, size_t num) const { |
| 335 | copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); |
| 336 | } |
| 337 | |
| 338 | template<class TYPE> |
| 339 | void Vector<TYPE>::do_splat(void* dest, const void* item, size_t num) const { |
| 340 | splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num ); |
| 341 | } |
| 342 | |
| 343 | template<class TYPE> |
| 344 | void Vector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const { |
| 345 | move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); |
| 346 | } |
| 347 | |
| 348 | template<class TYPE> |
| 349 | void Vector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const { |
| 350 | move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); |
| 351 | } |
| 352 | |
| 353 | }; // namespace android |
| 354 | |
| 355 | |
| 356 | // --------------------------------------------------------------------------- |
| 357 | |
| 358 | #endif // ANDROID_VECTOR_H |