sorted_vector_types have move inserts
authorMarc Celani <marccelani@fb.com>
Mon, 17 Mar 2014 16:17:50 +0000 (09:17 -0700)
committerDave Watson <davejwatson@fb.com>
Tue, 18 Mar 2014 17:02:24 +0000 (10:02 -0700)
Summary: sorted_vector_types were missing move inserts that come with std::map in c++11. This prevents me from using move iterators in folly::merge use case.

Test Plan: unit tests

Reviewed By: delong.j@fb.com

FB internal diff: D1223426

folly/sorted_vector_types.h
folly/test/sorted_vector_test.cpp

index e848605548cba2082ee586b7e0e0f90f56d99bb3..86b5787027535e9e0273937fca421de0e6ca8c4f 100644 (file)
@@ -116,27 +116,27 @@ namespace detail {
   insert_with_hint(OurContainer& sorted,
                    Vector& cont,
                    typename OurContainer::iterator hint,
-                   typename OurContainer::value_type value,
+                   typename OurContainer::value_type&& value,
                    GrowthPolicy& po)
   {
     const typename OurContainer::value_compare& cmp(sorted.value_comp());
     if (hint == cont.end() || cmp(value, *hint)) {
       if (hint == cont.begin()) {
         po.increase_capacity(cont, cont.begin());
-        return cont.insert(cont.begin(), value);
+        return cont.insert(cont.begin(), std::move(value));
       }
       if (cmp(*(hint - 1), value)) {
         hint = po.increase_capacity(cont, hint);
-        return cont.insert(hint, value);
+        return cont.insert(hint, std::move(value));
       }
-      return sorted.insert(value).first;
+      return sorted.insert(std::move(value)).first;
     }
 
     if (cmp(*hint, value)) {
       if (hint + 1 == cont.end() || cmp(value, *(hint + 1))) {
         typename OurContainer::iterator it =
           po.increase_capacity(cont, hint + 1);
-        return cont.insert(it, value);
+        return cont.insert(it, std::move(value));
       }
     }
 
@@ -244,16 +244,24 @@ public:
   size_type capacity() const    { return m_.cont_.capacity(); }
 
   std::pair<iterator,bool> insert(const value_type& value) {
+    return insert(value_type(value));
+  }
+
+  std::pair<iterator,bool> insert(value_type&& value) {
     iterator it = lower_bound(value);
     if (it == end() || value_comp()(value, *it)) {
       it = get_growth_policy().increase_capacity(m_.cont_, it);
-      return std::make_pair(m_.cont_.insert(it, value), true);
+      return std::make_pair(m_.cont_.insert(it, std::move(value)), true);
     }
     return std::make_pair(it, false);
   }
 
   iterator insert(iterator hint, const value_type& value) {
-    return detail::insert_with_hint(*this, m_.cont_, hint, value,
+    return insert(hint, value_type(value));
+  }
+
+  iterator insert(iterator hint, value_type&& value) {
+    return detail::insert_with_hint(*this, m_.cont_, hint, std::move(value),
       get_growth_policy());
   }
 
@@ -479,16 +487,24 @@ public:
   size_type capacity() const    { return m_.cont_.capacity(); }
 
   std::pair<iterator,bool> insert(const value_type& value) {
+    return insert(value_type(value));
+  }
+
+  std::pair<iterator,bool> insert(value_type&& value) {
     iterator it = lower_bound(value.first);
     if (it == end() || value_comp()(value, *it)) {
       it = get_growth_policy().increase_capacity(m_.cont_, it);
-      return std::make_pair(m_.cont_.insert(it, value), true);
+      return std::make_pair(m_.cont_.insert(it, std::move(value)), true);
     }
     return std::make_pair(it, false);
   }
 
   iterator insert(iterator hint, const value_type& value) {
-    return detail::insert_with_hint(*this, m_.cont_, hint, value,
+    return insert(hint, value_type(value));
+  }
+
+  iterator insert(iterator hint, value_type&& value) {
+    return detail::insert_with_hint(*this, m_.cont_, hint, std::move(value),
       get_growth_policy());
   }
 
index 80038a0a89aa461eb17a3cde0a2b84e1396ce2b2..2ea86d3eba1794aecf3b193c5461fd82be18b167 100644 (file)
@@ -301,3 +301,21 @@ TEST(SortedVectorTest, EmptyTest) {
   EXPECT_TRUE(emptyMap.lower_bound(10) == emptyMap.end());
   EXPECT_TRUE(emptyMap.find(10) == emptyMap.end());
 }
+
+TEST(SortedVectorTest, MoveTest) {
+  sorted_vector_set<std::unique_ptr<int>> s;
+  s.insert(std::unique_ptr<int>(new int(5)));
+  s.insert(s.end(), std::unique_ptr<int>(new int(10)));
+  EXPECT_EQ(s.size(), 2);
+
+  for (const auto& p : s) {
+    EXPECT_TRUE(*p == 5 || *p == 10);
+  }
+
+  sorted_vector_map<int, std::unique_ptr<int>> m;
+  m.insert(std::make_pair(5, std::unique_ptr<int>(new int(5))));
+  m.insert(m.end(), std::make_pair(10, std::unique_ptr<int>(new int(10))));
+
+  EXPECT_EQ(*m[5], 5);
+  EXPECT_EQ(*m[10], 10);
+}