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