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_SORTED_VECTOR_H |
michael@0 | 18 | #define ANDROID_SORTED_VECTOR_H |
michael@0 | 19 | |
michael@0 | 20 | #include <assert.h> |
michael@0 | 21 | #include <stdint.h> |
michael@0 | 22 | #include <sys/types.h> |
michael@0 | 23 | |
michael@0 | 24 | #include <utils/Vector.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 | template <class TYPE> |
michael@0 | 33 | class SortedVector : private SortedVectorImpl |
michael@0 | 34 | { |
michael@0 | 35 | public: |
michael@0 | 36 | typedef TYPE value_type; |
michael@0 | 37 | |
michael@0 | 38 | /*! |
michael@0 | 39 | * Constructors and destructors |
michael@0 | 40 | */ |
michael@0 | 41 | |
michael@0 | 42 | SortedVector(); |
michael@0 | 43 | SortedVector(const SortedVector<TYPE>& rhs); |
michael@0 | 44 | virtual ~SortedVector(); |
michael@0 | 45 | |
michael@0 | 46 | /*! copy operator */ |
michael@0 | 47 | const SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const; |
michael@0 | 48 | SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs); |
michael@0 | 49 | |
michael@0 | 50 | /* |
michael@0 | 51 | * empty the vector |
michael@0 | 52 | */ |
michael@0 | 53 | |
michael@0 | 54 | inline void clear() { VectorImpl::clear(); } |
michael@0 | 55 | |
michael@0 | 56 | /*! |
michael@0 | 57 | * vector stats |
michael@0 | 58 | */ |
michael@0 | 59 | |
michael@0 | 60 | //! returns number of items in the vector |
michael@0 | 61 | inline size_t size() const { return VectorImpl::size(); } |
michael@0 | 62 | //! returns wether or not the vector is empty |
michael@0 | 63 | inline bool isEmpty() const { return VectorImpl::isEmpty(); } |
michael@0 | 64 | //! returns how many items can be stored without reallocating the backing store |
michael@0 | 65 | inline size_t capacity() const { return VectorImpl::capacity(); } |
michael@0 | 66 | //! setst the capacity. capacity can never be reduced less than size() |
michael@0 | 67 | inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); } |
michael@0 | 68 | |
michael@0 | 69 | /*! |
michael@0 | 70 | * C-style array access |
michael@0 | 71 | */ |
michael@0 | 72 | |
michael@0 | 73 | //! read-only C-style access |
michael@0 | 74 | inline const TYPE* array() const; |
michael@0 | 75 | |
michael@0 | 76 | //! read-write C-style access. BE VERY CAREFUL when modifying the array |
michael@0 | 77 | //! you ust keep it sorted! You usually don't use this function. |
michael@0 | 78 | TYPE* editArray(); |
michael@0 | 79 | |
michael@0 | 80 | //! finds the index of an item |
michael@0 | 81 | ssize_t indexOf(const TYPE& item) const; |
michael@0 | 82 | |
michael@0 | 83 | //! finds where this item should be inserted |
michael@0 | 84 | size_t orderOf(const TYPE& item) const; |
michael@0 | 85 | |
michael@0 | 86 | |
michael@0 | 87 | /*! |
michael@0 | 88 | * accessors |
michael@0 | 89 | */ |
michael@0 | 90 | |
michael@0 | 91 | //! read-only access to an item at a given index |
michael@0 | 92 | inline const TYPE& operator [] (size_t index) const; |
michael@0 | 93 | //! alternate name for operator [] |
michael@0 | 94 | inline const TYPE& itemAt(size_t index) const; |
michael@0 | 95 | //! stack-usage of the vector. returns the top of the stack (last element) |
michael@0 | 96 | const TYPE& top() const; |
michael@0 | 97 | //! same as operator [], but allows to access the vector backward (from the end) with a negative index |
michael@0 | 98 | const TYPE& mirrorItemAt(ssize_t index) const; |
michael@0 | 99 | |
michael@0 | 100 | /*! |
michael@0 | 101 | * modifing the array |
michael@0 | 102 | */ |
michael@0 | 103 | |
michael@0 | 104 | //! add an item in the right place (and replace the one that is there) |
michael@0 | 105 | ssize_t add(const TYPE& item); |
michael@0 | 106 | |
michael@0 | 107 | //! editItemAt() MUST NOT change the order of this item |
michael@0 | 108 | TYPE& editItemAt(size_t index) { |
michael@0 | 109 | return *( static_cast<TYPE *>(VectorImpl::editItemLocation(index)) ); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | //! merges a vector into this one |
michael@0 | 113 | ssize_t merge(const Vector<TYPE>& vector); |
michael@0 | 114 | ssize_t merge(const SortedVector<TYPE>& vector); |
michael@0 | 115 | |
michael@0 | 116 | //! removes an item |
michael@0 | 117 | ssize_t remove(const TYPE&); |
michael@0 | 118 | |
michael@0 | 119 | //! remove several items |
michael@0 | 120 | inline ssize_t removeItemsAt(size_t index, size_t count = 1); |
michael@0 | 121 | //! remove one item |
michael@0 | 122 | inline ssize_t removeAt(size_t index) { return removeItemsAt(index); } |
michael@0 | 123 | |
michael@0 | 124 | protected: |
michael@0 | 125 | virtual void do_construct(void* storage, size_t num) const; |
michael@0 | 126 | virtual void do_destroy(void* storage, size_t num) const; |
michael@0 | 127 | virtual void do_copy(void* dest, const void* from, size_t num) const; |
michael@0 | 128 | virtual void do_splat(void* dest, const void* item, size_t num) const; |
michael@0 | 129 | virtual void do_move_forward(void* dest, const void* from, size_t num) const; |
michael@0 | 130 | virtual void do_move_backward(void* dest, const void* from, size_t num) const; |
michael@0 | 131 | virtual int do_compare(const void* lhs, const void* rhs) const; |
michael@0 | 132 | }; |
michael@0 | 133 | |
michael@0 | 134 | |
michael@0 | 135 | // --------------------------------------------------------------------------- |
michael@0 | 136 | // No user serviceable parts from here... |
michael@0 | 137 | // --------------------------------------------------------------------------- |
michael@0 | 138 | |
michael@0 | 139 | template<class TYPE> inline |
michael@0 | 140 | SortedVector<TYPE>::SortedVector() |
michael@0 | 141 | : SortedVectorImpl(sizeof(TYPE), |
michael@0 | 142 | ((traits<TYPE>::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0) |
michael@0 | 143 | |(traits<TYPE>::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0) |
michael@0 | 144 | |(traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0)) |
michael@0 | 145 | ) |
michael@0 | 146 | { |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | template<class TYPE> inline |
michael@0 | 150 | SortedVector<TYPE>::SortedVector(const SortedVector<TYPE>& rhs) |
michael@0 | 151 | : SortedVectorImpl(rhs) { |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | template<class TYPE> inline |
michael@0 | 155 | SortedVector<TYPE>::~SortedVector() { |
michael@0 | 156 | finish_vector(); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | template<class TYPE> inline |
michael@0 | 160 | SortedVector<TYPE>& SortedVector<TYPE>::operator = (const SortedVector<TYPE>& rhs) { |
michael@0 | 161 | SortedVectorImpl::operator = (rhs); |
michael@0 | 162 | return *this; |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | template<class TYPE> inline |
michael@0 | 166 | const SortedVector<TYPE>& SortedVector<TYPE>::operator = (const SortedVector<TYPE>& rhs) const { |
michael@0 | 167 | SortedVectorImpl::operator = (rhs); |
michael@0 | 168 | return *this; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | template<class TYPE> inline |
michael@0 | 172 | const TYPE* SortedVector<TYPE>::array() const { |
michael@0 | 173 | return static_cast<const TYPE *>(arrayImpl()); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | template<class TYPE> inline |
michael@0 | 177 | TYPE* SortedVector<TYPE>::editArray() { |
michael@0 | 178 | return static_cast<TYPE *>(editArrayImpl()); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | |
michael@0 | 182 | template<class TYPE> inline |
michael@0 | 183 | const TYPE& SortedVector<TYPE>::operator[](size_t index) const { |
michael@0 | 184 | assert( index<size() ); |
michael@0 | 185 | return *(array() + index); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | template<class TYPE> inline |
michael@0 | 189 | const TYPE& SortedVector<TYPE>::itemAt(size_t index) const { |
michael@0 | 190 | return operator[](index); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | template<class TYPE> inline |
michael@0 | 194 | const TYPE& SortedVector<TYPE>::mirrorItemAt(ssize_t index) const { |
michael@0 | 195 | assert( (index>0 ? index : -index)<size() ); |
michael@0 | 196 | return *(array() + ((index<0) ? (size()-index) : index)); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | template<class TYPE> inline |
michael@0 | 200 | const TYPE& SortedVector<TYPE>::top() const { |
michael@0 | 201 | return *(array() + size() - 1); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | template<class TYPE> inline |
michael@0 | 205 | ssize_t SortedVector<TYPE>::add(const TYPE& item) { |
michael@0 | 206 | return SortedVectorImpl::add(&item); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | template<class TYPE> inline |
michael@0 | 210 | ssize_t SortedVector<TYPE>::indexOf(const TYPE& item) const { |
michael@0 | 211 | return SortedVectorImpl::indexOf(&item); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | template<class TYPE> inline |
michael@0 | 215 | size_t SortedVector<TYPE>::orderOf(const TYPE& item) const { |
michael@0 | 216 | return SortedVectorImpl::orderOf(&item); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | template<class TYPE> inline |
michael@0 | 220 | ssize_t SortedVector<TYPE>::merge(const Vector<TYPE>& vector) { |
michael@0 | 221 | return SortedVectorImpl::merge(reinterpret_cast<const VectorImpl&>(vector)); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | template<class TYPE> inline |
michael@0 | 225 | ssize_t SortedVector<TYPE>::merge(const SortedVector<TYPE>& vector) { |
michael@0 | 226 | return SortedVectorImpl::merge(reinterpret_cast<const SortedVectorImpl&>(vector)); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | template<class TYPE> inline |
michael@0 | 230 | ssize_t SortedVector<TYPE>::remove(const TYPE& item) { |
michael@0 | 231 | return SortedVectorImpl::remove(&item); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | template<class TYPE> inline |
michael@0 | 235 | ssize_t SortedVector<TYPE>::removeItemsAt(size_t index, size_t count) { |
michael@0 | 236 | return VectorImpl::removeItemsAt(index, count); |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | // --------------------------------------------------------------------------- |
michael@0 | 240 | |
michael@0 | 241 | template<class TYPE> |
michael@0 | 242 | void SortedVector<TYPE>::do_construct(void* storage, size_t num) const { |
michael@0 | 243 | construct_type( reinterpret_cast<TYPE*>(storage), num ); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | template<class TYPE> |
michael@0 | 247 | void SortedVector<TYPE>::do_destroy(void* storage, size_t num) const { |
michael@0 | 248 | destroy_type( reinterpret_cast<TYPE*>(storage), num ); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | template<class TYPE> |
michael@0 | 252 | void SortedVector<TYPE>::do_copy(void* dest, const void* from, size_t num) const { |
michael@0 | 253 | copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | template<class TYPE> |
michael@0 | 257 | void SortedVector<TYPE>::do_splat(void* dest, const void* item, size_t num) const { |
michael@0 | 258 | splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num ); |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | template<class TYPE> |
michael@0 | 262 | void SortedVector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const { |
michael@0 | 263 | move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | template<class TYPE> |
michael@0 | 267 | void SortedVector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const { |
michael@0 | 268 | move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | template<class TYPE> |
michael@0 | 272 | int SortedVector<TYPE>::do_compare(const void* lhs, const void* rhs) const { |
michael@0 | 273 | return compare_type( *reinterpret_cast<const TYPE*>(lhs), *reinterpret_cast<const TYPE*>(rhs) ); |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | }; // namespace android |
michael@0 | 277 | |
michael@0 | 278 | |
michael@0 | 279 | // --------------------------------------------------------------------------- |
michael@0 | 280 | |
michael@0 | 281 | #endif // ANDROID_SORTED_VECTOR_H |