-
Notifications
You must be signed in to change notification settings - Fork 1
/
Common.h
64 lines (54 loc) · 2 KB
/
Common.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
60
61
62
63
64
// 2022-11-11 Common functions
struct Range
{
uint32_t begin = 0;
uint32_t end = 0;
};
template <typename T>
class Span
{
T* begin_ = nullptr;
T* end_ = nullptr;
public:
Span() = default;
Span(const Span&) = default;
Span(Span&&) = default;
template<typename U>
Span(Span<U>&& u) : begin_(u.begin()), end_(u.end())
{
// Ensure no accidental type slicing from child class to base class,
// which would access the wrong memory for later elements.
static_assert(sizeof(*u.begin()) == sizeof(*begin_));
}
Span(T* p, size_t s) : begin_(p), end_(begin_ + s) {}
Span(T* b, T* e) : begin_(b), end_(e) {}
const T* data() const noexcept { return begin_; }
T* data() noexcept { return begin_; }
size_t size() const noexcept { return end_ - begin_; }
T* begin() const noexcept { return begin_; }
T* end() const noexcept { return end_; }
T* begin() noexcept { return begin_; }
T* end() noexcept { return end_; }
T& operator [](size_t index) noexcept { return begin_[index]; }
const T& operator [](size_t index) const noexcept { return begin_[index]; }
T& front() noexcept { return *begin_; }
const T& front() const noexcept { return *begin_; }
T& back() noexcept { return *(end_ - 1); }
const T& back() const noexcept { return *(end_ - 1); }
bool empty() const noexcept { return begin_ == end_; }
void PopFront() { ++begin_; }
void PopBack() { ++end_; }
};
template <typename FullType, typename SmallType>
struct SmallEnum
{
SmallEnum(FullType t = {}) : value(static_cast<SmallType>(t)) {}
operator FullType() {return static_cast<FullType>(value);}
SmallType value;
};
template <typename ContainerType>
auto MakeSpan(ContainerType& container) -> Span<std::remove_reference_t<decltype(*container.data())> >
{
using T = std::remove_reference_t<decltype(*container.data())>;
return Span<T>(container.data(), container.size());
}