-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoundedSlice.hpp
144 lines (112 loc) · 3 KB
/
BoundedSlice.hpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
#include "SDLW.hpp"
namespace TappyPlane {
template<class T>
T wrap(T value, T min, T max) noexcept;
template<class T>
class BoundedSlice {
public:
BoundedSlice(T leftBound, T rightBound, T length) noexcept;
BoundedSlice(const BoundedSlice&) = delete;
BoundedSlice(BoundedSlice&&) = delete;
BoundedSlice& operator=(const BoundedSlice&) = delete;
BoundedSlice& operator=(BoundedSlice&&) = delete;
void operator+=(T) noexcept;
void operator-=(T) noexcept;
T leftBound() const noexcept;
T rightBound() const noexcept;
T length() const noexcept;
T leftEnd() const noexcept;
T rightEnd() const noexcept;
bool isWrapped() const noexcept;
T leftPieceLength() const noexcept;
T rightPieceLength() const noexcept;
private:
T mLeftEnd;
T mRightEnd;
T mLeftBound;
T mRightBound;
};
template<class T>
T wrap(const T value, const T min, const T max) noexcept
{
if (value < min) {
const auto minMaxDifference = max - min;
const auto difference = min - value;
return max - (int(difference) % int(minMaxDifference));
} else if (value > max) {
const auto minMaxDifference = max - min;
const auto difference = value - max;
return min + (int(difference) % int(minMaxDifference));
}
return value;
}
template<class T>
BoundedSlice<T>::BoundedSlice(const T leftBound, const T rightBound,
const T length) noexcept
: mLeftBound(leftBound)
, mRightBound(rightBound)
{
SDLW_ASSERT(leftBound <= rightBound);
SDLW_ASSERT(length <= rightBound - leftBound);
mLeftEnd = mLeftBound;
mRightEnd = mLeftBound + length;
}
template<class T>
void BoundedSlice<T>::operator+=(const T value) noexcept
{
mLeftEnd = wrap(mLeftEnd + value, mLeftBound, mRightBound);
mRightEnd = wrap(mRightEnd + value, mLeftBound, mRightBound);
}
template<class T>
void BoundedSlice<T>::operator-=(const T value) noexcept
{
mLeftEnd = wrap(mLeftEnd - value, mLeftBound, mRightBound);
mRightEnd = wrap(mRightEnd - value, mLeftBound, mRightBound);
}
template<class T>
T BoundedSlice<T>::leftBound() const noexcept
{
return mLeftBound;
}
template<class T>
T BoundedSlice<T>::rightBound() const noexcept
{
return mRightBound;
}
template<class T>
T BoundedSlice<T>::length() const noexcept
{
if (isWrapped()) {
return leftPieceLength() + rightPieceLength();
}
return mRightEnd - mLeftEnd;
}
template<class T>
T BoundedSlice<T>::leftEnd() const noexcept
{
return mLeftEnd;
}
template<class T>
T BoundedSlice<T>::rightEnd() const noexcept
{
return mRightEnd;
}
template<class T>
bool BoundedSlice<T>::isWrapped() const noexcept
{
return mLeftEnd > mRightEnd;
}
template<class T>
T BoundedSlice<T>::leftPieceLength() const noexcept
{
SDLW_ASSERT(isWrapped());
return mRightEnd - mLeftBound;
}
template<class T>
T BoundedSlice<T>::rightPieceLength() const noexcept
{
SDLW_ASSERT(isWrapped());
return mRightBound - mLeftEnd;
}
} // namespace TappyPlane