From 7ce33845b2248016806757217e513cce1ee270b6 Mon Sep 17 00:00:00 2001 From: Marc Celani Date: Tue, 5 Aug 2014 06:04:24 -0700 Subject: [PATCH] sorted_vector containers have a shrink_to_fit() method Summary: Adds a shrink_to_fit() method to sorted vector types. Test Plan: unit test Reviewed By: mshneer@fb.com FB internal diff: D1477864 --- folly/sorted_vector_types.h | 2 ++ folly/test/sorted_vector_test.cpp | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/folly/sorted_vector_types.h b/folly/sorted_vector_types.h index 86b57870..ef451cb2 100644 --- a/folly/sorted_vector_types.h +++ b/folly/sorted_vector_types.h @@ -241,6 +241,7 @@ public: size_type max_size() const { return m_.cont_.max_size(); } bool empty() const { return m_.cont_.empty(); } void reserve(size_type s) { return m_.cont_.reserve(s); } + void shrink_to_fit() { m_.cont_.shrink_to_fit(); } size_type capacity() const { return m_.cont_.capacity(); } std::pair insert(const value_type& value) { @@ -484,6 +485,7 @@ public: size_type max_size() const { return m_.cont_.max_size(); } bool empty() const { return m_.cont_.empty(); } void reserve(size_type s) { return m_.cont_.reserve(s); } + void shrink_to_fit() { m_.cont_.shrink_to_fit(); } size_type capacity() const { return m_.cont_.capacity(); } std::pair insert(const value_type& value) { diff --git a/folly/test/sorted_vector_test.cpp b/folly/test/sorted_vector_test.cpp index eef89715..ca806aa0 100644 --- a/folly/test/sorted_vector_test.cpp +++ b/folly/test/sorted_vector_test.cpp @@ -319,3 +319,17 @@ TEST(SortedVectorTest, MoveTest) { EXPECT_EQ(*m[5], 5); EXPECT_EQ(*m[10], 10); } + +TEST(SortedVectorTest, ShrinkTest) { + sorted_vector_set s; + int i = 0; + // Hopefully your resize policy doubles when capacity is full, or this will + // hang forever :( + while (s.capacity() == s.size()) { + s.insert(i++); + } + s.shrink_to_fit(); + // The standard does not actually enforce that this be true, but assume that + // vector::shrink_to_fit respects the caller. + EXPECT_EQ(s.capacity(), s.size()); +} -- 2.34.1