-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
348 lines (252 loc) · 8.78 KB
/
list.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright (c) 2024 pieteraerens.eu
// All rights reserved.
// The file list.h is a part of PythonDataType.
// Last modified: 6/5/24 11:09 AM
#ifndef LIST_H
#define LIST_H
#ifndef LIST_CPP_VERSION
#define LIST_CPP_VERSION 20
#include <optional>
#include <algorithm>
#include <ostream>
#endif
#if LIST_CPP_VERSION > __cplusplus
#error "This list implementation requires at least C++" LIST_CPP_VERSION
#endif
namespace Max {
template<typename T> class list {
T* data;
size_t size_{};
size_t capacity_{};
// Resize table +
void resize_table(const size_t increment_size = 0) {
size_t new_capacity = std::max(capacity_ * 3 / 2, capacity_ + increment_size);
if (new_capacity == 0) {
new_capacity = 2;
}
T* new_data = new T[new_capacity];
if (size_ != 0) {
std::copy(data, data + size_, new_data);
}
delete[] data;
data = new_data;
capacity_ = new_capacity;
}
// Resize table -
void resize_table_minder() {
size_t new_capacity = capacity_ / 3 * 2;
if (new_capacity <= size_) {
return;
}else if (new_capacity < 2) {
new_capacity = 2;
}
T* new_data = new T[new_capacity];
if (size_ != 0) {
std::copy(data, data + size_, new_data);
}
delete[] data;
data = new_data;
capacity_ = new_capacity;
}
// Append element
void append_element(const T new_element) {
data[size_++] = new_element;
}
// Insert element
void insert_element(const T new_element, const size_t index) {
if constexpr (std::is_trivially_copyable_v<T>) {
memmove(data + index + 1, data + index, (size_ - index) * sizeof(T));
} else {
std::move_backward(data + index, data + size_, data + size_ + 1);
}
data[index] = new_element;
size_++;
}
public:
// Constructor
list() : data(nullptr), size_(0), capacity_(0) {}
list(std::initializer_list<T> init) : data(new T[init.size()]), size_(init.size()), capacity_(init.size()) {
std::copy(init.begin(), init.end(), data);
}
// Deconstruct
~list() {
delete[] data;
}
// Getter
[[nodiscard]] const size_t& size() const {
return size_;
}
[[nodiscard]] const size_t& capacity() const {
return capacity_;
}
[[nodiscard]] bool isEmpty() const {
return size_ == 0;
}
[[nodiscard]] bool isNotEmpty() const {
return size_ > 0;
}
// Clear
void clear() {
if (data == nullptr) {
size_ = 0;
capacity_ = 0;
return;
}
delete[] data;
data = nullptr;
size_ = 0;
capacity_ = 0;
}
// Append
void append(const T element) {
if (size_ + 1 < capacity_) {
append_element(element);
return;
}
resize_table();
append_element(element);
}
// Insert
void insert(const T element, const size_t index) {
if (index > size_) {
throw std::out_of_range("Index out of range");
}
if (index == size_) {
append(element);
}
if (size_ + 1 < capacity_) {
insert_element(element, index);
return;
}
resize_table();
insert_element(element, index);
}
// Extend
void extend(const list<T>& other) {
if (this == &other) {
return;
}
if (size_ + other.size() > capacity_) {
resize_table(other.size());
}
std::copy(other.data, other.data + other.size(), data + size_);
size_ += other.size();
}
// Remove item
void remove(const T element) {
if (size_ == 0) {
return;
}
auto it = std::find(begin(), end(), element);
if (it == end()) {
return;
}
const size_t index = std::distance(begin(), it);
pop(index);
}
// Count
size_t count(const T element) {
size_t count = 0;
for (const T& e : *this) {
if (e == element) {
count++;
}
}
return count;
}
// Reverse
void reverse() {
std::reverse(begin(), end());
}
// Pop
std::optional<T> pop () {
if (size_ == 0) {
return std::nullopt;
}
resize_table_minder();
size_--;
return data[size_];
}
T pop(const size_t index) {
if (index >= size_) {
throw std::out_of_range("Index out of range");
}
resize_table_minder();
T element = data[index];
std::move(data + index + 1, data + size_, data + index);
size_--;
return element;
}
// At
const T& at(const size_t index) const {
if (index >= size_) {
throw std::out_of_range("Index out of range");
}
return data[index];
}
// Operator surcharge
T& operator[](const size_t index) {
if (index >= size_) {
throw std::out_of_range("Index out of range");
}
return data[index];
}
const T& operator[](const size_t index) const {
if (index >= size_) {
throw std::out_of_range("Index out of range");
}
return data[index];
}
friend std::ostream& operator<<(std::ostream& os, list<T>& elements) {
os << "{";
for (auto it = elements.begin(); it != elements.end(); ++it) {
os << *it;
if (std::next(it) != elements.end()) {
os << ", ";
}
}
os << "}";
return os;
}
// Iterator
class iterator {
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
explicit iterator(pointer ptr) : ptr_(ptr) {}
reference operator*() const { return *ptr_; }
pointer operator->() { return ptr_; }
iterator& operator++() { ++ptr_; return *this; }
iterator operator++(int) { iterator tmp = *this; ++(*this); return tmp; }
iterator& operator--() { --ptr_; return *this; }
iterator operator--(int) { iterator tmp = *this; --(*this); return tmp; }
iterator operator+(difference_type n) const { return iterator(ptr_ + n); }
iterator& operator+=(difference_type n) { ptr_ += n; return *this; }
iterator operator-(difference_type n) const { return iterator(ptr_ - n); }
iterator& operator-=(difference_type n) { ptr_ -= n; return *this; }
difference_type operator-(const iterator& other) const { return ptr_ - other.ptr_; }
reference operator[](difference_type n) const { return ptr_[n]; }
bool operator==(const iterator& other) const { return ptr_ == other.ptr_; }
bool operator!=(const iterator& other) const { return ptr_ != other.ptr_; }
bool operator<(const iterator& other) const { return ptr_ < other.ptr_; }
bool operator<=(const iterator& other) const { return ptr_ <= other.ptr_; }
bool operator>(const iterator& other) const { return ptr_ > other.ptr_; }
bool operator>=(const iterator& other) const { return ptr_ >= other.ptr_; }
private:
pointer ptr_;
};
class const_iterator : public iterator {
using iterator::iterator;
const T& operator*() const { return *iterator::ptr_; }
const T* operator->() const { return iterator::ptr_; }
};
iterator begin() { return iterator(data); }
iterator end() { return iterator(data + size_); }
const_iterator cbegin() const { return const_iterator(data); }
const_iterator cend() const { return const_iterator(data + size_); }
};
}
#endif //LIST_H