| 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 |  | 
| Mathias Agopian | 22dbf39 | 2017-02-28 15:06:51 -0800 | [diff] [blame] | 19 | #include <utils/VectorImpl.h> | 
 | 20 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <stdio.h> | 
| Mark Salyzyn | 66ce3e0 | 2016-09-28 10:07:20 -0700 | [diff] [blame] | 22 | #include <stdlib.h> | 
 | 23 | #include <string.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 |  | 
| Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 25 | #include <log/log.h> | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 |  | 
| Sergio Giro | d2529f2 | 2015-09-23 16:22:59 +0100 | [diff] [blame] | 27 | #include "SharedBuffer.h" | 
 | 28 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 45 |     : mStorage(nullptr), mCount(0), mFlags(flags), mItemSize(itemSize) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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)); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 64 |     // We can't call _do_destroy() here because the vtable is already gone. | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 78 |             mStorage = nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 |             mCount = 0; | 
 | 80 |         } | 
 | 81 |     } | 
 | 82 |     return *this; | 
 | 83 | } | 
 | 84 |  | 
 | 85 | void* VectorImpl::editArrayImpl() | 
 | 86 | { | 
 | 87 |     if (mStorage) { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 88 |         const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage); | 
 | 89 |         SharedBuffer* editable = sb->attemptEdit(); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 90 |         if (editable == nullptr) { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 91 |             // If we're here, we're not the only owner of the buffer. | 
 | 92 |             // We must make a copy of it. | 
 | 93 |             editable = SharedBuffer::alloc(sb->size()); | 
 | 94 |             // Fail instead of returning a pointer to storage that's not | 
 | 95 |             // editable. Otherwise we'd be editing the contents of a buffer | 
 | 96 |             // for which we're not the only owner, which is undefined behaviour. | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 97 |             LOG_ALWAYS_FATAL_IF(editable == nullptr); | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 98 |             _do_copy(editable->data(), mStorage, mCount); | 
 | 99 |             release_storage(); | 
 | 100 |             mStorage = editable->data(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 |         } | 
 | 102 |     } | 
 | 103 |     return mStorage; | 
 | 104 | } | 
 | 105 |  | 
 | 106 | size_t VectorImpl::capacity() const | 
 | 107 | { | 
 | 108 |     if (mStorage) { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 109 |         return SharedBuffer::bufferFromData(mStorage)->size() / mItemSize; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 |     } | 
 | 111 |     return 0; | 
 | 112 | } | 
 | 113 |  | 
 | 114 | ssize_t VectorImpl::insertVectorAt(const VectorImpl& vector, size_t index) | 
 | 115 | { | 
| Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 116 |     return insertArrayAt(vector.arrayImpl(), index, vector.size()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | } | 
 | 118 |  | 
 | 119 | ssize_t VectorImpl::appendVector(const VectorImpl& vector) | 
 | 120 | { | 
 | 121 |     return insertVectorAt(vector, size()); | 
 | 122 | } | 
 | 123 |  | 
| Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 124 | ssize_t VectorImpl::insertArrayAt(const void* array, size_t index, size_t length) | 
 | 125 | { | 
 | 126 |     if (index > size()) | 
 | 127 |         return BAD_INDEX; | 
 | 128 |     void* where = _grow(index, length); | 
 | 129 |     if (where) { | 
 | 130 |         _do_copy(where, array, length); | 
 | 131 |     } | 
 | 132 |     return where ? index : (ssize_t)NO_MEMORY; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | ssize_t VectorImpl::appendArray(const void* array, size_t length) | 
 | 136 | { | 
 | 137 |     return insertArrayAt(array, size(), length); | 
 | 138 | } | 
 | 139 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | ssize_t VectorImpl::insertAt(size_t index, size_t numItems) | 
 | 141 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 142 |     return insertAt(nullptr, index, numItems); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | } | 
 | 144 |  | 
 | 145 | ssize_t VectorImpl::insertAt(const void* item, size_t index, size_t numItems) | 
 | 146 | { | 
 | 147 |     if (index > size()) | 
 | 148 |         return BAD_INDEX; | 
 | 149 |     void* where = _grow(index, numItems); | 
 | 150 |     if (where) { | 
 | 151 |         if (item) { | 
 | 152 |             _do_splat(where, item, numItems); | 
 | 153 |         } else { | 
 | 154 |             _do_construct(where, numItems); | 
 | 155 |         } | 
 | 156 |     } | 
 | 157 |     return where ? index : (ssize_t)NO_MEMORY; | 
 | 158 | } | 
 | 159 |  | 
 | 160 | static int sortProxy(const void* lhs, const void* rhs, void* func) | 
 | 161 | { | 
 | 162 |     return (*(VectorImpl::compar_t)func)(lhs, rhs); | 
 | 163 | } | 
 | 164 |  | 
 | 165 | status_t VectorImpl::sort(VectorImpl::compar_t cmp) | 
 | 166 | { | 
 | 167 |     return sort(sortProxy, (void*)cmp); | 
 | 168 | } | 
 | 169 |  | 
 | 170 | status_t VectorImpl::sort(VectorImpl::compar_r_t cmp, void* state) | 
 | 171 | { | 
 | 172 |     // the sort must be stable. we're using insertion sort which | 
 | 173 |     // is well suited for small and already sorted arrays | 
 | 174 |     // for big arrays, it could be better to use mergesort | 
 | 175 |     const ssize_t count = size(); | 
 | 176 |     if (count > 1) { | 
 | 177 |         void* array = const_cast<void*>(arrayImpl()); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 178 |         void* temp = nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 |         ssize_t i = 1; | 
 | 180 |         while (i < count) { | 
 | 181 |             void* item = reinterpret_cast<char*>(array) + mItemSize*(i); | 
 | 182 |             void* curr = reinterpret_cast<char*>(array) + mItemSize*(i-1); | 
 | 183 |             if (cmp(curr, item, state) > 0) { | 
 | 184 |  | 
 | 185 |                 if (!temp) { | 
 | 186 |                     // we're going to have to modify the array... | 
 | 187 |                     array = editArrayImpl(); | 
 | 188 |                     if (!array) return NO_MEMORY; | 
 | 189 |                     temp = malloc(mItemSize); | 
 | 190 |                     if (!temp) return NO_MEMORY; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 |                     item = reinterpret_cast<char*>(array) + mItemSize*(i); | 
 | 192 |                     curr = reinterpret_cast<char*>(array) + mItemSize*(i-1); | 
| Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 193 |                 } else { | 
 | 194 |                     _do_destroy(temp, 1); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 |                 } | 
 | 196 |  | 
 | 197 |                 _do_copy(temp, item, 1); | 
 | 198 |  | 
 | 199 |                 ssize_t j = i-1; | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 200 |                 void* next = reinterpret_cast<char*>(array) + mItemSize*(i); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 |                 do { | 
| Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 202 |                     _do_destroy(next, 1); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 |                     _do_copy(next, curr, 1); | 
 | 204 |                     next = curr; | 
 | 205 |                     --j; | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 206 |                     curr = nullptr; | 
| Nick Kralevich | c76698f | 2015-08-28 06:40:23 -0700 | [diff] [blame] | 207 |                     if (j >= 0) { | 
 | 208 |                         curr = reinterpret_cast<char*>(array) + mItemSize*(j); | 
 | 209 |                     } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 |                 } while (j>=0 && (cmp(curr, temp, state) > 0)); | 
 | 211 |  | 
| Mathias Agopian | 15d0edc | 2010-03-29 13:45:18 -0700 | [diff] [blame] | 212 |                 _do_destroy(next, 1); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 |                 _do_copy(next, temp, 1); | 
 | 214 |             } | 
 | 215 |             i++; | 
 | 216 |         } | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 217 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 |         if (temp) { | 
 | 219 |             _do_destroy(temp, 1); | 
 | 220 |             free(temp); | 
 | 221 |         } | 
 | 222 |     } | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 223 |     return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | } | 
 | 225 |  | 
 | 226 | void VectorImpl::pop() | 
 | 227 | { | 
 | 228 |     if (size()) | 
 | 229 |         removeItemsAt(size()-1, 1); | 
 | 230 | } | 
 | 231 |  | 
 | 232 | void VectorImpl::push() | 
 | 233 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 234 |     push(nullptr); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | } | 
 | 236 |  | 
 | 237 | void VectorImpl::push(const void* item) | 
 | 238 | { | 
 | 239 |     insertAt(item, size()); | 
 | 240 | } | 
 | 241 |  | 
 | 242 | ssize_t VectorImpl::add() | 
 | 243 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 244 |     return add(nullptr); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } | 
 | 246 |  | 
| Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 247 | ssize_t VectorImpl::add(const void* item) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | { | 
| Jeff Brown | 9efaaa4 | 2010-06-16 01:53:36 -0700 | [diff] [blame] | 249 |     return insertAt(item, size()); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | } | 
 | 251 |  | 
 | 252 | ssize_t VectorImpl::replaceAt(size_t index) | 
 | 253 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 254 |     return replaceAt(nullptr, index); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | } | 
 | 256 |  | 
 | 257 | ssize_t VectorImpl::replaceAt(const void* prototype, size_t index) | 
 | 258 | { | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 259 |     ALOG_ASSERT(index<size(), | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 |         "[%p] replace: index=%d, size=%d", this, (int)index, (int)size()); | 
 | 261 |  | 
| Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 262 |     if (index >= size()) { | 
 | 263 |         return BAD_INDEX; | 
 | 264 |     } | 
 | 265 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 |     void* item = editItemLocation(index); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 267 |     if (item != prototype) { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 268 |         if (item == nullptr) | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 269 |             return NO_MEMORY; | 
 | 270 |         _do_destroy(item, 1); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 271 |         if (prototype == nullptr) { | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 272 |             _do_construct(item, 1); | 
 | 273 |         } else { | 
 | 274 |             _do_copy(item, prototype, 1); | 
 | 275 |         } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 |     } | 
 | 277 |     return ssize_t(index); | 
 | 278 | } | 
 | 279 |  | 
 | 280 | ssize_t VectorImpl::removeItemsAt(size_t index, size_t count) | 
 | 281 | { | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 282 |     ALOG_ASSERT((index+count)<=size(), | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 |         "[%p] remove: index=%d, count=%d, size=%d", | 
 | 284 |                this, (int)index, (int)count, (int)size()); | 
 | 285 |  | 
 | 286 |     if ((index+count) > size()) | 
 | 287 |         return BAD_VALUE; | 
 | 288 |    _shrink(index, count); | 
 | 289 |    return index; | 
 | 290 | } | 
 | 291 |  | 
 | 292 | void VectorImpl::finish_vector() | 
 | 293 | { | 
 | 294 |     release_storage(); | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 295 |     mStorage = nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 |     mCount = 0; | 
 | 297 | } | 
 | 298 |  | 
 | 299 | void VectorImpl::clear() | 
 | 300 | { | 
 | 301 |     _shrink(0, mCount); | 
 | 302 | } | 
 | 303 |  | 
 | 304 | void* VectorImpl::editItemLocation(size_t index) | 
 | 305 | { | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 306 |     ALOG_ASSERT(index<capacity(), | 
| Mathias Agopian | 266a7d6 | 2011-07-11 16:26:18 -0700 | [diff] [blame] | 307 |         "[%p] editItemLocation: index=%d, capacity=%d, count=%d", | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 |         this, (int)index, (int)capacity(), (int)mCount); | 
| Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 309 |  | 
 | 310 |     if (index < capacity()) { | 
 | 311 |         void* buffer = editArrayImpl(); | 
 | 312 |         if (buffer) { | 
 | 313 |             return reinterpret_cast<char*>(buffer) + index*mItemSize; | 
 | 314 |         } | 
 | 315 |     } | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 316 |     return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } | 
 | 318 |  | 
 | 319 | const void* VectorImpl::itemLocation(size_t index) const | 
 | 320 | { | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 321 |     ALOG_ASSERT(index<capacity(), | 
| Mathias Agopian | 266a7d6 | 2011-07-11 16:26:18 -0700 | [diff] [blame] | 322 |         "[%p] itemLocation: index=%d, capacity=%d, count=%d", | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 |         this, (int)index, (int)capacity(), (int)mCount); | 
 | 324 |  | 
| Mathias Agopian | bdf73c7 | 2012-08-09 19:39:15 -0700 | [diff] [blame] | 325 |     if (index < capacity()) { | 
 | 326 |         const  void* buffer = arrayImpl(); | 
 | 327 |         if (buffer) { | 
 | 328 |             return reinterpret_cast<const char*>(buffer) + index*mItemSize; | 
 | 329 |         } | 
 | 330 |     } | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 331 |     return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | } | 
 | 333 |  | 
 | 334 | ssize_t VectorImpl::setCapacity(size_t new_capacity) | 
 | 335 | { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 336 |     // The capacity must always be greater than or equal to the size | 
 | 337 |     // of this vector. | 
 | 338 |     if (new_capacity <= size()) { | 
 | 339 |         return capacity(); | 
 | 340 |     } | 
 | 341 |  | 
 | 342 |     size_t new_allocation_size = 0; | 
| Elliott Hughes | 85528e8 | 2018-08-28 13:38:45 -0700 | [diff] [blame] | 343 |     LOG_ALWAYS_FATAL_IF(__builtin_mul_overflow(new_capacity, mItemSize, &new_allocation_size)); | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 344 |     SharedBuffer* sb = SharedBuffer::alloc(new_allocation_size); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 |     if (sb) { | 
 | 346 |         void* array = sb->data(); | 
 | 347 |         _do_copy(array, mStorage, size()); | 
 | 348 |         release_storage(); | 
 | 349 |         mStorage = const_cast<void*>(array); | 
 | 350 |     } else { | 
 | 351 |         return NO_MEMORY; | 
 | 352 |     } | 
 | 353 |     return new_capacity; | 
 | 354 | } | 
 | 355 |  | 
