-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
262 lines (230 loc) · 5.4 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
256
257
258
259
260
261
262
#pragma once
#define DEFAULT_CAPACITY 4
template<typename T>
class Vector {
protected:
int _capacity;
int _size;
T* _elem;
void copyFrom(T const* _elem, int left, int right);
void expand();
void shrink();
public:
Vector(int c = DEFAULT_CAPACITY, int s = 0, T v = 0) { //默认构造函数
_capacity = c;
_size = s;
_elem = new T[_capacity];
for (int i = 0; i < _size; i++) {
_elem[i] = v;
}
}
Vector(Vector<T> const& A) { //复制构造函数
copyFrom(A._elem, 0, A._size);
}
Vector(T* A, int n) { //数组赋值构造函数
copyFrom(A, 0, n);
}
~Vector() { delete[]_elem; } //析构函数
int size() { return _size; }
bool empty() { return !_size; }
T& operator[](int r) const; //[]的重载
Vector<T>& operator=(Vector<T> const& A); //=的重载
int find(T const& e, int left, int right) const; //无序向量的区间查找
int find(T const& e) const { //无序向量的全局查找
return find(e, 0, _size);
}
int disordered() const; //判断是否有逆序对
int insert(int pos, T const& e); //插入给定值到给定位置,返回插入位置
int remove(int left, int right); //删除给定区间的元素,返回删除个数
int remove(int r); //删除给定位置的元素,返回被删除的值
int deduplicate(); //唯一化向量,返回被删元素总数
int binSearch(T const& e, int left, int right); //部分区间二分查找,查找成功返回位置,查找失败返回-1
int uniquify(); //有序向量唯一化,返回被删元素总数
void unsort(int left, int right); //向量区间的打乱
template<typename VST> //遍历操作
void traverse(VST& vistit);
bool bubble(int left, int right); //从左到右的一次起泡
void bubbleSort(int left, int right) { //起泡排序
while (!bubble(left, right--));
}
void merge(int left, int mid, int right);//一次归并
void mergeSort(int left, int right); //归并排序
};
template<typename T>
void Vector<T>::expand() {
if (_size < _capacity) return;
if (_capacity < DEFAULT_CAPACITY) _capacity = DEFAULT_CAPACITY;
T* oldelem = _elem;
delete[]_elem;
_capacity *= 2;
_elem = new T[_capacity];
for (int i = 0; i < _size; i++) {
_elem[i] = oldelem[i];
}
delete[]oldelem;
}
template<typename T>
void Vector<T>::shrink() {
if (_capacity < DEFAULT_CAPACITY * 2) return;
if (_size * 4 > _capacity) return;
T* oldelem = _elem;
_elem = new T[_capacity / 2];
for (int i = 0; i < _size; i++) {
_elem[i] = oldelem[i];
}
delete[]oldelem;
}
template<typename T>
void Vector<T>::copyFrom(T const* V, int left, int right) {
_size = right - left;
_capacity = 2 * _size;
_elem = new T[_capacity];
for (int i = 0; i < _size; i++) {
_elem[i] = V[i];
}
}
template<typename T>
T& Vector<T>::operator[](int r) const {
return *(_elem + r);
}
template<typename T>
Vector<T>& Vector<T>::operator=(Vector<T> const& A) {
if (_elem) delete[]_elem;
copyFrom(A._elem, 0, A._size);
return *this;
}
template<typename T>
int Vector<T>::find(T const& e, int left, int right) const {
int r = right;
while ((left < r--) && (e != _elem[r]));
if (r < left)
return -1;
else
return r;
}
template<typename T>
int Vector<T>::disordered() const {
int n = 0;
for (int i = 1; i < _size; i++) {
if (_elem[i - 1] > _elem[i])
n++;
}
return n;
}
template<typename T>
int Vector<T>::insert(int pos, T const& e) {
expand();
for (int i = _size; i > pos; i--) {
_elem[i] = _elem[i - 1];
}
_elem[pos] = e;
_size++;
return pos;
}
template<typename T>
int Vector<T>::remove(int left, int right) {
if (left == right)
return 0;
while (right < _size) {
_elem[left] = _elem[right];
left++;
right++;
}
_size = right;
shrink();
return right - left;
}
template<typename T>
int Vector<T>::remove(int r) {
T e = _elem[r];
remove(r, r + 1);
return e;
}
template<typename T>
int Vector<T>::deduplicate() {
int oldSize = _size;
int i = 1;
while (i < (_size - 1)) {
if (find(_elem[i], 0, i) < 0)
i++;
else
remove(i);
}
return oldSize - _size;
}
template<typename T>
int Vector<T>::binSearch(T const& e, int left, int right) {
while (left < right) {
int mid = left + (right - left) / 2;
if (e < _elem[mid])
right = mid;
else if (e > _elem[mid])
left = mid + 1;
else
return mid;
}
return -1;
}
template<typename T>
int Vector<T>::uniquify() {
int i = 0;
int j = 0;
while (++j < _size) {
if (_elem[i] != _elem[j]) {
_elem[++i] = _elem[j];
}
}
_size = ++i;
shrink();
return j - i;
}
template<typename T>
void Vector<T>::unsort(int left, int right) {
T* v = _elem + left;
for (int i = right - left; i > 0; i--) {
swap(v[i], v[rand() % i]);
}
}
template<typename T> template<typename VST>
void Vector<T>::traverse(VST& visit) {
for (int i = 0; i < _size; i++) {
visit(_elem[i]);
}
}
template<typename T>
bool Vector<T>::bubble(int left, int right) {
bool sorted = true;
while (++left < right)
if (_elem[left - 1] > _elem[left]) {
sorted = false;
T tmp = _elem[left - 1];
_elem[left - 1] = _elem[left];
_elem[left] = tmp;
}
return sorted;
}
template<typename T>
void Vector<T>::mergeSort(int left, int right) {
if (right - left < 2)
return;
int mid = (left + right) / 2;
mergeSort(left, mid);
mergeSort(mid, right);
merge(left, mid, right);
}
template<typename T>
void Vector<T>::merge(int left, int mid, int right) {
T* A = _elem + left;
int l = mid - left;
int r = right - mid;
T* L = new T[l];
for (int i = 0; i < l; L[i] = A[i++]);
T* R = _elem + mid;
for (int i = 0, j = 0, k = 0; (j < l) || (k < r);) {
if ((j < l) && (!(k < r) || (L[j] <= R[k])))
A[i++] = L[j++];
else if ((k < r) && (!(j < l) || (R[k] < L[j])))
A[i++] = R[k++];
}
delete[]L;
}