blob: de65a6cba534d5a6c49b4290584875860ba9a0b0 [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 Kamathc609c312015-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 Kamathc609c312015-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 Kamathc609c312015-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 Kamathc609c312015-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 Kamathc609c312015-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 Kamathc609c312015-08-28 12:59:48 +0100413 SharedBuffer* sb = cur_sb->editResize(new_alloc_size);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800414 if (sb) {
415 mStorage = sb->data();
416 } else {
417 return NULL;
418 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800419 } else {
Narayan Kamathc609c312015-08-28 12:59:48 +0100420 SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800421 if (sb) {
422 void* array = sb->data();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700423 if (where != 0) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800424 _do_copy(array, mStorage, where);
425 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700426 if (where != mCount) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800427 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize;
428 void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
429 _do_copy(dest, from, mCount-where);
430 }
431 release_storage();
432 mStorage = const_cast<void*>(array);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800433 } else {
434 return NULL;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800435 }
436 }
437 } else {
Mathias Agopian03b168a2012-05-17 16:52:21 -0700438 void* array = editArrayImpl();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700439 if (where != mCount) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800440 const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700441 void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
442 _do_move_forward(to, from, mCount - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800443 }
444 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700445 mCount = new_size;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800446 void* free_space = const_cast<void*>(itemLocation(where));
447 return free_space;
448}
449
450void VectorImpl::_shrink(size_t where, size_t amount)
451{
452 if (!mStorage)
453 return;
454
Steve Blockb37fbe92011-10-20 11:56:00 +0100455// ALOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800456// this, (int)where, (int)amount, (int)mCount, (int)capacity());
457
Steve Blockae074452012-01-09 18:35:44 +0000458 ALOG_ASSERT(where + amount <= mCount,
Jeff Brownaa5daed2011-07-13 22:22:02 -0700459 "[%p] _shrink: where=%d, amount=%d, count=%d",
460 this, (int)where, (int)amount, (int)mCount); // caller already checked
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800461
Narayan Kamathc609c312015-08-28 12:59:48 +0100462 size_t new_size;
463 LOG_ALWAYS_FATAL_IF(!safe_sub(&new_size, mCount, amount));
464
465 if (new_size < (capacity() / 2)) {
466 // NOTE: (new_size * 2) is safe because capacity didn't overflow and
467 // new_size < (capacity / 2)).
468 const size_t new_capacity = max(kMinVectorCapacity, new_size * 2);
469
470 // NOTE: (new_capacity * mItemSize), (where * mItemSize) and
471 // ((where + amount) * mItemSize) beyond this point are safe because
472 // we are always reducing the capacity of the underlying SharedBuffer.
473 // In other words, (old_capacity * mItemSize) did not overflow, and
474 // where < (where + amount) < new_capacity < old_capacity.
Jeff Brownbbbd7612011-07-14 00:29:49 -0700475 if ((where == new_size) &&
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800476 (mFlags & HAS_TRIVIAL_COPY) &&
477 (mFlags & HAS_TRIVIAL_DTOR))
478 {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700479 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800480 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800481 if (sb) {
482 mStorage = sb->data();
483 } else {
484 return;
485 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800486 } else {
487 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
488 if (sb) {
489 void* array = sb->data();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700490 if (where != 0) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800491 _do_copy(array, mStorage, where);
492 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700493 if (where != new_size) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800494 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize;
495 void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700496 _do_copy(dest, from, new_size - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800497 }
498 release_storage();
499 mStorage = const_cast<void*>(array);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800500 } else{
501 return;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800502 }
503 }
504 } else {
Jeff Brownbbbd7612011-07-14 00:29:49 -0700505 void* array = editArrayImpl();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800506 void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
507 _do_destroy(to, amount);
Jeff Brownbbbd7612011-07-14 00:29:49 -0700508 if (where != new_size) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800509 const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700510 _do_move_backward(to, from, new_size - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800511 }
512 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700513 mCount = new_size;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800514}
515
516size_t VectorImpl::itemSize() const {
517 return mItemSize;
518}
519
520void VectorImpl::_do_construct(void* storage, size_t num) const
521{
522 if (!(mFlags & HAS_TRIVIAL_CTOR)) {
523 do_construct(storage, num);
524 }
525}
526
527void VectorImpl::_do_destroy(void* storage, size_t num) const
528{
529 if (!(mFlags & HAS_TRIVIAL_DTOR)) {
530 do_destroy(storage, num);
531 }
532}
533
534void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const
535{
536 if (!(mFlags & HAS_TRIVIAL_COPY)) {
537 do_copy(dest, from, num);
538 } else {
539 memcpy(dest, from, num*itemSize());
540 }
541}
542
543void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const {
544 do_splat(dest, item, num);
545}
546
547void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const {
548 do_move_forward(dest, from, num);
549}
550
551void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const {
552 do_move_backward(dest, from, num);
553}
554
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800555/*****************************************************************************/
556
557SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags)
558 : VectorImpl(itemSize, flags)
559{
560}
561
562SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs)
563: VectorImpl(rhs)
564{
565}
566
567SortedVectorImpl::~SortedVectorImpl()
568{
569}
570
571SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs)
572{
573 return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) );
574}
575
576ssize_t SortedVectorImpl::indexOf(const void* item) const
577{
578 return _indexOrderOf(item);
579}
580
581size_t SortedVectorImpl::orderOf(const void* item) const
582{
583 size_t o;
584 _indexOrderOf(item, &o);
585 return o;
586}
587
588ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const
589{
590 // binary search
591 ssize_t err = NAME_NOT_FOUND;
592 ssize_t l = 0;
593 ssize_t h = size()-1;
594 ssize_t mid;
595 const void* a = arrayImpl();
596 const size_t s = itemSize();
597 while (l <= h) {
598 mid = l + (h - l)/2;
599 const void* const curr = reinterpret_cast<const char *>(a) + (mid*s);
600 const int c = do_compare(curr, item);
601 if (c == 0) {
602 err = l = mid;
603 break;
604 } else if (c < 0) {
605 l = mid + 1;
606 } else {
607 h = mid - 1;
608 }
609 }
610 if (order) *order = l;
611 return err;
612}
613
614ssize_t SortedVectorImpl::add(const void* item)
615{
616 size_t order;
617 ssize_t index = _indexOrderOf(item, &order);
618 if (index < 0) {
619 index = VectorImpl::insertAt(item, order, 1);
620 } else {
621 index = VectorImpl::replaceAt(item, index);
622 }
623 return index;
624}
625
626ssize_t SortedVectorImpl::merge(const VectorImpl& vector)
627{
628 // naive merge...
629 if (!vector.isEmpty()) {
630 const void* buffer = vector.arrayImpl();
631 const size_t is = itemSize();
632 size_t s = vector.size();
633 for (size_t i=0 ; i<s ; i++) {
634 ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is );
635 if (err<0) {
636 return err;
637 }
638 }
639 }
640 return NO_ERROR;
641}
642
643ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector)
644{
645 // we've merging a sorted vector... nice!
646 ssize_t err = NO_ERROR;
647 if (!vector.isEmpty()) {
648 // first take care of the case where the vectors are sorted together
649 if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) {
650 err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0);
651 } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) {
652 err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector));
653 } else {
654 // this could be made a little better
655 err = merge(static_cast<const VectorImpl&>(vector));
656 }
657 }
658 return err;
659}
660
661ssize_t SortedVectorImpl::remove(const void* item)
662{
663 ssize_t i = indexOf(item);
664 if (i>=0) {
665 VectorImpl::removeItemsAt(i, 1);
666 }
667 return i;
668}
669
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800670/*****************************************************************************/
671
672}; // namespace android
673