autovector::resize

Summary: Resize the autovector!

Test Plan: test

Reviewers: sdong

Reviewed By: sdong

CC: leveldb

Differential Revision: https://reviews.facebook.net/D18543
This commit is contained in:
Igor Canadi 2014-05-08 13:50:49 -07:00
parent 8e37a29bfb
commit af7453a673
2 changed files with 35 additions and 1 deletions

View File

@ -24,7 +24,7 @@ class autovector : public std::vector<T> {};
// full-fledged generic container.
//
// Currently we don't support:
// * reserve()/shrink_to_fit()/resize()
// * reserve()/shrink_to_fit()
// If used correctly, in most cases, people should not touch the
// underlying vector at all.
// * random insert()/erase(), please only use push_back()/pop_back().
@ -176,6 +176,18 @@ class autovector {
size_type size() const { return num_stack_items_ + vect_.size(); }
// resize does not guarantee anything about the contents of the newly
// available elements
void resize(size_type n) {
if (n > kSize) {
vect_.resize(n - kSize);
num_stack_items_ = kSize;
} else {
vect_.clear();
num_stack_items_ = n;
}
}
bool empty() const { return size() == 0; }
// will not check boundry

View File

@ -70,6 +70,28 @@ TEST(AutoVectorTest, EmplaceBack) {
ASSERT_TRUE(!vec.only_in_stack());
}
TEST(AutoVectorTest, Resize) {
autovector<size_t, kSize> vec;
vec.resize(kSize);
ASSERT_TRUE(vec.only_in_stack());
for (size_t i = 0; i < kSize; ++i) {
vec[i] = i;
}
vec.resize(kSize * 2);
ASSERT_TRUE(!vec.only_in_stack());
for (size_t i = 0; i < kSize; ++i) {
ASSERT_EQ(vec[i], i);
}
for (size_t i = 0; i < kSize; ++i) {
vec[i + kSize] = i;
}
vec.resize(1);
ASSERT_EQ(1U, vec.size());
}
namespace {
void AssertEqual(
const autovector<size_t, kSize>& a, const autovector<size_t, kSize>& b) {