-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqueue.hpp
More file actions
117 lines (99 loc) · 2.67 KB
/
queue.hpp
File metadata and controls
117 lines (99 loc) · 2.67 KB
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
#pragma once
//simple circular ring buffer
#include <nall/range.hpp>
#include <nall/serializer.hpp>
namespace nall {
template<typename T>
struct queue {
queue() = default;
queue(const queue& source) { operator=(source); }
queue(queue&& source) { operator=(move(source)); }
~queue() { reset(); }
auto operator=(const queue& source) -> queue& {
if(this == &source) return *this;
delete[] _data;
_data = new T[source._capacity];
_capacity = source._capacity;
_size = source._size;
_read = source._read;
_write = source._write;
for(uint n : range(_capacity)) _data[n] = source._data[n];
return *this;
}
auto operator=(queue&& source) -> queue& {
if(this == &source) return *this;
_data = source._data;
_capacity = source._capacity;
_size = source._size;
_read = source._read;
_write = source._write;
source._data = nullptr;
source.reset();
return *this;
}
template<typename U = T> auto capacity() const -> uint { return _capacity * sizeof(T) / sizeof(U); }
template<typename U = T> auto size() const -> uint { return _size * sizeof(T) / sizeof(U); }
auto empty() const -> bool { return _size == 0; }
auto pending() const -> bool { return _size > 0; }
auto full() const -> bool { return _size >= (int)_capacity; }
auto underflow() const -> bool { return _size < 0; }
auto overflow() const -> bool { return _size > (int)_capacity; }
auto data() -> T* { return _data; }
auto data() const -> const T* { return _data; }
auto reset() {
delete[] _data;
_data = nullptr;
_capacity = 0;
_size = 0;
_read = 0;
_write = 0;
}
auto resize(uint capacity, const T& value = {}) -> void {
delete[] _data;
_data = new T[capacity];
_capacity = capacity;
_size = 0;
_read = 0;
_write = 0;
for(uint n : range(_capacity)) _data[n] = value;
}
auto flush() -> void {
_size = 0;
_read = 0;
_write = 0;
}
auto fill(const T& value = {}) -> void {
_size = 0;
_read = 0;
_write = 0;
for(uint n : range(_capacity)) _data[n] = value;
}
auto peek(uint index) const -> T {
return _data[(_read + index) % _size];
}
auto read() -> T {
T value = _data[_read++];
if(_read >= _capacity) _read = 0;
_size--;
return value;
}
auto write(const T& value) -> void {
_data[_write++] = value;
if(_write >= _capacity) _write = 0;
_size++;
}
auto serialize(serializer& s) -> void {
s.array(_data, _capacity);
s.integer(_capacity);
s.integer(_size);
s.integer(_read);
s.integer(_write);
}
private:
T* _data = nullptr;
uint _capacity = 0;
int _size = 0;
uint _read = 0;
uint _write = 0;
};
}