blob: 3bcf54bc0ab979ee7ea6aae24891417da421cde9 [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#define LOG_TAG "Vector"
18
19#include <string.h>
20#include <stdlib.h>
21#include <stdio.h>
22
Mathias Agopianbdf73c72012-08-09 19:39:15 -070023#include <cutils/log.h>
Narayan Kamathb6381262015-08-28 12:59:48 +010024#include <safe_iop.h>
Mathias Agopianbdf73c72012-08-09 19:39:15 -070025
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026#include <utils/Errors.h>
27#include <utils/SharedBuffer.h>
28#include <utils/VectorImpl.h>
29
30/*****************************************************************************/
31
32
33namespace android {
34
35// ----------------------------------------------------------------------------
36
37const size_t kMinVectorCapacity = 4;
38
39static inline size_t max(size_t a, size_t b) {
40 return a>b ? a : b;
41}
42
43// ----------------------------------------------------------------------------
44
45VectorImpl::VectorImpl(size_t itemSize, uint32_t flags)
46 : mStorage(0), mCount(0), mFlags(flags), mItemSize(itemSize)
47{
48}
49
50VectorImpl::VectorImpl(const VectorImpl& rhs)
51 : mStorage(rhs.mStorage), mCount(rhs.mCount),
52 mFlags(rhs.mFlags), mItemSize(rhs.mItemSize)
53{
54 if (mStorage) {
Mathias Agopiane79aadd2012-08-31 16:20:23 -070055 SharedBuffer::bufferFromData(mStorage)->acquire();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080056 }
57}
58
59VectorImpl::~VectorImpl()
60{
Mathias Agopianbdf73c72012-08-09 19:39:15 -070061 ALOGW_IF(mCount,
62 "[%p] subclasses of VectorImpl must call finish_vector()"
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080063 " in their destructor. Leaking %d bytes.",
64 this, (int)(mCount*mItemSize));
65 // We can't call _do_destroy() here because the vtable is already gone.
66}
67
68VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
69{
Mathias Agopianbdf73c72012-08-09 19:39:15 -070070 LOG_ALWAYS_FATAL_IF(mItemSize != rhs.mItemSize,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080071 "Vector<> have different types (this=%p, rhs=%p)", this, &rhs);
72 if (this != &rhs) {
73 release_storage();
74 if (rhs.mCount) {
75 mStorage = rhs.mStorage;
76 mCount = rhs.mCount;
Mathias Agopiane79aadd2012-08-31 16:20:23 -070077 SharedBuffer::bufferFromData(mStorage)->acquire();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080078 } else {
79 mStorage = 0;
80 mCount = 0;
81 }
82 }
83 return *this;
84}
85
86void* VectorImpl::editArrayImpl()
87{
88 if (mStorage) {
Narayan Kamathb6381262015-08-28 12:59:48 +010089 const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
90 SharedBuffer* editable = sb->attemptEdit();
91 if (editable == 0) {
92 // If we're here, we're not the only owner of the buffer.
93 // We must make a copy of it.
94 editable = SharedBuffer::alloc(sb->size());
95 // Fail instead of returning a pointer to storage that's not
96 // editable. Otherwise we'd be editing the contents of a buffer
97 // for which we're not the only owner, which is undefined behaviour.
98 LOG_ALWAYS_FATAL_IF(editable == NULL);
99 _do_copy(editable->data(), mStorage, mCount);
100 release_storage();
101 mStorage = editable->data();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800102 }
103 }
104 return mStorage;
105}
106
107size_t VectorImpl::capacity() const
108{
109 if (mStorage) {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700110 return SharedBuffer::bufferFromData(mStorage)->size() / mItemSize;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800111 }
112 return 0;
113}
114
115ssize_t VectorImpl::insertVectorAt(const VectorImpl& vector, size_t index)
116{
Jeff Brown9efaaa42010-06-16 01:53:36 -0700117 return insertArrayAt(vector.arrayImpl(), index, vector.size());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800118}
119
120ssize_t VectorImpl::appendVector(const VectorImpl& vector)
121{
122 return insertVectorAt(vector, size());
123}
124
Jeff Brown9efaaa42010-06-16 01:53:36 -0700125ssize_t VectorImpl::insertArrayAt(const void* array, size_t index, size_t length)
126{
127 if (index > size())
128 return BAD_INDEX;
129 void* where = _grow(index, length);
130 if (where) {
131 _do_copy(where, array, length);
132 }
133 return where ? index : (ssize_t)NO_MEMORY;
134}
135
136ssize_t VectorImpl::appendArray(const void* array, size_t length)
137{
138 return insertArrayAt(array, size(), length);
139}
140
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800141ssize_t VectorImpl::insertAt(size_t index, size_t numItems)
142{
143 return insertAt(0, index, numItems);
144}
145
146ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems)
147{
148 if (index > size())
149 return BAD_INDEX;
150 void* where = _grow(index, numItems);
151 if (where) {
152 if (item) {
153 _do_splat(where, item, numItems);
154 } else {
155 _do_construct(where, numItems);
156 }
157 }
158 return where ? index : (ssize_t)NO_MEMORY;
159}
160
161static int sortProxy(const void* lhs, const void* rhs, void* func)
162{
163 return (*(VectorImpl::compar_t)func)(lhs, rhs);
164}
165
166status_t VectorImpl::sort(VectorImpl::compar_t cmp)
167{
168 return sort(sortProxy, (void*)cmp);
169}
170
171status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
172{
173 // the sort must be stable. we're using insertion sort which
174 // is well suited for small and already sorted arrays
175 // for big arrays, it could be better to use mergesort
176 const ssize_t count = size();
177 if (count > 1) {
178 void* array = const_cast<void*>(arrayImpl());
179 void* temp = 0;
180 ssize_t i = 1;
181 while (i < count) {
182 void* item = reinterpret_cast<char*>(array) + mItemSize*(i);
183 void* curr = reinterpret_cast<char*>(array) + mItemSize*(i-1);
184 if (cmp(curr, item, state) > 0) {
185
186 if (!temp) {
187 // we're going to have to modify the array...
188 array = editArrayImpl();
189 if (!array) return NO_MEMORY;
190 temp = malloc(mItemSize);
191 if (!temp) return NO_MEMORY;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800192 item = reinterpret_cast<char*>(array) + mItemSize*(i);
193 curr = reinterpret_cast<char*>(array) + mItemSize*(i-1);
Mathias Agopian15d0edc2010-03-29 13:45:18 -0700194 } else {
195 _do_destroy(temp, 1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800196 }
197
198 _do_copy(temp, item, 1);
199
200 ssize_t j = i-1;
201 void* next = reinterpret_cast<char*>(array) + mItemSize*(i);
202 do {
Mathias Agopian15d0edc2010-03-29 13:45:18 -0700203 _do_destroy(next, 1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800204 _do_copy(next, curr, 1);
205 next = curr;
206 --j;
207 curr = reinterpret_cast<char*>(array) + mItemSize*(j);
208 } while (j>=0 && (cmp(curr, temp, state) > 0));
209
Mathias Agopian15d0edc2010-03-29 13:45:18 -0700210 _do_destroy(next, 1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800211 _do_copy(next, temp, 1);
212 }
213 i++;
214 }
215
216 if (temp) {
217 _do_destroy(temp, 1);
218 free(temp);
219 }
220 }
221 return NO_ERROR;
222}
223
224void VectorImpl::pop()
225{
226 if (size())
227 removeItemsAt(size()-1, 1);
228}
229
230void VectorImpl::push()
231{
232 push(0);
233}
234
235void VectorImpl::push(const void* item)
236{
237 insertAt(item, size());
238}
239
240ssize_t VectorImpl::add()
241{
242 return add(0);
243}
244
Jeff Brown9efaaa42010-06-16 01:53:36 -0700245ssize_t VectorImpl::add(const void* item)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800246{
Jeff Brown9efaaa42010-06-16 01:53:36 -0700247 return insertAt(item, size());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800248}
249
250ssize_t VectorImpl::replaceAt(size_t index)
251{
252 return replaceAt(0, index);
253}
254
255ssize_t VectorImpl::replaceAt(const void* prototype, size_t index)
256{
Steve Blockae074452012-01-09 18:35:44 +0000257 ALOG_ASSERT(index<size(),
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800258 "[%p] replace: index=%d, size=%d", this, (int)index, (int)size());
259
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700260 if (index >= size()) {
261 return BAD_INDEX;
262 }
263
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800264 void* item = editItemLocation(index);
Jeff Brownbbbd7612011-07-14 00:29:49 -0700265 if (item != prototype) {
266 if (item == 0)
267 return NO_MEMORY;
268 _do_destroy(item, 1);
269 if (prototype == 0) {
270 _do_construct(item, 1);
271 } else {
272 _do_copy(item, prototype, 1);
273 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800274 }
275 return ssize_t(index);
276}
277
278ssize_t VectorImpl::removeItemsAt(size_t index, size_t count)
279{
Steve Blockae074452012-01-09 18:35:44 +0000280 ALOG_ASSERT((index+count)<=size(),
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800281 "[%p] remove: index=%d, count=%d, size=%d",
282 this, (int)index, (int)count, (int)size());
283
284 if ((index+count) > size())
285 return BAD_VALUE;
286 _shrink(index, count);
287 return index;
288}
289
290void VectorImpl::finish_vector()
291{
292 release_storage();
293 mStorage = 0;
294 mCount = 0;
295}
296
297void VectorImpl::clear()
298{
299 _shrink(0, mCount);
300}
301
302void* VectorImpl::editItemLocation(size_t index)
303{
Steve Blockae074452012-01-09 18:35:44 +0000304 ALOG_ASSERT(index<capacity(),
Mathias Agopian266a7d62011-07-11 16:26:18 -0700305 "[%p] editItemLocation: index=%d, capacity=%d, count=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800306 this, (int)index, (int)capacity(), (int)mCount);
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700307
308 if (index < capacity()) {
309 void* buffer = editArrayImpl();
310 if (buffer) {
311 return reinterpret_cast<char*>(buffer) + index*mItemSize;
312 }
313 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800314 return 0;
315}
316
317const void* VectorImpl::itemLocation(size_t index) const
318{
Steve Blockae074452012-01-09 18:35:44 +0000319 ALOG_ASSERT(index<capacity(),
Mathias Agopian266a7d62011-07-11 16:26:18 -0700320 "[%p] itemLocation: index=%d, capacity=%d, count=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800321 this, (int)index, (int)capacity(), (int)mCount);
322
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700323 if (index < capacity()) {
324 const void* buffer = arrayImpl();
325 if (buffer) {
326 return reinterpret_cast<const char*>(buffer) + index*mItemSize;
327 }
328 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800329 return 0;
330}
331
332ssize_t VectorImpl::setCapacity(size_t new_capacity)
333{
Narayan Kamathb6381262015-08-28 12:59:48 +0100334 // The capacity must always be greater than or equal to the size
335 // of this vector.
336 if (new_capacity <= size()) {
337 return capacity();
338 }
339
340 size_t new_allocation_size = 0;
341 LOG_ALWAYS_FATAL_IF(!safe_mul(&new_allocation_size, new_capacity, mItemSize));
342 SharedBuffer* sb = SharedBuffer::alloc(new_allocation_size);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800343 if (sb) {
344 void* array = sb->data();
345 _do_copy(array, mStorage, size());
346 release_storage();
347 mStorage = const_cast<void*>(array);
348 } else {
349 return NO_MEMORY;
350 }
351 return new_capacity;
352}
353
Jesse Hallb73559d2013-03-11 10:16:48 -0700354ssize_t VectorImpl::resize(size_t size) {
355 ssize_t result = NO_ERROR;
356 if (size > mCount) {
357 result = insertAt(mCount, size - mCount);
358 } else if (size < mCount) {
359 result = removeItemsAt(size, mCount - size);
360 }
361 return result < 0 ? result : size;
362}
363
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800364void VectorImpl::release_storage()
365{
366 if (mStorage) {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700367 const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800368 if (sb->release(SharedBuffer::eKeepStorage) == 1) {
369 _do_destroy(mStorage, mCount);
370 SharedBuffer::dealloc(sb);
371 }
372 }
373}
374
375void* VectorImpl::_grow(size_t where, size_t amount)
376{
Steve Blockb37fbe92011-10-20 11:56:00 +0100377// ALOGV("_grow(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800378// this, (int)where, (int)amount, (int)mCount, (int)capacity());
379
Steve Blockae074452012-01-09 18:35:44 +0000380 ALOG_ASSERT(where <= mCount,
Jeff Brownaa5daed2011-07-13 22:22:02 -0700381 "[%p] _grow: where=%d, amount=%d, count=%d",
382 this, (int)where, (int)amount, (int)mCount); // caller already checked
383
Narayan Kamathb6381262015-08-28 12:59:48 +0100384 size_t new_size;
385 LOG_ALWAYS_FATAL_IF(!safe_add(&new_size, mCount, amount), "new_size overflow");
386
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800387 if (capacity() < new_size) {
Narayan Kamathb6381262015-08-28 12:59:48 +0100388 // NOTE: This implementation used to resize vectors as per ((3*x + 1) / 2)
389 // (sigh..). Also note, the " + 1" was necessary to handle the special case
390 // where x == 1, where the resized_capacity will be equal to the old
391 // capacity without the +1. The old calculation wouldn't work properly
392 // if x was zero.
393 //
394 // This approximates the old calculation, using (x + (x/2) + 1) instead.
395 size_t new_capacity = 0;
396 LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_size, (new_size / 2)),
397 "new_capacity overflow");
398 LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_capacity, static_cast<size_t>(1u)),
399 "new_capacity overflow");
400 new_capacity = max(kMinVectorCapacity, new_capacity);
401
402 size_t new_alloc_size = 0;
403 LOG_ALWAYS_FATAL_IF(!safe_mul(&new_alloc_size, new_capacity, mItemSize),
404 "new_alloc_size overflow");
405
Steve Blockb37fbe92011-10-20 11:56:00 +0100406// ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800407 if ((mStorage) &&
408 (mCount==where) &&
409 (mFlags & HAS_TRIVIAL_COPY) &&
410 (mFlags & HAS_TRIVIAL_DTOR))
411 {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700412 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
Narayan Kamathb6381262015-08-28 12:59:48 +0100413 SharedBuffer* sb = cur_sb->editResize(new_alloc_size);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800414 mStorage = sb->data();
415 } else {
Narayan Kamathb6381262015-08-28 12:59:48 +0100416 SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800417 if (sb) {
418 void* array = sb->data();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700419 if (where != 0) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800420 _do_copy(array, mStorage, where);
421 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700422 if (where != mCount) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800423 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize;
424 void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
425 _do_copy(dest, from, mCount-where);
426 }
427 release_storage();
428 mStorage = const_cast<void*>(array);
429 }
430 }
431 } else {
Mathias Agopian03b168a2012-05-17 16:52:21 -0700432 void* array = editArrayImpl();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700433 if (where != mCount) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800434 const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700435 void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
436 _do_move_forward(to, from, mCount - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800437 }
438 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700439 mCount = new_size;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800440 void* free_space = const_cast<void*>(itemLocation(where));
441 return free_space;
442}
443
444void VectorImpl::_shrink(size_t where, size_t amount)
445{
446 if (!mStorage)
447 return;
448
Steve Blockb37fbe92011-10-20 11:56:00 +0100449// ALOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800450// this, (int)where, (int)amount, (int)mCount, (int)capacity());
451
Steve Blockae074452012-01-09 18:35:44 +0000452 ALOG_ASSERT(where + amount <= mCount,
Jeff Brownaa5daed2011-07-13 22:22:02 -0700453 "[%p] _shrink: where=%d, amount=%d, count=%d",
454 this, (int)where, (int)amount, (int)mCount); // caller already checked
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800455
Narayan Kamathb6381262015-08-28 12:59:48 +0100456 size_t new_size;
457 LOG_ALWAYS_FATAL_IF(!safe_sub(&new_size, mCount, amount));
458
459 if (new_size < (capacity() / 2)) {
460 // NOTE: (new_size * 2) is safe because capacity didn't overflow and
461 // new_size < (capacity / 2)).
462 const size_t new_capacity = max(kMinVectorCapacity, new_size * 2);
463
464 // NOTE: (new_capacity * mItemSize), (where * mItemSize) and
465 // ((where + amount) * mItemSize) beyond this point are safe because
466 // we are always reducing the capacity of the underlying SharedBuffer.
467 // In other words, (old_capacity * mItemSize) did not overflow, and
468 // where < (where + amount) < new_capacity < old_capacity.
Jeff Brownbbbd7612011-07-14 00:29:49 -0700469 if ((where == new_size) &&
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800470 (mFlags & HAS_TRIVIAL_COPY) &&
471 (mFlags & HAS_TRIVIAL_DTOR))
472 {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700473 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800474 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800475 mStorage = sb->data();
476 } else {
477 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
478 if (sb) {
479 void* array = sb->data();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700480 if (where != 0) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800481 _do_copy(array, mStorage, where);
482 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700483 if (where != new_size) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800484 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize;
485 void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700486 _do_copy(dest, from, new_size - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800487 }
488 release_storage();
489 mStorage = const_cast<void*>(array);
490 }
491 }
492 } else {
Jeff Brownbbbd7612011-07-14 00:29:49 -0700493 void* array = editArrayImpl();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800494 void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
495 _do_destroy(to, amount);
Jeff Brownbbbd7612011-07-14 00:29:49 -0700496 if (where != new_size) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800497 const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700498 _do_move_backward(to, from, new_size - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800499 }
500 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700501 mCount = new_size;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800502}
503
504size_t VectorImpl::itemSize() const {
505 return mItemSize;
506}
507
508void VectorImpl::_do_construct(void* storage, size_t num) const
509{
510 if (!(mFlags & HAS_TRIVIAL_CTOR)) {
511 do_construct(storage, num);
512 }
513}
514
515void VectorImpl::_do_destroy(void* storage, size_t num) const
516{
517 if (!(mFlags & HAS_TRIVIAL_DTOR)) {
518 do_destroy(storage, num);
519 }
520}
521
522void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const
523{
524 if (!(mFlags & HAS_TRIVIAL_COPY)) {
525 do_copy(dest, from, num);
526 } else {
527 memcpy(dest, from, num*itemSize());
528 }
529}
530
531void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const {
532 do_splat(dest, item, num);
533}
534
535void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const {
536 do_move_forward(dest, from, num);
537}
538
539void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const {
540 do_move_backward(dest, from, num);
541}
542
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800543/*****************************************************************************/
544
545SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags)
546 : VectorImpl(itemSize, flags)
547{
548}
549
550SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs)
551: VectorImpl(rhs)
552{
553}
554
555SortedVectorImpl::~SortedVectorImpl()
556{
557}
558
559SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs)
560{
561 return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) );
562}
563
564ssize_t SortedVectorImpl::indexOf(const void* item) const
565{
566 return _indexOrderOf(item);
567}
568
569size_t SortedVectorImpl::orderOf(const void* item) const
570{
571 size_t o;
572 _indexOrderOf(item, &o);
573 return o;
574}
575
576ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const
577{
578 // binary search
579 ssize_t err = NAME_NOT_FOUND;
580 ssize_t l = 0;
581 ssize_t h = size()-1;
582 ssize_t mid;
583 const void* a = arrayImpl();
584 const size_t s = itemSize();
585 while (l <= h) {
586 mid = l + (h - l)/2;
587 const void* const curr = reinterpret_cast<const char *>(a) + (mid*s);
588 const int c = do_compare(curr, item);
589 if (c == 0) {
590 err = l = mid;
591 break;
592 } else if (c < 0) {
593 l = mid + 1;
594 } else {
595 h = mid - 1;
596 }
597 }
598 if (order) *order = l;
599 return err;
600}
601
602ssize_t SortedVectorImpl::add(const void* item)
603{
604 size_t order;
605 ssize_t index = _indexOrderOf(item, &order);
606 if (index < 0) {
607 index = VectorImpl::insertAt(item, order, 1);
608 } else {
609 index = VectorImpl::replaceAt(item, index);
610 }
611 return index;
612}
613
614ssize_t SortedVectorImpl::merge(const VectorImpl& vector)
615{
616 // naive merge...
617 if (!vector.isEmpty()) {
618 const void* buffer = vector.arrayImpl();
619 const size_t is = itemSize();
620 size_t s = vector.size();
621 for (size_t i=0 ; i<s ; i++) {
622 ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is );
623 if (err<0) {
624 return err;
625 }
626 }
627 }
628 return NO_ERROR;
629}
630
631ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector)
632{
633 // we've merging a sorted vector... nice!
634 ssize_t err = NO_ERROR;
635 if (!vector.isEmpty()) {
636 // first take care of the case where the vectors are sorted together
637 if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) {
638 err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0);
639 } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) {
640 err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector));
641 } else {
642 // this could be made a little better
643 err = merge(static_cast<const VectorImpl&>(vector));
644 }
645 }
646 return err;
647}
648
649ssize_t SortedVectorImpl::remove(const void* item)
650{
651 ssize_t i = indexOf(item);
652 if (i>=0) {
653 VectorImpl::removeItemsAt(i, 1);
654 }
655 return i;
656}
657
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800658/*****************************************************************************/
659
660}; // namespace android
661