diff options
author | Arthur Ishiguro <arthuri@google.com> | 2019-03-20 16:24:05 -0700 |
---|---|---|
committer | Arthur Ishiguro <arthuri@google.com> | 2019-03-20 16:24:05 -0700 |
commit | 1c1b835e72e1d65935dcf353d502c1088563714f (patch) | |
tree | d65af3e1a2ea90135fc26d0a7ce8590ec6edb80b | |
parent | b7cc914a041ace50dd74f851b7346b4d16c1c07c (diff) | |
download | chre-1c1b835e72e1d65935dcf353d502c1088563714f.tar.gz |
Adds pop_back() method to FixedSizeVector
Test: ./run_tests.sh and verify all pass
Change-Id: I0756ce103cfa37d7c5642406d504b1dc3058c094
-rw-r--r-- | util/include/chre/util/fixed_size_vector.h | 7 | ||||
-rw-r--r-- | util/include/chre/util/fixed_size_vector_impl.h | 6 | ||||
-rw-r--r-- | util/tests/fixed_size_vector_test.cc | 22 |
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) { |