| Jesse Hall | b73559d | 2013-03-11 10:16:48 -0700 | [diff] [blame] | 356 | ssize_t VectorImpl::resize(size_t size) { | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 357 |     ssize_t result = OK; | 
| Jesse Hall | b73559d | 2013-03-11 10:16:48 -0700 | [diff] [blame] | 358 |     if (size > mCount) { | 
 | 359 |         result = insertAt(mCount, size - mCount); | 
 | 360 |     } else if (size < mCount) { | 
 | 361 |         result = removeItemsAt(size, mCount - size); | 
 | 362 |     } | 
 | 363 |     return result < 0 ? result : size; | 
 | 364 | } | 
 | 365 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | void VectorImpl::release_storage() | 
 | 367 | { | 
 | 368 |     if (mStorage) { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 369 |         const SharedBuffer* sb = SharedBuffer::bufferFromData(mStorage); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 |         if (sb->release(SharedBuffer::eKeepStorage) == 1) { | 
 | 371 |             _do_destroy(mStorage, mCount); | 
 | 372 |             SharedBuffer::dealloc(sb); | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 373 |         } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 |     } | 
 | 375 | } | 
 | 376 |  | 
 | 377 | void* VectorImpl::_grow(size_t where, size_t amount) | 
 | 378 | { | 
| Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 379 | //    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] | 380 | //        this, (int)where, (int)amount, (int)mCount, (int)capacity()); | 
 | 381 |  | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 382 |     ALOG_ASSERT(where <= mCount, | 
| Jeff Brown | aa5daed | 2011-07-13 22:22:02 -0700 | [diff] [blame] | 383 |             "[%p] _grow: where=%d, amount=%d, count=%d", | 
 | 384 |             this, (int)where, (int)amount, (int)mCount); // caller already checked | 
 | 385 |  | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 386 |     size_t new_size; | 
| Elliott Hughes | 85528e8 | 2018-08-28 13:38:45 -0700 | [diff] [blame] | 387 |     LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(mCount, amount, &new_size), "new_size overflow"); | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 388 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 |     if (capacity() < new_size) { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 390 |         // NOTE: This implementation used to resize vectors as per ((3*x + 1) / 2) | 
 | 391 |         // (sigh..). Also note, the " + 1" was necessary to handle the special case | 
 | 392 |         // where x == 1, where the resized_capacity will be equal to the old | 
 | 393 |         // capacity without the +1. The old calculation wouldn't work properly | 
 | 394 |         // if x was zero. | 
 | 395 |         // | 
 | 396 |         // This approximates the old calculation, using (x + (x/2) + 1) instead. | 
 | 397 |         size_t new_capacity = 0; | 
| Elliott Hughes | 85528e8 | 2018-08-28 13:38:45 -0700 | [diff] [blame] | 398 |         LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(new_size, (new_size / 2), &new_capacity), | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 399 |                             "new_capacity overflow"); | 
| Elliott Hughes | 85528e8 | 2018-08-28 13:38:45 -0700 | [diff] [blame] | 400 |         LOG_ALWAYS_FATAL_IF( | 
 | 401 |                 __builtin_add_overflow(new_capacity, static_cast<size_t>(1u), &new_capacity), | 
 | 402 |                 "new_capacity overflow"); | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 403 |         new_capacity = max(kMinVectorCapacity, new_capacity); | 
 | 404 |  | 
 | 405 |         size_t new_alloc_size = 0; | 
