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