blob: 7c534729961aa1e07d362da9d618721efc96538b [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;
Nick Kralevichc76698f2015-08-28 06:40:23 -0700207 curr = NULL;
208 if (j >= 0) {
209 curr = reinterpret_cast<char*>(array) + mItemSize*(j);
210 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800211 } while (j>=0 && (cmp(curr, temp, state) > 0));
212
Mathias Agopian15d0edc2010-03-29 13:45:18 -0700213 _do_destroy(next, 1);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800214 _do_copy(next, temp, 1);
215 }
216 i++;
217 }
218
219 if (temp) {
220 _do_destroy(temp, 1);
221 free(temp);
222 }
223 }
224 return NO_ERROR;
225}
226
227void VectorImpl::pop()
228{
229 if (size())
230 removeItemsAt(size()-1, 1);
231}
232
233void VectorImpl::push()
234{
235 push(0);
236}
237
238void VectorImpl::push(const void* item)
239{
240 insertAt(item, size());
241}
242
243ssize_t VectorImpl::add()
244{
245 return add(0);
246}
247
Jeff Brown9efaaa42010-06-16 01:53:36 -0700248ssize_t VectorImpl::add(const void* item)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800249{
Jeff Brown9efaaa42010-06-16 01:53:36 -0700250 return insertAt(item, size());
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800251}
252
253ssize_t VectorImpl::replaceAt(size_t index)
254{
255 return replaceAt(0, index);
256}
257
258ssize_t VectorImpl::replaceAt(const void* prototype, size_t index)
259{
Steve Blockae074452012-01-09 18:35:44 +0000260 ALOG_ASSERT(index<size(),
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800261 "[%p] replace: index=%d, size=%d", this, (int)index, (int)size());
262
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700263 if (index >= size()) {
264 return BAD_INDEX;
265 }
266
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800267 void* item = editItemLocation(index);
Jeff Brownbbbd7612011-07-14 00:29:49 -0700268 if (item != prototype) {
269 if (item == 0)
270 return NO_MEMORY;
271 _do_destroy(item, 1);
272 if (prototype == 0) {
273 _do_construct(item, 1);
274 } else {
275 _do_copy(item, prototype, 1);
276 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800277 }
278 return ssize_t(index);
279}
280
281ssize_t VectorImpl::removeItemsAt(size_t index, size_t count)
282{
Steve Blockae074452012-01-09 18:35:44 +0000283 ALOG_ASSERT((index+count)<=size(),
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800284 "[%p] remove: index=%d, count=%d, size=%d",
285 this, (int)index, (int)count, (int)size());
286
287 if ((index+count) > size())
288 return BAD_VALUE;
289 _shrink(index, count);
290 return index;
291}
292
293void VectorImpl::finish_vector()
294{
295 release_storage();
296 mStorage = 0;
297 mCount = 0;
298}
299
300void VectorImpl::clear()
301{
302 _shrink(0, mCount);
303}
304
305void* VectorImpl::editItemLocation(size_t index)
306{
Steve Blockae074452012-01-09 18:35:44 +0000307 ALOG_ASSERT(index<capacity(),
Mathias Agopian266a7d62011-07-11 16:26:18 -0700308 "[%p] editItemLocation: index=%d, capacity=%d, count=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800309 this, (int)index, (int)capacity(), (int)mCount);
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700310
311 if (index < capacity()) {
312 void* buffer = editArrayImpl();
313 if (buffer) {
314 return reinterpret_cast<char*>(buffer) + index*mItemSize;
315 }
316 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800317 return 0;
318}
319
320const void* VectorImpl::itemLocation(size_t index) const
321{
Steve Blockae074452012-01-09 18:35:44 +0000322 ALOG_ASSERT(index<capacity(),
Mathias Agopian266a7d62011-07-11 16:26:18 -0700323 "[%p] itemLocation: index=%d, capacity=%d, count=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800324 this, (int)index, (int)capacity(), (int)mCount);
325
Mathias Agopianbdf73c72012-08-09 19:39:15 -0700326 if (index < capacity()) {
327 const void* buffer = arrayImpl();
328 if (buffer) {
329 return reinterpret_cast<const char*>(buffer) + index*mItemSize;
330 }
331 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800332 return 0;
333}
334
335ssize_t VectorImpl::setCapacity(size_t new_capacity)
336{
Narayan Kamathc609c312015-08-28 12:59:48 +0100337 // The capacity must always be greater than or equal to the size
338 // of this vector.
339 if (new_capacity <= size()) {
340 return capacity();
341 }
342
343 size_t new_allocation_size = 0;
344 LOG_ALWAYS_FATAL_IF(!safe_mul(&new_allocation_size, new_capacity, mItemSize));
345 SharedBuffer* sb = SharedBuffer::alloc(new_allocation_size);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800346 if (sb) {
347 void* array = sb->data();
348 _do_copy(array, mStorage, size());
349 release_storage();
350 mStorage = const_cast<void*>(array);
351 } else {
352 return NO_MEMORY;
353 }
354 return new_capacity;
355}
356
Jesse Hallb73559d2013-03-11 10:16:48 -0700357ssize_t VectorImpl::resize(size_t size) {
358 ssize_t result = NO_ERROR;
359 if (size > mCount) {
360 result = insertAt(mCount, size - mCount);
361 } else if (size < mCount) {
362 result = removeItemsAt(size, mCount - size);
363 }
364 return result < 0 ? result : size;
365}
366
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800367void VectorImpl::release_storage()
368{
369 if (mStorage) {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700370 const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800371 if (sb->release(SharedBuffer::eKeepStorage) == 1) {
372 _do_destroy(mStorage, mCount);
373 SharedBuffer::dealloc(sb);
374 }
375 }
376}
377
378void* VectorImpl::_grow(size_t where, size_t amount)
379{
Steve Blockb37fbe92011-10-20 11:56:00 +0100380// ALOGV("_grow(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800381// this, (int)where, (int)amount, (int)mCount, (int)capacity());
382
Steve Blockae074452012-01-09 18:35:44 +0000383 ALOG_ASSERT(where <= mCount,
Jeff Brownaa5daed2011-07-13 22:22:02 -0700384 "[%p] _grow: where=%d, amount=%d, count=%d",
385 this, (int)where, (int)amount, (int)mCount); // caller already checked
386
Narayan Kamathc609c312015-08-28 12:59:48 +0100387 size_t new_size;
388 LOG_ALWAYS_FATAL_IF(!safe_add(&new_size, mCount, amount), "new_size overflow");
389
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800390 if (capacity() < new_size) {
Narayan Kamathc609c312015-08-28 12:59:48 +0100391 // NOTE: This implementation used to resize vectors as per ((3*x + 1) / 2)
392 // (sigh..). Also note, the " + 1" was necessary to handle the special case
393 // where x == 1, where the resized_capacity will be equal to the old
394 // capacity without the +1. The old calculation wouldn't work properly
395 // if x was zero.
396 //
397 // This approximates the old calculation, using (x + (x/2) + 1) instead.
398 size_t new_capacity = 0;
399 LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_size, (new_size / 2)),
400 "new_capacity overflow");
401 LOG_ALWAYS_FATAL_IF(!safe_add(&new_capacity, new_capacity, static_cast<size_t>(1u)),
402 "new_capacity overflow");
403 new_capacity = max(kMinVectorCapacity, new_capacity);
404
405 size_t new_alloc_size = 0;
406 LOG_ALWAYS_FATAL_IF(!safe_mul(&new_alloc_size, new_capacity, mItemSize),
407 "new_alloc_size overflow");
408
Steve Blockb37fbe92011-10-20 11:56:00 +0100409// ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800410 if ((mStorage) &&
411 (mCount==where) &&
412 (mFlags & HAS_TRIVIAL_COPY) &&
413 (mFlags & HAS_TRIVIAL_DTOR))
414 {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700415 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
Narayan Kamathc609c312015-08-28 12:59:48 +0100416 SharedBuffer* sb = cur_sb->editResize(new_alloc_size);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800417 if (sb) {
418 mStorage = sb->data();
419 } else {
420 return NULL;
421 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800422 } else {
Narayan Kamathc609c312015-08-28 12:59:48 +0100423 SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800424 if (sb) {
425 void* array = sb->data();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700426 if (where != 0) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800427 _do_copy(array, mStorage, where);
428 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700429 if (where != mCount) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800430 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize;
431 void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
432 _do_copy(dest, from, mCount-where);
433 }
434 release_storage();
435 mStorage = const_cast<void*>(array);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800436 } else {
437 return NULL;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800438 }
439 }
440 } else {
Mathias Agopian03b168a2012-05-17 16:52:21 -0700441 void* array = editArrayImpl();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700442 if (where != mCount) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800443 const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700444 void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
445 _do_move_forward(to, from, mCount - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800446 }
447 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700448 mCount = new_size;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800449 void* free_space = const_cast<void*>(itemLocation(where));
450 return free_space;
451}
452
453void VectorImpl::_shrink(size_t where, size_t amount)
454{
455 if (!mStorage)
456 return;
457
Steve Blockb37fbe92011-10-20 11:56:00 +0100458// ALOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800459// this, (int)where, (int)amount, (int)mCount, (int)capacity());
460
Steve Blockae074452012-01-09 18:35:44 +0000461 ALOG_ASSERT(where + amount <= mCount,
Jeff Brownaa5daed2011-07-13 22:22:02 -0700462 "[%p] _shrink: where=%d, amount=%d, count=%d",
463 this, (int)where, (int)amount, (int)mCount); // caller already checked
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800464
Narayan Kamathc609c312015-08-28 12:59:48 +0100465 size_t new_size;
466 LOG_ALWAYS_FATAL_IF(!safe_sub(&new_size, mCount, amount));
467
468 if (new_size < (capacity() / 2)) {
469 // NOTE: (new_size * 2) is safe because capacity didn't overflow and
470 // new_size < (capacity / 2)).
471 const size_t new_capacity = max(kMinVectorCapacity, new_size * 2);
472
473 // NOTE: (new_capacity * mItemSize), (where * mItemSize) and
474 // ((where + amount) * mItemSize) beyond this point are safe because
475 // we are always reducing the capacity of the underlying SharedBuffer.
476 // In other words, (old_capacity * mItemSize) did not overflow, and
477 // where < (where + amount) < new_capacity < old_capacity.
Jeff Brownbbbd7612011-07-14 00:29:49 -0700478 if ((where == new_size) &&
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800479 (mFlags & HAS_TRIVIAL_COPY) &&
480 (mFlags & HAS_TRIVIAL_DTOR))
481 {
Mathias Agopiane79aadd2012-08-31 16:20:23 -0700482 const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800483 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800484 if (sb) {
485 mStorage = sb->data();
486 } else {
487 return;
488 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800489 } else {
490 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
491 if (sb) {
492 void* array = sb->data();
Jeff Brownbbbd7612011-07-14 00:29:49 -0700493 if (where != 0) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800494 _do_copy(array, mStorage, where);
495 }
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<const uint8_t *>(mStorage) + (where+amount)*mItemSize;
498 void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700499 _do_copy(dest, from, new_size - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800500 }
501 release_storage();
502 mStorage = const_cast<void*>(array);
Shuo Gaoeb0eb4f2013-10-17 11:36:11 +0800503 } else{
504 return;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800505 }
506 }
507 } else {
Jeff Brownbbbd7612011-07-14 00:29:49 -0700508 void* array = editArrayImpl();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800509 void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
510 _do_destroy(to, amount);
Jeff Brownbbbd7612011-07-14 00:29:49 -0700511 if (where != new_size) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800512 const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
Jeff Brownbbbd7612011-07-14 00:29:49 -0700513 _do_move_backward(to, from, new_size - where);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800514 }
515 }
Jeff Brownbbbd7612011-07-14 00:29:49 -0700516 mCount = new_size;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800517}
518
519size_t VectorImpl::itemSize() const {
520 return mItemSize;
521}
522
523void VectorImpl::_do_construct(void* storage, size_t num) const
524{
525 if (!(mFlags & HAS_TRIVIAL_CTOR)) {
526 do_construct(storage, num);
527 }
528}
529
530void VectorImpl::_do_destroy(void* storage, size_t num) const
531{
532 if (!(mFlags & HAS_TRIVIAL_DTOR)) {
533 do_destroy(storage, num);
534 }
535}
536
537void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const
538{
539 if (!(mFlags & HAS_TRIVIAL_COPY)) {
540 do_copy(dest, from, num);
541 } else {
542 memcpy(dest, from, num*itemSize());
543 }
544}
545
546void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const {
547 do_splat(dest, item, num);
548}
549
550void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const {
551 do_move_forward(dest, from, num);
552}
553
554void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const {
555 do_move_backward(dest, from, num);
556}
557
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800558/*****************************************************************************/
559
560SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags)
561 : VectorImpl(itemSize, flags)
562{
563}
564
565SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs)
566: VectorImpl(rhs)
567{
568}
569
570SortedVectorImpl::~SortedVectorImpl()
571{
572}
573
574SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs)
575{
576 return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) );
577}
578
579ssize_t SortedVectorImpl::indexOf(const void* item) const
580{
581 return _indexOrderOf(item);
582}
583
584size_t SortedVectorImpl::orderOf(const void* item) const
585{
586 size_t o;
587 _indexOrderOf(item, &o);
588 return o;
589}
590
591ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const
592{
Nick Kralevich1f286982015-08-22 14:27:03 -0700593 if (order) *order = 0;
594 if (isEmpty()) {
595 return NAME_NOT_FOUND;
596 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800597 // binary search
598 ssize_t err = NAME_NOT_FOUND;
599 ssize_t l = 0;
600 ssize_t h = size()-1;
601 ssize_t mid;
602 const void* a = arrayImpl();
603 const size_t s = itemSize();
604 while (l <= h) {
605 mid = l + (h - l)/2;
606 const void* const curr = reinterpret_cast<const char *>(a) + (mid*s);
607 const int c = do_compare(curr, item);
608 if (c == 0) {
609 err = l = mid;
610 break;
611 } else if (c < 0) {
612 l = mid + 1;
613 } else {
614 h = mid - 1;
615 }
616 }
617 if (order) *order = l;
618 return err;
619}
620
621ssize_t SortedVectorImpl::add(const void* item)
622{
623 size_t order;
624 ssize_t index = _indexOrderOf(item, &order);
625 if (index < 0) {
626 index = VectorImpl::insertAt(item, order, 1);
627 } else {
628 index = VectorImpl::replaceAt(item, index);
629 }
630 return index;
631}
632
633ssize_t SortedVectorImpl::merge(const VectorImpl& vector)
634{
635 // naive merge...
636 if (!vector.isEmpty()) {
637 const void* buffer = vector.arrayImpl();
638 const size_t is = itemSize();
639 size_t s = vector.size();
640 for (size_t i=0 ; i<s ; i++) {
641 ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is );
642 if (err<0) {
643 return err;
644 }
645 }
646 }
647 return NO_ERROR;
648}
649
650ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector)
651{
652 // we've merging a sorted vector... nice!
653 ssize_t err = NO_ERROR;
654 if (!vector.isEmpty()) {
655 // first take care of the case where the vectors are sorted together
656 if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) {
657 err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0);
658 } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) {
659 err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector));
660 } else {
661 // this could be made a little better
662 err = merge(static_cast<const VectorImpl&>(vector));
663 }
664 }
665 return err;
666}
667
668ssize_t SortedVectorImpl::remove(const void* item)
669{
670 ssize_t i = indexOf(item);
671 if (i>=0) {
672 VectorImpl::removeItemsAt(i, 1);
673 }
674 return i;
675}
676
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800677/*****************************************************************************/
678
679}; // namespace android
680