forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_range.hh
45 lines (36 loc) · 1.09 KB
/
int_range.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Copyright (C) 2017-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#pragma once
#include "utils/assert.hh"
#include "interval.hh"
#include <seastar/core/format.hh>
#include "seastarx.hh"
using int_range = interval<int>;
inline
unsigned cardinality(const int_range& r) {
SCYLLA_ASSERT(r.start());
SCYLLA_ASSERT(r.end());
return r.end()->value() - r.start()->value() + r.start()->is_inclusive() + r.end()->is_inclusive() - 1;
}
inline
unsigned cardinality(const std::optional<int_range>& ropt) {
return ropt ? cardinality(*ropt) : 0;
}
inline
std::optional<int_range> intersection(const int_range& a, const int_range& b) {
auto int_tri_cmp = [] (int x, int y) {
return x <=> y;
};
return a.intersection(b, int_tri_cmp);
}
inline
int_range make_int_range(int start_inclusive, int end_exclusive) {
if (end_exclusive <= start_inclusive) {
throw std::runtime_error(format("invalid range: [{:d}, {:d})", start_inclusive, end_exclusive));
}
return int_range({start_inclusive}, {end_exclusive - 1});
}