The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 23 | #include <cutils/log.h> |
| 24 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/Errors.h> |
| 26 | #include <utils/SharedBuffer.h> |
| 27 | #include <utils/VectorImpl.h> |
| 28 | |
| 29 | /*****************************************************************************/ |
| 30 | |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | |
| 36 | const size_t kMinVectorCapacity = 4; |
| 37 | |
| 38 | static inline size_t max(size_t a, size_t b) { |
| 39 | return a>b ? a : b; |
| 40 | } |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | VectorImpl::VectorImpl(size_t itemSize, uint32_t flags) |
| 45 | : mStorage(0), mCount(0), mFlags(flags), mItemSize(itemSize) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | VectorImpl::VectorImpl(const VectorImpl& rhs) |
| 50 | : mStorage(rhs.mStorage), mCount(rhs.mCount), |
| 51 | mFlags(rhs.mFlags), mItemSize(rhs.mItemSize) |
| 52 | { |
| 53 | if (mStorage) { |
Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 54 | SharedBuffer::bufferFromData(mStorage)->acquire(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | VectorImpl::~VectorImpl() |
| 59 | { |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 60 | ALOGW_IF(mCount, |
| 61 | "[%p] subclasses of VectorImpl must call finish_vector()" |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 67 | VectorImpl& VectorImpl::operator = (const VectorImpl& rhs) |
| 68 | { |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 69 | LOG_ALWAYS_FATAL_IF(mItemSize != rhs.mItemSize, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 76 | SharedBuffer::bufferFromData(mStorage)->acquire(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | } else { |
| 78 | mStorage = 0; |
| 79 | mCount = 0; |
| 80 | } |
| 81 | } |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | void* VectorImpl::editArrayImpl() |
| 86 | { |
| 87 | if (mStorage) { |
Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 88 | SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage)->attemptEdit(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 101 | size_t VectorImpl::capacity() const |
| 102 | { |
| 103 | if (mStorage) { |
Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 104 | return SharedBuffer::bufferFromData(mStorage)->size() / mItemSize; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | ssize_t VectorImpl::insertVectorAt(const VectorImpl& vector, size_t index) |
| 110 | { |
Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 111 | return insertArrayAt(vector.arrayImpl(), index, vector.size()); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | ssize_t VectorImpl::appendVector(const VectorImpl& vector) |
| 115 | { |
| 116 | return insertVectorAt(vector, size()); |
| 117 | } |
| 118 | |
Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 119 | ssize_t VectorImpl::insertArrayAt(const void* array, size_t index, size_t length) |
| 120 | { |
| 121 | if (index > size()) |
| 122 | return BAD_INDEX; |
| 123 | void* where = _grow(index, length); |
| 124 | if (where) { |
| 125 | _do_copy(where, array, length); |
| 126 | } |
| 127 | return where ? index : (ssize_t)NO_MEMORY; |
| 128 | } |
| 129 | |
| 130 | ssize_t VectorImpl::appendArray(const void* array, size_t length) |
| 131 | { |
| 132 | return insertArrayAt(array, size(), length); |
| 133 | } |
| 134 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | ssize_t VectorImpl::insertAt(size_t index, size_t numItems) |
| 136 | { |
| 137 | return insertAt(0, index, numItems); |
| 138 | } |
| 139 | |
| 140 | ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems) |
| 141 | { |
| 142 | if (index > size()) |
| 143 | return BAD_INDEX; |
| 144 | void* where = _grow(index, numItems); |
| 145 | if (where) { |
| 146 | if (item) { |
| 147 | _do_splat(where, item, numItems); |
| 148 | } else { |
| 149 | _do_construct(where, numItems); |
| 150 | } |
| 151 | } |
| 152 | return where ? index : (ssize_t)NO_MEMORY; |
| 153 | } |
| 154 | |
| 155 | static int sortProxy(const void* lhs, const void* rhs, void* func) |
| 156 | { |
| 157 | return (*(VectorImpl::compar_t)func)(lhs, rhs); |
| 158 | } |
| 159 | |
| 160 | status_t VectorImpl::sort(VectorImpl::compar_t cmp) |
| 161 | { |
| 162 | return sort(sortProxy, (void*)cmp); |
| 163 | } |
| 164 | |
| 165 | status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state) |
| 166 | { |
| 167 | // the sort must be stable. we're using insertion sort which |
| 168 | // is well suited for small and already sorted arrays |
| 169 | // for big arrays, it could be better to use mergesort |
| 170 | const ssize_t count = size(); |
| 171 | if (count > 1) { |
| 172 | void* array = const_cast<void*>(arrayImpl()); |
| 173 | void* temp = 0; |
| 174 | ssize_t i = 1; |
| 175 | while (i < count) { |
| 176 | void* item = reinterpret_cast<char*>(array) + mItemSize*(i); |
| 177 | void* curr = reinterpret_cast<char*>(array) + mItemSize*(i-1); |
| 178 | if (cmp(curr, item, state) > 0) { |
| 179 | |
| 180 | if (!temp) { |
| 181 | // we're going to have to modify the array... |
| 182 | array = editArrayImpl(); |
| 183 | if (!array) return NO_MEMORY; |
| 184 | temp = malloc(mItemSize); |
| 185 | if (!temp) return NO_MEMORY; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | item = reinterpret_cast<char*>(array) + mItemSize*(i); |
| 187 | curr = reinterpret_cast<char*>(array) + mItemSize*(i-1); |
Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 188 | } else { |
| 189 | _do_destroy(temp, 1); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | _do_copy(temp, item, 1); |
| 193 | |
| 194 | ssize_t j = i-1; |
| 195 | void* next = reinterpret_cast<char*>(array) + mItemSize*(i); |
| 196 | do { |
Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 197 | _do_destroy(next, 1); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | _do_copy(next, curr, 1); |
| 199 | next = curr; |
| 200 | --j; |
Nick Kralevich | c76698f | 2015-08-28 06:40:23 -0700 | [diff] [blame^] | 201 | curr = NULL; |
| 202 | if (j >= 0) { |
| 203 | curr = reinterpret_cast<char*>(array) + mItemSize*(j); |
| 204 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | } while (j>=0 && (cmp(curr, temp, state) > 0)); |
| 206 | |
Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 207 | _do_destroy(next, 1); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | _do_copy(next, temp, 1); |
| 209 | } |
| 210 | i++; |
| 211 | } |
| 212 | |
| 213 | if (temp) { |
| 214 | _do_destroy(temp, 1); |
| 215 | free(temp); |
| 216 | } |
| 217 | } |
| 218 | return NO_ERROR; |
| 219 | } |
| 220 | |
| 221 | void VectorImpl::pop() |
| 222 | { |
| 223 | if (size()) |
| 224 | removeItemsAt(size()-1, 1); |
| 225 | } |
| 226 | |
| 227 | void VectorImpl::push() |
| 228 | { |
| 229 | push(0); |
| 230 | } |
| 231 | |
| 232 | void VectorImpl::push(const void* item) |
| 233 | { |
| 234 | insertAt(item, size()); |
| 235 | } |
| 236 | |
| 237 | ssize_t VectorImpl::add() |
| 238 | { |
| 239 | return add(0); |
| 240 | } |
| 241 | |
Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 242 | ssize_t VectorImpl::add(const void* item) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | { |
Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 244 | return insertAt(item, size()); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | ssize_t VectorImpl::replaceAt(size_t index) |
| 248 | { |
| 249 | return replaceAt(0, index); |
| 250 | } |
| 251 | |
| 252 | ssize_t VectorImpl::replaceAt(const void* prototype, size_t index) |
| 253 | { |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 254 | ALOG_ASSERT(index<size(), |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | "[%p] replace: index=%d, size=%d", this, (int)index, (int)size()); |
| 256 | |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 257 | if (index >= size()) { |
| 258 | return BAD_INDEX; |
| 259 | } |
| 260 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | void* item = editItemLocation(index); |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 262 | if (item != prototype) { |
| 263 | if (item == 0) |
| 264 | return NO_MEMORY; |
| 265 | _do_destroy(item, 1); |
| 266 | if (prototype == 0) { |
| 267 | _do_construct(item, 1); |
| 268 | } else { |
| 269 | _do_copy(item, prototype, 1); |
| 270 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | } |
| 272 | return ssize_t(index); |
| 273 | } |
| 274 | |
| 275 | ssize_t VectorImpl::removeItemsAt(size_t index, size_t count) |
| 276 | { |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 277 | ALOG_ASSERT((index+count)<=size(), |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | "[%p] remove: index=%d, count=%d, size=%d", |
| 279 | this, (int)index, (int)count, (int)size()); |
| 280 | |
| 281 | if ((index+count) > size()) |
| 282 | return BAD_VALUE; |
| 283 | _shrink(index, count); |
| 284 | return index; |
| 285 | } |
| 286 | |
| 287 | void VectorImpl::finish_vector() |
| 288 | { |
| 289 | release_storage(); |
| 290 | mStorage = 0; |
| 291 | mCount = 0; |
| 292 | } |
| 293 | |
| 294 | void VectorImpl::clear() |
| 295 | { |
| 296 | _shrink(0, mCount); |
| 297 | } |
| 298 | |
| 299 | void* VectorImpl::editItemLocation(size_t index) |
| 300 | { |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 301 | ALOG_ASSERT(index<capacity(), |
Mathias Agopian | 266a7d6 | 2011-07-11 16:26:18 -0700 | [diff] [blame] | 302 | "[%p] editItemLocation: index=%d, capacity=%d, count=%d", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | this, (int)index, (int)capacity(), (int)mCount); |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 304 | |
| 305 | if (index < capacity()) { |
| 306 | void* buffer = editArrayImpl(); |
| 307 | if (buffer) { |
| 308 | return reinterpret_cast<char*>(buffer) + index*mItemSize; |
| 309 | } |
| 310 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | const void* VectorImpl::itemLocation(size_t index) const |
| 315 | { |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 316 | ALOG_ASSERT(index<capacity(), |
Mathias Agopian | 266a7d6 | 2011-07-11 16:26:18 -0700 | [diff] [blame] | 317 | "[%p] itemLocation: index=%d, capacity=%d, count=%d", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | this, (int)index, (int)capacity(), (int)mCount); |
| 319 | |
Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 320 | if (index < capacity()) { |
| 321 | const void* buffer = arrayImpl(); |
| 322 | if (buffer) { |
| 323 | return reinterpret_cast<const char*>(buffer) + index*mItemSize; |
| 324 | } |
| 325 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | ssize_t VectorImpl::setCapacity(size_t new_capacity) |
| 330 | { |
| 331 | size_t current_capacity = capacity(); |
| 332 | ssize_t amount = new_capacity - size(); |
| 333 | if (amount <= 0) { |
| 334 | // we can't reduce the capacity |
| 335 | return current_capacity; |
| 336 | } |
| 337 | SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); |
| 338 | if (sb) { |
| 339 | void* array = sb->data(); |
| 340 | _do_copy(array, mStorage, size()); |
| 341 | release_storage(); |
| 342 | mStorage = const_cast<void*>(array); |
| 343 | } else { |
| 344 | return NO_MEMORY; |
| 345 | } |
| 346 | return new_capacity; |
| 347 | } |
| 348 | |
Jesse Hall | b73559d | 2013-03-11 10:16:48 -0700 | [diff] [blame] | 349 | ssize_t VectorImpl::resize(size_t size) { |
| 350 | ssize_t result = NO_ERROR; |
| 351 | if (size > mCount) { |
| 352 | result = insertAt(mCount, size - mCount); |
| 353 | } else if (size < mCount) { |
| 354 | result = removeItemsAt(size, mCount - size); |
| 355 | } |
| 356 | return result < 0 ? result : size; |
| 357 | } |
| 358 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | void VectorImpl::release_storage() |
| 360 | { |
| 361 | if (mStorage) { |
Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 362 | const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | if (sb->release(SharedBuffer::eKeepStorage) == 1) { |
| 364 | _do_destroy(mStorage, mCount); |
| 365 | SharedBuffer::dealloc(sb); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void* VectorImpl::_grow(size_t where, size_t amount) |
| 371 | { |
Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 372 | // ALOGV("_grow(this=%p, where=%d, amount=%d) count=%d, capacity=%d", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | // this, (int)where, (int)amount, (int)mCount, (int)capacity()); |
| 374 | |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 375 | ALOG_ASSERT(where <= mCount, |
Jeff Brown | aa5daed | 2011-07-13 22:22:02 -0700 | [diff] [blame] | 376 | "[%p] _grow: where=%d, amount=%d, count=%d", |
| 377 | this, (int)where, (int)amount, (int)mCount); // caller already checked |
| 378 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | const size_t new_size = mCount + amount; |
| 380 | if (capacity() < new_size) { |
| 381 | const size_t new_capacity = max(kMinVectorCapacity, ((new_size*3)+1)/2); |
Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 382 | // ALOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | if ((mStorage) && |
| 384 | (mCount==where) && |
| 385 | (mFlags & HAS_TRIVIAL_COPY) && |
| 386 | (mFlags & HAS_TRIVIAL_DTOR)) |
| 387 | { |
Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 388 | const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); |
Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 390 | if (sb) { |
| 391 | mStorage = sb->data(); |
| 392 | } else { |
| 393 | return NULL; |
| 394 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | } else { |
| 396 | SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); |
| 397 | if (sb) { |
| 398 | void* array = sb->data(); |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 399 | if (where != 0) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | _do_copy(array, mStorage, where); |
| 401 | } |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 402 | if (where != mCount) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize; |
| 404 | void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; |
| 405 | _do_copy(dest, from, mCount-where); |
| 406 | } |
| 407 | release_storage(); |
| 408 | mStorage = const_cast<void*>(array); |
Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 409 | } else { |
| 410 | return NULL; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } else { |
Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 414 | void* array = editArrayImpl(); |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 415 | if (where != mCount) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize; |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 417 | void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; |
| 418 | _do_move_forward(to, from, mCount - where); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | } |
| 420 | } |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 421 | mCount = new_size; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | void* free_space = const_cast<void*>(itemLocation(where)); |
| 423 | return free_space; |
| 424 | } |
| 425 | |
| 426 | void VectorImpl::_shrink(size_t where, size_t amount) |
| 427 | { |
| 428 | if (!mStorage) |
| 429 | return; |
| 430 | |
Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 431 | // ALOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | // this, (int)where, (int)amount, (int)mCount, (int)capacity()); |
| 433 | |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 434 | ALOG_ASSERT(where + amount <= mCount, |
Jeff Brown | aa5daed | 2011-07-13 22:22:02 -0700 | [diff] [blame] | 435 | "[%p] _shrink: where=%d, amount=%d, count=%d", |
| 436 | this, (int)where, (int)amount, (int)mCount); // caller already checked |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | |
| 438 | const size_t new_size = mCount - amount; |
| 439 | if (new_size*3 < capacity()) { |
| 440 | const size_t new_capacity = max(kMinVectorCapacity, new_size*2); |
Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 441 | // ALOGV("shrink vector %p, new_capacity=%d", this, (int)new_capacity); |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 442 | if ((where == new_size) && |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | (mFlags & HAS_TRIVIAL_COPY) && |
| 444 | (mFlags & HAS_TRIVIAL_DTOR)) |
| 445 | { |
Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 446 | const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); |
Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 448 | if (sb) { |
| 449 | mStorage = sb->data(); |
| 450 | } else { |
| 451 | return; |
| 452 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | } else { |
| 454 | SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); |
| 455 | if (sb) { |
| 456 | void* array = sb->data(); |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 457 | if (where != 0) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | _do_copy(array, mStorage, where); |
| 459 | } |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 460 | if (where != new_size) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize; |
| 462 | void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize; |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 463 | _do_copy(dest, from, new_size - where); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | } |
| 465 | release_storage(); |
| 466 | mStorage = const_cast<void*>(array); |
Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 467 | } else{ |
| 468 | return; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | } else { |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 472 | void* array = editArrayImpl(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 | void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize; |
| 474 | _do_destroy(to, amount); |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 475 | if (where != new_size) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 477 | _do_move_backward(to, from, new_size - where); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | } |
| 479 | } |
Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 480 | mCount = new_size; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | size_t VectorImpl::itemSize() const { |
| 484 | return mItemSize; |
| 485 | } |
| 486 | |
| 487 | void VectorImpl::_do_construct(void* storage, size_t num) const |
| 488 | { |
| 489 | if (!(mFlags & HAS_TRIVIAL_CTOR)) { |
| 490 | do_construct(storage, num); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | void VectorImpl::_do_destroy(void* storage, size_t num) const |
| 495 | { |
| 496 | if (!(mFlags & HAS_TRIVIAL_DTOR)) { |
| 497 | do_destroy(storage, num); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const |
| 502 | { |
| 503 | if (!(mFlags & HAS_TRIVIAL_COPY)) { |
| 504 | do_copy(dest, from, num); |
| 505 | } else { |
| 506 | memcpy(dest, from, num*itemSize()); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const { |
| 511 | do_splat(dest, item, num); |
| 512 | } |
| 513 | |
| 514 | void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const { |
| 515 | do_move_forward(dest, from, num); |
| 516 | } |
| 517 | |
| 518 | void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const { |
| 519 | do_move_backward(dest, from, num); |
| 520 | } |
| 521 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | /*****************************************************************************/ |
| 523 | |
| 524 | SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags) |
| 525 | : VectorImpl(itemSize, flags) |
| 526 | { |
| 527 | } |
| 528 | |
| 529 | SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs) |
| 530 | : VectorImpl(rhs) |
| 531 | { |
| 532 | } |
| 533 | |
| 534 | SortedVectorImpl::~SortedVectorImpl() |
| 535 | { |
| 536 | } |
| 537 | |
| 538 | SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs) |
| 539 | { |
| 540 | return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) ); |
| 541 | } |
| 542 | |
| 543 | ssize_t SortedVectorImpl::indexOf(const void* item) const |
| 544 | { |
| 545 | return _indexOrderOf(item); |
| 546 | } |
| 547 | |
| 548 | size_t SortedVectorImpl::orderOf(const void* item) const |
| 549 | { |
| 550 | size_t o; |
| 551 | _indexOrderOf(item, &o); |
| 552 | return o; |
| 553 | } |
| 554 | |
| 555 | ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const |
| 556 | { |
Nick Kralevich | 1f28698 | 2015-08-22 14:27:03 -0700 | [diff] [blame] | 557 | if (order) *order = 0; |
| 558 | if (isEmpty()) { |
| 559 | return NAME_NOT_FOUND; |
| 560 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | // binary search |
| 562 | ssize_t err = NAME_NOT_FOUND; |
| 563 | ssize_t l = 0; |
| 564 | ssize_t h = size()-1; |
| 565 | ssize_t mid; |
| 566 | const void* a = arrayImpl(); |
| 567 | const size_t s = itemSize(); |
| 568 | while (l <= h) { |
| 569 | mid = l + (h - l)/2; |
| 570 | const void* const curr = reinterpret_cast<const char *>(a) + (mid*s); |
| 571 | const int c = do_compare(curr, item); |
| 572 | if (c == 0) { |
| 573 | err = l = mid; |
| 574 | break; |
| 575 | } else if (c < 0) { |
| 576 | l = mid + 1; |
| 577 | } else { |
| 578 | h = mid - 1; |
| 579 | } |
| 580 | } |
| 581 | if (order) *order = l; |
| 582 | return err; |
| 583 | } |
| 584 | |
| 585 | ssize_t SortedVectorImpl::add(const void* item) |
| 586 | { |
| 587 | size_t order; |
| 588 | ssize_t index = _indexOrderOf(item, &order); |
| 589 | if (index < 0) { |
| 590 | index = VectorImpl::insertAt(item, order, 1); |
| 591 | } else { |
| 592 | index = VectorImpl::replaceAt(item, index); |
| 593 | } |
| 594 | return index; |
| 595 | } |
| 596 | |
| 597 | ssize_t SortedVectorImpl::merge(const VectorImpl& vector) |
| 598 | { |
| 599 | // naive merge... |
| 600 | if (!vector.isEmpty()) { |
| 601 | const void* buffer = vector.arrayImpl(); |
| 602 | const size_t is = itemSize(); |
| 603 | size_t s = vector.size(); |
| 604 | for (size_t i=0 ; i<s ; i++) { |
| 605 | ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is ); |
| 606 | if (err<0) { |
| 607 | return err; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | return NO_ERROR; |
| 612 | } |
| 613 | |
| 614 | ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector) |
| 615 | { |
| 616 | // we've merging a sorted vector... nice! |
| 617 | ssize_t err = NO_ERROR; |
| 618 | if (!vector.isEmpty()) { |
| 619 | // first take care of the case where the vectors are sorted together |
| 620 | if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) { |
| 621 | err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0); |
| 622 | } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) { |
| 623 | err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector)); |
| 624 | } else { |
| 625 | // this could be made a little better |
| 626 | err = merge(static_cast<const VectorImpl&>(vector)); |
| 627 | } |
| 628 | } |
| 629 | return err; |
| 630 | } |
| 631 | |
| 632 | ssize_t SortedVectorImpl::remove(const void* item) |
| 633 | { |
| 634 | ssize_t i = indexOf(item); |
| 635 | if (i>=0) { |
| 636 | VectorImpl::removeItemsAt(i, 1); |
| 637 | } |
| 638 | return i; |
| 639 | } |
| 640 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | /*****************************************************************************/ |
| 642 | |
| 643 | }; // namespace android |
| 644 | |