The Android Open Source Project | 9066cfe | 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 | |
| 23 | #include <utils/Log.h> |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/SharedBuffer.h> |
| 26 | #include <utils/VectorImpl.h> |
| 27 | |
| 28 | /*****************************************************************************/ |
| 29 | |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | const size_t kMinVectorCapacity = 4; |
| 36 | |
| 37 | static inline size_t max(size_t a, size_t b) { |
| 38 | return a>b ? a : b; |
| 39 | } |
| 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | |
| 43 | VectorImpl::VectorImpl(size_t itemSize, uint32_t flags) |
| 44 | : mStorage(0), mCount(0), mFlags(flags), mItemSize(itemSize) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | VectorImpl::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 | |
| 57 | VectorImpl::~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 | |
| 67 | VectorImpl& 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 | |
| 85 | void* 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 | |
| 101 | size_t VectorImpl::capacity() const |
| 102 | { |
| 103 | if (mStorage) { |
| 104 | return SharedBuffer::sharedBuffer(mStorage)->size() / mItemSize; |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | ssize_t VectorImpl::insertVectorAt(const VectorImpl& vector, size_t index) |
| 110 | { |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 111 | return insertArrayAt(vector.arrayImpl(), index, vector.size()); |
The Android Open Source Project | 9066cfe | 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 | 5c225b1 | 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 | 9066cfe | 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 | 9066cfe | 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 | d741452 | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 188 | } else { |
| 189 | _do_destroy(temp, 1); |
The Android Open Source Project | 9066cfe | 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 | d741452 | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 197 | _do_destroy(next, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | _do_copy(next, curr, 1); |
| 199 | next = curr; |
| 200 | --j; |
| 201 | curr = reinterpret_cast<char*>(array) + mItemSize*(j); |
| 202 | } while (j>=0 && (cmp(curr, temp, state) > 0)); |
| 203 | |
Mathias Agopian | d741452 | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 204 | _do_destroy(next, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | _do_copy(next, temp, 1); |
| 206 | } |
| 207 | i++; |
| 208 | } |
| 209 | |
| 210 | if (temp) { |
| 211 | _do_destroy(temp, 1); |
| 212 | free(temp); |
| 213 | } |
| 214 | } |
| 215 | return NO_ERROR; |
| 216 | } |
| 217 | |
| 218 | void VectorImpl::pop() |
| 219 | { |
| 220 | if (size()) |
| 221 | removeItemsAt(size()-1, 1); |
| 222 | } |
| 223 | |
| 224 | void VectorImpl::push() |
| 225 | { |
| 226 | push(0); |
| 227 | } |
| 228 | |
| 229 | void VectorImpl::push(const void* item) |
| 230 | { |
| 231 | insertAt(item, size()); |
| 232 | } |
| 233 | |
| 234 | ssize_t VectorImpl::add() |
| 235 | { |
| 236 | return add(0); |
| 237 | } |
| 238 | |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 239 | ssize_t VectorImpl::add(const void* item) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | { |
Jeff Brown | 5c225b1 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 241 | return insertAt(item, size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | ssize_t VectorImpl::replaceAt(size_t index) |
| 245 | { |
| 246 | return replaceAt(0, index); |
| 247 | } |
| 248 | |
| 249 | ssize_t VectorImpl::replaceAt(const void* prototype, size_t index) |
| 250 | { |
| 251 | LOG_ASSERT(index<size(), |
| 252 | "[%p] replace: index=%d, size=%d", this, (int)index, (int)size()); |
| 253 | |
| 254 | void* item = editItemLocation(index); |
| 255 | if (item == 0) |
| 256 | return NO_MEMORY; |
| 257 | _do_destroy(item, 1); |
| 258 | if (prototype == 0) { |
| 259 | _do_construct(item, 1); |
| 260 | } else { |
| 261 | _do_copy(item, prototype, 1); |
| 262 | } |
| 263 | return ssize_t(index); |
| 264 | } |
| 265 | |
| 266 | ssize_t VectorImpl::removeItemsAt(size_t index, size_t count) |
| 267 | { |
| 268 | LOG_ASSERT((index+count)<=size(), |
| 269 | "[%p] remove: index=%d, count=%d, size=%d", |
| 270 | this, (int)index, (int)count, (int)size()); |
| 271 | |
| 272 | if ((index+count) > size()) |
| 273 | return BAD_VALUE; |
| 274 | _shrink(index, count); |
| 275 | return index; |
| 276 | } |
| 277 | |
| 278 | void VectorImpl::finish_vector() |
| 279 | { |
| 280 | release_storage(); |
| 281 | mStorage = 0; |
| 282 | mCount = 0; |
| 283 | } |
| 284 | |
| 285 | void VectorImpl::clear() |
| 286 | { |
| 287 | _shrink(0, mCount); |
| 288 | } |
| 289 | |
| 290 | void* VectorImpl::editItemLocation(size_t index) |
| 291 | { |
| 292 | LOG_ASSERT(index<capacity(), |
Mathias Agopian | cf7e3a5 | 2011-07-11 16:26:18 -0700 | [diff] [blame] | 293 | "[%p] editItemLocation: index=%d, capacity=%d, count=%d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | this, (int)index, (int)capacity(), (int)mCount); |
| 295 | |
| 296 | void* buffer = editArrayImpl(); |
| 297 | if (buffer) |
| 298 | return reinterpret_cast<char*>(buffer) + index*mItemSize; |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | const void* VectorImpl::itemLocation(size_t index) const |
| 303 | { |
| 304 | LOG_ASSERT(index<capacity(), |
Mathias Agopian | cf7e3a5 | 2011-07-11 16:26:18 -0700 | [diff] [blame] | 305 | "[%p] itemLocation: index=%d, capacity=%d, count=%d", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | this, (int)index, (int)capacity(), (int)mCount); |
| 307 | |
| 308 | const void* buffer = arrayImpl(); |
| 309 | if (buffer) |
| 310 | return reinterpret_cast<const char*>(buffer) + index*mItemSize; |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | ssize_t VectorImpl::setCapacity(size_t new_capacity) |
| 315 | { |
| 316 | size_t current_capacity = capacity(); |
| 317 | ssize_t amount = new_capacity - size(); |
| 318 | if (amount <= 0) { |
| 319 | // we can't reduce the capacity |
| 320 | return current_capacity; |
| 321 | } |
| 322 | SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); |
| 323 | if (sb) { |
| 324 | void* array = sb->data(); |
| 325 | _do_copy(array, mStorage, size()); |
| 326 | release_storage(); |
| 327 | mStorage = const_cast<void*>(array); |
| 328 | } else { |
| 329 | return NO_MEMORY; |
| 330 | } |
| 331 | return new_capacity; |
| 332 | } |
| 333 | |
| 334 | void VectorImpl::release_storage() |
| 335 | { |
| 336 | if (mStorage) { |
| 337 | const SharedBuffer* sb = SharedBuffer::sharedBuffer(mStorage); |
| 338 | if (sb->release(SharedBuffer::eKeepStorage) == 1) { |
| 339 | _do_destroy(mStorage, mCount); |
| 340 | SharedBuffer::dealloc(sb); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void* VectorImpl::_grow(size_t where, size_t amount) |
| 346 | { |
| 347 | // LOGV("_grow(this=%p, where=%d, amount=%d) count=%d, capacity=%d", |
| 348 | // this, (int)where, (int)amount, (int)mCount, (int)capacity()); |
| 349 | |
Jeff Brown | 11bf79d6 | 2011-07-13 22:22:02 -0700 | [diff] [blame^] | 350 | LOG_ASSERT(where <= mCount, |
| 351 | "[%p] _grow: where=%d, amount=%d, count=%d", |
| 352 | this, (int)where, (int)amount, (int)mCount); // caller already checked |
| 353 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | const size_t new_size = mCount + amount; |
| 355 | if (capacity() < new_size) { |
| 356 | const size_t new_capacity = max(kMinVectorCapacity, ((new_size*3)+1)/2); |
| 357 | // LOGV("grow vector %p, new_capacity=%d", this, (int)new_capacity); |
| 358 | if ((mStorage) && |
| 359 | (mCount==where) && |
| 360 | (mFlags & HAS_TRIVIAL_COPY) && |
| 361 | (mFlags & HAS_TRIVIAL_DTOR)) |
| 362 | { |
| 363 | const SharedBuffer* cur_sb = SharedBuffer::sharedBuffer(mStorage); |
| 364 | SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); |
| 365 | mStorage = sb->data(); |
| 366 | } else { |
| 367 | SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); |
| 368 | if (sb) { |
| 369 | void* array = sb->data(); |
| 370 | if (where>0) { |
| 371 | _do_copy(array, mStorage, where); |
| 372 | } |
| 373 | if (mCount>where) { |
| 374 | const void* from = reinterpret_cast<const uint8_t *>(mStorage) + where*mItemSize; |
| 375 | void* dest = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; |
| 376 | _do_copy(dest, from, mCount-where); |
| 377 | } |
| 378 | release_storage(); |
| 379 | mStorage = const_cast<void*>(array); |
| 380 | } |
| 381 | } |
| 382 | } else { |
| 383 | ssize_t s = mCount-where; |
| 384 | if (s>0) { |
| 385 | void* array = editArrayImpl(); |
| 386 | void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; |
| 387 | const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize; |
| 388 | _do_move_forward(to, from, s); |
| 389 | } |
| 390 | } |
| 391 | mCount += amount; |
| 392 | void* free_space = const_cast<void*>(itemLocation(where)); |
| 393 | return free_space; |
| 394 | } |
| 395 | |
| 396 | void VectorImpl::_shrink(size_t where, size_t amount) |
| 397 | { |
| 398 | if (!mStorage) |
| 399 | return; |
| 400 | |
| 401 | // LOGV("_shrink(this=%p, where=%d, amount=%d) count=%d, capacity=%d", |
| 402 | // this, (int)where, (int)amount, (int)mCount, (int)capacity()); |
| 403 | |
Jeff Brown | 11bf79d6 | 2011-07-13 22:22:02 -0700 | [diff] [blame^] | 404 | LOG_ASSERT(where + amount <= mCount, |
| 405 | "[%p] _shrink: where=%d, amount=%d, count=%d", |
| 406 | this, (int)where, (int)amount, (int)mCount); // caller already checked |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | |
| 408 | const size_t new_size = mCount - amount; |
| 409 | if (new_size*3 < capacity()) { |
| 410 | const size_t new_capacity = max(kMinVectorCapacity, new_size*2); |
| 411 | // LOGV("shrink vector %p, new_capacity=%d", this, (int)new_capacity); |
| 412 | if ((where == mCount-amount) && |
| 413 | (mFlags & HAS_TRIVIAL_COPY) && |
| 414 | (mFlags & HAS_TRIVIAL_DTOR)) |
| 415 | { |
| 416 | const SharedBuffer* cur_sb = SharedBuffer::sharedBuffer(mStorage); |
| 417 | SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | mStorage = sb->data(); |
| 419 | } else { |
| 420 | SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); |
| 421 | if (sb) { |
| 422 | void* array = sb->data(); |
| 423 | if (where>0) { |
| 424 | _do_copy(array, mStorage, where); |
| 425 | } |
| 426 | if (mCount > where+amount) { |
| 427 | const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize; |
| 428 | void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize; |
| 429 | _do_copy(dest, from, mCount-(where+amount)); |
| 430 | } |
| 431 | release_storage(); |
| 432 | mStorage = const_cast<void*>(array); |
| 433 | } |
| 434 | } |
| 435 | } else { |
| 436 | void* array = editArrayImpl(); |
| 437 | void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize; |
| 438 | _do_destroy(to, amount); |
| 439 | ssize_t s = mCount-(where+amount); |
| 440 | if (s>0) { |
| 441 | const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; |
| 442 | _do_move_backward(to, from, s); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // adjust the number of items... |
| 447 | mCount -= amount; |
| 448 | } |
| 449 | |
| 450 | size_t VectorImpl::itemSize() const { |
| 451 | return mItemSize; |
| 452 | } |
| 453 | |
| 454 | void VectorImpl::_do_construct(void* storage, size_t num) const |
| 455 | { |
| 456 | if (!(mFlags & HAS_TRIVIAL_CTOR)) { |
| 457 | do_construct(storage, num); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | void VectorImpl::_do_destroy(void* storage, size_t num) const |
| 462 | { |
| 463 | if (!(mFlags & HAS_TRIVIAL_DTOR)) { |
| 464 | do_destroy(storage, num); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void VectorImpl::_do_copy(void* dest, const void* from, size_t num) const |
| 469 | { |
| 470 | if (!(mFlags & HAS_TRIVIAL_COPY)) { |
| 471 | do_copy(dest, from, num); |
| 472 | } else { |
| 473 | memcpy(dest, from, num*itemSize()); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const { |
| 478 | do_splat(dest, item, num); |
| 479 | } |
| 480 | |
| 481 | void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const { |
| 482 | do_move_forward(dest, from, num); |
| 483 | } |
| 484 | |
| 485 | void VectorImpl::_do_move_backward(void* dest, const void* from, size_t num) const { |
| 486 | do_move_backward(dest, from, num); |
| 487 | } |
| 488 | |
| 489 | void VectorImpl::reservedVectorImpl1() { } |
| 490 | void VectorImpl::reservedVectorImpl2() { } |
| 491 | void VectorImpl::reservedVectorImpl3() { } |
| 492 | void VectorImpl::reservedVectorImpl4() { } |
| 493 | void VectorImpl::reservedVectorImpl5() { } |
| 494 | void VectorImpl::reservedVectorImpl6() { } |
| 495 | void VectorImpl::reservedVectorImpl7() { } |
| 496 | void VectorImpl::reservedVectorImpl8() { } |
| 497 | |
| 498 | /*****************************************************************************/ |
| 499 | |
| 500 | SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags) |
| 501 | : VectorImpl(itemSize, flags) |
| 502 | { |
| 503 | } |
| 504 | |
| 505 | SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs) |
| 506 | : VectorImpl(rhs) |
| 507 | { |
| 508 | } |
| 509 | |
| 510 | SortedVectorImpl::~SortedVectorImpl() |
| 511 | { |
| 512 | } |
| 513 | |
| 514 | SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs) |
| 515 | { |
| 516 | return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) ); |
| 517 | } |
| 518 | |
| 519 | ssize_t SortedVectorImpl::indexOf(const void* item) const |
| 520 | { |
| 521 | return _indexOrderOf(item); |
| 522 | } |
| 523 | |
| 524 | size_t SortedVectorImpl::orderOf(const void* item) const |
| 525 | { |
| 526 | size_t o; |
| 527 | _indexOrderOf(item, &o); |
| 528 | return o; |
| 529 | } |
| 530 | |
| 531 | ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const |
| 532 | { |
| 533 | // binary search |
| 534 | ssize_t err = NAME_NOT_FOUND; |
| 535 | ssize_t l = 0; |
| 536 | ssize_t h = size()-1; |
| 537 | ssize_t mid; |
| 538 | const void* a = arrayImpl(); |
| 539 | const size_t s = itemSize(); |
| 540 | while (l <= h) { |
| 541 | mid = l + (h - l)/2; |
| 542 | const void* const curr = reinterpret_cast<const char *>(a) + (mid*s); |
| 543 | const int c = do_compare(curr, item); |
| 544 | if (c == 0) { |
| 545 | err = l = mid; |
| 546 | break; |
| 547 | } else if (c < 0) { |
| 548 | l = mid + 1; |
| 549 | } else { |
| 550 | h = mid - 1; |
| 551 | } |
| 552 | } |
| 553 | if (order) *order = l; |
| 554 | return err; |
| 555 | } |
| 556 | |
| 557 | ssize_t SortedVectorImpl::add(const void* item) |
| 558 | { |
| 559 | size_t order; |
| 560 | ssize_t index = _indexOrderOf(item, &order); |
| 561 | if (index < 0) { |
| 562 | index = VectorImpl::insertAt(item, order, 1); |
| 563 | } else { |
| 564 | index = VectorImpl::replaceAt(item, index); |
| 565 | } |
| 566 | return index; |
| 567 | } |
| 568 | |
| 569 | ssize_t SortedVectorImpl::merge(const VectorImpl& vector) |
| 570 | { |
| 571 | // naive merge... |
| 572 | if (!vector.isEmpty()) { |
| 573 | const void* buffer = vector.arrayImpl(); |
| 574 | const size_t is = itemSize(); |
| 575 | size_t s = vector.size(); |
| 576 | for (size_t i=0 ; i<s ; i++) { |
| 577 | ssize_t err = add( reinterpret_cast<const char*>(buffer) + i*is ); |
| 578 | if (err<0) { |
| 579 | return err; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | return NO_ERROR; |
| 584 | } |
| 585 | |
| 586 | ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector) |
| 587 | { |
| 588 | // we've merging a sorted vector... nice! |
| 589 | ssize_t err = NO_ERROR; |
| 590 | if (!vector.isEmpty()) { |
| 591 | // first take care of the case where the vectors are sorted together |
| 592 | if (do_compare(vector.itemLocation(vector.size()-1), arrayImpl()) <= 0) { |
| 593 | err = VectorImpl::insertVectorAt(static_cast<const VectorImpl&>(vector), 0); |
| 594 | } else if (do_compare(vector.arrayImpl(), itemLocation(size()-1)) >= 0) { |
| 595 | err = VectorImpl::appendVector(static_cast<const VectorImpl&>(vector)); |
| 596 | } else { |
| 597 | // this could be made a little better |
| 598 | err = merge(static_cast<const VectorImpl&>(vector)); |
| 599 | } |
| 600 | } |
| 601 | return err; |
| 602 | } |
| 603 | |
| 604 | ssize_t SortedVectorImpl::remove(const void* item) |
| 605 | { |
| 606 | ssize_t i = indexOf(item); |
| 607 | if (i>=0) { |
| 608 | VectorImpl::removeItemsAt(i, 1); |
| 609 | } |
| 610 | return i; |
| 611 | } |
| 612 | |
| 613 | void SortedVectorImpl::reservedSortedVectorImpl1() { }; |
| 614 | void SortedVectorImpl::reservedSortedVectorImpl2() { }; |
| 615 | void SortedVectorImpl::reservedSortedVectorImpl3() { }; |
| 616 | void SortedVectorImpl::reservedSortedVectorImpl4() { }; |
| 617 | void SortedVectorImpl::reservedSortedVectorImpl5() { }; |
| 618 | void SortedVectorImpl::reservedSortedVectorImpl6() { }; |
| 619 | void SortedVectorImpl::reservedSortedVectorImpl7() { }; |
| 620 | void SortedVectorImpl::reservedSortedVectorImpl8() { }; |
| 621 | |
| 622 | |
| 623 | /*****************************************************************************/ |
| 624 | |
| 625 | }; // namespace android |
| 626 | |