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