-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathslice.h
28 lines (22 loc) · 901 Bytes
/
slice.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
#ifndef SLICE_H
#define SLICE_H
#include <cstddef>
namespace refactor {
template<class t>
struct slice_t {
t const *begin_, *end_;
using Iterator = t const *;
bool empty() const noexcept { return end_ == begin_; }
size_t size() const noexcept { return end_ - begin_; }
t const &at(size_t i) const {
ASSERT(i < size(), "Index out of range");
return operator[](i);
}
t const &operator[](int i) const noexcept { return begin_[i]; }
Iterator begin() const noexcept { return begin_; }
Iterator end() const noexcept { return end_; }
};
template<class t> slice_t<t> slice(t const *begin, t const *end) noexcept { return {begin, end}; }
template<class t> slice_t<t> slice(t const *begin, size_t size) noexcept { return {begin, begin + size}; }
}// namespace refactor
#endif// SLICE_H