blob: 4954ffee7d252c96bc026fc0c699408309842f0c [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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
23#include <utils/Log.h>
24#include <utils/Errors.h>
25#include <utils/SharedBuffer.h>
26#include <utils/VectorImpl.h>
27
28/*****************************************************************************/
29
30
31namespace android {
32
33// ----------------------------------------------------------------------------
34
35const size_t kMinVectorCapacity = 4;
36
37static inline size_t max(size_t a, size_t b) {
38 return a>b ? a : b;
39}
40
41// ----------------------------------------------------------------------------
42
43VectorImpl::VectorImpl(size_t itemSize, uint32_t flags)
44 : mStorage(0), mCount(0), mFlags(flags), mItemSize(itemSize)
45{
46}
47
48VectorImpl::VectorImpl(const VectorImpl& rhs)
49 : mStorage(rhs.mStorage), mCount(rhs.mCount),
50 mFlags(rhs.mFlags), mItemSize(rhs.mItemSize)
51{
52 if (mStorage) {
53 SharedBuffer::sharedBuffer(mStorage)->acquire();
54 }
55}
56
57VectorImpl::~VectorImpl()
58{
59 LOG_ASSERT(!mCount,
60 "[%p] "
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.
65}
66
67VectorImpl& VectorImpl::operator = (const VectorImpl& rhs)
68{
69 LOG_ASSERT(mItemSize == rhs.mItemSize,
70 "Vector<> have different types (this=%p, rhs=%p)", this, &rhs);
71 if (this != &rhs) {
72 release_storage();
73 if (rhs.mCount) {
74 mStorage = rhs.mStorage;
75 mCount = rhs.mCount;
76 SharedBuffer::sharedBuffer(mStorage)->acquire();
77 } else {
78 mStorage = 0;
79 mCount = 0;
80 }
81 }
82 return *this;
83}
84
85void* VectorImpl::editArrayImpl()
86{
87 if (mStorage) {
88 SharedBuffer* sb = SharedBuffer::sharedBuffer(mStorage)->attemptEdit();
89 if (sb == 0) {
90 sb = SharedBuffer::alloc(capacity() * mItemSize);
91 if (sb) {
92 _do_copy(sb->data(), mStorage, mCount);
93 release_storage();
94 mStorage = sb->data();
95 }
96 }
97 }
98 return mStorage;
99}
100
101size_t VectorImpl::capacity() const
102{
103 if (mStorage) {
104 return SharedBuffer::sharedBuffer(mStorage)->size() / mItemSize;
105 }
106 return 0;
107}
108
109ssize_t VectorImpl::insertVectorAt(const VectorImpl& vector, size_t index)
110{
111 if (index > size())
112 return BAD_INDEX;
113 void* where = _grow(index, vector.size());
114 if (where) {
115 _do_copy(where, vector.arrayImpl(), vector.size());
116 }
117 return where ? index : (ssize_t)NO_MEMORY;
118}
119
120ssize_t VectorImpl::appendVector(const VectorImpl& vector)
121{
122 return insertVectorAt(vector, size());
123}
124
125ssize_t VectorImpl::insertAt(size_t index, size_t numItems)
126{
127 return insertAt(0, index, numItems);
128}
129
130ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems)
131{
132 if (index > size())
133 return BAD_INDEX;
134 void* where = _grow(index, numItems);
135 if (where) {
136 if (item) {
137 _do_splat(where, item, numItems);
138 } else {
139 _do_construct(where, numItems);
140 }
141 }
142 return where ? index : (ssize_t)NO_MEMORY;
143}
144
145static int sortProxy(const void* lhs, const void* rhs, void* func)
146{
147 return (*(VectorImpl::compar_t)func)(lhs, rhs);
148}
149
150status_t VectorImpl::sort(VectorImpl::compar_t cmp)
151{
152 return sort(sortProxy, (void*)cmp);
153}
154
155status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state)
156{
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();
161 if (count > 1) {
162 void* array = const_cast<void*>(arrayImpl());
163 void* temp = 0;
164 ssize_t i = 1;
165 while (i < count) {
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) {
169
170 if (!temp) {
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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 item = reinterpret_cast<char*>(array) + mItemSize*(i);
177 curr = reinterpret_cast<char*>(array) + mItemSize*(i-1);
Mathias Agopiancaeddc72010-03-29 13:45:18 -0700178 } else {
179 _do_destroy(temp, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 }
181
182 _do_copy(temp, item, 1);
183
184 ssize_t j = i-1;
185 void* next = reinterpret_cast<char*>(array) + mItemSize*(i);
186 do {
Mathias Agopiancaeddc72010-03-29 13:45:18 -0700187 _do_destroy(next, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 _do_copy(next, curr, 1);
189 next = curr;
190 --j;
191 curr = reinterpret_cast<char*>(array) + mItemSize*(j);
192 } while (j>=0 && (cmp(curr, temp, state) > 0));
193
Mathias Agopiancaeddc72010-03-29 13:45:18 -0700194 _do_destroy(next, 1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 _do_copy(next, temp, 1);
196 }
197 i++;
198 }
199
200 if (temp) {
201 _do_destroy(temp, 1);
202 free(temp);
203 }
204 }
205 return NO_ERROR;
206}
207
208void VectorImpl::pop()
209{
210 if (size())
211 removeItemsAt(size()-1, 1);
212}
213
214void VectorImpl::push()
215{
216 push(0);
217}
218
219void VectorImpl::push(const void* item)
220{
221 insertAt(item, size());
222}
223
224ssize_t VectorImpl::add()
225{
226 return add(0);
227}
228
229ssize_t VectorImpl::add(const void* item)
230{
231 return insertAt(item, size());
232}
233
234ssize_t VectorImpl::replaceAt(size_t index)
235{
236 return replaceAt(0, index);
237}
238
239ssize_t VectorImpl::replaceAt(const void* prototype, size_t index)
240{
241 LOG_ASSERT(index<size(),
242 "[%p] replace: index=%d, size=%d", this, (int)index, (int)size());
243
244 void* item = editItemLocation(index);
245 if (item == 0)
246 return NO_MEMORY;
247 _do_destroy(item, 1);
248 if (prototype == 0) {
249 _do_construct(item, 1);
250 } else {
251 _do_copy(item, prototype, 1);
252 }
253 return ssize_t(index);
254}
255
256ssize_t VectorImpl::removeItemsAt(size_t index, size_t count)
257{
258 LOG_ASSERT((index+count)<=size(),
259 "[%p] remove: index=%d, count=%d, size=%d",
260 this, (int)index, (int)count, (int)size());
261
262 if ((index+count) > size())
263 return BAD_VALUE;
264 _shrink(index, count);
265 return index;
266}
267
268void VectorImpl::finish_vector()
269{
270 release_storage();
271 mStorage = 0;
272 mCount = 0;
273}
274
275void VectorImpl::clear()
276{
277 _shrink(0, mCount);
278}
279
280void* VectorImpl::editItemLocation(size_t index)
281{
282 LOG_ASSERT(index<capacity(),
283 "[%p] itemLocation: index=%d, capacity=%d, count=%d",
284 this, (int)index, (int)capacity(), (int)mCount);
285
286 void* buffer = editArrayImpl();
287 if (buffer)
288 return reinterpret_cast<char*>(buffer) + index*mItemSize;
289 return 0;
290}
291
292const void* VectorImpl::itemLocation(size_t index) const
293{
294 LOG_ASSERT(index<capacity(),
295 "[%p] editItemLocation: index=%d, capacity=%d, count=%d",
296 this, (int)index, (int)capacity(), (int)mCount);
297
298 const void* buffer = arrayImpl();
299 if (buffer)
300 return reinterpret_cast<const char*>(buffer) + index*mItemSize;
301 return 0;
302}
303
304ssize_t VectorImpl::setCapacity(size_t new_capacity)
305{
306 size_t current_capacity = capacity();
307 ssize_t amount = new_capacity - size();
308 if (amount <= 0) {
309 // we can't reduce the capacity
310 return current_capacity;
311 }
312 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
313 if (sb) {
314 void* array = sb->data();
315 _do_copy(array, mStorage, size());
316 release_storage();
317 mStorage = const_cast<void*>(array);
318 } else {
319 return NO_MEMORY;
320 }
321 return new_capacity;
322}
323
324void VectorImpl::release_storage()
325{
326 if (mStorage) {
327 const SharedBuffer* sb = SharedBuffer::sharedBuffer(mStorage);
328 if (sb->release(SharedBuffer::eKeepStorage) == 1) {
329 _do_destroy(mStorage, mCount);
330 SharedBuffer::dealloc(sb);
331 }
332 }
333}
334
335void* VectorImpl::_grow(size_t where, size_t amount)
336{
337// LOGV("_grow(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
338// this, (int)where, (int)amount, (int)mCount, (int)capacity());
339
340 if (where > mCount)
341 where = mCount;
342
343 const size_t new_size = mCount + amount;
344 if (capacity() < new_size) {
345 const size_t new_capacity = max(kMinVectorCapacity, ((new_size*3)+1)/2);
346// LOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity);
347 if ((mStorage) &&
348 (mCount==where) &&
349 (mFlags & HAS_TRIVIAL_COPY) &&
350 (mFlags & HAS_TRIVIAL_DTOR))
351 {
352 const SharedBuffer* cur_sb = SharedBuffer::sharedBuffer(mStorage);
353 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
Mathias Agopian77e26d62010-03-30 16:53:40 -0700354 release_storage();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800355 mStorage = sb->data();
356 } else {
357 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
358 if (sb) {
359 void* array = sb->data();
360 if (where>0) {
361 _do_copy(array, mStorage, where);
362 }
363 if (mCount>where) {
364 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize;
365 void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
366 _do_copy(dest, from, mCount-where);
367 }
368 release_storage();
369 mStorage = const_cast<void*>(array);
370 }
371 }
372 } else {
373 ssize_t s = mCount-where;
374 if (s>0) {
375 void* array = editArrayImpl();
376 void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
377 const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize;
378 _do_move_forward(to, from, s);
379 }
380 }
381 mCount += amount;
382 void* free_space = const_cast<void*>(itemLocation(where));
383 return free_space;
384}
385
386void VectorImpl::_shrink(size_t where, size_t amount)
387{
388 if (!mStorage)
389 return;
390
391// LOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d",
392// this, (int)where, (int)amount, (int)mCount, (int)capacity());
393
394 if (where >= mCount)
395 where = mCount - amount;
396
397 const size_t new_size = mCount - amount;
398 if (new_size*3 < capacity()) {
399 const size_t new_capacity = max(kMinVectorCapacity, new_size*2);
400// LOGV("shrink vector %p, new_capacity=%d", this, (int)new_capacity);
401 if ((where == mCount-amount) &&
402 (mFlags & HAS_TRIVIAL_COPY) &&
403 (mFlags & HAS_TRIVIAL_DTOR))
404 {
405 const SharedBuffer* cur_sb = SharedBuffer::sharedBuffer(mStorage);
406 SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize);
Mathias Agopian77e26d62010-03-30 16:53:40 -0700407 release_storage();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 mStorage = sb->data();
409 } else {
410 SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
411 if (sb) {
412 void* array = sb->data();
413 if (where>0) {
414 _do_copy(array, mStorage, where);
415 }
416 if (mCount > where+amount) {
417 const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize;
418 void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
419 _do_copy(dest, from, mCount-(where+amount));
420 }
421 release_storage();
422 mStorage = const_cast<void*>(array);
423 }
424 }
425 } else {
426 void* array = editArrayImpl();
427 void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize;
428 _do_destroy(to, amount);
429 ssize_t s = mCount-(where+amount);
430 if (s>0) {
431 const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize;
432 _do_move_backward(to, from, s);
433 }
434 }
435
436 // adjust the number of items...
437 mCount -= amount;
438}
439
440size_t VectorImpl::itemSize() const {
441 return mItemSize;
442}
443
444void VectorImpl::_do_construct(void* storage, size_t num) const
445{
446 if (!(mFlags & HAS_TRIVIAL_CTOR)) {
447 do_construct(storage, num);
448 }
449}
450
451void VectorImpl::_do_destroy(void* storage, size_t num) const
452{
453 if (!(mFlags & HAS_TRIVIAL_DTOR)) {
454 do_destroy(storage, num);
455 }
456}
457
458void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const
459{
460 if (!(mFlags & HAS_TRIVIAL_COPY)) {
461 do_copy(dest, from, num);
462 } else {
463 memcpy(dest, from, num*itemSize());
464 }
465}
466
467void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const {
468 do_splat(dest, item, num);
469}
470
471void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const {
472 do_move_forward(dest, from, num);
473}
474
475void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const {
476 do_move_backward(dest, from, num);
477}
478
479void VectorImpl::reservedVectorImpl1() { }
480void VectorImpl::reservedVectorImpl2() { }
481void VectorImpl::reservedVectorImpl3() { }
482void VectorImpl::reservedVectorImpl4() { }
483void VectorImpl::reservedVectorImpl5() { }
484void VectorImpl::reservedVectorImpl6() { }
485void VectorImpl::reservedVectorImpl7() { }
486void VectorImpl::reservedVectorImpl8() { }
487
488/*****************************************************************************/
489
490SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags)
491 : VectorImpl(itemSize, flags)
492{
493}
494
495SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs)
496: VectorImpl(rhs)
497{
498}
499
500SortedVectorImpl::~SortedVectorImpl()
501{
502}
503
504SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs)
505{
506 return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) );
507}
508
509ssize_t SortedVectorImpl::indexOf(const void* item) const
510{
511 return _indexOrderOf(item);
512}
513
514size_t SortedVectorImpl::orderOf(const void* item) const
515{
516 size_t o;
517 _indexOrderOf(item, &o);
518 return o;
519}
520
521ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const
522{
523 // binary search
524 ssize_t err = NAME_NOT_FOUND;
525 ssize_t l = 0;
526 ssize_t h = size()-1;
527 ssize_t mid;
528 const void* a = arrayImpl();
529 const size_t s = itemSize();
530 while (l <= h) {
531 mid = l + (h - l)/2;
532 const void* const curr = reinterpret_cast<const char *>(a) + (mid*s);
533 const int c = do_compare(curr, item);
534 if (c == 0) {
535 err = l = mid;
536 break;
537 } else if (c < 0) {
538 l = mid + 1;
539 } else {
540 h = mid - 1;
541 }
542 }
543 if (order) *order = l;
544 return err;
545}
546
547ssize_t SortedVectorImpl::add(const void* item)
548{
549 size_t order;
550 ssize_t index = _indexOrderOf(item, &order);
551 if (index < 0) {
552 index = VectorImpl::insertAt(item, order, 1);
553 } else {
554 index = VectorImpl::replaceAt(item, index);
555 }
556 return index;
557}
558
559ssize_t SortedVectorImpl::merge(const VectorImpl& vector)
560{
561 // naive merge...
562 if (!vector.isEmpty()) {
563 const void* buffer = vector.arrayImpl();
564 const size_t is = itemSize();
565 size_t s = vector.size();
566 for (size_t i=0 ; i<s ; i++) {
567 ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is );
568 if (err<0) {
569 return err;
570 }
571 }
572 }
573 return NO_ERROR;
574}
575
576ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector)
577{
578 // we've merging a sorted vector... nice!
579 ssize_t err = NO_ERROR;
580 if (!vector.isEmpty()) {
581 // first take care of the case where the vectors are sorted together
582 if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) {
583 err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0);
584 } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) {
585 err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector));
586 } else {
587 // this could be made a little better
588 err = merge(static_cast<const VectorImpl&>(vector));
589 }
590 }
591 return err;
592}
593
594ssize_t SortedVectorImpl::remove(const void* item)
595{
596 ssize_t i = indexOf(item);
597 if (i>=0) {
598 VectorImpl::removeItemsAt(i, 1);
599 }
600 return i;
601}
602
603void SortedVectorImpl::reservedSortedVectorImpl1() { };
604void SortedVectorImpl::reservedSortedVectorImpl2() { };
605void SortedVectorImpl::reservedSortedVectorImpl3() { };
606void SortedVectorImpl::reservedSortedVectorImpl4() { };
607void SortedVectorImpl::reservedSortedVectorImpl5() { };
608void SortedVectorImpl::reservedSortedVectorImpl6() { };
609void SortedVectorImpl::reservedSortedVectorImpl7() { };
610void SortedVectorImpl::reservedSortedVectorImpl8() { };
611
612
613/*****************************************************************************/
614
615}; // namespace android
616