blob: 394db12153067b12eed4556fffc81c7c8a5cb86c [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Salyzyncfd5b082016-10-17 14:28:00 -070024#include <log/log.h>
Mark Salyzyn66ce3e02016-09-28 10:07:20 -070025#include <utils/TypeHelpers.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026#include <utils/Vector.h>
27#include <utils/VectorImpl.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080028
29// ---------------------------------------------------------------------------
30
31namespace android {
32
Steven Morelandb8f152d2017-12-18 16:14:13 -080033// DO NOT USE: please use std::set
34
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080035template <class TYPE>
36class SortedVector : private SortedVectorImpl
37{
Mathias Agopian320a2b42011-06-28 19:09:31 -070038 friend class Vector<TYPE>;
39
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080040public:
41 typedef TYPE value_type;
Irvel662008a2016-07-11 08:34:53 -070042
43 /*!
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080044 * Constructors and destructors
45 */
Irvel662008a2016-07-11 08:34:53 -070046
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080047 SortedVector();
48 SortedVector(const SortedVector<TYPE>& rhs);
49 virtual ~SortedVector();
50
51 /*! copy operator */
Irvel662008a2016-07-11 08:34:53 -070052 const SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const;
53 SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080054
55 /*
56 * empty the vector
57 */
58
59 inline void clear() { VectorImpl::clear(); }
60
Irvel662008a2016-07-11 08:34:53 -070061 /*!
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080062 * vector stats
63 */
64
65 //! returns number of items in the vector
66 inline size_t size() const { return VectorImpl::size(); }
Mathias Agopianbdf73c72012-08-09 19:39:15 -070067 //! returns whether or not the vector is empty
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080068 inline bool isEmpty() const { return VectorImpl::isEmpty(); }
69 //! returns how many items can be stored without reallocating the backing store
70 inline size_t capacity() const { return VectorImpl::capacity(); }
Mathias Agopianbdf73c72012-08-09 19:39:15 -070071 //! sets the capacity. capacity can never be reduced less than size()
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080072 inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); }
73
Irvel662008a2016-07-11 08:34:53 -070074 /*!
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080075 * C-style array access
76 */
Irvel662008a2016-07-11 08:34:53 -070077
78 //! read-only C-style access
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080079 inline const TYPE* array() const;
80
81 //! read-write C-style access. BE VERY CAREFUL when modifying the array
Mathias Agopianbdf73c72012-08-09 19:39:15 -070082 //! you must keep it sorted! You usually don't use this function.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080083 TYPE* editArray();
84
85 //! finds the index of an item
86 ssize_t indexOf(const TYPE& item) const;
Irvel662008a2016-07-11 08:34:53 -070087
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080088 //! finds where this item should be inserted
89 size_t orderOf(const TYPE& item) const;
Irvel662008a2016-07-11 08:34:53 -070090
91
92 /*!
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080093 * accessors
94 */
95
96 //! read-only access to an item at a given index
97 inline const TYPE& operator [] (size_t index) const;
98 //! alternate name for operator []
99 inline const TYPE& itemAt(size_t index) const;
100 //! stack-usage of the vector. returns the top of the stack (last element)
101 const TYPE& top() const;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800102
103 /*!
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700104 * modifying the array
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800105 */
106
107 //! add an item in the right place (and replace the one that is there)
108 ssize_t add(const TYPE& item);
Irvel662008a2016-07-11 08:34:53 -0700109
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800110 //! editItemAt() MUST NOT change the order of this item
111 TYPE& editItemAt(size_t index) {
112 return *( static_cast<TYPE *>(VectorImpl::editItemLocation(index)) );
113 }
114
115 //! merges a vector into this one
116 ssize_t merge(const Vector<TYPE>& vector);
117 ssize_t merge(const SortedVector<TYPE>& vector);
Irvel662008a2016-07-11 08:34:53 -0700118
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800119 //! removes an item
120 ssize_t remove(const TYPE&);
121
122 //! remove several items
123 inline ssize_t removeItemsAt(size_t index, size_t count = 1);
124 //! remove one item
125 inline ssize_t removeAt(size_t index) { return removeItemsAt(index); }
Irvel662008a2016-07-11 08:34:53 -0700126
127 /*
128 * these inlines add some level of compatibility with STL.
129 */
130 typedef TYPE* iterator;
131 typedef TYPE const* const_iterator;
132
133 inline iterator begin() { return editArray(); }
134 inline iterator end() { return editArray() + size(); }
135 inline const_iterator begin() const { return array(); }
136 inline const_iterator end() const { return array() + size(); }
137 inline void reserve(size_t n) { setCapacity(n); }
138 inline bool empty() const{ return isEmpty(); }
139 inline iterator erase(iterator pos) {
140 ssize_t index = removeItemsAt(pos-array());
141 return begin() + index;
142 }
143
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800144protected:
145 virtual void do_construct(void* storage, size_t num) const;
146 virtual void do_destroy(void* storage, size_t num) const;
147 virtual void do_copy(void* dest, const void* from, size_t num) const;
148 virtual void do_splat(void* dest, const void* item, size_t num) const;
149 virtual void do_move_forward(void* dest, const void* from, size_t num) const;
150 virtual void do_move_backward(void* dest, const void* from, size_t num) const;
151 virtual int do_compare(const void* lhs, const void* rhs) const;
152};
153
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800154// ---------------------------------------------------------------------------
155// No user serviceable parts from here...
156// ---------------------------------------------------------------------------
157
158template<class TYPE> inline
159SortedVector<TYPE>::SortedVector()
160 : SortedVectorImpl(sizeof(TYPE),
161 ((traits<TYPE>::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0)
162 |(traits<TYPE>::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0)
Mathias Agopiana33bd162009-06-22 01:17:46 -0700163 |(traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0))
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800164 )
165{
166}
167
168template<class TYPE> inline
169SortedVector<TYPE>::SortedVector(const SortedVector<TYPE>& rhs)
170 : SortedVectorImpl(rhs) {
171}
172
173template<class TYPE> inline
174SortedVector<TYPE>::~SortedVector() {
175 finish_vector();
176}
177
178template<class TYPE> inline
179SortedVector<TYPE>& SortedVector<TYPE>::operator = (const SortedVector<TYPE>& rhs) {
180 SortedVectorImpl::operator = (rhs);
Irvel662008a2016-07-11 08:34:53 -0700181 return *this;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800182}
183
184template<class TYPE> inline
185const SortedVector<TYPE>& SortedVector<TYPE>::operator = (const SortedVector<TYPE>& rhs) const {
186 SortedVectorImpl::operator = (rhs);
Irvel662008a2016-07-11 08:34:53 -0700187 return *this;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800188}
189
190template<class TYPE> inline
191const TYPE* SortedVector<TYPE>::array() const {
192 return static_cast<const TYPE *>(arrayImpl());
193}
194
195template<class TYPE> inline
196TYPE* SortedVector<TYPE>::editArray() {
197 return static_cast<TYPE *>(editArrayImpl());
198}
199
200
201template<class TYPE> inline
202const TYPE& SortedVector<TYPE>::operator[](size_t index) const {
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700203 LOG_FATAL_IF(index>=size(),
204 "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__,
205 int(index), int(size()));
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800206 return *(array() + index);
207}
208
209template<class TYPE> inline
210const TYPE& SortedVector<TYPE>::itemAt(size_t index) const {
211 return operator[](index);
212}
213
214template<class TYPE> inline
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800215const TYPE& SortedVector<TYPE>::top() const {
216 return *(array() + size() - 1);
217}
218
219template<class TYPE> inline
220ssize_t SortedVector<TYPE>::add(const TYPE& item) {
221 return SortedVectorImpl::add(&item);
222}
223
224template<class TYPE> inline
225ssize_t SortedVector<TYPE>::indexOf(const TYPE& item) const {
226 return SortedVectorImpl::indexOf(&item);
227}
228
229template<class TYPE> inline
230size_t SortedVector<TYPE>::orderOf(const TYPE& item) const {
231 return SortedVectorImpl::orderOf(&item);
232}
233
234template<class TYPE> inline
235ssize_t SortedVector<TYPE>::merge(const Vector<TYPE>& vector) {
236 return SortedVectorImpl::merge(reinterpret_cast<const VectorImpl&>(vector));
237}
238
239template<class TYPE> inline
240ssize_t SortedVector<TYPE>::merge(const SortedVector<TYPE>& vector) {
241 return SortedVectorImpl::merge(reinterpret_cast<const SortedVectorImpl&>(vector));
242}
243
244template<class TYPE> inline
245ssize_t SortedVector<TYPE>::remove(const TYPE& item) {
246 return SortedVectorImpl::remove(&item);
247}
248
249template<class TYPE> inline
250ssize_t SortedVector<TYPE>::removeItemsAt(size_t index, size_t count) {
251 return VectorImpl::removeItemsAt(index, count);
252}
253
254// ---------------------------------------------------------------------------
255
256template<class TYPE>
Vishwath Mohan27a7aa02017-03-16 17:59:57 -0700257UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_construct(void* storage, size_t num) const {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800258 construct_type( reinterpret_cast<TYPE*>(storage), num );
259}
260
261template<class TYPE>
262void SortedVector<TYPE>::do_destroy(void* storage, size_t num) const {
263 destroy_type( reinterpret_cast<TYPE*>(storage), num );
264}
265
266template<class TYPE>
Vishwath Mohan27a7aa02017-03-16 17:59:57 -0700267UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_copy(void* dest, const void* from, size_t num) const {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800268 copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
269}
270
271template<class TYPE>
Vishwath Mohan27a7aa02017-03-16 17:59:57 -0700272UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_splat(void* dest, const void* item, size_t num) const {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800273 splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num );
274}
275
276template<class TYPE>
Vishwath Mohan27a7aa02017-03-16 17:59:57 -0700277UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800278 move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
279}
280
281template<class TYPE>
Vishwath Mohan27a7aa02017-03-16 17:59:57 -0700282UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800283 move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num );
284}
285
286template<class TYPE>
287int SortedVector<TYPE>::do_compare(const void* lhs, const void* rhs) const {
288 return compare_type( *reinterpret_cast<const TYPE*>(lhs), *reinterpret_cast<const TYPE*>(rhs) );
289}
290
Pirama Arumuga Nainar90affce2018-04-11 23:10:28 -0700291} // namespace android
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800292
293// ---------------------------------------------------------------------------
294
295#endif // ANDROID_SORTED_VECTOR_H