Added initializer_list ctors to sorted_vector_{set,map}
authorSoren Lassen <soren@fb.com>
Fri, 27 Sep 2013 19:23:06 +0000 (12:23 -0700)
committerPeter Griess <pgriess@fb.com>
Tue, 15 Oct 2013 01:46:00 +0000 (18:46 -0700)
Test Plan: unittest

Reviewed By: tudorb@fb.com

FB internal diff: D987910

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

index d55038499f02f984fdee12f524637c1c8ef38f6f..03db4cdcbb05f3a139fdb1af4a5581490c80ebd8 100644 (file)
 #ifndef FOLLY_SORTED_VECTOR_TYPES_H_
 #define FOLLY_SORTED_VECTOR_TYPES_H_
 
-#include <vector>
 #include <algorithm>
-#include <utility>
+#include <initializer_list>
 #include <iterator>
+#include <utility>
+#include <vector>
 #include <boost/operators.hpp>
 #include <boost/bind.hpp>
 #include <boost/type_traits/is_same.hpp>
@@ -214,6 +215,15 @@ public:
     insert(first, last);
   }
 
+  explicit sorted_vector_set(
+      std::initializer_list<value_type> list,
+      const Compare& comp = Compare(),
+      const Allocator& alloc = Allocator())
+    : m_(comp, alloc)
+  {
+    insert(list.begin(), list.end());
+  }
+
   key_compare key_comp() const { return m_; }
   value_compare value_comp() const { return m_; }
 
@@ -440,6 +450,15 @@ public:
     insert(first, last);
   }
 
+  explicit sorted_vector_map(
+      std::initializer_list<value_type> list,
+      const Compare& comp = Compare(),
+      const Allocator& alloc = Allocator())
+    : m_(value_compare(comp), alloc)
+  {
+    insert(list.begin(), list.end());
+  }
+
   key_compare key_comp() const { return m_; }
   value_compare value_comp() const { return m_; }
 
index 2ad5fd8c765c8f96ef5e0c90ddd06500d9df7e1a..e78e0ab5b189b5005eade6edaae439dcab7f4a71 100644 (file)
@@ -213,6 +213,36 @@ TEST(SortedVectorTypes, Sizes) {
   EXPECT_EQ(sizeof(MapT), sizeof(std::vector<std::pair<int,int> >));
 }
 
+TEST(SortedVectorTypes, InitializerLists) {
+  sorted_vector_set<int> empty_initialized_set{};
+  EXPECT_TRUE(empty_initialized_set.empty());
+
+  sorted_vector_set<int> singleton_initialized_set{1};
+  EXPECT_EQ(1, singleton_initialized_set.size());
+  EXPECT_EQ(1, *singleton_initialized_set.begin());
+
+  sorted_vector_set<int> forward_initialized_set{1, 2};
+  sorted_vector_set<int> backward_initialized_set{2, 1};
+  EXPECT_EQ(2, forward_initialized_set.size());
+  EXPECT_EQ(1, *forward_initialized_set.begin());
+  EXPECT_EQ(2, *forward_initialized_set.rbegin());
+  EXPECT_TRUE(forward_initialized_set == backward_initialized_set);
+
+  sorted_vector_map<int,int> empty_initialized_map{};
+  EXPECT_TRUE(empty_initialized_map.empty());
+
+  sorted_vector_map<int,int> singleton_initialized_map{{1,10}};
+  EXPECT_EQ(1, singleton_initialized_map.size());
+  EXPECT_EQ(10, singleton_initialized_map[1]);
+
+  sorted_vector_map<int,int> forward_initialized_map{{1,10}, {2,20}};
+  sorted_vector_map<int,int> backward_initialized_map{{2,20}, {1,10}};
+  EXPECT_EQ(2, forward_initialized_map.size());
+  EXPECT_EQ(10, forward_initialized_map[1]);
+  EXPECT_EQ(20, forward_initialized_map[2]);
+  EXPECT_TRUE(forward_initialized_map == backward_initialized_map);
+}
+
 TEST(SortedVectorTypes, CustomCompare) {
   sorted_vector_set<int,less_invert<int> > s;
   for (int i = 0; i < 200; ++i)