-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
255 lines (241 loc) · 6.27 KB
/
vector.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
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
#include <initializer_list>
#include <cstdlib>
#include <limits>
#include <vector>
template <class T, class... Args>
class myVector //Full vector implementation, except for operator invalidation
{
private:
T* arr; // pointer to array
int size; // size of vector
int capacity; // capacity of vector
public:
// constructors
myVector() : arr(new T[1]), size(0), capacity(1) {}; // default constructor
myVector(int size) : arr(new T[size]), size(size), capacity(size) {}; // constructor with size
myVector(std::initializer_list<T> init) : size(init.size()), capacity(init.size()) // constructor with initializer list
{
arr = new T[capacity];
int i = 0;
for (auto it = init.begin(); it != init.end(); ++it)
{
arr[i++] = *it;
}
}
template<typename InputIt>
myVector(InputIt first, InputIt last) : myVector(std::distance(first, last)) // constructor with iterators
{
std::copy(first, last, arr);
}
myVector(const myVector<T>& other) : myVector(other.size) // copy constructor
{
std::copy(other.arr, other.arr + other.size, arr);
}
~myVector() // destructor
{
// for (int i = 0; i < size; i++)
// {
// for(int i = 0; i < size; i++)
// {
// delete arr[i];
// }
// }
delete[] arr;
}
// member functions
void clear() // clears vector, sets size to 0, capacity to 1, deletes array and creates new array with capacity 1
{
size = 0;
capacity = 1;
delete[] arr;
arr = new T[1];
}
void print() // prints vector size, capacity and elements
{
std::cout << "Size: " << size << ", Capacity: " << capacity << ", Elements: ";
for (int i = 0; i < size; i++)
{
std::cout << arr[i] << " ";
}
std::cout << "\n" << std::endl;
}
void resize(int newSize) // resizes vector to newSize, fills new elements with default value
{
if (newSize < 0)
{
throw std::out_of_range("New size cannot be negative");
}
if(newSize > capacity)
{
reserve(newSize);
}
if(newSize > size)
{
for (int i = size; i < newSize; i++)
{
arr[i] = T();
}
}
size = newSize;
}
void push_back(T data) // adds element to the end of vector
{
if(size == capacity)
{
reserve(2 * capacity);
}
arr[size] = data;
size++;
}
bool empty() // checks if vector is empty
{
return size == 0;
}
void pop_back() // removes last element
{
if(size > 0)
{
size--;
}
}
void reserve(int newCapacity) // reserves newCapacity for vector
{
T* temp = new T[newCapacity];
for (int i = 0; i < size; i++)
{
temp[i] = arr[i];
}
delete[] arr;
arr = temp;
capacity = newCapacity;
}
void shrink_to_fit() // reduces capacity to size
{
if(size < capacity)
{
T* temp = new T[size];
for (int i = 0; i < size; i++)
{
temp[i] = arr[i];
}
delete[] arr;
arr = temp;
capacity = size;
}
}
void insert(T* position, const T& val) // inserts val before position
{
// std::cout << "Inserting value " << val << " at position " << (position - arr) << std::endl;
if (position < arr || position > arr + size)
{
throw std::out_of_range("Position out of range");
}
if (size == capacity)
{
T* old_arr = arr; // save pointer to old array
reserve(2 * capacity);
position = arr + (position - old_arr); // adjust position relative to new array
}
for (T* i = arr + size; i > position; i--)
{
*i = *(i - 1);
}
*position = val;
size++;
}
template <typename... TArgs>
void emplace_back(TArgs&&... args) // constructs element at the end of vector
{
if (size == capacity)
{
reserve(2 * capacity);
}
new (arr + size) T(std::forward<TArgs>(args)...);
size++;
}
void erase(T* position) // removes element at position
{
if (position < arr || position >= arr + size)
{
throw std::out_of_range("Position out of range");
}
for (T* i = position; i < arr + size - 1; i++)
{
*i = *(i + 1);
}
size--;
}
void swap(myVector<T>& other) // swaps contents of two vectors
{
std::swap(this->arr, other.arr);
std::swap(this->size, other.size);
std::swap(this->capacity, other.capacity);
}
//element access
T& at(int index) // returns element at index, throws out_of_range exception if index is out of range
{
if(index < 0 || index >= size)
{
throw std::out_of_range("Index out of range");
}
return arr[index];
}
T& operator[](int index) // overloads [] operator, returns element at index
{
return arr[index];
}
T& front() // returns first element
{
if (size == 0)
{
throw std::out_of_range("Vector is empty");
}
return arr[0];
}
T& back() // returns last element
{
if (size == 0)
{
throw std::out_of_range("Vector is empty");
}
return arr[size - 1];
}
T* data() // returns pointer to array
{
return arr;
}
//iterators
T* begin() // returns pointer to first element
{
return arr;
}
T* end() // returns pointer to one past last element
{
return arr + size;
}
T* rbegin()
{
return arr + size - 1;
}
T* rend()
{
return arr - 1;
}
// getters
int getSize()
{
return size;
}
int getCapacity()
{
return capacity;
}
size_t max_size()
{
return std::numeric_limits<size_t>::max();
}
};
#endif