Commit 8957e832 by Lingfeng Yang Committed by Commit Bot

Add pop_back() / back() method for FixedVector

Good for implementing stacks of fixed size (in the pop operation). + fix an issue where push_back() only allows adding up to max_size() - 1 elements. BUG=angleproject:2306 Change-Id: I99b3c8416055f069fa563b684c102fd6aec116bc Reviewed-on: https://chromium-review.googlesource.com/1000031Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org> Commit-Queue: Lingfeng Yang <lfy@google.com>
parent d2488aba
...@@ -71,6 +71,10 @@ class FixedVector final ...@@ -71,6 +71,10 @@ class FixedVector final
void push_back(const value_type &value); void push_back(const value_type &value);
void push_back(value_type &&value); void push_back(value_type &&value);
void pop_back();
reference back();
const_reference back() const;
void swap(FixedVector<T, N, Storage> &other); void swap(FixedVector<T, N, Storage> &other);
void resize(size_type count); void resize(size_type count);
...@@ -241,7 +245,7 @@ void FixedVector<T, N, Storage>::clear() ...@@ -241,7 +245,7 @@ void FixedVector<T, N, Storage>::clear()
template <class T, size_t N, class Storage> template <class T, size_t N, class Storage>
void FixedVector<T, N, Storage>::push_back(const value_type &value) void FixedVector<T, N, Storage>::push_back(const value_type &value)
{ {
ASSERT(mSize + 1 < N); ASSERT(mSize + 1 <= N);
mStorage[mSize] = value; mStorage[mSize] = value;
mSize++; mSize++;
} }
...@@ -249,12 +253,33 @@ void FixedVector<T, N, Storage>::push_back(const value_type &value) ...@@ -249,12 +253,33 @@ void FixedVector<T, N, Storage>::push_back(const value_type &value)
template <class T, size_t N, class Storage> template <class T, size_t N, class Storage>
void FixedVector<T, N, Storage>::push_back(value_type &&value) void FixedVector<T, N, Storage>::push_back(value_type &&value)
{ {
ASSERT(mSize + 1 < N); ASSERT(mSize + 1 <= N);
mStorage[mSize] = std::move(value); mStorage[mSize] = std::move(value);
mSize++; mSize++;
} }
template <class T, size_t N, class Storage> template <class T, size_t N, class Storage>
void FixedVector<T, N, Storage>::pop_back()
{
ASSERT(mSize > 0);
mSize--;
}
template <class T, size_t N, class Storage>
typename FixedVector<T, N, Storage>::reference FixedVector<T, N, Storage>::back()
{
ASSERT(mSize > 0);
return mStorage[mSize - 1];
}
template <class T, size_t N, class Storage>
typename FixedVector<T, N, Storage>::const_reference FixedVector<T, N, Storage>::back() const
{
ASSERT(mSize > 0);
return mStorage[mSize - 1];
}
template <class T, size_t N, class Storage>
void FixedVector<T, N, Storage>::swap(FixedVector<T, N, Storage> &other) void FixedVector<T, N, Storage>::swap(FixedVector<T, N, Storage> &other)
{ {
std::swap(mSize, other.mSize); std::swap(mSize, other.mSize);
......
...@@ -61,6 +61,30 @@ TEST(FixedVector, PushBack) ...@@ -61,6 +61,30 @@ TEST(FixedVector, PushBack)
FixedVector<int, 5> vec; FixedVector<int, 5> vec;
vec.push_back(1); vec.push_back(1);
EXPECT_EQ(1, vec[0]); EXPECT_EQ(1, vec[0]);
vec.push_back(1);
vec.push_back(1);
vec.push_back(1);
vec.push_back(1);
EXPECT_EQ(vec.size(), vec.max_size());
}
// Test the pop_back function
TEST(FixedVector, PopBack)
{
FixedVector<int, 5> vec;
vec.push_back(1);
EXPECT_EQ(1, (int)vec.size());
vec.pop_back();
EXPECT_EQ(0, (int)vec.size());
}
// Test the back function
TEST(FixedVector, Back)
{
FixedVector<int, 5> vec;
vec.push_back(1);
vec.push_back(2);
EXPECT_EQ(2, vec.back());
} }
// Test the sizing operations // Test the sizing operations
...@@ -99,7 +123,7 @@ TEST(FixedVector, Resize) ...@@ -99,7 +123,7 @@ TEST(FixedVector, Resize)
// Test iterating over the vector // Test iterating over the vector
TEST(FixedVector, Iteration) TEST(FixedVector, Iteration)
{ {
FixedVector<int, 5> vec = { 0, 1, 2, 3 }; FixedVector<int, 5> vec = {0, 1, 2, 3};
int vistedCount = 0; int vistedCount = 0;
for (int value : vec) for (int value : vec)
...@@ -110,4 +134,4 @@ TEST(FixedVector, Iteration) ...@@ -110,4 +134,4 @@ TEST(FixedVector, Iteration)
EXPECT_EQ(4, vistedCount); EXPECT_EQ(4, vistedCount);
} }
} // namespace angle } // namespace angle
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment