-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrange.h
59 lines (48 loc) · 1.76 KB
/
range.h
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef RANGE_H
#define RANGE_H
#include "error_handler.h"
#include "natural.h"
namespace refactor {
template<class t> struct range_t;
template<class t> struct rev_range_t;
template<class t = size_t>
struct range_t {
t begin_, end_;
using Iterator = natural_t<t>;
bool empty() const noexcept { return end_ == begin_; }
size_t size() const noexcept { return end_ - begin_; }
t at(size_t i) const {
ASSERT(i < size(), "Index out of range");
return operator[](i);
}
t operator[](size_t i) const noexcept { return begin_ + i; }
Iterator begin() const noexcept { return begin_; }
Iterator end() const noexcept { return end_; }
rev_range_t<t> rev() const noexcept { return {end_, begin_}; }
};
template<class t = size_t>
struct rev_range_t {
t begin_, end_;
using Iterator = rev_natural_t<t>;
bool empty() const noexcept { return end_ == begin_; }
size_t size() const noexcept { return end_ - begin_; }
t at(size_t i) const {
ASSERT(i < size(), "Index out of range");
return operator[](i);
}
t operator[](size_t i) const noexcept { return begin_ + i; }
Iterator begin() const noexcept { return begin_; }
Iterator end() const noexcept { return end_; }
};
template<class t = size_t> range_t<t> range0_(t end) {
if constexpr (std::is_signed_v<t>) {
ASSERT(end >= 0, "end must be greater than 0");
}
return {0, end};
}
template<class t = size_t> range_t<t> range(t begin, t end) {
ASSERT(begin <= end, "begin must be less than end");
return {begin, end};
}
}// namespace refactor
#endif// RANGE_H