1 #ifndef CIRCULAR_VECTOR_HPP
2 #define CIRCULAR_VECTOR_HPP
12 CircularVector() : is_empty(true), data(nullptr), i_start(0), i_end(0) {
13 this->data = (T*)malloc(16 *
sizeof(T));
21 void clear() noexcept {
22 this->i_end = this->i_start;
23 this->is_empty =
true;
27 if (this->i_end > 0) {
28 return this->data[this->i_end - 1];
30 return this->data[this->allocated - 1];
35 return this->data[this->i_start];
38 template<
class InnerT>
39 void push_back(InnerT && value) {
40 if (__builtin_expect((i_end == i_start) && (!this->is_empty),
false)) {
44 this->data[i_end] = std::forward<InnerT>(value);
46 if (this->i_end == this->allocated) {
50 this->is_empty =
false;
53 void pop_back() noexcept {
54 if (__builtin_expect(this->i_end == 0,
false)) {
55 this->i_end = this->allocated - 1;
60 if (__builtin_expect(this->i_end == this->i_start,
false)) {
61 this->is_empty =
true;
65 void pop_front() noexcept {
66 if (__builtin_expect(this->i_start == this->allocated - 1,
false)) {
72 if (__builtin_expect(this->i_end == this->i_start,
false)) {
73 this->is_empty =
true;
77 template<
class InnerT>
78 void push_front(InnerT && value) {
79 if (__builtin_expect((i_end == i_start) && (!this->is_empty),
false)) {
84 if (__builtin_expect((this->i_start == 0),
false)) {
85 this->i_start = this->allocated - 1;
90 this->data[this->i_start] = std::forward<InnerT>(value);
92 this->is_empty =
false;
95 T & operator[](
size_t index) noexcept {
96 return this->data[(this->i_start + index) % this->allocated];
99 size_t size() const noexcept {
100 if (this->i_start < this->i_end) {
101 return this->i_end - this->i_start;
102 }
else if (this->i_start > this->i_end) {
103 return (this->allocated - this->i_start + this->i_end);
104 }
else if (!this->is_empty) {
105 return this->allocated;
111 bool empty() const noexcept {
112 return this->is_empty;
117 T * new_data = (T*) malloc(
sizeof(T) * this->allocated * 2);
120 std::memcpy(new_data, this->data + this->i_start, this->allocated - this->i_start);
121 std::memcpy(new_data + (this->allocated - this->i_start), this->data, this->i_end);
124 this->data = new_data;