-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
229 lines (187 loc) · 3.88 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
#pragma once
#include <stdlib.h>
#include "Memory.h"
#include "Algorithm.h"
template <class T>
class Vector
{
private:
//Raw data
T* data;
//Number of items stored
size_t count;
//Maximum number of objects which can be stored before reallocating
size_t capacity;
public:
Vector()
{
capacity = 0;
count = 0;
data = NULL;
}
explicit Vector(size_t capacity)
{
this->capacity = capacity;
count = 0;
data = new T[capacity];
}
template <size_t N>
explicit Vector(T (&arr)[N])
{
count = N;
capacity = N;
data = new T[capacity];
for(size_t i = 0; i < count; i++)
{
data[i] = arr[i];
}
}
Vector(const Vector<T>& other)
{
count = other.count;
capacity = other.capacity;
data = new T[capacity];
for(size_t i = 0; i < count; i++)
{
data[i] = other[i];
}
}
Vector& operator=(const Vector<T>& other)
{
data = Memory::realloc(data, capacity, other.capacity);
capacity = other.capacity;
count = other.count;
for(size_t i = 0; i < count; i++)
{
data[i] = other[i];
}
return *this;
}
~Vector()
{
delete[] data;
}
void push(const T& e)
{
if(count >= capacity)
{
size_t oldCapacity = capacity;
capacity *= 2;
data = Memory::realloc(data, oldCapacity, capacity);
}
data[count++] = e;
}
void push(const Vector<T>& v)
{
for(size_t i = 0; i < v.getSize(); i++)
{
push(v[i]);
}
}
T& operator[](size_t index)
{
return data[index];
}
const T& operator[](size_t index) const
{
return data[index];
}
size_t getSize() const
{
return count;
}
/**
* Sorts vector using mergesort
* @tparam Compare Function object which can be called in the form bool(*compare)(const T& a, const T& b)
* @param compare Returns true if A goes before than B: A <= B
*/
template <class Compare>
void sort(Compare compare)
{
mergeSort(0, count - 1, compare);
}
void sort()
{
mergeSort(0, count - 1, Algorithm::less<T>());
}
protected:
/**
* Merges two sorted subarrays into a sorted array
* First subarray is [l..m]
* Second subarray is [m+1..r]
* @tparam Compare Function object which can be called in the form bool(*compare)(const T& a, const T& b)
* @param l Left boundary of first array
* @param m Right boundary of first array and left boundary of second array
* @param r Right boundary of second array
* @param compare Returns true if A goes before than B: A <= B
*/
template<class Compare>
void merge(size_t l, size_t m, size_t r, Compare compare)
{
//Array sizes
size_t n1 = m - l + 1;
size_t n2 = r - m;
//Temp arrays
Vector<T> L(n1), R(n2);
for(size_t i = 0; i < n1; i++)
{
L.push(data[l + i]);
}
for(size_t i = 0; i < n2; i++)
{
R.push(data[m + 1 + i]);
}
//Merge temporal arrays bach into arr[l..r]
size_t i = 0; //Initial index of first subarray
size_t j = 0; //Initial index of second subarray
size_t k = l; //Initial index of merged subarray
while(i < n1 && j < n2)
{
if(compare(L[i], R[j]))
{
data[k] = L[i];
i++;
}
else
{
data[k] = R[j];
j++;
}
k++;
}
//Copy the remaining elements of L if there are any
while(i < n1)
{
data[k] = L[i];
i++;
k++;
}
//Copy the remaining elements of R if there are any
while(j < n2)
{
data[k] = R[j];
j++;
k++;
}
}
/**
* Sorts using mergesort an array [l..r]
* @tparam Compare Function object which can be called in the form bool(*compare)(const T& a, const T& b)
* @param l Left boundary of the array
* @param r Right boundary of the array
* @param compare Returns true if A goes before than B: A <= B
*/
template <class Compare>
void mergeSort(size_t l, size_t r, Compare compare)
{
if(l < r)
{
//Same thing as (l+r)/2, but avoids overflow for large l and h
size_t m = l + (r - l) / 2;
//Sort first and second array halves
mergeSort(l, m, compare);
mergeSort(m + 1, r, compare);
merge(l, m, r, compare);
}
}
};