Add folly::sorted_vector_map::at()
authorLouis Kruger <louisk@fb.com>
Tue, 13 Jan 2015 20:03:43 +0000 (12:03 -0800)
committerwoo <woo@fb.com>
Mon, 2 Feb 2015 21:09:02 +0000 (13:09 -0800)
Summary: http://www.cplusplus.com/reference/map/map/at/

Test Plan: unittest

Reviewed By: ldbrandy@fb.com

Subscribers: trunkagent, jkong, folly-diffs@

FB internal diff: D1773996

Signature: t1:1773996:1421176834:33c8b39f03bf085ca2dd45df113cba948d0f2dd0

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

index e59ef9b3e9d85f33b426c28c0f9d85c539f2b35f..b9b86a8c91e8570c4e4f3505387a02a1832d3bef 100644 (file)
@@ -552,6 +552,22 @@ public:
     return end();
   }
 
+  mapped_type& at(const key_type& key) {
+    iterator it = find(key);
+    if (it != end()) {
+      return it->second;
+    }
+    throw std::out_of_range("sorted_vector_map::at");
+  }
+
+  const mapped_type& at(const key_type& key) const {
+    const_iterator it = find(key);
+    if (it != end()) {
+      return it->second;
+    }
+    throw std::out_of_range("sorted_vector_map::at");
+  }
+
   size_type count(const key_type& key) const {
     return find(key) == end() ? 0 : 1;
   }
index 2b75a5202866d7e01fce4966a1cb48220141b703..d3810a2a1293fd230ed0208c0989376bfc446027 100644 (file)
@@ -149,10 +149,12 @@ TEST(SortedVectorTypes, SimpleMapTest) {
   m[32] = 100.0;
   check_invariant(m);
   EXPECT_TRUE(m.count(32) == 1);
+  EXPECT_DOUBLE_EQ(100.0, m.at(32));
   EXPECT_FALSE(m.find(32) == m.end());
   m.erase(32);
   EXPECT_TRUE(m.find(32) == m.end());
   check_invariant(m);
+  EXPECT_THROW(m.at(32), std::out_of_range);
 
   sorted_vector_map<int,float> m2 = m;
   EXPECT_TRUE(m2 == m);
@@ -300,6 +302,7 @@ TEST(SortedVectorTest, EmptyTest) {
   sorted_vector_map<int,int> emptyMap;
   EXPECT_TRUE(emptyMap.lower_bound(10) == emptyMap.end());
   EXPECT_TRUE(emptyMap.find(10) == emptyMap.end());
+  EXPECT_THROW(emptyMap.at(10), std::out_of_range);
 }
 
 TEST(SortedVectorTest, MoveTest) {