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

Function coalesce

src/common/values.cpp:181–255  ·  view source on GitHub ↗

Coalesces the vector of ranges provided and modifies `result` to contain the solution. The algorithm first sorts all the individual intervals so that we can iterate over them sequentially. The algorithm does a single pass, after the sort, and builds up the solution in place. It then modifies the `result` with as few steps as possible. The expensive part of this operation is modification of the pro

Source from the content-addressed store, hash-verified

179// expensive part of this operation is modification of the protobuf, which is
180// why we prefer to build up the solution in a temporary vector.
181void coalesce(Value::Ranges* result, vector<Range> ranges)
182{
183 // Exit early if empty.
184 if (ranges.empty()) {
185 result->clear_range();
186 return;
187 }
188
189 std::sort(
190 ranges.begin(),
191 ranges.end(),
192 [](const Range& left, const Range& right) {
193 return std::tie(left.start, left.end) <
194 std::tie(right.start, right.end);
195 });
196
197 // We build up initial state of the current range.
198 CHECK(!ranges.empty());
199 int count = 1;
200 Range current = ranges.front();
201
202 // In a single pass, we compute the size of the end result, as well as modify
203 // in place the intermediate data structure to build up result as we
204 // solve it.
205 foreach (const Range& range, ranges) {
206 // Skip if this range is equivalent to the current range.
207 if (range.start == current.start && range.end == current.end) {
208 continue;
209 }
210
211 // If the current range just needs to be extended on the right.
212 if (range.start == current.start && range.end > current.end) {
213 current.end = range.end;
214 } else if (range.start > current.start) {
215 // If we are starting farther ahead, then there are 2 cases:
216 if (range.start <= current.end + 1) {
217 // 1. Ranges are overlapping and we can merge them.
218 current.end = max(current.end, range.end);
219 } else {
220 // 2. No overlap and we are adding a new range.
221 ranges[count - 1] = current;
222 ++count;
223 current = range;
224 }
225 }
226 }
227
228 // Record the state of the last range into of ranges vector.
229 ranges[count - 1] = current;
230
231 CHECK(count <= static_cast<int>(ranges.size()));
232
233 // Shrink result if it is too large by deleting trailing subrange.
234 if (count < result->range_size()) {
235 result->mutable_range()->DeleteSubrange(
236 count, result->range_size() - count);
237 }
238

Callers 5

operator==Function · 0.70
operator<=Function · 0.70
operator+Function · 0.70
values.cppFile · 0.70
parseFunction · 0.70

Calls 7

fillFunction · 0.85
CopyFromMethod · 0.80
emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
sizeMethod · 0.45
reserveMethod · 0.45

Tested by

no test coverage detected