Add InlinedVector::resize()
diff --git a/src/core/ext/filters/client_channel/xds/xds_api.cc b/src/core/ext/filters/client_channel/xds/xds_api.cc
index 13ce47f..9b29af9 100644
--- a/src/core/ext/filters/client_channel/xds/xds_api.cc
+++ b/src/core/ext/filters/client_channel/xds/xds_api.cc
@@ -68,8 +68,7 @@
XdsPriorityListUpdate::LocalityMap::Locality locality) {
// Pad the missing priorities in case the localities are not ordered by
// priority.
- // TODO(juanlishen): Implement InlinedVector::resize() and use that instead.
- while (!Contains(locality.priority)) priorities_.emplace_back();
+ if (!Contains(locality.priority)) priorities_.resize(locality.priority + 1);
LocalityMap& locality_map = priorities_[locality.priority];
locality_map.localities.emplace(locality.name, std::move(locality));
}
diff --git a/src/core/lib/gprpp/inlined_vector.h b/src/core/lib/gprpp/inlined_vector.h
index 74e012e..bfbc6ed 100644
--- a/src/core/lib/gprpp/inlined_vector.h
+++ b/src/core/lib/gprpp/inlined_vector.h
@@ -125,6 +125,11 @@
}
}
+ void resize(size_t new_size) {
+ while (new_size > size_) emplace_back();
+ while (new_size < size_) pop_back();
+ }
+
template <typename... Args>
void emplace_back(Args&&... args) {
if (size_ == capacity_) {
diff --git a/test/core/gprpp/inlined_vector_test.cc b/test/core/gprpp/inlined_vector_test.cc
index f227999..03e116a 100644
--- a/test/core/gprpp/inlined_vector_test.cc
+++ b/test/core/gprpp/inlined_vector_test.cc
@@ -486,6 +486,19 @@
EXPECT_EQ(sz - 1, v.size());
}
+TEST(InlinedVectorTest, Resize) {
+ const int kInlinedSize = 2;
+ InlinedVector<UniquePtr<int>, kInlinedSize> v;
+ // Size up.
+ v.resize(5);
+ EXPECT_EQ(5UL, v.size());
+ EXPECT_EQ(nullptr, v[4]);
+ // Size down.
+ v[4] = MakeUnique<int>(5);
+ v.resize(1);
+ EXPECT_EQ(1UL, v.size());
+}
+
} // namespace testing
} // namespace grpc_core