diff options
author | Kyle Suarez <kyle.suarez@mongodb.com> | 2018-03-13 15:15:14 -0400 |
---|---|---|
committer | Kyle Suarez <kyle.suarez@mongodb.com> | 2018-03-13 15:15:14 -0400 |
commit | f176b8eefccdb1708c01f7a7ba508ccaae4709bc (patch) | |
tree | 3d26fb6ae7919933bc63d3e9653b375afb4ba769 /src/mongo/bson/simple_bsonobj_comparator.h | |
parent | 64e649a622b5ac4c1bfad0933132dc7d994b9458 (diff) | |
download | mongo-f176b8eefccdb1708c01f7a7ba508ccaae4709bc.tar.gz |
SERVER-33847 create type aliases for BSONObj containers with binary comparison semantics
Diffstat (limited to 'src/mongo/bson/simple_bsonobj_comparator.h')
-rw-r--r-- | src/mongo/bson/simple_bsonobj_comparator.h | 97 |
1 files changed, 96 insertions, 1 deletions
diff --git a/src/mongo/bson/simple_bsonobj_comparator.h b/src/mongo/bson/simple_bsonobj_comparator.h index 2202e221696..59a61e27dd8 100644 --- a/src/mongo/bson/simple_bsonobj_comparator.h +++ b/src/mongo/bson/simple_bsonobj_comparator.h @@ -28,7 +28,12 @@ #pragma once +#include <map> +#include <set> + #include "mongo/bson/bsonobj_comparator_interface.h" +#include "mongo/stdx/unordered_map.h" +#include "mongo/stdx/unordered_set.h" namespace mongo { @@ -38,7 +43,7 @@ namespace mongo { class SimpleBSONObjComparator final : public BSONObj::ComparatorInterface { public: // Global simple comparator for stateless BSONObj comparisons. BSONObj comparisons that require - // database logic, such as collations, much instantiate their own comparator. + // database logic, such as collations, must instantiate their own comparator. static const SimpleBSONObjComparator kInstance; int compare(const BSONObj& lhs, const BSONObj& rhs) const final { @@ -48,6 +53,96 @@ public: void hash_combine(size_t& seed, const BSONObj& toHash) const final { hashCombineBSONObj(seed, toHash, ComparisonRules::kConsiderFieldName, nullptr); } + + /** + * A functor with simple binary comparison semantics that's suitable for use with ordered STL + * containers. + */ + class LessThan { + public: + explicit LessThan() = default; + + bool operator()(const BSONObj& lhs, const BSONObj& rhs) const { + return kInstance.compare(lhs, rhs) < 0; + } + }; + + /** + * A functor with simple binary comparison semantics that's suitable for use with unordered STL + * containers. + */ + class EqualTo { + public: + explicit EqualTo() = default; + + bool operator()(const BSONObj& lhs, const BSONObj& rhs) const { + return kInstance.compare(lhs, rhs) == 0; + } + }; + + /** + * Functor for computing the hash of a BSONObj, compatible for use with unordered STL + * containers. + */ + class Hasher { + public: + explicit Hasher() = default; + + size_t operator()(const BSONObj& obj) const { + return kInstance.hash(obj); + } + }; }; +/** + * A set of BSONObjs that performs comparisons with simple binary semantics. + */ +using SimpleBSONObjSet = std::set<BSONObj, SimpleBSONObjComparator::LessThan>; + +/** + * A multiset of BSONObjs that performs comparisons with simple binary semantics. + */ +using SimpleBSONObjMultiSet = std::multiset<BSONObj, SimpleBSONObjComparator::LessThan>; + +/** + * An unordered_set of BSONObjs that performs equality checks using simple binary semantics. + */ +using SimpleBSONObjUnorderedSet = + stdx::unordered_set<BSONObj, SimpleBSONObjComparator::Hasher, SimpleBSONObjComparator::EqualTo>; + +/** + * An unordered_multiset of BSONObjs that performs equality checks using simple binary semantics. + */ +using SimpleBSONObjUnorderedMultiset = stdx::unordered_multiset<BSONObj, + SimpleBSONObjComparator::Hasher, + SimpleBSONObjComparator::EqualTo>; + +/** + * A map keyed on BSONObj that performs comparisons with simple binary semantics. + */ +template <typename T> +using SimpleBSONObjMap = std::map<BSONObj, T, SimpleBSONObjComparator::LessThan>; + +/** + * A multimap keyed on BSONObj that performs comparisons with simple binary semantics. + */ +template <typename T> +using SimpleBSONObjMultiMap = std::multimap<BSONObj, T, SimpleBSONObjComparator::LessThan>; + +/** + * An unordered_map keyed on BSONObj that performs equality checks using simple binary semantics. + */ +template <typename T> +using SimpleBSONObjUnorderedMap = stdx:: + unordered_map<BSONObj, T, SimpleBSONObjComparator::Hasher, SimpleBSONObjComparator::EqualTo>; + +/** + * An unordered_multimap keyed on BSONObj that performs equality checks using simple binary + * semantics. + */ +template <typename T> +using SimpleBSONObjUnorderedMultiMap = stdx::unordered_multimap<BSONObj, + T, + SimpleBSONObjComparator::Hasher, + SimpleBSONObjComparator::EqualTo>; } // namespace mongo |