| 56 | bool overlaps(const GenericRange& other) const { return !(other.m_start > m_end || m_start > other.m_end); } |
| 57 | |
| 58 | vector<GenericRange> split(const GenericRange& nextInterval) const |
| 59 | { |
| 60 | vector<GenericRange> result; |
| 61 | if (overlaps(nextInterval)) |
| 62 | { |
| 63 | // Find overlap start and end |
| 64 | uint64_t intersectionStart = std::max(m_start, nextInterval.m_start); |
| 65 | uint64_t intersectionEnd = std::min(m_end, nextInterval.m_end); |
| 66 | |
| 67 | // Add part of this section to before the intersecting region if it starts earlier |
| 68 | if (m_start < intersectionStart) |
| 69 | result.push_back({m_start, intersectionStart - 1, m_items}); |
| 70 | |
| 71 | // Add the intersecting range, plus both sets of items |
| 72 | GenericRange intersection(intersectionStart, intersectionEnd, m_items); |
| 73 | intersection.m_items.insert(intersection.m_items.end(), nextInterval.m_items.begin(), nextInterval.m_items.end()); |
| 74 | result.push_back(intersection); |
| 75 | |
| 76 | // If the an interval's end is after the intersection (only up to one will be) add it after |
| 77 | if (nextInterval.m_end > intersectionEnd) |
| 78 | result.push_back({intersectionEnd + 1, nextInterval.m_end, nextInterval.m_items}); |
| 79 | else if (m_end > intersectionEnd) |
| 80 | result.push_back({intersectionEnd + 1, m_end, m_items}); |
| 81 | } |
| 82 | |
| 83 | return result; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | // A map of ranges to items. The ranges are flattened and sorted, and the map is used to quickly find the items. Range values are inclusive. |