dynamic_vector.h revision 30f18903edc1dfe46e12b9989760ef7a56cb31d3
1/* 2 * Copyright (C) 2016 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#ifndef CHRE_UTIL_DYNAMIC_VECTOR_H_ 18#define CHRE_UTIL_DYNAMIC_VECTOR_H_ 19 20#include <cstddef> 21 22#include "chre/util/non_copyable.h" 23 24namespace chre { 25 26/** 27 * A container for storing a sequential array of elements. This container 28 * resizes dynamically using heap allocations. 29 */ 30template<typename ElementType> 31class DynamicVector : public NonCopyable { 32 public: 33 /** 34 * Default-constructs a dynamic vector. 35 */ 36 DynamicVector(); 37 38 /** 39 * Move-constructs a dynamic vector from another. The other dynamic vector is 40 * left in an empty state. 41 * 42 * @param other The other vector to move from. 43 */ 44 DynamicVector(DynamicVector<ElementType>&& other); 45 46 /** 47 * Destructs the objects and releases the memory owned by the vector. 48 */ 49 ~DynamicVector(); 50 51 /** 52 * Removes all elements from the vector, but does not change the capacity. 53 * All iterators and references are invalidated. 54 */ 55 void clear(); 56 57 /** 58 * Returns a pointer to the underlying buffer. Note that this should not be 59 * considered to be persistent as the vector will be moved and resized 60 * automatically. 61 * 62 * @return The pointer to the underlying buffer. 63 */ 64 ElementType *data(); 65 66 /** 67 * Returns a const pointer to the underlying buffer. Note that this should not 68 * be considered to be persistent as the vector will be moved and resized 69 * automatically. 70 * 71 * @return The const pointer to the underlying buffer. 72 */ 73 const ElementType *data() const; 74 75 /** 76 * Returns the current number of elements in the vector. 77 * 78 * @return The number of elements in the vector. 79 */ 80 size_t size() const; 81 82 /** 83 * Returns the maximum number of elements that can be stored in this vector 84 * without a resize operation. 85 * 86 * @return The capacity of the vector. 87 */ 88 size_t capacity() const; 89 90 /** 91 * Determines whether the vector is empty or not. 92 * 93 * @return true if the vector is empty. 94 */ 95 bool empty() const; 96 97 /** 98 * Pushes an element onto the back of the vector. If the vector requires a 99 * resize and that allocation fails this function will return false. All 100 * iterators and references are invalidated if the container has been 101 * resized. Otherwise, only the past-the-end iterator is invalidated. 102 * 103 * @param The element to push onto the vector. 104 * @return true if the element was pushed successfully. 105 */ 106 bool push_back(const ElementType& element); 107 108 /** 109 * Moves an element onto the back of the vector. If the vector requires a 110 * resize and that allocation fails this function will return false. All 111 * iterators and references are invalidated if the container has been 112 * resized. Otherwise, only the past-the-end iterator is invalidated. 113 * 114 * @param The element to move onto the vector. 115 * @return true if the element was moved successfully. 116 */ 117 bool push_back(ElementType&& element); 118 119 /** 120 * Constructs an element onto the back of the vector. All iterators and 121 * references are invalidated if the container has been resized. Otherwise, 122 * only the past-the-end iterator is invalidated. 123 * 124 * @param The arguments to the constructor 125 * @return true is the element is constructed successfully. 126 */ 127 template<typename... Args> 128 bool emplace_back(Args&&... args); 129 130 /** 131 * Obtains an element of the vector given an index. It is illegal to index 132 * this vector out of bounds and the user of the API must check the size() 133 * function prior to indexing this vector to ensure that they will not read 134 * out of bounds. 135 * 136 * @param The index of the element. 137 * @return The element. 138 */ 139 ElementType& operator[](size_t index); 140 141 /** 142 * Obtains a const element of the vector given an index. It is illegal to 143 * index this vector out of bounds and the user of the API must check the 144 * size() function prior to indexing this vector to ensure that they will not 145 * read out of bounds. 146 * 147 * @param The index of the element. 148 * @return The element. 149 */ 150 const ElementType& operator[](size_t index) const; 151 152 /** 153 * Resizes the vector to a new capacity returning true if allocation was 154 * successful. If the new capacity is smaller than the current capacity, 155 * the operation is a no-op and true is returned. If a memory allocation 156 * fails, the contents of the vector are not modified and false is returned. 157 * This is intended to be similar to the reserve function of the std::vector. 158 * All iterators and references are invalidated unless the container did not 159 * resize. 160 * 161 * @param The new capacity of the vector. 162 * @return true if the resize operation was successful. 163 */ 164 bool reserve(size_t newCapacity); 165 166 /** 167 * Inserts an element into the vector at a given index. If a resize of the 168 * vector is required and the allocation fails, false will be returned. This 169 * will shift all vector elements after the given index one position backward 170 * in the list. The supplied index must be <= the size of the vector. It is 171 * not possible to have a sparse list of items. If the index is > the current 172 * size of the vector, false will be returned. All iterators and references 173 * to and after the indexed element are invalidated. Iterators and references 174 * to before the indexed elements are unaffected if the container did not resize. 175 * 176 * @param index The index to insert an element at. 177 * @param element The element to insert. 178 * @return Whether or not the insert operation was successful. 179 */ 180 bool insert(size_t index, const ElementType& element); 181 182 /** 183 * Similar to wrap(), except makes a copy of the supplied C-style array, 184 * maintaining ownership of the buffer within the DynamicVector container. The 185 * vector's capacity is increased if necessary to fit the given array, though 186 * note that this function will not cause the capacity to shrink. Upon 187 * successful reservation of necessary capacity, any pre-existing items in the 188 * vector are removed (via clear()), the supplied array is copied, and the 189 * vector's size is set to elementCount. All iterators and references are 190 * invalidated unless the container did not resize. 191 * 192 * This is essentially equivalent to calling these functions from std::vector: 193 * vector.clear(); 194 * vector.insert(vector.begin(), array, &array[elementCount]); 195 * 196 * This function is not valid to call on a vector where owns_data() is false. 197 * Use unwrap() first in that case. 198 * 199 * @param array Pointer to the start of an array 200 * @param elementCount Number of elements in the supplied array to copy 201 * 202 * @return true if capacity was reserved to fit the supplied array (or the 203 * vector already had sufficient capacity), and the supplied array was 204 * copied into the vector. If false, the vector is not modified. 205 */ 206 bool copy_array(const ElementType *array, size_t elementCount); 207 208 /** 209 * Removes an element from the vector given an index. All elements after the 210 * indexed one are moved forward one position. The destructor is invoked on 211 * on the invalid item left at the end of the vector. The index passed in 212 * must be less than the size() of the vector. If the index is greater than or 213 * equal to the size no operation is performed. All iterators and references 214 * to and after the indexed element are invalidated. 215 * 216 * @param index The index to remove an element at. 217 */ 218 void erase(size_t index); 219 220 /** 221 * Searches the vector for an element. 222 * 223 * @param element The element to comare against. 224 * @return The index of the element found. If the return is equal to size() 225 * then the element was not found. 226 */ 227 size_t find(const ElementType& element) const; 228 229 /** 230 * Swaps the location of two elements stored in the vector. The indices 231 * passed in must be less than the size() of the vector. If the index is 232 * greater than or equal to the size, no operation is performed. All 233 * iterators and references to these two indexed elements are invalidated. 234 * 235 * @param index0 The index of the first element 236 * @param index1 The index of the second element 237 */ 238 void swap(size_t index0, size_t index1); 239 240 /** 241 * Wraps an existing C-style array so it can be used as a DynamicVector. A 242 * reference to the supplied array is kept, as opposed to making a copy. The 243 * caller retains ownership of the memory. Calling code must therefore ensure 244 * that the lifetime of the supplied array is at least as long as that of this 245 * vector, and that the memory is released after this vector is destructed, as 246 * the vector will not attempt to free the memory itself. 247 * 248 * Once a vector wraps another buffer, it cannot be resized except through 249 * another call to wrap(). However, elements can be erased to make room for 250 * adding new elements. 251 * 252 * Destruction of elements within a wrapped array remains the responsibility 253 * of the calling code. While the vector may invoke the element destructor as 254 * a result of explicit calls to functions like erase() or clear(), it will 255 * not destruct elements remaining in the array when the vector is destructed. 256 * Therefore, special care must be taken when wrapping an array of elements 257 * that have a non-trivial destructor. 258 * 259 * @param array Pointer to a pre-allocated array 260 * @param elementCount Number of elements in the array (NOT the array's size 261 * in bytes); will become the vector's size() and capacity() 262 */ 263 void wrap(ElementType *array, size_t elementCount); 264 265 266 /** 267 * Returns a vector that is wrapping an array to the newly-constructed state, 268 * with capacity equal to 0, and owns_data() is true. 269 */ 270 void unwrap(); 271 272 /** 273 * @return false if this vector is wrapping an array passed in via wrap() 274 */ 275 bool owns_data() const; 276 277 /** 278 * Returns a reference to the first element in the vector. It is illegal to 279 * call this on an empty vector. 280 * 281 * @return The first element in the vector. 282 */ 283 ElementType& front(); 284 285 /** 286 * Returns a const reference to the first element in the vector. It is illegal 287 * to call this on an empty vector. 288 * 289 * @return The first element in the vector. 290 */ 291 const ElementType& front() const; 292 293 /** 294 * Returns a reference to the last element in the vector. It is illegal to 295 * call this on an empty vector. 296 * 297 * @return The last element in the vector. 298 */ 299 ElementType& back(); 300 301 /** 302 * Returns a const reference to the last element in the vector. It is illegal 303 * to call this on an empty vector. 304 * 305 * @return The last element in the vector. 306 */ 307 const ElementType& back() const; 308 309 /** 310 * Prepares a vector to push a minimum of one element onto the back. The 311 * vector may be resized if required. The capacity of the vector increases 312 * with the growth policy of this vector (doubles for each resize for now). 313 * 314 * @return Whether or not the resize was successful. 315 */ 316 bool prepareForPush(); 317 318 /** 319 * Random-access iterator that points to some element in the container. 320 */ 321 typedef ElementType* iterator; 322 typedef const ElementType* const_iterator; 323 324 /** 325 * @return A random-access iterator to the beginning. 326 */ 327 typename DynamicVector<ElementType>::iterator begin(); 328 typename DynamicVector<ElementType>::const_iterator cbegin() const; 329 330 /** 331 * @return A random-access iterator to the end. 332 */ 333 typename DynamicVector<ElementType>::iterator end(); 334 typename DynamicVector<ElementType>::const_iterator cend() const; 335 336 private: 337 //! A pointer to the underlying data buffer. 338 ElementType *mData = nullptr; 339 340 //! The current size of the vector, as in the number of elements stored. 341 size_t mSize = 0; 342 343 //! The current capacity of the vector, as in the maximum number of elements 344 //! that can be stored. 345 size_t mCapacity = 0; 346 347 //! Set to true when the buffer (mData) was supplied via wrap() 348 bool mDataIsWrapped = false; 349}; 350 351} // namespace chre 352 353#include "chre/util/dynamic_vector_impl.h" 354 355#endif // CHRE_UTIL_DYNAMIC_VECTOR_H_ 356