1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/omx-plugin/include/ics/utils/VectorImpl.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,184 @@ 1.4 +/* 1.5 + * Copyright (C) 2005 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +#ifndef ANDROID_VECTOR_IMPL_H 1.21 +#define ANDROID_VECTOR_IMPL_H 1.22 + 1.23 +#include <assert.h> 1.24 +#include <stdint.h> 1.25 +#include <sys/types.h> 1.26 +#include <utils/Errors.h> 1.27 + 1.28 +// --------------------------------------------------------------------------- 1.29 +// No user serviceable parts in here... 1.30 +// --------------------------------------------------------------------------- 1.31 + 1.32 +namespace android { 1.33 + 1.34 +/*! 1.35 + * Implementation of the guts of the vector<> class 1.36 + * this ensures backward binary compatibility and 1.37 + * reduces code size. 1.38 + * For performance reasons, we expose mStorage and mCount 1.39 + * so these fields are set in stone. 1.40 + * 1.41 + */ 1.42 + 1.43 +class VectorImpl 1.44 +{ 1.45 +public: 1.46 + enum { // flags passed to the ctor 1.47 + HAS_TRIVIAL_CTOR = 0x00000001, 1.48 + HAS_TRIVIAL_DTOR = 0x00000002, 1.49 + HAS_TRIVIAL_COPY = 0x00000004, 1.50 + }; 1.51 + 1.52 + VectorImpl(size_t itemSize, uint32_t flags); 1.53 + VectorImpl(const VectorImpl& rhs); 1.54 + virtual ~VectorImpl(); 1.55 + 1.56 + /*! must be called from subclasses destructor */ 1.57 + void finish_vector(); 1.58 + 1.59 + VectorImpl& operator = (const VectorImpl& rhs); 1.60 + 1.61 + /*! C-style array access */ 1.62 + inline const void* arrayImpl() const { return mStorage; } 1.63 + void* editArrayImpl(); 1.64 + 1.65 + /*! vector stats */ 1.66 + inline size_t size() const { return mCount; } 1.67 + inline bool isEmpty() const { return mCount == 0; } 1.68 + size_t capacity() const; 1.69 + ssize_t setCapacity(size_t size); 1.70 + ssize_t resize(size_t size); 1.71 + 1.72 + /*! append/insert another vector or array */ 1.73 + ssize_t insertVectorAt(const VectorImpl& vector, size_t index); 1.74 + ssize_t appendVector(const VectorImpl& vector); 1.75 + ssize_t insertArrayAt(const void* array, size_t index, size_t length); 1.76 + ssize_t appendArray(const void* array, size_t length); 1.77 + 1.78 + /*! add/insert/replace items */ 1.79 + ssize_t insertAt(size_t where, size_t numItems = 1); 1.80 + ssize_t insertAt(const void* item, size_t where, size_t numItems = 1); 1.81 + void pop(); 1.82 + void push(); 1.83 + void push(const void* item); 1.84 + ssize_t add(); 1.85 + ssize_t add(const void* item); 1.86 + ssize_t replaceAt(size_t index); 1.87 + ssize_t replaceAt(const void* item, size_t index); 1.88 + 1.89 + /*! remove items */ 1.90 + ssize_t removeItemsAt(size_t index, size_t count = 1); 1.91 + void clear(); 1.92 + 1.93 + const void* itemLocation(size_t index) const; 1.94 + void* editItemLocation(size_t index); 1.95 + 1.96 + typedef int (*compar_t)(const void* lhs, const void* rhs); 1.97 + typedef int (*compar_r_t)(const void* lhs, const void* rhs, void* state); 1.98 + status_t sort(compar_t cmp); 1.99 + status_t sort(compar_r_t cmp, void* state); 1.100 + 1.101 +protected: 1.102 + size_t itemSize() const; 1.103 + void release_storage(); 1.104 + 1.105 + virtual void do_construct(void* storage, size_t num) const = 0; 1.106 + virtual void do_destroy(void* storage, size_t num) const = 0; 1.107 + virtual void do_copy(void* dest, const void* from, size_t num) const = 0; 1.108 + virtual void do_splat(void* dest, const void* item, size_t num) const = 0; 1.109 + virtual void do_move_forward(void* dest, const void* from, size_t num) const = 0; 1.110 + virtual void do_move_backward(void* dest, const void* from, size_t num) const = 0; 1.111 + 1.112 +private: 1.113 + void* _grow(size_t where, size_t amount); 1.114 + void _shrink(size_t where, size_t amount); 1.115 + 1.116 + inline void _do_construct(void* storage, size_t num) const; 1.117 + inline void _do_destroy(void* storage, size_t num) const; 1.118 + inline void _do_copy(void* dest, const void* from, size_t num) const; 1.119 + inline void _do_splat(void* dest, const void* item, size_t num) const; 1.120 + inline void _do_move_forward(void* dest, const void* from, size_t num) const; 1.121 + inline void _do_move_backward(void* dest, const void* from, size_t num) const; 1.122 + 1.123 + // These 2 fields are exposed in the inlines below, 1.124 + // so they're set in stone. 1.125 + void * mStorage; // base address of the vector 1.126 + size_t mCount; // number of items 1.127 + 1.128 + const uint32_t mFlags; 1.129 + const size_t mItemSize; 1.130 +}; 1.131 + 1.132 + 1.133 + 1.134 +class SortedVectorImpl : public VectorImpl 1.135 +{ 1.136 +public: 1.137 + SortedVectorImpl(size_t itemSize, uint32_t flags); 1.138 + SortedVectorImpl(const VectorImpl& rhs); 1.139 + virtual ~SortedVectorImpl(); 1.140 + 1.141 + SortedVectorImpl& operator = (const SortedVectorImpl& rhs); 1.142 + 1.143 + //! finds the index of an item 1.144 + ssize_t indexOf(const void* item) const; 1.145 + 1.146 + //! finds where this item should be inserted 1.147 + size_t orderOf(const void* item) const; 1.148 + 1.149 + //! add an item in the right place (or replaces it if there is one) 1.150 + ssize_t add(const void* item); 1.151 + 1.152 + //! merges a vector into this one 1.153 + ssize_t merge(const VectorImpl& vector); 1.154 + ssize_t merge(const SortedVectorImpl& vector); 1.155 + 1.156 + //! removes an item 1.157 + ssize_t remove(const void* item); 1.158 + 1.159 +protected: 1.160 + virtual int do_compare(const void* lhs, const void* rhs) const = 0; 1.161 + 1.162 +private: 1.163 + ssize_t _indexOrderOf(const void* item, size_t* order = 0) const; 1.164 + 1.165 + // these are made private, because they can't be used on a SortedVector 1.166 + // (they don't have an implementation either) 1.167 + ssize_t add(); 1.168 + void pop(); 1.169 + void push(); 1.170 + void push(const void* item); 1.171 + ssize_t insertVectorAt(const VectorImpl& vector, size_t index); 1.172 + ssize_t appendVector(const VectorImpl& vector); 1.173 + ssize_t insertArrayAt(const void* array, size_t index, size_t length); 1.174 + ssize_t appendArray(const void* array, size_t length); 1.175 + ssize_t insertAt(size_t where, size_t numItems = 1); 1.176 + ssize_t insertAt(const void* item, size_t where, size_t numItems = 1); 1.177 + ssize_t replaceAt(size_t index); 1.178 + ssize_t replaceAt(const void* item, size_t index); 1.179 +}; 1.180 + 1.181 +}; // namespace android 1.182 + 1.183 + 1.184 +// --------------------------------------------------------------------------- 1.185 + 1.186 +#endif // ANDROID_VECTOR_IMPL_H 1.187 +