MimIR
0.5-dev
MimIR is my Intermediate Representation
Toggle main menu visibility
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
11
namespace
automaton
{
12
13
using
Range
= std::pair<std::uint64_t, std::uint64_t>;
14
15
struct
RangeCompare
{
16
inline
bool
operator()
(
const
Range
& a,
const
Range
& b)
const
noexcept
{
return
a.first < b.first; }
17
};
18
19
inline
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
29
template
<
class
Vec>
30
Vec
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
45
template
<
class
Vec>
46
auto
merge_ranges
(
const
Vec& old_ranges) {
47
return
merge_ranges
(old_ranges, [](std::string_view) {});
48
}
49
50
}
// namespace automaton
automaton
Definition
automaton.h:15
automaton::Range
std::pair< std::uint64_t, std::uint64_t > Range
Definition
range_helper.h:13
automaton::merge_ranges
std::optional< Range > merge_ranges(Range a, Range b) noexcept
Definition
range_helper.h:19
automaton::RangeCompare
Definition
range_helper.h:15
automaton::RangeCompare::operator()
bool operator()(const Range &a, const Range &b) const noexcept
Definition
range_helper.h:16
include
automaton
range_helper.h
Generated by
1.18.0