Make StringKeyed* more complete
authorPhil Willoughby <philwill@fb.com>
Mon, 5 Jun 2017 17:58:29 +0000 (10:58 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 5 Jun 2017 18:10:50 +0000 (11:10 -0700)
Summary: Add `count()` and `swap()` methods. Ensure that `operator==` works.

Reviewed By: yfeldblum

Differential Revision: D5169393

fbshipit-source-id: a8025f6fdf251e38b0d2f27733d18967a55c6a15

folly/experimental/StringKeyedMap.h
folly/experimental/StringKeyedSet.h
folly/experimental/StringKeyedUnorderedMap.h
folly/experimental/StringKeyedUnorderedSet.h

index 3024b33fd27a128f6da6019433160623337f8bbf..d8b822c74474365832b21c4ec421bc537019a9bf 100644 (file)
@@ -131,6 +131,12 @@ public:
   using Base::crbegin;
   using Base::crend;
 
+  bool operator==(StringKeyedMap const& other) const {
+    Base const& lhs = *this;
+    Base const& rhs = static_cast<Base const&>(other);
+    return lhs == rhs;
+  }
+
   // no need for copy/move overload as StringPiece is small struct
   mapped_type& operator[](StringPiece key) {
     auto it = find(key);
@@ -144,6 +150,7 @@ public:
 
   using Base::at;
   using Base::find;
+  using Base::count;
   using Base::lower_bound;
   using Base::upper_bound;
 
@@ -190,6 +197,10 @@ public:
     Base::clear();
   }
 
+  void swap(StringKeyedMap& other) & {
+    return Base::swap(other);
+  }
+
   ~StringKeyedMap() {
     // Here we assume that map doesn't use keys in destructor
     for (auto& it : *this) {
index f2b3af841b9ff4186c4377ecdf9005b1b06b62be..2b3f8f3e11a7dc1c22bf48dcfaeca1e32318df42 100644 (file)
@@ -127,9 +127,16 @@ public:
   using Base::cbegin;
   using Base::cend;
   using Base::find;
+  using Base::count;
   using Base::lower_bound;
   using Base::upper_bound;
 
+  bool operator==(StringKeyedSetBase const& other) const {
+    Base const& lhs = *this;
+    Base const& rhs = static_cast<Base const&>(other);
+    return lhs == rhs;
+  }
+
   template <class... Args>
   std::pair<iterator, bool> emplace(Args&&... args) {
     auto key = StringPiece(std::forward<Args>(args)...);
@@ -173,6 +180,10 @@ public:
 
   using Base::get_allocator;
 
+  void swap(StringKeyedSetBase& other) & {
+    return Base::swap(other);
+  }
+
   ~StringKeyedSetBase() {
     // Here we assume that set doesn't use keys in destructor
     for (auto it : *this) {
index fe2fb4111c45f141a3b1050ceca86198f213abb0..7099332ab3fc66207e79481b36d7b3df6f5186c0 100644 (file)
@@ -151,11 +151,16 @@ public:
   using Base::cbegin;
   using Base::cend;
 
-  bool operator==(const StringKeyedUnorderedMap& rhs) {
-    const Base& lhs = *this;
+  bool operator==(StringKeyedUnorderedMap const& other) const {
+    Base const& lhs = *this;
+    Base const& rhs = static_cast<Base const&>(other);
     return lhs == rhs;
   }
 
+  void swap(StringKeyedUnorderedMap& other) & {
+    return Base::swap(other);
+  }
+
   // No need for copy/move overload as StringPiece is small struct.
   mapped_type& operator[](StringPiece key) {
     auto it = find(key);
index a72040ad4e485e5f69b8402d016ea7cee650b3c9..0956bacf807fe2a6a60d0bde03ea663284223ef5 100644 (file)
@@ -160,9 +160,11 @@ public:
   using Base::cbegin;
   using Base::cend;
   using Base::find;
+  using Base::count;
 
-  bool operator==(const BasicStringKeyedUnorderedSet& rhs) const {
-    const Base& lhs = *this;
+  bool operator==(const BasicStringKeyedUnorderedSet& other) const {
+    Base const& lhs = *this;
+    Base const& rhs = static_cast<Base const&>(other);
     return lhs == rhs;
   }
 
@@ -214,6 +216,10 @@ public:
   using Base::bucket_size;
   using Base::bucket;
 
+  void swap(BasicStringKeyedUnorderedSet& other) & {
+    return Base::swap(other);
+  }
+
   ~BasicStringKeyedUnorderedSet() {
     // Here we assume that unordered_set doesn't use keys in destructor
     for (auto& it : *this) {