summaryrefslogtreecommitdiff
path: root/src/mongo/bson/bsonobj_comparator_interface.h
diff options
context:
space:
mode:
authorDavid Storch <david.storch@10gen.com>2016-08-26 16:23:22 -0400
committerDavid Storch <david.storch@10gen.com>2016-09-02 10:24:02 -0400
commit231de89b7c8e84b7b6cf638008b483ecab6ba1b6 (patch)
tree2b0143e02e4aa93ffee23c2a412264309f383d1e /src/mongo/bson/bsonobj_comparator_interface.h
parent54488beeea99b3046931109c170d7e51cea0964d (diff)
downloadmongo-231de89b7c8e84b7b6cf638008b483ecab6ba1b6.tar.gz
SERVER-24508 BSONElement::ComparatorInterface
Diffstat (limited to 'src/mongo/bson/bsonobj_comparator_interface.h')
-rw-r--r--src/mongo/bson/bsonobj_comparator_interface.h156
1 files changed, 25 insertions, 131 deletions
diff --git a/src/mongo/bson/bsonobj_comparator_interface.h b/src/mongo/bson/bsonobj_comparator_interface.h
index a063f638391..4240f19b3f4 100644
--- a/src/mongo/bson/bsonobj_comparator_interface.h
+++ b/src/mongo/bson/bsonobj_comparator_interface.h
@@ -28,15 +28,8 @@
#pragma once
-#include <initializer_list>
-#include <map>
-#include <set>
-#include <unordered_map>
-#include <unordered_set>
-
-#include "mongo/base/disallow_copying.h"
+#include "mongo/bson/bson_comparator_interface_base.h"
#include "mongo/bson/bsonobj.h"
-#include "mongo/util/assert_util.h"
namespace mongo {
@@ -57,152 +50,53 @@ namespace mongo {
*
* All methods are thread-safe.
*/
-class BSONObj::ComparatorInterface {
- MONGO_DISALLOW_COPYING(ComparatorInterface);
-
+class BSONObj::ComparatorInterface : public BSONComparatorInterfaceBase<BSONObj> {
public:
/**
- * Functor compatible for use with ordered STL containers.
- */
- class LessThan {
- public:
- explicit LessThan(const ComparatorInterface* comparator) : _comparator(comparator) {}
-
- bool operator()(const BSONObj& lhs, const BSONObj& rhs) const {
- return _comparator->compare(lhs, rhs) < 0;
- }
-
- private:
- const ComparatorInterface* _comparator;
- };
-
- /**
- * Functor compatible for use with unordered STL containers.
- */
- class EqualTo {
- public:
- explicit EqualTo(const ComparatorInterface* comparator) : _comparator(comparator) {}
-
- bool operator()(const BSONObj& lhs, const BSONObj& rhs) const {
- return _comparator->compare(lhs, rhs) == 0;
- }
-
- private:
- const ComparatorInterface* _comparator;
- };
-
- using BSONObjSet = std::set<BSONObj, BSONObj::ComparatorInterface::LessThan>;
-
- // TODO SERVER-23990: Make the BSONObj hash collation-aware.
- using BSONObjUnorderedSet =
- std::unordered_set<BSONObj, BSONObj::Hasher, BSONObj::ComparatorInterface::EqualTo>;
-
- template <typename T>
- using BSONObjIndexedMap = std::map<BSONObj, T, BSONObj::ComparatorInterface::LessThan>;
-
- // TODO SERVER-23990: Make the BSONObj hash collation-aware.
- template <typename T>
- using BSONObjIndexedUnorderedMap =
- std::unordered_map<BSONObj, T, BSONObj::Hasher, BSONObj::ComparatorInterface::EqualTo>;
-
- virtual ~ComparatorInterface() = default;
-
- /**
- * Compares two BSONObj objects. Returns <0, 0, >0 if 'lhs' < 'rhs', 'lhs' == 'rhs', or 'lhs' >
- * 'rhs' respectively.
+ * Constructs a BSONObjSet whose equivalence classes are given by this comparator. This
+ * comparator must outlive the returned set.
*/
- virtual int compare(const BSONObj& lhs, const BSONObj& rhs) const = 0;
-
- /**
- * Evaluates a deferred comparison object generated by invocation of one of the BSONObj operator
- * overloads for relops.
- */
- bool evaluate(BSONObj::DeferredComparison deferredComparison) const {
- int cmp = compare(deferredComparison.lhs, deferredComparison.rhs);
- switch (deferredComparison.type) {
- case BSONObj::DeferredComparison::Type::kLT:
- return cmp < 0;
- case BSONObj::DeferredComparison::Type::kLTE:
- return cmp <= 0;
- case BSONObj::DeferredComparison::Type::kEQ:
- return cmp == 0;
- case BSONObj::DeferredComparison::Type::kGT:
- return cmp > 0;
- case BSONObj::DeferredComparison::Type::kGTE:
- return cmp >= 0;
- case BSONObj::DeferredComparison::Type::kNE:
- return cmp != 0;
- }
-
- MONGO_UNREACHABLE;
- }
-
- /**
- * Returns a function object which computes whether one BSONObj is less than another under this
- * comparator. This comparator must outlive the returned function object.
- */
- LessThan makeLessThan() const {
- return LessThan(this);
- }
-
- /**
- * Returns a function object which computes whether one BSONObj is equal to another under this
- * comparator. This comparator must outlive the returned function object.
- */
- EqualTo makeEqualTo() const {
- return EqualTo(this);
- }
-
- /**
- * Constructs a BSONObjSet whose ordering is given by this comparator. This comparator must
- * outlive the returned set.
- */
- BSONObjSet makeBSONObjSet(std::initializer_list<BSONObj> init = {}) const {
- return BSONObjSet(init, LessThan(this));
+ Set makeBSONObjSet(std::initializer_list<BSONObj> init = {}) const {
+ return makeSet(init);
}
/**
* Constructs a BSONObjUnorderedSet whose equivalence classes are given by this
* comparator. This comparator must outlive the returned set.
*/
- BSONObjUnorderedSet makeBSONObjUnorderedSet(std::initializer_list<BSONObj> init = {}) const {
- // TODO SERVER-23990: Make the BSONObj hash collation-aware.
- return BSONObjUnorderedSet(init, 0, BSONObj::Hasher(), EqualTo(this));
+ UnorderedSet makeBSONObjUnorderedSet(std::initializer_list<BSONObj> init = {}) const {
+ return makeUnorderedSet(init);
}
/**
- * Constructs an ordered map from BSONObj to type T whose ordering is given by this comparator.
- * This comparator must outlive the returned map.
+ * Constructs an ordered map from BSONObj to type ValueType whose ordering is given by this
+ * comparator. This comparator must outlive the returned map.
*/
- template <typename T>
- BSONObjIndexedMap<T> makeBSONObjIndexedMap(
- std::initializer_list<std::pair<const BSONObj, T>> init = {}) const {
- return BSONObjIndexedMap<T>(init, LessThan(this));
+ template <typename ValueType>
+ Map<ValueType> makeBSONObjIndexedMap(
+ std::initializer_list<std::pair<const BSONObj, ValueType>> init = {}) const {
+ return makeMap(init);
}
/**
- * Constructs an unordered map from BSONObj to type T whose ordering is given by this
+ * Constructs an unordered map from BSONObj to type ValueType whose ordering is given by this
* comparator. This comparator must outlive the returned map.
*/
- template <typename T>
- BSONObjIndexedUnorderedMap<T> makeBSONObjIndexedUnorderedMap(
- std::initializer_list<std::pair<const BSONObj, T>> init = {}) const {
- // TODO SERVER-23990: Make the BSONObj hash collation-aware.
- return BSONObjIndexedUnorderedMap<T>(init, 0, BSONObj::Hasher(), EqualTo(this));
+ template <typename ValueType>
+ UnorderedMap<ValueType> makeBSONObjIndexedUnorderedMap(
+ std::initializer_list<std::pair<const BSONObj, ValueType>> init = {}) const {
+ return makeUnorderedMap(init);
}
-
-protected:
- constexpr ComparatorInterface() = default;
};
-using BSONObjSet = BSONObj::ComparatorInterface::BSONObjSet;
+using BSONObjSet = BSONComparatorInterfaceBase<BSONObj>::Set;
-using BSONObjUnorderedSet = BSONObj::ComparatorInterface::BSONObjUnorderedSet;
+using BSONObjUnorderedSet = BSONComparatorInterfaceBase<BSONObj>::UnorderedSet;
-template <typename T>
-using BSONObjIndexedMap = BSONObj::ComparatorInterface::BSONObjIndexedMap<T>;
+template <typename ValueType>
+using BSONObjIndexedMap = BSONComparatorInterfaceBase<BSONObj>::Map<ValueType>;
-template <typename T>
-using BSONObjIndexedUnorderedMap = BSONObj::ComparatorInterface::BSONObjIndexedUnorderedMap<T>;
+template <typename ValueType>
+using BSONObjIndexedUnorderedMap = BSONComparatorInterfaceBase<BSONObj>::UnorderedMap<ValueType>;
} // namespace mongo