Commit 4ce8cc70 authored by Phil Willoughby's avatar Phil Willoughby Committed by Facebook Github Bot

Make StringKeyed* more complete

Summary: Add `count()` and `swap()` methods. Ensure that `operator==` works.

Reviewed By: yfeldblum

Differential Revision: D5169393

fbshipit-source-id: a8025f6fdf251e38b0d2f27733d18967a55c6a15
parent 356d04f6
...@@ -131,6 +131,12 @@ public: ...@@ -131,6 +131,12 @@ public:
using Base::crbegin; using Base::crbegin;
using Base::crend; 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 // no need for copy/move overload as StringPiece is small struct
mapped_type& operator[](StringPiece key) { mapped_type& operator[](StringPiece key) {
auto it = find(key); auto it = find(key);
...@@ -144,6 +150,7 @@ public: ...@@ -144,6 +150,7 @@ public:
using Base::at; using Base::at;
using Base::find; using Base::find;
using Base::count;
using Base::lower_bound; using Base::lower_bound;
using Base::upper_bound; using Base::upper_bound;
...@@ -190,6 +197,10 @@ public: ...@@ -190,6 +197,10 @@ public:
Base::clear(); Base::clear();
} }
void swap(StringKeyedMap& other) & {
return Base::swap(other);
}
~StringKeyedMap() { ~StringKeyedMap() {
// Here we assume that map doesn't use keys in destructor // Here we assume that map doesn't use keys in destructor
for (auto& it : *this) { for (auto& it : *this) {
......
...@@ -127,9 +127,16 @@ public: ...@@ -127,9 +127,16 @@ public:
using Base::cbegin; using Base::cbegin;
using Base::cend; using Base::cend;
using Base::find; using Base::find;
using Base::count;
using Base::lower_bound; using Base::lower_bound;
using Base::upper_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> template <class... Args>
std::pair<iterator, bool> emplace(Args&&... args) { std::pair<iterator, bool> emplace(Args&&... args) {
auto key = StringPiece(std::forward<Args>(args)...); auto key = StringPiece(std::forward<Args>(args)...);
...@@ -173,6 +180,10 @@ public: ...@@ -173,6 +180,10 @@ public:
using Base::get_allocator; using Base::get_allocator;
void swap(StringKeyedSetBase& other) & {
return Base::swap(other);
}
~StringKeyedSetBase() { ~StringKeyedSetBase() {
// Here we assume that set doesn't use keys in destructor // Here we assume that set doesn't use keys in destructor
for (auto it : *this) { for (auto it : *this) {
......
...@@ -151,11 +151,16 @@ public: ...@@ -151,11 +151,16 @@ public:
using Base::cbegin; using Base::cbegin;
using Base::cend; using Base::cend;
bool operator==(const StringKeyedUnorderedMap& rhs) { bool operator==(StringKeyedUnorderedMap const& other) const {
const Base& lhs = *this; Base const& lhs = *this;
Base const& rhs = static_cast<Base const&>(other);
return lhs == rhs; return lhs == rhs;
} }
void swap(StringKeyedUnorderedMap& other) & {
return Base::swap(other);
}
// No need for copy/move overload as StringPiece is small struct. // No need for copy/move overload as StringPiece is small struct.
mapped_type& operator[](StringPiece key) { mapped_type& operator[](StringPiece key) {
auto it = find(key); auto it = find(key);
......
...@@ -160,9 +160,11 @@ public: ...@@ -160,9 +160,11 @@ public:
using Base::cbegin; using Base::cbegin;
using Base::cend; using Base::cend;
using Base::find; using Base::find;
using Base::count;
bool operator==(const BasicStringKeyedUnorderedSet& rhs) const { bool operator==(const BasicStringKeyedUnorderedSet& other) const {
const Base& lhs = *this; Base const& lhs = *this;
Base const& rhs = static_cast<Base const&>(other);
return lhs == rhs; return lhs == rhs;
} }
...@@ -214,6 +216,10 @@ public: ...@@ -214,6 +216,10 @@ public:
using Base::bucket_size; using Base::bucket_size;
using Base::bucket; using Base::bucket;
void swap(BasicStringKeyedUnorderedSet& other) & {
return Base::swap(other);
}
~BasicStringKeyedUnorderedSet() { ~BasicStringKeyedUnorderedSet() {
// Here we assume that unordered_set doesn't use keys in destructor // Here we assume that unordered_set doesn't use keys in destructor
for (auto& it : *this) { for (auto& it : *this) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment