2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "Vector"
23 #include <utils/Log.h>
24 #include <utils/Errors.h>
25 #include <utils/SharedBuffer.h>
26 #include <utils/VectorImpl.h>
28 /*****************************************************************************/
33 // ----------------------------------------------------------------------------
35 const size_t kMinVectorCapacity = 4;
37 static inline size_t max(size_t a, size_t b) {
41 // ----------------------------------------------------------------------------
43 VectorImpl::VectorImpl(size_t itemSize, uint32_t flags)
44 : mStorage(0), mCount(0), mFlags(flags), mItemSize(itemSize)
48 VectorImpl::VectorImpl(const VectorImpl& rhs)
49 : mStorage(rhs.mStorage), mCount(rhs.mCount),
50 mFlags(rhs.mFlags), mItemSize(rhs.mItemSize)
53 SharedBuffer::sharedBuffer(mStorage)->acquire();
57 VectorImpl::~VectorImpl()
61 "subclasses of VectorImpl must call finish_vector()"
62 " in their destructor. Leaking %d bytes.",
63 this, (int)(mCount*mItemSize));
64 // We can't call _do_destroy() here because the vtable is already gone.
67 VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
69 LOG_ASSERT(mItemSize == rhs.mItemSize,
70 "Vector<> have different types (this=%p, rhs=%p)", this, &rhs);
74 mStorage = rhs.mStorage;
76 SharedBuffer::sharedBuffer(mStorage)->acquire();
85 void* VectorImpl::editArrayImpl()
88 SharedBuffer* sb = SharedBuffer::sharedBuffer(mStorage)->attemptEdit();
90 sb = SharedBuffer::alloc(capacity() * mItemSize);
92 _do_copy(sb->data(), mStorage, mCount);
94 mStorage = sb->data();
101 size_t VectorImpl::capacity() const
104 return SharedBuffer::sharedBuffer(mStorage)->size() / mItemSize;
109 ssize_t VectorImpl::insertVectorAt(const VectorImpl& vector, size_t index)
113 void* where = _grow(index, vector.size());
115 _do_copy(where, vector.arrayImpl(), vector.size());
117 return where ? index : (ssize_t)NO_MEMORY;
120 ssize_t VectorImpl::appendVector(const VectorImpl& vector)
122 return insertVectorAt(vector, size());
125 ssize_t VectorImpl::insertAt(size_t index, size_t numItems)
127 return insertAt(0, index, numItems);
130 ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems)
134 void* where = _grow(index, numItems);
137 _do_splat(where, item, numItems);
139 _do_construct(where, numItems);
142 return where ? index : (ssize_t)NO_MEMORY;
145 static int sortProxy(const void* lhs, const void* rhs, void* func)
147 return (*(VectorImpl::compar_t)func)(lhs, rhs);
150 status_t VectorImpl::sort(VectorImpl::compar_t cmp)
152 return sort(sortProxy, (void*)cmp);
155 status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
157 // the sort must be stable. we're using insertion sort which
158 // is well suited for small and already sorted arrays
159 // for big arrays, it could be better to use mergesort
160 const ssize_t count = size();
162 void* array = const_cast<void*>(arrayImpl());
166 void* item = reinterpret_cast<char*>(array) + mItemSize*(i);
167 void* curr = reinterpret_cast<char*>(array) + mItemSize*(i-1);
168 if (cmp(curr, item, state) > 0) {
171 // we're going to have to modify the array...
172 array = editArrayImpl();
173 if (!array) return NO_MEMORY;
174 temp = malloc(mItemSize);
175 if (!temp) return NO_MEMORY;
176 _do_construct(temp, 1);
177 item = reinterpret_cast<char*>(array) + mItemSize*(i);
178 curr = reinterpret_cast<char*>(array) + mItemSize*(i-1);
181 _do_copy(temp, item, 1);
184 void* next = reinterpret_cast<char*>(array) + mItemSize*(i);
186 _do_copy(next, curr, 1);
189 curr = reinterpret_cast<char*>(array) + mItemSize*(j);
190 } while (j>=0 && (cmp(curr, temp, state) > 0));
192 _do_copy(next, temp, 1);
198 _do_destroy(temp, 1);
205 void VectorImpl::pop()
208 removeItemsAt(size()-1, 1);
211 void VectorImpl::push()
216 void VectorImpl::push(const void* item)
218 insertAt(item, size());
221 ssize_t VectorImpl::add()
226 ssize_t VectorImpl::add(const void* item)
228 return insertAt(item, size());
231 ssize_t VectorImpl::replaceAt(size_t index)
233 return replaceAt(0, index);
236 ssize_t VectorImpl::replaceAt(const void* prototype, size_t index)
238 LOG_ASSERT(index<size(),
239 "[%p] replace: index=%d, size=%d", this, (int)index, (int)size());
241 void* item = editItemLocation(index);
244 _do_destroy(item, 1);
245 if (prototype == 0) {
246 _do_construct(item, 1);
248 _do_copy(item, prototype, 1);
250 return ssize_t(index);
253 ssize_t VectorImpl::removeItemsAt(size_t index, size_t count)
255 LOG_ASSERT((index+count)<=size(),
256 "[%p] remove: index=%d, count=%d, size=%d",
257 this, (int)index, (int)count, (int)size());
259 if ((index+count) > size())
261 _shrink(index, count);
265 void VectorImpl::finish_vector()
272 void VectorImpl::clear()
277 void* VectorImpl::editItemLocation(size_t index)
279 LOG_ASSERT(index<capacity(),
280 "[%p] itemLocation: index=%d, capacity=%d, count=%d",
281 this, (int)index, (int)capacity(), (int)mCount);
283 void* buffer = editArrayImpl();
285 return reinterpret_cast<char*>(buffer) + index*mItemSize;
289 const void* VectorImpl::itemLocation(size_t index) const
291 LOG_ASSERT(index<capacity(),
292 "[%p] editItemLocation: index=%d, capacity=%d, count=%d",
293 this, (int)index, (int)capacity(), (int)mCount);
295 const void* buffer = arrayImpl();
297 return reinterpret_cast<const char*>(buffer) + index*mItemSize;
301 ssize_t VectorImpl::setCapacity(size_t new_capacity)
303 size_t current_capacity = capacity();
304 ssize_t amount = new_capacity - size();
306 // we can't reduce the capacity
307 return current_capacity;
309 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
311 void* array = sb->data();
312 _do_copy(array, mStorage, size());
314 mStorage = const_cast<void*>(array);
321 void VectorImpl::release_storage()
324 const SharedBuffer* sb = SharedBuffer::sharedBuffer(mStorage);
325 if (sb->release(SharedBuffer::eKeepStorage) == 1) {
326 _do_destroy(mStorage, mCount);
327 SharedBuffer::dealloc(sb);
332 void* VectorImpl::_grow(size_t where, size_t amount)
334 // LOGV("_grow(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
335 // this, (int)where, (int)amount, (int)mCount, (int)capacity());
340 const size_t new_size = mCount + amount;
341 if (capacity() < new_size) {
342 const size_t new_capacity = max(kMinVectorCapacity, ((new_size*3)+1)/2);
343 // LOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity);
346 (mFlags & HAS_TRIVIAL_COPY) &&
347 (mFlags & HAS_TRIVIAL_DTOR))
349 const SharedBuffer* cur_sb = SharedBuffer::sharedBuffer(mStorage);
350 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
351 mStorage = sb->data();
353 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
355 void* array = sb->data();
357 _do_copy(array, mStorage, where);
360 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize;
361 void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
362 _do_copy(dest, from, mCount-where);
365 mStorage = const_cast<void*>(array);
369 ssize_t s = mCount-where;
371 void* array = editArrayImpl();
372 void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
373 const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize;
374 _do_move_forward(to, from, s);
378 void* free_space = const_cast<void*>(itemLocation(where));
382 void VectorImpl::_shrink(size_t where, size_t amount)
387 // LOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
388 // this, (int)where, (int)amount, (int)mCount, (int)capacity());
391 where = mCount - amount;
393 const size_t new_size = mCount - amount;
394 if (new_size*3 < capacity()) {
395 const size_t new_capacity = max(kMinVectorCapacity, new_size*2);
396 // LOGV("shrink vector %p, new_capacity=%d", this, (int)new_capacity);
397 if ((where == mCount-amount) &&
398 (mFlags & HAS_TRIVIAL_COPY) &&
399 (mFlags & HAS_TRIVIAL_DTOR))
401 const SharedBuffer* cur_sb = SharedBuffer::sharedBuffer(mStorage);
402 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
403 mStorage = sb->data();
405 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
407 void* array = sb->data();
409 _do_copy(array, mStorage, where);
411 if (mCount > where+amount) {
412 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize;
413 void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
414 _do_copy(dest, from, mCount-(where+amount));
417 mStorage = const_cast<void*>(array);
421 void* array = editArrayImpl();
422 void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
423 _do_destroy(to, amount);
424 ssize_t s = mCount-(where+amount);
426 const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
427 _do_move_backward(to, from, s);
431 // adjust the number of items...
435 size_t VectorImpl::itemSize() const {
439 void VectorImpl::_do_construct(void* storage, size_t num) const
441 if (!(mFlags & HAS_TRIVIAL_CTOR)) {
442 do_construct(storage, num);
446 void VectorImpl::_do_destroy(void* storage, size_t num) const
448 if (!(mFlags & HAS_TRIVIAL_DTOR)) {
449 do_destroy(storage, num);
453 void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const
455 if (!(mFlags & HAS_TRIVIAL_COPY)) {
456 do_copy(dest, from, num);
458 memcpy(dest, from, num*itemSize());
462 void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const {
463 do_splat(dest, item, num);
466 void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const {
467 do_move_forward(dest, from, num);
470 void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const {
471 do_move_backward(dest, from, num);
474 void VectorImpl::reservedVectorImpl1() { }
475 void VectorImpl::reservedVectorImpl2() { }
476 void VectorImpl::reservedVectorImpl3() { }
477 void VectorImpl::reservedVectorImpl4() { }
478 void VectorImpl::reservedVectorImpl5() { }
479 void VectorImpl::reservedVectorImpl6() { }
480 void VectorImpl::reservedVectorImpl7() { }
481 void VectorImpl::reservedVectorImpl8() { }
483 /*****************************************************************************/
485 SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags)
486 : VectorImpl(itemSize, flags)
490 SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs)
495 SortedVectorImpl::~SortedVectorImpl()
499 SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs)
501 return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) );
504 ssize_t SortedVectorImpl::indexOf(const void* item) const
506 return _indexOrderOf(item);
509 size_t SortedVectorImpl::orderOf(const void* item) const
512 _indexOrderOf(item, &o);
516 ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const
519 ssize_t err = NAME_NOT_FOUND;
521 ssize_t h = size()-1;
523 const void* a = arrayImpl();
524 const size_t s = itemSize();
527 const void* const curr = reinterpret_cast<const char *>(a) + (mid*s);
528 const int c = do_compare(curr, item);
538 if (order) *order = l;
542 ssize_t SortedVectorImpl::add(const void* item)
545 ssize_t index = _indexOrderOf(item, &order);
547 index = VectorImpl::insertAt(item, order, 1);
549 index = VectorImpl::replaceAt(item, index);
554 ssize_t SortedVectorImpl::merge(const VectorImpl& vector)
557 if (!vector.isEmpty()) {
558 const void* buffer = vector.arrayImpl();
559 const size_t is = itemSize();
560 size_t s = vector.size();
561 for (size_t i=0 ; i<s ; i++) {
562 ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is );
571 ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector)
573 // we've merging a sorted vector... nice!
574 ssize_t err = NO_ERROR;
575 if (!vector.isEmpty()) {
576 // first take care of the case where the vectors are sorted together
577 if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) {
578 err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0);
579 } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) {
580 err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector));
582 // this could be made a little better
583 err = merge(static_cast<const VectorImpl&>(vector));
589 ssize_t SortedVectorImpl::remove(const void* item)
591 ssize_t i = indexOf(item);
593 VectorImpl::removeItemsAt(i, 1);
598 void SortedVectorImpl::reservedSortedVectorImpl1() { };
599 void SortedVectorImpl::reservedSortedVectorImpl2() { };
600 void SortedVectorImpl::reservedSortedVectorImpl3() { };
601 void SortedVectorImpl::reservedSortedVectorImpl4() { };
602 void SortedVectorImpl::reservedSortedVectorImpl5() { };
603 void SortedVectorImpl::reservedSortedVectorImpl6() { };
604 void SortedVectorImpl::reservedSortedVectorImpl7() { };
605 void SortedVectorImpl::reservedSortedVectorImpl8() { };
608 /*****************************************************************************/
610 }; // namespace android