Skip to content

Commit

Permalink
Merge pull request ceph#17265 from zmedico/interval-set-intersection-…
Browse files Browse the repository at this point in the history
…insert-optimization

interval_set: optimize intersect_of insert operations

Reviewed-by: Adam Emerson <[email protected]>
  • Loading branch information
liewegas authored Sep 1, 2017
2 parents 30a99d4 + b6a0356 commit e59508e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/include/interval_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,31 @@ class interval_set {

typename std::map<T,T>::const_iterator pa = a.m.begin();
typename std::map<T,T>::const_iterator pb = b.m.begin();

typename decltype(m)::iterator mi = m.begin();

while (pa != a.m.end() && pb != b.m.end()) {
// passing?
if (pa->first + pa->second <= pb->first)
{ pa++; continue; }
if (pb->first + pb->second <= pa->first)
{ pb++; continue; }

if (*pa == *pb) {
do {
mi = m.insert(mi, *pa);
_size += pa->second;
++pa;
++pb;
} while (pa != a.m.end() && pb != b.m.end() && *pa == *pb);
continue;
}

T start = MAX(pa->first, pb->first);
T en = MIN(pa->first+pa->second, pb->first+pb->second);
assert(en > start);
insert(start, en-start);
typename decltype(m)::value_type i{start, en - start};
mi = m.insert(mi, i);
_size += i.second;
if (pa->first+pa->second > pb->first+pb->second)
pb++;
else
Expand Down

0 comments on commit e59508e

Please sign in to comment.