summaryrefslogtreecommitdiff
path: root/src/mongo/db/range_arithmetic.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/db/range_arithmetic.cpp')
-rw-r--r--src/mongo/db/range_arithmetic.cpp179
1 files changed, 88 insertions, 91 deletions
diff --git a/src/mongo/db/range_arithmetic.cpp b/src/mongo/db/range_arithmetic.cpp
index fae99baf1f0..603dcd324f5 100644
--- a/src/mongo/db/range_arithmetic.cpp
+++ b/src/mongo/db/range_arithmetic.cpp
@@ -32,109 +32,106 @@
namespace mongo {
- using std::make_pair;
- using std::pair;
- using std::string;
- using std::stringstream;
-
- bool rangeContains( const BSONObj& inclusiveLower,
- const BSONObj& exclusiveUpper,
- const BSONObj& point )
- {
- return point.woCompare( inclusiveLower ) >= 0 && point.woCompare( exclusiveUpper ) < 0;
- }
-
- bool rangeOverlaps( const BSONObj& inclusiveLower1,
- const BSONObj& exclusiveUpper1,
- const BSONObj& inclusiveLower2,
- const BSONObj& exclusiveUpper2 )
- {
- return ( exclusiveUpper1.woCompare( inclusiveLower2 ) > 0 )
- && ( exclusiveUpper2.woCompare( inclusiveLower1 ) > 0 );
- }
-
- int compareRanges( const BSONObj& rangeMin1,
- const BSONObj& rangeMax1,
- const BSONObj& rangeMin2,
- const BSONObj& rangeMax2 )
- {
- const int minCmp = rangeMin1.woCompare( rangeMin2 );
- if ( minCmp != 0 ) return minCmp;
- return rangeMax1.woCompare( rangeMax2 );
- }
-
- // Represents the start and end of an overlap of a tested range
- typedef pair<RangeMap::const_iterator, RangeMap::const_iterator> OverlapBounds;
-
- // Internal-only, shared functionality
- OverlapBounds rangeMapOverlapBounds( const RangeMap& ranges,
- const BSONObj& inclusiveLower,
- const BSONObj& exclusiveUpper ) {
-
- // Returns the first chunk with a min key that is >= lower bound - the previous chunk
- // might overlap.
- RangeMap::const_iterator low = ranges.lower_bound( inclusiveLower );
+using std::make_pair;
+using std::pair;
+using std::string;
+using std::stringstream;
+
+bool rangeContains(const BSONObj& inclusiveLower,
+ const BSONObj& exclusiveUpper,
+ const BSONObj& point) {
+ return point.woCompare(inclusiveLower) >= 0 && point.woCompare(exclusiveUpper) < 0;
+}
- // See if the previous chunk overlaps our range, not clear from just min key
- if ( low != ranges.begin() ) {
+bool rangeOverlaps(const BSONObj& inclusiveLower1,
+ const BSONObj& exclusiveUpper1,
+ const BSONObj& inclusiveLower2,
+ const BSONObj& exclusiveUpper2) {
+ return (exclusiveUpper1.woCompare(inclusiveLower2) > 0) &&
+ (exclusiveUpper2.woCompare(inclusiveLower1) > 0);
+}
- RangeMap::const_iterator next = low;
- --low;
+int compareRanges(const BSONObj& rangeMin1,
+ const BSONObj& rangeMax1,
+ const BSONObj& rangeMin2,
+ const BSONObj& rangeMax2) {
+ const int minCmp = rangeMin1.woCompare(rangeMin2);
+ if (minCmp != 0)
+ return minCmp;
+ return rangeMax1.woCompare(rangeMax2);
+}
- // If the previous range's max value is lte our min value
- if ( low->second.woCompare( inclusiveLower ) < 1 ) {
- low = next;
- }
+// Represents the start and end of an overlap of a tested range
+typedef pair<RangeMap::const_iterator, RangeMap::const_iterator> OverlapBounds;
+
+// Internal-only, shared functionality
+OverlapBounds rangeMapOverlapBounds(const RangeMap& ranges,
+ const BSONObj& inclusiveLower,
+ const BSONObj& exclusiveUpper) {
+ // Returns the first chunk with a min key that is >= lower bound - the previous chunk
+ // might overlap.
+ RangeMap::const_iterator low = ranges.lower_bound(inclusiveLower);
+
+ // See if the previous chunk overlaps our range, not clear from just min key
+ if (low != ranges.begin()) {
+ RangeMap::const_iterator next = low;
+ --low;
+
+ // If the previous range's max value is lte our min value
+ if (low->second.woCompare(inclusiveLower) < 1) {
+ low = next;
}
+ }
- // Returns the first chunk with a max key that is >= upper bound - implies the
- // chunk does not overlap upper bound
- RangeMap::const_iterator high = ranges.lower_bound( exclusiveUpper );
+ // Returns the first chunk with a max key that is >= upper bound - implies the
+ // chunk does not overlap upper bound
+ RangeMap::const_iterator high = ranges.lower_bound(exclusiveUpper);
- return OverlapBounds( low, high );
- }
+ return OverlapBounds(low, high);
+}
- void getRangeMapOverlap( const RangeMap& ranges,
- const BSONObj& inclusiveLower,
- const BSONObj& exclusiveUpper,
- RangeVector* overlap ) {
- overlap->clear();
- OverlapBounds bounds = rangeMapOverlapBounds( ranges, inclusiveLower, exclusiveUpper );
- for ( RangeMap::const_iterator it = bounds.first; it != bounds.second; ++it ) {
- overlap->push_back( make_pair( it->first, it->second ) );
- }
+void getRangeMapOverlap(const RangeMap& ranges,
+ const BSONObj& inclusiveLower,
+ const BSONObj& exclusiveUpper,
+ RangeVector* overlap) {
+ overlap->clear();
+ OverlapBounds bounds = rangeMapOverlapBounds(ranges, inclusiveLower, exclusiveUpper);
+ for (RangeMap::const_iterator it = bounds.first; it != bounds.second; ++it) {
+ overlap->push_back(make_pair(it->first, it->second));
}
+}
- bool rangeMapOverlaps( const RangeMap& ranges,
- const BSONObj& inclusiveLower,
- const BSONObj& exclusiveUpper ) {
- OverlapBounds bounds = rangeMapOverlapBounds( ranges, inclusiveLower, exclusiveUpper );
- return bounds.first != bounds.second;
- }
+bool rangeMapOverlaps(const RangeMap& ranges,
+ const BSONObj& inclusiveLower,
+ const BSONObj& exclusiveUpper) {
+ OverlapBounds bounds = rangeMapOverlapBounds(ranges, inclusiveLower, exclusiveUpper);
+ return bounds.first != bounds.second;
+}
- bool rangeMapContains( const RangeMap& ranges,
- const BSONObj& inclusiveLower,
- const BSONObj& exclusiveUpper ) {
- OverlapBounds bounds = rangeMapOverlapBounds( ranges, inclusiveLower, exclusiveUpper );
- if ( bounds.first == ranges.end() ) return false;
+bool rangeMapContains(const RangeMap& ranges,
+ const BSONObj& inclusiveLower,
+ const BSONObj& exclusiveUpper) {
+ OverlapBounds bounds = rangeMapOverlapBounds(ranges, inclusiveLower, exclusiveUpper);
+ if (bounds.first == ranges.end())
+ return false;
- return bounds.first->first.woCompare( inclusiveLower ) == 0
- && bounds.first->second.woCompare( exclusiveUpper ) == 0;
- }
+ return bounds.first->first.woCompare(inclusiveLower) == 0 &&
+ bounds.first->second.woCompare(exclusiveUpper) == 0;
+}
- string rangeToString( const BSONObj& inclusiveLower, const BSONObj& exclusiveUpper ) {
- stringstream ss;
- ss << "[" << inclusiveLower.toString() << ", " << exclusiveUpper.toString() << ")";
- return ss.str();
- }
+string rangeToString(const BSONObj& inclusiveLower, const BSONObj& exclusiveUpper) {
+ stringstream ss;
+ ss << "[" << inclusiveLower.toString() << ", " << exclusiveUpper.toString() << ")";
+ return ss.str();
+}
- string overlapToString( RangeVector overlap ) {
- stringstream ss;
- for ( RangeVector::const_iterator it = overlap.begin(); it != overlap.end(); ++it ) {
- if ( it != overlap.begin() ) ss << ", ";
- ss << rangeToString( it->first, it->second );
- }
- return ss.str();
+string overlapToString(RangeVector overlap) {
+ stringstream ss;
+ for (RangeVector::const_iterator it = overlap.begin(); it != overlap.end(); ++it) {
+ if (it != overlap.begin())
+ ss << ", ";
+ ss << rangeToString(it->first, it->second);
}
-
+ return ss.str();
+}
}