MCPcopy Create free account
hub / github.com/apache/mesos / subtract

Function subtract

src/common/values.cpp:259–353  ·  view source on GitHub ↗

Subtract `right_` from `left_`, and return the result as Value::Ranges.

Source from the content-addressed store, hash-verified

257
258// Subtract `right_` from `left_`, and return the result as Value::Ranges.
259Value::Ranges subtract(const Value::Ranges& left_, const Value::Ranges& right_)
260{
261 if (left_.range_size() == 0 || right_.range_size() == 0) {
262 return left_;
263 }
264
265 // Convert the input `Ranges` to `vector<internal::Range>` and
266 // sort the vector based on the start of a range.
267 auto sortRanges = [](const Value::Ranges& ranges) {
268 vector<internal::Range> result;
269 result.reserve(ranges.range_size());
270
271 foreach (const Value::Range& range, ranges.range()) {
272 result.push_back({range.begin(), range.end()});
273 }
274
275 std::sort(
276 result.begin(),
277 result.end(),
278 [](const internal::Range& left, const internal::Range& right) {
279 return left.start < right.start;
280 });
281
282 return result;
283 };
284
285 Value::Ranges result;
286
287 vector<internal::Range> left = sortRanges(left_);
288 vector<internal::Range> right = sortRanges(right_);
289
290 vector<internal::Range>::iterator itLeft = left.begin();
291 for (vector<internal::Range>::const_iterator itRight = right.cbegin();
292 itLeft != left.end() && itRight != right.cend();) {
293 // Non-overlap:
294 // L: |___|
295 // R: |___|
296 if (itLeft->end < itRight->start) {
297 Value::Range* newRange = result.add_range();
298 newRange->set_begin(itLeft->start);
299 newRange->set_end(itLeft->end);
300
301 itLeft++;
302 continue;
303 }
304
305 // Non-overlap:
306 // L: |___|
307 // R: |___|
308 if (itLeft->start > itRight->end) {
309 itRight++;
310 continue;
311 }
312
313 if (itLeft->start < itRight->start) {
314 Value::Range* newRange = result.add_range();
315 newRange->set_begin(itLeft->start);
316 newRange->set_end(itRight->start - 1);

Callers 4

resources.cppFile · 0.70
foreachFunction · 0.70
operator-Function · 0.70
values.cppFile · 0.70

Calls 3

reserveMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected