• 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_IMPL_H_
18 #define CHRE_UTIL_FIXED_SIZE_VECTOR_IMPL_H_
19 
20 #include "chre/util/fixed_size_vector.h"
21 
22 #include <new>
23 
24 #include "chre/util/container_support.h"
25 #include "chre/util/memory.h"
26 
27 namespace chre {
28 
29 template <typename ElementType, size_t kCapacity>
~FixedSizeVector()30 FixedSizeVector<ElementType, kCapacity>::~FixedSizeVector() {
31   destroy(data(), size());
32 }
33 
34 template <typename ElementType, size_t kCapacity>
back()35 ElementType &FixedSizeVector<ElementType, kCapacity>::back() {
36   CHRE_ASSERT(mSize > 0);
37   return data()[mSize - 1];
38 }
39 
40 template <typename ElementType, size_t kCapacity>
back()41 const ElementType &FixedSizeVector<ElementType, kCapacity>::back() const {
42   CHRE_ASSERT(mSize > 0);
43   return data()[mSize - 1];
44 }
45 
46 template <typename ElementType, size_t kCapacity>
front()47 ElementType &FixedSizeVector<ElementType, kCapacity>::front() {
48   CHRE_ASSERT(mSize > 0);
49   return data()[0];
50 }
51 
52 template <typename ElementType, size_t kCapacity>
front()53 const ElementType &FixedSizeVector<ElementType, kCapacity>::front() const {
54   CHRE_ASSERT(mSize > 0);
55   return data()[0];
56 }
57 
58 template <typename ElementType, size_t kCapacity>
data()59 ElementType *FixedSizeVector<ElementType, kCapacity>::data() {
60   return mData.data();
61 }
62 
63 template <typename ElementType, size_t kCapacity>
data()64 const ElementType *FixedSizeVector<ElementType, kCapacity>::data() const {
65   return mData.data();
66 }
67 
68 template <typename ElementType, size_t kCapacity>
size()69 size_t FixedSizeVector<ElementType, kCapacity>::size() const {
70   return mSize;
71 }
72 
73 template <typename ElementType, size_t kCapacity>
capacity()74 size_t FixedSizeVector<ElementType, kCapacity>::capacity() const {
75   return kCapacity;
76 }
77 
78 template <typename ElementType, size_t kCapacity>
empty()79 bool FixedSizeVector<ElementType, kCapacity>::empty() const {
80   return (mSize == 0);
81 }
82 
83 template <typename ElementType, size_t kCapacity>
full()84 bool FixedSizeVector<ElementType, kCapacity>::full() const {
85   return (mSize == kCapacity);
86 }
87 
88 template <typename ElementType, size_t kCapacity>
push_back(const ElementType & element)89 void FixedSizeVector<ElementType, kCapacity>::push_back(
90     const ElementType &element) {
91   CHRE_ASSERT(!full());
92   if (!full()) {
93     new (&data()[mSize++]) ElementType(element);
94   }
95 }
96 
97 template <typename ElementType, size_t kCapacity>
push_back(ElementType && element)98 void FixedSizeVector<ElementType, kCapacity>::push_back(ElementType &&element) {
99   CHRE_ASSERT(!full());
100   if (!full()) {
101     new (&data()[mSize++]) ElementType(std::move(element));
102   }
103 }
104 
105 template <typename ElementType, size_t kCapacity>
106 template <typename... Args>
emplace_back(Args &&...args)107 void FixedSizeVector<ElementType, kCapacity>::emplace_back(Args &&... args) {
108   CHRE_ASSERT(!full());
109   if (!full()) {
110     new (&data()[mSize++]) ElementType(std::forward<Args>(args)...);
111   }
112 }
113 
114 template <typename ElementType, size_t kCapacity>
pop_back()115 void FixedSizeVector<ElementType, kCapacity>::pop_back() {
116   CHRE_ASSERT(!empty());
117   erase(mSize - 1);
118 }
119 
120 template <typename ElementType, size_t kCapacity>
121 ElementType &FixedSizeVector<ElementType, kCapacity>::operator[](size_t index) {
122   CHRE_ASSERT(index < mSize);
123   if (index >= kCapacity) {
124     index = kCapacity - 1;
125   }
126 
127   return data()[index];
128 }
129 
130 template <typename ElementType, size_t kCapacity>
131 const ElementType &FixedSizeVector<ElementType, kCapacity>::operator[](
132     size_t index) const {
133   CHRE_ASSERT(index < mSize);
134   if (index >= kCapacity) {
135     index = kCapacity - 1;
136   }
137 
138   return data()[index];
139 }
140 
141 template <typename ElementType, size_t kCapacity>
erase(size_t index)142 void FixedSizeVector<ElementType, kCapacity>::erase(size_t index) {
143   CHRE_ASSERT(index < mSize);
144   if (index < mSize) {
145     mSize--;
146     for (size_t i = index; i < mSize; i++) {
147       moveOrCopyAssign(data()[i], data()[i + 1]);
148     }
149 
150     data()[mSize].~ElementType();
151   }
152 }
153 
154 template <typename ElementType, size_t kCapacity>
swap(size_t index0,size_t index1)155 void FixedSizeVector<ElementType, kCapacity>::swap(size_t index0,
156                                                    size_t index1) {
157   CHRE_ASSERT(index0 < mSize && index1 < mSize);
158   if (index0 < mSize && index1 < mSize && index0 != index1) {
159     typename std::aligned_storage<sizeof(ElementType),
160                                   alignof(ElementType)>::type tempStorage;
161     ElementType &temp = *reinterpret_cast<ElementType *>(&tempStorage);
162     uninitializedMoveOrCopy(&data()[index0], 1, &temp);
163     moveOrCopyAssign(data()[index0], data()[index1]);
164     moveOrCopyAssign(data()[index1], temp);
165   }
166 }
167 
168 template <typename ElementType, size_t kCapacity>
169 typename FixedSizeVector<ElementType, kCapacity>::iterator
begin()170 FixedSizeVector<ElementType, kCapacity>::begin() {
171   return data();
172 }
173 
174 template <typename ElementType, size_t kCapacity>
175 typename FixedSizeVector<ElementType, kCapacity>::iterator
end()176 FixedSizeVector<ElementType, kCapacity>::end() {
177   return (data() + mSize);
178 }
179 
180 template <typename ElementType, size_t kCapacity>
181 typename FixedSizeVector<ElementType, kCapacity>::const_iterator
begin()182 FixedSizeVector<ElementType, kCapacity>::begin() const {
183   return cbegin();
184 }
185 
186 template <typename ElementType, size_t kCapacity>
187 typename FixedSizeVector<ElementType, kCapacity>::const_iterator
end()188 FixedSizeVector<ElementType, kCapacity>::end() const {
189   return cend();
190 }
191 
192 template <typename ElementType, size_t kCapacity>
193 typename FixedSizeVector<ElementType, kCapacity>::const_iterator
cbegin()194 FixedSizeVector<ElementType, kCapacity>::cbegin() const {
195   return data();
196 }
197 
198 template <typename ElementType, size_t kCapacity>
199 typename FixedSizeVector<ElementType, kCapacity>::const_iterator
cend()200 FixedSizeVector<ElementType, kCapacity>::cend() const {
201   return (data() + mSize);
202 }
203 
204 template <typename ElementType, size_t kCapacity>
resize(size_t newSize)205 void FixedSizeVector<ElementType, kCapacity>::resize(size_t newSize) {
206   CHRE_ASSERT(newSize <= kCapacity);
207   if (newSize > kCapacity) {
208     newSize = kCapacity;
209   }
210 
211   if (newSize > size()) {
212     for (size_t i = size(); i < newSize; i++) {
213       emplace_back();
214     }
215   } else {
216     for (size_t i = newSize; i < size(); i++) {
217       data()[i].~ElementType();
218     }
219 
220     mSize = newSize;
221   }
222 }
223 
224 }  // namespace chre
225 
226 #endif  // CHRE_UTIL_FIXED_SIZE_VECTOR_IMPL_H_
227