sorted_vector containers have a shrink_to_fit() method
authorMarc Celani <marccelani@fb.com>
Tue, 5 Aug 2014 13:04:24 +0000 (06:04 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 14 Aug 2014 18:49:04 +0000 (11:49 -0700)
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
folly/test/sorted_vector_test.cpp

index 86b5787027535e9e0273937fca421de0e6ca8c4f..ef451cb2b7559bc238afb15e61bec4419d22dfd8 100644 (file)
@@ -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<iterator,bool> 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<iterator,bool> insert(const value_type& value) {
index eef897154f42545b79c34b94a1c0ca75810f5813..ca806aa0a01f05584de80809358b2dfb3fcc02b6 100644 (file)
@@ -319,3 +319,17 @@ TEST(SortedVectorTest, MoveTest) {
   EXPECT_EQ(*m[5], 5);
   EXPECT_EQ(*m[10], 10);
 }
+
+TEST(SortedVectorTest, ShrinkTest) {
+  sorted_vector_set<int> 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());
+}