| Elliott Hughes | 85528e8 | 2018-08-28 13:38:45 -0700 | [diff] [blame] | 406 |         LOG_ALWAYS_FATAL_IF(__builtin_mul_overflow(new_capacity, mItemSize, &new_alloc_size), | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 407 |                             "new_alloc_size overflow"); | 
 | 408 |  | 
| Elliott Hughes | 85528e8 | 2018-08-28 13:38:45 -0700 | [diff] [blame] | 409 |         // 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] | 410 |         if ((mStorage) && | 
 | 411 |             (mCount==where) && | 
 | 412 |             (mFlags & HAS_TRIVIAL_COPY) && | 
 | 413 |             (mFlags & HAS_TRIVIAL_DTOR)) | 
 | 414 |         { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 415 |             const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage); | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 416 |             SharedBuffer* sb = cur_sb->editResize(new_alloc_size); | 
| Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 417 |             if (sb) { | 
 | 418 |                 mStorage = sb->data(); | 
 | 419 |             } else { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 420 |                 return nullptr; | 
| Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 421 |             } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 |         } else { | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 423 |             SharedBuffer* sb = SharedBuffer::alloc(new_alloc_size); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 |             if (sb) { | 
 | 425 |                 void* array = sb->data(); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 426 |                 if (where != 0) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 |                     _do_copy(array, mStorage, where); | 
 | 428 |                 } | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 429 |                 if (where != mCount) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 |                     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 Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 436 |             } else { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 437 |                 return nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 |             } | 
 | 439 |         } | 
 | 440 |     } else { | 
| Mathias Agopian | 03b168a | 2012-05-17 16:52:21 -0700 | [diff] [blame] | 441 |         void* array = editArrayImpl(); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 442 |         if (where != mCount) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 |             const void* from = reinterpret_cast<const uint8_t *>(array) + where*mItemSize; | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 444 |             void* to = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; | 
 | 445 |             _do_move_forward(to, from, mCount - where); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 |         } | 
 | 447 |     } | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 448 |     mCount = new_size; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 |     void* free_space = const_cast<void*>(itemLocation(where)); | 
 | 450 |     return free_space; | 
 | 451 | } | 
 | 452 |  | 
 | 453 | void VectorImpl::_shrink(size_t where, size_t amount) | 
 | 454 | { | 
 | 455 |     if (!mStorage) | 
 | 456 |         return; | 
 | 457 |  | 
| Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 458 | //    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] | 459 | //        this, (int)where, (int)amount, (int)mCount, (int)capacity()); | 
 | 460 |  | 
| Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 461 |     ALOG_ASSERT(where + amount <= mCount, | 
| Jeff Brown | aa5daed | 2011-07-13 22:22:02 -0700 | [diff] [blame] | 462 |             "[%p] _shrink: where=%d, amount=%d, count=%d", | 
 | 463 |             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] | 464 |  | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 465 |     size_t new_size; | 
| Elliott Hughes | 85528e8 | 2018-08-28 13:38:45 -0700 | [diff] [blame] | 466 |     LOG_ALWAYS_FATAL_IF(__builtin_sub_overflow(mCount, amount, &new_size)); | 
| Narayan Kamath | c609c31 | 2015-08-28 12:59:48 +0100 | [diff] [blame] | 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 Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 478 |         if ((where == new_size) && | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 |             (mFlags & HAS_TRIVIAL_COPY) && | 
 | 480 |             (mFlags & HAS_TRIVIAL_DTOR)) | 
 | 481 |         { | 
| Mathias Agopian | e79aadd | 2012-08-31 16:20:23 -0700 | [diff] [blame] | 482 |             const SharedBuffer* cur_sb = SharedBuffer::bufferFromData(mStorage); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 |             SharedBuffer* sb = cur_sb->editResize(new_capacity * mItemSize); | 
| Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 484 |             if (sb) { | 
 | 485 |                 mStorage = sb->data(); | 
 | 486 |             } else { | 
 | 487 |                 return; | 
 | 488 |             } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 |         } else { | 
 | 490 |             SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize); | 
 | 491 |             if (sb) { | 
 | 492 |                 void* array = sb->data(); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 493 |                 if (where != 0) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 |                     _do_copy(array, mStorage, where); | 
 | 495 |                 } | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 496 |                 if (where != new_size) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 497 |                     const void* from = reinterpret_cast<const uint8_t *>(mStorage) + (where+amount)*mItemSize; | 
 | 498 |                     void* dest = reinterpret_cast<uint8_t *>(array) + where*mItemSize; | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 499 |                     _do_copy(dest, from, new_size - where); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 |                 } | 
 | 501 |                 release_storage(); | 
 | 502 |                 mStorage = const_cast<void*>(array); | 
