1 /*
2 * Copyright (C) 2010 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_HWUI_SORTED_LIST_H
18 #define ANDROID_HWUI_SORTED_LIST_H
19
20 #include <stdint.h>
21 #include <sys/types.h>
22
23 #include <utils/Vector.h>
24 #include <utils/TypeHelpers.h>
25
26 #include "SortedListImpl.h"
27
28 namespace android {
29 namespace uirenderer {
30
31 ///////////////////////////////////////////////////////////////////////////////
32 // Sorted list
33 ///////////////////////////////////////////////////////////////////////////////
34
35 template<class TYPE>
36 class SortedList: private SortedListImpl {
37 public:
38 typedef TYPE value_type;
39
40 SortedList();
41 SortedList(const SortedList<TYPE>& rhs);
42 virtual ~SortedList();
43
44 const SortedList<TYPE>& operator =(const SortedList<TYPE>& rhs) const;
45 SortedList<TYPE>& operator =(const SortedList<TYPE>& rhs);
46
clear()47 inline void clear() {
48 VectorImpl::clear();
49 }
50
size()51 inline size_t size() const {
52 return VectorImpl::size();
53 }
54
isEmpty()55 inline bool isEmpty() const {
56 return VectorImpl::isEmpty();
57 }
58
capacity()59 inline size_t capacity() const {
60 return VectorImpl::capacity();
61 }
62
setCapacity(size_t size)63 inline ssize_t setCapacity(size_t size) {
64 return VectorImpl::setCapacity(size);
65 }
66
67 inline const TYPE* array() const;
68
69 TYPE* editArray();
70
71 ssize_t indexOf(const TYPE& item) const;
72 size_t orderOf(const TYPE& item) const;
73
74 inline const TYPE& operator [](size_t index) const;
75 inline const TYPE& itemAt(size_t index) const;
76 const TYPE& top() const;
77 const TYPE& mirrorItemAt(ssize_t index) const;
78
79 ssize_t add(const TYPE& item);
80
editItemAt(size_t index)81 TYPE& editItemAt(size_t index) {
82 return *(static_cast<TYPE *> (VectorImpl::editItemLocation(index)));
83 }
84
85 ssize_t merge(const Vector<TYPE>& vector);
86 ssize_t merge(const SortedList<TYPE>& vector);
87
88 ssize_t remove(const TYPE&);
89
90 inline ssize_t removeItemsAt(size_t index, size_t count = 1);
removeAt(size_t index)91 inline ssize_t removeAt(size_t index) {
92 return removeItemsAt(index);
93 }
94
95 protected:
96 virtual void do_construct(void* storage, size_t num) const;
97 virtual void do_destroy(void* storage, size_t num) const;
98 virtual void do_copy(void* dest, const void* from, size_t num) const;
99 virtual void do_splat(void* dest, const void* item, size_t num) const;
100 virtual void do_move_forward(void* dest, const void* from, size_t num) const;
101 virtual void do_move_backward(void* dest, const void* from, size_t num) const;
102 virtual int do_compare(const void* lhs, const void* rhs) const;
103 }; // class SortedList
104
105 ///////////////////////////////////////////////////////////////////////////////
106 // Implementation
107 ///////////////////////////////////////////////////////////////////////////////
108
109 template<class TYPE>
SortedList()110 inline SortedList<TYPE>::SortedList():
111 SortedListImpl(sizeof(TYPE), ((traits<TYPE>::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0)
112 | (traits<TYPE>::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0)
113 | (traits<TYPE>::has_trivial_copy ? HAS_TRIVIAL_COPY : 0))) {
114 }
115
116 template<class TYPE>
SortedList(const SortedList<TYPE> & rhs)117 inline SortedList<TYPE>::SortedList(const SortedList<TYPE>& rhs): SortedListImpl(rhs) {
118 }
119
~SortedList()120 template<class TYPE> inline SortedList<TYPE>::~SortedList() {
121 finish_vector();
122 }
123
124 template<class TYPE>
125 inline SortedList<TYPE>& SortedList<TYPE>::operator =(const SortedList<TYPE>& rhs) {
126 SortedListImpl::operator =(rhs);
127 return *this;
128 }
129
130 template<class TYPE>
131 inline const SortedList<TYPE>& SortedList<TYPE>::operator =(
132 const SortedList<TYPE>& rhs) const {
133 SortedListImpl::operator =(rhs);
134 return *this;
135 }
136
137 template<class TYPE>
array()138 inline const TYPE* SortedList<TYPE>::array() const {
139 return static_cast<const TYPE *> (arrayImpl());
140 }
141
142 template<class TYPE>
editArray()143 inline TYPE* SortedList<TYPE>::editArray() {
144 return static_cast<TYPE *> (editArrayImpl());
145 }
146
147 template<class TYPE>
148 inline const TYPE& SortedList<TYPE>::operator[](size_t index) const {
149 assert( index<size() );
150 return *(array() + index);
151 }
152
153 template<class TYPE>
itemAt(size_t index)154 inline const TYPE& SortedList<TYPE>::itemAt(size_t index) const {
155 return operator[](index);
156 }
157
158 template<class TYPE>
mirrorItemAt(ssize_t index)159 inline const TYPE& SortedList<TYPE>::mirrorItemAt(ssize_t index) const {
160 assert( (index>0 ? index : -index)<size() );
161 return *(array() + ((index < 0) ? (size() - index) : index));
162 }
163
164 template<class TYPE>
top()165 inline const TYPE& SortedList<TYPE>::top() const {
166 return *(array() + size() - 1);
167 }
168
169 template<class TYPE>
add(const TYPE & item)170 inline ssize_t SortedList<TYPE>::add(const TYPE& item) {
171 return SortedListImpl::add(&item);
172 }
173
174 template<class TYPE>
indexOf(const TYPE & item)175 inline ssize_t SortedList<TYPE>::indexOf(const TYPE& item) const {
176 return SortedListImpl::indexOf(&item);
177 }
178
179 template<class TYPE>
orderOf(const TYPE & item)180 inline size_t SortedList<TYPE>::orderOf(const TYPE& item) const {
181 return SortedListImpl::orderOf(&item);
182 }
183
184 template<class TYPE>
merge(const Vector<TYPE> & vector)185 inline ssize_t SortedList<TYPE>::merge(const Vector<TYPE>& vector) {
186 return SortedListImpl::merge(reinterpret_cast<const VectorImpl&> (vector));
187 }
188
189 template<class TYPE>
merge(const SortedList<TYPE> & vector)190 inline ssize_t SortedList<TYPE>::merge(const SortedList<TYPE>& vector) {
191 return SortedListImpl::merge(reinterpret_cast<const SortedListImpl&> (vector));
192 }
193
194 template<class TYPE>
remove(const TYPE & item)195 inline ssize_t SortedList<TYPE>::remove(const TYPE& item) {
196 return SortedListImpl::remove(&item);
197 }
198
199 template<class TYPE>
removeItemsAt(size_t index,size_t count)200 inline ssize_t SortedList<TYPE>::removeItemsAt(size_t index, size_t count) {
201 return VectorImpl::removeItemsAt(index, count);
202 }
203
204 template<class TYPE>
do_construct(void * storage,size_t num)205 void SortedList<TYPE>::do_construct(void* storage, size_t num) const {
206 construct_type(reinterpret_cast<TYPE*> (storage), num);
207 }
208
209 template<class TYPE>
do_destroy(void * storage,size_t num)210 void SortedList<TYPE>::do_destroy(void* storage, size_t num) const {
211 destroy_type(reinterpret_cast<TYPE*> (storage), num);
212 }
213
214 template<class TYPE>
do_copy(void * dest,const void * from,size_t num)215 void SortedList<TYPE>::do_copy(void* dest, const void* from, size_t num) const {
216 copy_type(reinterpret_cast<TYPE*> (dest), reinterpret_cast<const TYPE*> (from), num);
217 }
218
219 template<class TYPE>
do_splat(void * dest,const void * item,size_t num)220 void SortedList<TYPE>::do_splat(void* dest, const void* item, size_t num) const {
221 splat_type(reinterpret_cast<TYPE*> (dest), reinterpret_cast<const TYPE*> (item), num);
222 }
223
224 template<class TYPE>
do_move_forward(void * dest,const void * from,size_t num)225 void SortedList<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const {
226 move_forward_type(reinterpret_cast<TYPE*> (dest), reinterpret_cast<const TYPE*> (from), num);
227 }
228
229 template<class TYPE>
do_move_backward(void * dest,const void * from,size_t num)230 void SortedList<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const {
231 move_backward_type(reinterpret_cast<TYPE*> (dest), reinterpret_cast<const TYPE*> (from), num);
232 }
233
234 template<class TYPE>
do_compare(const void * lhs,const void * rhs)235 int SortedList<TYPE>::do_compare(const void* lhs, const void* rhs) const {
236 return compare_type(*reinterpret_cast<const TYPE*> (lhs), *reinterpret_cast<const TYPE*> (rhs));
237 }
238
239 }; // namespace uirenderer
240 }; // namespace android
241
242 #endif // ANDROID_HWUI_SORTED_LIST_H
243