autovector::resize

Summary: Resize the autovector!

Test Plan: test

Reviewers: sdong

Reviewed By: sdong

CC: leveldb

Differential Revision: https://reviews.facebook.net/D18543
main
Igor Canadi 11 years ago
parent 8e37a29bfb
commit af7453a673
  1. 14
      util/autovector.h
  2. 22
      util/autovector_test.cc

@ -24,7 +24,7 @@ class autovector : public std::vector<T> {};
// full-fledged generic container. // full-fledged generic container.
// //
// Currently we don't support: // 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 // If used correctly, in most cases, people should not touch the
// underlying vector at all. // underlying vector at all.
// * random insert()/erase(), please only use push_back()/pop_back(). // * 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(); } 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; } bool empty() const { return size() == 0; }
// will not check boundry // will not check boundry

@ -70,6 +70,28 @@ TEST(AutoVectorTest, EmplaceBack) {
ASSERT_TRUE(!vec.only_in_stack()); 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 { namespace {
void AssertEqual( void AssertEqual(
const autovector<size_t, kSize>& a, const autovector<size_t, kSize>& b) { const autovector<size_t, kSize>& a, const autovector<size_t, kSize>& b) {

Loading…
Cancel
Save