-
Notifications
You must be signed in to change notification settings - Fork 1
/
Buffer.h
219 lines (183 loc) · 3.55 KB
/
Buffer.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#pragma once
#include <stdexcept>
#include <array>
#include <memory>
template<typename dtype>
class BaseBuffer
{
private:
int length;
dtype* buffer;
public:
void release()
{
length = 0;
if (buffer != nullptr)
delete[] buffer;
buffer = nullptr;
}
BaseBuffer slice(int count)
{
BaseBuffer buf(count);
memcpy(buf.buffer, buffer, count);
return buf;
}
void reserve(int _size)
{
if (_size <= 0)
{
release();
throw std::runtime_error("buffer size can't be under zero");
}
length = _size;
if (buffer != nullptr)
delete[] buffer;
buffer = new dtype[length];
}
int size() const
{
return length;
}
dtype* getBuffer() const
{
return buffer;
}
BaseBuffer()
{
length = 0;
buffer = nullptr;
}
BaseBuffer(int size) : buffer(nullptr)
{
reserve(size);
}
BaseBuffer(BaseBuffer&& o) noexcept
{
length = o.length;
buffer = o.buffer;
o.length = 0;
o.buffer = nullptr;
}
BaseBuffer& operator=(BaseBuffer&& o) noexcept {
length = o.length;
buffer = o.buffer;
o.length = 0;
o.buffer = nullptr;
return *this;
}
~BaseBuffer()
{
if (buffer)
delete[] buffer;
}
};
using CBuffer = BaseBuffer<char>;
template <class T, int N>
class SequentArrayList {
T arr[N];
int last;
public:
SequentArrayList() {
last = -1;
}
auto& getArray() const {
return arr;
}
void push_front(T& e) {
if (last + 1 == N)
throw std::runtime_error("SequentArrayList push_front&: overflow");
arr[last+1] = arr[0];
arr[0] = e;
++last;
}
void push_front(T&& e) {
if (last + 1 == N)
throw std::runtime_error("SequentArrayList push_front&: overflow");
arr[last+1] = move(arr[0]);
arr[0] = move(e);
++last;
}
void push(T& e) {
if (last + 1 == N)
throw std::runtime_error("SequentArrayList push&: overflow");
arr[last+1] = e;
++last;
}
void push(T&& e) {
if (last + 1 == N)
throw std::runtime_error("SequentArrayList push&&: overflow");
arr[last+1] = std::move(e);
++last;
}
void pop(int n) {
if (last == -1)
throw std::runtime_error("SequentArrayList pop: poped empty");
arr[n] = arr[last];
--last;
}
T& at(int n) {
if (n >= N)
throw std::runtime_error("SequentArrayList at: invalid indexing");
return arr[n];
}
int size() const {
return last+1;
}
};
template <int N>
class RecyclerBuffer {
using BYTE = unsigned char;
BYTE buffer[N];
int start;
int end;
int used;
public:
RecyclerBuffer() : start(0), end(0), used(0) {
}
auto push(const CBuffer& buf, int sz) {
if (used + sz > N)
throw std::runtime_error("RecyclerBuffer push: overflow");
if (end + sz > N) {
Arrange();
}
memcpy(buffer + end, buf.getBuffer(), sz);
end += sz;
used += sz;
return;// true;
}
auto push(const CBuffer& buf) {
push(buf, buf.length);
return;
}
auto poll(CBuffer& dst, size_t sz) {
if (used < sz) {
throw std::runtime_error("RecyclerBuffer push2param: overflow");
}
memcpy(dst.getBuffer(), buffer + start, sz);
start += sz;
used -= sz;
return;
}
auto Used() const {
return used;
}
private:
auto Arrange() {
if (!start)
return;
if (!used) {
start = 0;
end = 0;
return;
}
BYTE* tmpB = reinterpret_cast<BYTE*>(malloc(used));
if (!tmpB)
throw std::runtime_error("Recycler malloc failed");
memcpy(tmpB, buffer + start, used);
memcpy(buffer, tmpB, used);
free(tmpB);
start = 0;
end = used;
return;
}
};