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 reinterpret_cast<ElementType *>(mData);
61 }
62 
63 template <typename ElementType, size_t kCapacity>
data()64 const ElementType *FixedSizeVector<ElementType, kCapacity>::data() const {
65   return reinterpret_cast<const ElementType *>(mData);
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>
98 template <typename... Args>
emplace_back(Args &&...args)99 void FixedSizeVector<ElementType, kCapacity>::emplace_back(Args &&... args) {
100   CHRE_ASSERT(!full());
101   if (!full()) {
102     new (&data()[mSize++]) ElementType(std::forward<Args>(args)...);
103   }
104 }
105 
106 template <typename ElementType, size_t kCapacity>
pop_back()107 void FixedSizeVector<ElementType, kCapacity>::pop_back() {
108   CHRE_ASSERT(!empty());
109   erase(mSize - 1);
110 }
111 
112 template <typename ElementType, size_t kCapacity>
113 ElementType &FixedSizeVector<ElementType, kCapacity>::operator[](size_t index) {
114   CHRE_ASSERT(index < mSize);
115   if (index >= kCapacity) {
116     index = kCapacity - 1;
117   }
118 
119   return data()[index];
120 }
121 
122 template <typename ElementType, size_t kCapacity>
123 const ElementType &FixedSizeVector<ElementType, kCapacity>::operator[](
124     size_t index) const {
125   CHRE_ASSERT(index < mSize);
126   if (index >= kCapacity) {
127     index = kCapacity - 1;
128   }
129 
130   return data()[index];
131 }
132 
133 template <typename ElementType, size_t kCapacity>
erase(size_t index)134 void FixedSizeVector<ElementType, kCapacity>::erase(size_t index) {
135   CHRE_ASSERT(index < mSize);
136   if (index < mSize) {
137     mSize--;
138     for (size_t i = index; i < mSize; i++) {
139       moveOrCopyAssign(data()[i], data()[i + 1]);
140     }
141 
142     data()[mSize].~ElementType();
143   }
144 }
145 
146 template <typename ElementType, size_t kCapacity>
swap(size_t index0,size_t index1)147 void FixedSizeVector<ElementType, kCapacity>::swap(size_t index0,
148                                                    size_t index1) {
149   CHRE_ASSERT(index0 < mSize && index1 < mSize);
150   if (index0 < mSize && index1 < mSize && index0 != index1) {
151     typename std::aligned_storage<sizeof(ElementType),
152                                   alignof(ElementType)>::type tempStorage;
153     ElementType &temp = *reinterpret_cast<ElementType *>(&tempStorage);
154     uninitializedMoveOrCopy(&data()[index0], 1, &temp);
155     moveOrCopyAssign(data()[index0], data()[index1]);
156     moveOrCopyAssign(data()[index1], temp);
157   }
158 }
159 
160 template <typename ElementType, size_t kCapacity>
161 typename FixedSizeVector<ElementType, kCapacity>::iterator
begin()162 FixedSizeVector<ElementType, kCapacity>::begin() {
163   return data();
164 }
165 
166 template <typename ElementType, size_t kCapacity>
167 typename FixedSizeVector<ElementType, kCapacity>::iterator
end()168 FixedSizeVector<ElementType, kCapacity>::end() {
169   return (data() + mSize);
170 }
171 
172 template <typename ElementType, size_t kCapacity>
173 typename FixedSizeVector<ElementType, kCapacity>::const_iterator
begin()174 FixedSizeVector<ElementType, kCapacity>::begin() const {
175   return cbegin();
176 }
177 
178 template <typename ElementType, size_t kCapacity>
179 typename FixedSizeVector<ElementType, kCapacity>::const_iterator
end()180 FixedSizeVector<ElementType, kCapacity>::end() const {
181   return cend();
182 }
183 
184 template <typename ElementType, size_t kCapacity>
185 typename FixedSizeVector<ElementType, kCapacity>::const_iterator
cbegin()186 FixedSizeVector<ElementType, kCapacity>::cbegin() const {
187   return data();
188 }
189 
190 template <typename ElementType, size_t kCapacity>
191 typename FixedSizeVector<ElementType, kCapacity>::const_iterator
cend()192 FixedSizeVector<ElementType, kCapacity>::cend() const {
193   return (data() + mSize);
194 }
195 
196 template <typename ElementType, size_t kCapacity>
resize(size_t newSize)197 void FixedSizeVector<ElementType, kCapacity>::resize(size_t newSize) {
198   CHRE_ASSERT(newSize <= kCapacity);
199   if (newSize > kCapacity) {
200     newSize = kCapacity;
201   }
202 
203   if (newSize > size()) {
204     for (size_t i = size(); i < newSize; i++) {
205       emplace_back();
206     }
207   } else {
208     for (size_t i = newSize; i < size(); i++) {
209       data()[i].~ElementType();
210     }
211 
212     mSize = newSize;
213   }
214 }
215 
216 }  // namespace chre
217 
218 #endif  // CHRE_UTIL_FIXED_SIZE_VECTOR_IMPL_H_
219