-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_deque.h
More file actions
179 lines (152 loc) · 4.55 KB
/
Copy patharray_deque.h
File metadata and controls
179 lines (152 loc) · 4.55 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
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
#pragma once
#include <iostream>
#include <utility>
namespace gush {
/**
* Deque based an array. Constant time on front insert and back insert.
*
* @tparam T - type of stored values.
*/
template <class T>
class array_deque {
struct iterator {
iterator(const array_deque<T>& queue, const size_t id): queue_(queue), id_(id) {}
iterator& operator++() {
id_ += 1;
return *this;
}
iterator operator++(int) {
iterator copy = *this;
id_ += 1;
return copy;
}
bool operator!=(const iterator iterator) {
return iterator.id_ != id_;
}
const T& operator*() {
const size_t index = (queue_.head_ + id_) % queue_.capacity_;
return queue_.data_[index];
}
const array_deque<T>& queue_;
size_t id_;
};
public:
array_deque() {
resize_to(capacity_);
}
/**
* Removes a value from the the front of the deque
* Time complexity: O(1)
*
* @return optional with a value if the deque is not empty, otherwise an empty optional
*/
std::optional<T> pop_front() {
if (size_ == 0) {
return {};
}
const auto index = head_;
head_ = (head_ + 1) % capacity_;
size_ -= 1;
return {std::move(data_[index])};
}
/**
* Removes a value from the back of the deque
* Time complexity: O(1)
*
* @return optional with a value if the deque is not empty, otherwise an empty optional
*/
std::optional<T> pop_back() {
if (size_ == 0) {
return {};
}
const auto index = (head_ + size_ - 1) % capacity_;
size_ -= 1;
return {std::move(data_[index])};
}
/**
* Inserts a value at the back of the deque
* Time complexity: O(1)
*
* @param value - value to insert
*/
template <class V = T>
void push_back(V&& value) {
if (size_ == capacity_) {
resize_to(capacity_ * 2);
}
const auto index = (head_ + size_) % capacity_;
this->data_[index] = std::forward<V>(value);
size_ += 1;
}
/**
* Inserts a value at the front of the deque
* Time complexity: O(1)
*
* @param value - value to insert
*/
template <class V = T>
void push_front(V&& value) {
if (size_ == capacity_) {
resize_to(capacity_ * 2);
}
head_ -= 1;
if (head_ < 0) { head_ = capacity_ - 1; }
this->data_[head_] = std::forward<V>(value);
size_ += 1;
}
/**
* Checks if collections contains specified value
* Time complexity: O(N)
*
* @param value - value to search for
* @return true if value is present in the collection
*/
[[nodiscard]] bool contains(const T& value) const {
for (const auto& v : *this) {
if (v == value) { return true; }
}
return false;
}
iterator begin() {
return {*this, 0};
}
iterator end() {
return {*this, size_};
}
~array_deque() {
// we need to call destructors for all initialized values
// before deallocating memory
for (auto& element : *this) { element.~T(); }
free(data_);
size_ = 0;
head_ = 0;
capacity_ = 0;
}
size_t size() const {
return size_;
}
private:
size_t size_ = 0;
size_t capacity_ = 4;
int head_ = 0;
T* data_ = nullptr;
void resize_to(const size_t capacity) {
// Allocate new memory
T* const new_data = static_cast<T*>(malloc(sizeof(T) * capacity));
if (data_ == nullptr) {
data_ = new_data;
return;
}
// Copy old elements to the new memory
for (size_t i = 0; i < size_; i++) {
const size_t index = (head_ + i) % this->capacity_;
new_data[i] = std::move(this->data_[index]);
}
// Delete old data and update capacity
delete[] this->data_;
this->data_ = new_data;
this->capacity_ = capacity;
head_ = 0;
}
};
}