| Shuo Gao | eb0eb4f | 2013-10-17 11:36:11 +0800 | [diff] [blame] | 503 |             } else{ | 
 | 504 |                 return; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 |             } | 
 | 506 |         } | 
 | 507 |     } else { | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 508 |         void* array = editArrayImpl(); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 |         void* to = reinterpret_cast<uint8_t *>(array) + where*mItemSize; | 
 | 510 |         _do_destroy(to, amount); | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 511 |         if (where != new_size) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 512 |             const void* from = reinterpret_cast<uint8_t *>(array) + (where+amount)*mItemSize; | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 513 |             _do_move_backward(to, from, new_size - where); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 |         } | 
 | 515 |     } | 
| Jeff Brown | bbbd761 | 2011-07-14 00:29:49 -0700 | [diff] [blame] | 516 |     mCount = new_size; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | } | 
 | 518 |  | 
 | 519 | size_t VectorImpl::itemSize() const { | 
 | 520 |     return mItemSize; | 
 | 521 | } | 
 | 522 |  | 
 | 523 | void VectorImpl::_do_construct(void* storage, size_t num) const | 
 | 524 | { | 
 | 525 |     if (!(mFlags & HAS_TRIVIAL_CTOR)) { | 
 | 526 |         do_construct(storage, num); | 
 | 527 |     } | 
 | 528 | } | 
 | 529 |  | 
 | 530 | void VectorImpl::_do_destroy(void* storage, size_t num) const | 
 | 531 | { | 
 | 532 |     if (!(mFlags & HAS_TRIVIAL_DTOR)) { | 
 | 533 |         do_destroy(storage, num); | 
 | 534 |     } | 
 | 535 | } | 
 | 536 |  | 
 | 537 | void 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 |  | 
 | 546 | void VectorImpl::_do_splat(void* dest, const void* item, size_t num) const { | 
 | 547 |     do_splat(dest, item, num); | 
 | 548 | } | 
 | 549 |  | 
 | 550 | void VectorImpl::_do_move_forward(void* dest, const void* from, size_t num) const { | 
 | 551 |     do_move_forward(dest, from, num); | 
 | 552 | } | 
 | 553 |  | 
 | 554 | void 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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | /*****************************************************************************/ | 
 | 559 |  | 
 | 560 | SortedVectorImpl::SortedVectorImpl(size_t itemSize, uint32_t flags) | 
 | 561 |     : VectorImpl(itemSize, flags) | 
 | 562 | { | 
 | 563 | } | 
 | 564 |  | 
 | 565 | SortedVectorImpl::SortedVectorImpl(const VectorImpl& rhs) | 
 | 566 | : VectorImpl(rhs) | 
 | 567 | { | 
 | 568 | } | 
 | 569 |  | 
 | 570 | SortedVectorImpl::~SortedVectorImpl() | 
 | 571 | { | 
 | 572 | } | 
 | 573 |  | 
 | 574 | SortedVectorImpl& SortedVectorImpl::operator = (const SortedVectorImpl& rhs) | 
 | 575 | { | 
 | 576 |     return static_cast<SortedVectorImpl&>( VectorImpl::operator = (static_cast<const VectorImpl&>(rhs)) ); | 
 | 577 | } | 
 | 578 |  | 
 | 579 | ssize_t SortedVectorImpl::indexOf(const void* item) const | 
 | 580 | { | 
 | 581 |     return _indexOrderOf(item); | 
 | 582 | } | 
 | 583 |  | 
 | 584 | size_t SortedVectorImpl::orderOf(const void* item) const | 
 | 585 | { | 
 | 586 |     size_t o; | 
 | 587 |     _indexOrderOf(item, &o); | 
 | 588 |     return o; | 
 | 589 | } | 
 | 590 |  | 
 | 591 | ssize_t SortedVectorImpl::_indexOrderOf(const void* item, size_t* order) const | 
 | 592 | { | 
| Nick Kralevich | 1f28698 | 2015-08-22 14:27:03 -0700 | [diff] [blame] | 593 |     if (order) *order = 0; | 
 | 594 |     if (isEmpty()) { | 
 | 595 |         return NAME_NOT_FOUND; | 
 | 596 |     } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 |     // 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 |  | 
 | 621 | ssize_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 |  | 
 | 633 | ssize_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 |     } | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 647 |     return OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | } | 
 | 649 |  | 
 | 650 | ssize_t SortedVectorImpl::merge(const SortedVectorImpl& vector) | 
 | 651 | { | 
 | 652 |     // we've merging a sorted vector... nice! | 
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 653 |     ssize_t err = OK; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |  | 
 | 668 | ssize_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 Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | /*****************************************************************************/ | 
 | 678 |  | 
 | 679 | }; // namespace android |