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