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