-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patharray_list.h
276 lines (234 loc) · 6.26 KB
/
array_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
#ifndef STRUCTURES_ARRAY_LIST_H
#define STRUCTURES_ARRAY_LIST_H
#include <cstdint>
#include <memory>
#include <stdexcept>
#include <traits.h>
#include <utils.h>
namespace structures {
/**
* @brief Implements a list(data structure), using arrays
*
* @tparam T Data type of the elements
*/
template <typename T>
class ArrayList {
public:
ArrayList() = default;
ArrayList(const ArrayList<T>& other)
: contents{copy_array(other.contents, other.size_, other.max_size_)}
, size_{other.size_}
, max_size_{other.max_size_} {}
ArrayList(ArrayList<T>&& other)
: contents{std::move(other.contents)}
, size_{std::move(other.size_)}
, max_size_{std::move(other.max_size_)} {
other.size_ = 0;
other.max_size_ = 0;
}
ArrayList<T>& operator=(const ArrayList<T>& other) {
ArrayList<T> copy{other};
std::swap(contents, copy.contents);
std::swap(size_, copy.size_);
std::swap(max_size_, copy.max_size_);
return *this;
}
ArrayList<T>& operator=(ArrayList<T>&& other) {
ArrayList<T> copy{std::move(other)};
std::swap(contents, copy.contents);
std::swap(size_, copy.size_);
std::swap(max_size_, copy.max_size_);
return *this;
}
virtual ~ArrayList() = default;
/**
* @brief Constructor with a given maximum size
*
* @param max_size The maximum size of the list
*/
explicit ArrayList(std::size_t max_size)
: contents{new T[max_size]}, max_size_{max_size} {}
/**
* @brief Clears the contents of the list
*/
void clear() { size_ = 0; }
/**
* @brief Adds 'data' to the end of the list
*
* @param data The element that'll be added
*/
void push_back(const T& data) { insert(data, size_); }
/**
* @brief Adds 'data' to the beginning of the list
*
* @param data The element that'll be added
*/
void push_front(const T& data) { insert(data, 0); }
/**
* @brief Inserts at a given position of the list
*
* @param data The element that'll be inserted
* @param index The position where 'data' will be inserted
*/
void insert(const T& data, std::size_t index) {
if (index > size_) {
throw std::out_of_range("Index out of bounds");
} else {
for (std::size_t i = size_; i > index; i--) {
contents[i] = contents[i - 1];
}
contents[index] = data;
size_++;
if (max_size_ == size_)
expand(2);
}
}
/**
* @brief Inserts the element sorted into the list
*
* @param data The element that'll be inserted
*/
void insert_sorted(const T& data) {
std::size_t i = 0;
while (i < size_ && data >= contents[i])
i++;
insert(data, i);
}
/**
* @brief Removes the element at the given position
*
* @param index The position of the element that'll be removed
*
* @return The element that was removed
*/
T erase(std::size_t index) {
if (empty()) {
throw std::out_of_range("List is empty");
} else if (index >= size_) {
throw std::out_of_range("Index out of bounds");
} else {
T deleted = contents[index];
for (std::size_t i = index; i < size_ - 1; ++i) {
contents[i] = contents[i + 1];
}
size_--;
if (max_size_ / 4 > size_)
expand(0.5);
return deleted;
}
}
/**
* @brief Removes the element at the end of the list
*
* @return The element that was removed
*/
T pop_back() { return erase(size_ - 1); }
/**
* @brief Removes the element at the beginning of the list
*
* @return The element that was removed
*/
T pop_front() { return erase(0); }
/**
* @brief Removes 'data' from the list
*
* @param data The element that'll be removed
*/
void remove(const T& data) { erase(find(data)); }
/**
* @brief Checks if the list is empty
*
* @return True if the list is empty
*/
bool empty() const { return size_ == 0; }
/**
* @brief Checks if the list contains an element(data)
*
* @param data The element that'll be checked if it is contained by the list
*
* @return True if the list contains 'data'
*/
bool contains(const T& data) const { return find(data) != size_; }
/**
* @brief Returns the position of 'data' on the list
*
* @param data The element that'll be searched
*
* @return The position of 'data' on the list
*/
std::size_t find(const T& data) const {
for (std::size_t i = 0; i < size_; ++i) {
if (contents[i] == data)
return i;
}
return size_;
}
/**
* @brief Size of the list
*
* @return Size of the list
*/
std::size_t size() const { return size_; }
/**
* @brief Checks if the index is valid, then returns a reference to the
* element at the given index of the list
*
* @param index The index on the list of the element that'll be returned
*
* @return A reference to the element at the given index
*/
T& at(std::size_t index) {
return const_cast<T&>(static_cast<const ArrayList*>(this)->at(index));
}
const T& at(std::size_t index) const {
if (index >= size_) {
throw std::out_of_range("Index out of bounds");
} else {
return contents[index];
}
}
/**
* @brief Overloads the operator '[]'
* @details Returns a reference to the element at 'index' position of the
* list
*
* @param index The index on the list of the element that'll be returned
*
* @return A reference to the element at the given index
*/
T& operator[](std::size_t index) {
return const_cast<T&>(
static_cast<const ArrayList*>(this)->operator[](index));
}
const T& operator[](std::size_t index) const { return contents[index]; }
T& front() { return contents[0]; }
const T& front() const { return contents[0]; }
T& back() { return contents[size_ - 1]; }
const T& back() const { return contents[size_ - 1]; }
private:
void expand(float ratio) {
contents = copy_array(contents, size_, max_size_ * ratio);
max_size_ *= ratio;
}
static std::unique_ptr<T[]> copy_array(
const std::unique_ptr<T[]>& original, std::size_t size,
std::size_t new_size) {
std::unique_ptr<T[]> copy{new T[new_size]};
for (std::size_t i = 0; i < size; i++) {
copy[i] = original[i];
}
return copy;
}
const static std::size_t starting_size{8};
std::unique_ptr<T[]> contents = make_unique<T[]>(starting_size);
std::size_t size_{0u};
std::size_t max_size_{starting_size};
};
} // namespace structures
/* list trait */
template <>
const bool traits::is_list<structures::ArrayList>::value = true;
/* name trait */
template <>
const std::string traits::type<structures::ArrayList>::name = "ArrayList";
#endif