MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
range_helper.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstdint>
5
6#include <format>
7#include <optional>
8#include <string_view>
9#include <vector>
10
11namespace automaton {
12
13using Range = std::pair<std::uint64_t, std::uint64_t>;
14
16 inline bool operator()(const Range& a, const Range& b) const noexcept { return a.first < b.first; }
17};
18
19inline std::optional<Range> merge_ranges(Range a, Range b) noexcept {
20 if (!(a.second + 1 < b.first || b.second + 1 < a.first)) {
21 return {
22 {std::min(a.first, b.first), std::max(a.second, b.second)}
23 };
24 }
25 return {};
26}
27
28// precondition: ranges are sorted by increasing lower bound
29template<class Vec>
30Vec merge_ranges(const Vec& old_ranges, std::invocable<std::string_view> auto log) {
31 Vec new_ranges;
32 for (const auto& range : old_ranges) {
33 log(std::format("old range: {}-{}", range.first, range.second));
34 if (!new_ranges.empty())
35 if (auto merged = merge_ranges(new_ranges.back(), range)) {
36 new_ranges.back() = *merged;
37 continue;
38 }
39 new_ranges.push_back(range);
40 }
41 return new_ranges;
42}
43
44// precondition: ranges are sorted by increasing lower bound
45template<class Vec>
46auto merge_ranges(const Vec& old_ranges) {
47 return merge_ranges(old_ranges, [](std::string_view) {});
48}
49
50} // namespace automaton
std::pair< std::uint64_t, std::uint64_t > Range
std::optional< Range > merge_ranges(Range a, Range b) noexcept
bool operator()(const Range &a, const Range &b) const noexcept