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