dynamic_vector.h revision ea9fd91360a23da4652a5c51eb2e909ff765fe9e
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   * Destructs the objects and releases the memory owned by the vector.
35   */
36  ~DynamicVector();
37
38  /**
39   * Returns a pointer to the underlying buffer. Note that this should not be
40   * considered to be persistent as the vector will be moved and resized
41   * automatically.
42   *
43   * @return the pointer to the underlying buffer.
44   */
45  ElementType *data();
46
47  /**
48   * Returns a const pointer to the underlying buffer. Note that this should not
49   * be considered to be persistent as the vector will be moved and resized
50   * automatically.
51   *
52   * @return the const pointer to the underlying buffer.
53   */
54  const ElementType *data() const;
55
56  /**
57   * Returns the current number of elements in the vector.
58   *
59   * @return the number of elements in the vector.
60   */
61  size_t size() const;
62
63  /**
64   * Returns the maximum number of elements that can be stored in this vector
65   * without a resize operation.
66   *
67   * @return the capacity of the vector.
68   */
69  size_t capacity() const;
70
71  /**
72   * Determines whether the vector is empty or not.
73   *
74   * @return Returns true if the vector is empty.
75   */
76  bool empty() const;
77
78  /**
79   * Pushes an element onto the back of the vector. If the vector requires a
80   * resize and that allocation fails this function will return false.
81   *
82   * @param The element to push onto the vector.
83   * @return Returns true if the element was pushed successfully.
84   */
85  bool push_back(const ElementType& element);
86
87  /**
88   * Constructs an element onto the back of the vector. It is illegal to
89   * construct an item onto a full vector. The user of the API must check the
90   * return of the full() function prior to constructing another element.
91   *
92   * @param The arguments to the constructor
93   */
94  template<typename... Args>
95  bool emplace_back(Args&&... args);
96
97  /**
98   * Obtains an element of the vector given an index. It is illegal to index
99   * this vector out of bounds and the user of the API must check the size()
100   * function prior to indexing this vector to ensure that they will not read
101   * out of bounds.
102   *
103   * @param The index of the element.
104   * @return The element.
105   */
106  ElementType& operator[](size_t index);
107
108  /**
109   * Obtains a const element of the vector given an index. It is illegal to
110   * index this vector out of bounds and the user of the API must check the
111   * size() function prior to indexing this vector to ensure that they will not
112   * read out of bounds.
113   *
114   * @param The index of the element.
115   * @return The element.
116   */
117  const ElementType& operator[](size_t index) const;
118
119  /**
120   * Resizes the vector to a new capacity returning true if allocation was
121   * successful. If the new capacity is smaller than the current capacity,
122   * the operation is a no-op and true is returned. If a memory allocation
123   * fails, the contents of the vector are not modified and false is returned.
124   * This is intended to be similar to the reserve function of the std::vector.
125   *
126   * @param The new capacity of the vector.
127   * @return True if the resize operation was successful.
128   */
129  bool reserve(size_t newCapacity);
130
131  /**
132   * Inserts an element into the vector at a given index. If a resize of the
133   * vector is required and the allocation fails, false will be returned. This
134   * will shift all vector elements after the given index one position backward
135   * in the list. The supplied index must be <= the size of the vector. It is
136   * not possible to have a sparse list of items. If the index is > the current
137   * size of the vector, false will be returned.
138   *
139   * @param index The index to insert an element at.
140   * @param element The element to insert.
141   * @return Whether or not the insert operation was successful.
142   */
143  bool insert(size_t index, const ElementType& element);
144
145  /**
146   * Removes an element from the vector given an index. All elements are moved
147   * forward one position. The destructor is invoked. The index passed in must
148   * be less than the size() of the vector. If the index is greater than or
149   * equal to the size no operation is performed.
150   *
151   * @param index The index to remove an element at.
152   */
153  void erase(size_t index);
154
155 private:
156  //! A pointer to the underlying data buffer.
157  ElementType *mData = nullptr;
158
159  //! The current size of the vector, as in the number of elements stored.
160  size_t mSize = 0;
161
162  //! The current capacity of the vector, as in the maximum number of elements
163  //! that can be stored.
164  size_t mCapacity = 0;
165
166  /**
167   * Prepares a vector to push one element onto the back. The vector may be
168   * resized if required.
169   *
170   * @return Whether or not the resize was successful.
171   */
172  bool prepareForPush();
173};
174
175}  // namespace chre
176
177#include "chre/util/dynamic_vector_impl.h"
178
179#endif  // CHRE_UTIL_DYNAMIC_VECTOR_H_
180