aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreeHugger Robot <treehugger-gerrit@google.com>2019-03-21 03:44:03 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2019-03-21 03:44:03 +0000
commit401e7d19a62a553f9f77b01372a5940141121052 (patch)
tree4997fdbd80f7d9a552ddb5948423b0179f30b98b
parentddc5307a1e7153b695f7f86091ae88ee77b1dd44 (diff)
parent1c1b835e72e1d65935dcf353d502c1088563714f (diff)
downloadchre-401e7d19a62a553f9f77b01372a5940141121052.tar.gz
Merge "Adds pop_back() method to FixedSizeVector"
-rw-r--r--util/include/chre/util/fixed_size_vector.h7
-rw-r--r--util/include/chre/util/fixed_size_vector_impl.h6
-rw-r--r--util/tests/fixed_size_vector_test.cc22
3 files changed, 35 insertions, 0 deletions
diff --git a/util/include/chre/util/fixed_size_vector.h b/util/include/chre/util/fixed_size_vector.h
index 3a4236d2..85384e59 100644
--- a/util/include/chre/util/fixed_size_vector.h
+++ b/util/include/chre/util/fixed_size_vector.h
@@ -110,6 +110,13 @@ class FixedSizeVector : public NonCopyable {
void emplace_back(Args&&... args);
/**
+ * Erases the last element in the vector. Invalid to call on an empty vector.
+ *
+ * Invalidates any references to back() and end()/cend().
+ */
+ void pop_back();
+
+ /**
* Obtains an element of the vector given an index. It is illegal to index
* this vector out of bounds and the user of the API must check the size()
* function prior to indexing this vector to ensure that they will not read
diff --git a/util/include/chre/util/fixed_size_vector_impl.h b/util/include/chre/util/fixed_size_vector_impl.h
index 4c8cbee7..79ef6a5f 100644
--- a/util/include/chre/util/fixed_size_vector_impl.h
+++ b/util/include/chre/util/fixed_size_vector_impl.h
@@ -104,6 +104,12 @@ void FixedSizeVector<ElementType, kCapacity>::emplace_back(Args&&... args) {
}
template<typename ElementType, size_t kCapacity>
+void FixedSizeVector<ElementType, kCapacity>::pop_back() {
+ CHRE_ASSERT(!empty());
+ erase(mSize - 1);
+}
+
+template<typename ElementType, size_t kCapacity>
ElementType& FixedSizeVector<ElementType, kCapacity>::operator[](
size_t index) {
CHRE_ASSERT(index < mSize);
diff --git a/util/tests/fixed_size_vector_test.cc b/util/tests/fixed_size_vector_test.cc
index 577b957f..89e3365c 100644
--- a/util/tests/fixed_size_vector_test.cc
+++ b/util/tests/fixed_size_vector_test.cc
@@ -99,6 +99,28 @@ TEST(FixedSizeVector, PushBackAndErase) {
ASSERT_EQ(vector.size(), 3);
}
+TEST(FixedSizeVector, PushBackAndPopBack) {
+ FixedSizeVector<int, 8> vector;
+ vector.push_back(0x1337);
+ vector.push_back(0xcafe);
+ vector.push_back(0xbeef);
+ vector.push_back(0xface);
+
+ ASSERT_EQ(vector.size(), 4);
+ ASSERT_EQ(vector.back(), 0xface);
+ vector.pop_back();
+ ASSERT_EQ(vector.size(), 3);
+ ASSERT_EQ(vector.back(), 0xbeef);
+ vector.pop_back();
+ ASSERT_EQ(vector.size(), 2);
+ ASSERT_EQ(vector.back(), 0xcafe);
+ vector.pop_back();
+ ASSERT_EQ(vector.size(), 1);
+ ASSERT_EQ(vector.back(), 0x1337);
+ vector.pop_back();
+ ASSERT_EQ(vector.size(), 0);
+}
+
TEST(FixedSizeVector, EraseDestructorCalled) {
FixedSizeVector<Foo, 4> vector;
for (size_t i = 0; i < 4; ++i) {