• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FIXED_SIZE_VECTOR_H_
18 #define CHRE_UTIL_FIXED_SIZE_VECTOR_H_
19 
20 #include <cstddef>
21 #include <type_traits>
22 
23 #include "chre/util/non_copyable.h"
24 #include "chre/util/raw_storage.h"
25 
26 namespace chre {
27 
28 template <typename ElementType, size_t kCapacity>
29 class FixedSizeVector : public NonCopyable {
30  public:
31   typedef ElementType value_type;
32 
33   /**
34    * Destructs the objects
35    */
36   ~FixedSizeVector();
37 
38   /**
39    * @return A reference to the last element in the vector
40    */
41   ElementType &back();
42   const ElementType &back() const;
43 
44   /**
45    * @return A reference to the first element in the vector
46    */
47   ElementType &front();
48   const ElementType &front() const;
49 
50   /**
51    * Obtains a pointer to the underlying storage for the vector.
52    *
53    * @return A pointer to the storage used for elements in this vector.
54    */
55   ElementType *data();
56 
57   /**
58    * Obtains a const pointer to the underlying storage for the vector.
59    *
60    * @return A const pointer to the storage used for elements in this vector.
61    */
62   const ElementType *data() const;
63 
64   /**
65    * Obtains the number of elements currently stored in the static vector.
66    *
67    * @return The number of elements currently stored in the vector.
68    */
69   size_t size() const;
70 
71   /**
72    * Obtains the maximum number of elements that can be stored in the static
73    * vector.
74    *
75    * @return The maximum capacity of the vector as defined by the template
76    * argument.
77    */
capacity()78   constexpr size_t capacity() const {
79     return kCapacity;
80   }
81 
82   /**
83    * Determines whether the vector is empty or not.
84    *
85    * @return true if the vector is empty.
86    */
87   bool empty() const;
88 
89   /**
90    * Determines whether the vector is full or not.
91    *
92    * @return true if the vector is full.
93    */
94   bool full() const;
95 
96   /**
97    * Pushes an element onto the back of the vector. It is illegal to push an
98    * item onto a full vector. The user of the API must check the return of the
99    * full() function prior to pushing another element. All iterators and
100    * references are unaffected.
101    *
102    * @param The element to push onto the vector.
103    */
104   void push_back(const ElementType &element);
105   void push_back(ElementType &&element);
106 
107   /**
108    * Constructs an element onto the back of the vector. It is illegal to
109    * construct an item onto a full vector. The user of the API must check the
110    * return of the full() function prior to constructing another element. All
111    * iterators and references are unaffected.
112    *
113    * @param The arguments to the constructor
114    */
115   template <typename... Args>
116   void emplace_back(Args &&... args);
117 
118   /**
119    * Erases the last element in the vector. Invalid to call on an empty vector.
120    *
121    * Invalidates any references to back() and end()/cend().
122    */
123   void pop_back();
124 
125   /**
126    * Obtains an element of the vector given an index. It is illegal to index
127    * this vector out of bounds and the user of the API must check the size()
128    * function prior to indexing this vector to ensure that they will not read
129    * out of bounds.
130    *
131    * @param The index of the element.
132    * @return The element.
133    */
134   ElementType &operator[](size_t index);
135 
136   /**
137    * Obtains a const element of the vector given an index. It is illegal to
138    * index this vector out of bounds and the user of the API must check the
139    * size() function prior to indexing this vector to ensure that they will not
140    * read out of bounds.
141    *
142    * @param The index of the element.
143    * @return The element.
144    */
145   const ElementType &operator[](size_t index) const;
146 
147   /**
148    * Removes an element from the vector given an index. All elements after the
149    * indexed one are moved forward one position. The destructor is invoked on
150    * on the invalid item left at the end of the vector. The index passed in
151    * must be less than the size() of the vector. If the index is greater than or
152    * equal to the size no operation is performed. All iterators and references
153    * to elements before the indexed one are unaffected.
154    *
155    * @param index The index to remove an element at.
156    */
157   void erase(size_t index);
158 
159   /**
160    * Swaps the location of two elements stored in the vector. The indices
161    * passed in must be less than the size() of the vector. If the index is
162    * greater than or equal to the size, no operation is performed. All iterators
163    * and references to these two indexed elements are invalidated.
164    *
165    * @param index0 The index of the first element
166    * @param index1 The index of the second element
167    */
168   void swap(size_t index0, size_t index1);
169 
170   /**
171    * Resizes the fixed size vector by default-constructing from the current
172    * size() to the newly requested size. If the new size is smaller than the
173    * current size(), the elements from the new size to the current size() are
174    * destructed and the vector is shrunk. A resize operation cannot be performed
175    * that is greater than kCapacity. This will result in an assertion failure
176    * and a resize to kCapacity if assertions are disabled. All iterators and
177    * references to elements before newSize are unaffected.
178    *
179    * @param newSize The new size of the vector.
180    */
181   void resize(size_t newSize);
182 
183   /**
184    * Random-access iterator that points to some element in the container.
185    */
186   typedef ElementType *iterator;
187   typedef const ElementType *const_iterator;
188 
189   /**
190    * @return A random-access iterator to the beginning.
191    */
192   typename FixedSizeVector<ElementType, kCapacity>::iterator begin();
193   typename FixedSizeVector<ElementType, kCapacity>::const_iterator begin()
194       const;
195   typename FixedSizeVector<ElementType, kCapacity>::const_iterator cbegin()
196       const;
197 
198   /**
199    * @return A random-access iterator to the end.
200    */
201   typename FixedSizeVector<ElementType, kCapacity>::iterator end();
202   typename FixedSizeVector<ElementType, kCapacity>::const_iterator end() const;
203   typename FixedSizeVector<ElementType, kCapacity>::const_iterator cend() const;
204 
205  protected:
206   //! Provides storage for elements, initially uninitialized.
207   RawStorage<ElementType, kCapacity> mData;
208 
209   //! The number of elements in the vector. This will never be more than
210   //! kCapacity.
211   size_t mSize = 0;
212 };
213 
214 }  // namespace chre
215 
216 #include "chre/util/fixed_size_vector_impl.h"  // IWYU pragma: export
217 
218 #endif  // CHRE_UTIL_FIXED_SIZE_VECTOR_H_
219