-
Notifications
You must be signed in to change notification settings - Fork 1
/
GCP_Vector.h
259 lines (219 loc) · 4.38 KB
/
GCP_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
#ifndef GCP_VectorH
#define GCP_VectorH
namespace gcp
{
template< class T>
class GCP_Vector
{
protected:
T* _mElems;
unsigned int _mSize;
unsigned int _iSize;
int _startsize;
int _mulsize;
void initValues()
{
_startsize = 4;
_mulsize = 2;
_mSize = _startsize;
_iSize = 0;
_mElems = new T[_mSize];
}
void resize(int newSize){
T* newElems = new T[newSize];
for (unsigned int i = 0; i < _iSize; i++)
newElems[i] = _mElems[i];
_mSize = newSize;
delete[]_mElems;
_mElems = newElems;
}
void quickSort(GCP_Vector<T> *a, int l, int r)
{
if (a->size() < 2)
return;
T x = a->at(l + (r - l) / 2); //(l+r)/2,
int i = l;
int j = r;
while (i <= j)
{
while (a->compareAltB(a->at(i), x)) i++; //a(i) < x
while (a->compareAgtB(a->at(j), x)) j--; //a(j) > x
if (i <= j)
{
a->swap(i, j);
i++;
j--;
}
}
if (i < r)
quickSort(a, i, r);
if (l < j)
quickSort(a, l, j);
}
public:
GCP_Vector() {
initValues();
}
GCP_Vector (GCP_Vector const &rhs)
{
initValues();
clear();
int size = rhs.size();
for (int i = 0; i < size; i++)
push_back(rhs.at(i));
}
~GCP_Vector() {
delete[]_mElems;
}
int find(T element)
{
for (size_t i = 0; i < _iSize; i++)
if (_mElems[i] == element)
return i;
return -1;
}
void push_back(T element) {
//cause SP to trouble if copying instance is another vector!!!
_mElems[_iSize] = element;
_iSize++;
if (_iSize >= _mSize)
resize(_mSize*_mulsize);
}
void slectDistinct()
{
size_t k = 0;
int *toDelete = new int[_iSize];
for (unsigned int i = 0; i < _iSize; i++)
{
for (unsigned int j = i + 1; j < _iSize; j++)
{
if (_mElems[j] == _mElems[i])
{
toDelete[k] = j;
k++;
}
}
}
//!REV
for (size_t i = 0; i < k; i++)
{
erase(toDelete[i]);
for (size_t j = i + 1; j < k; j++)
{
if (toDelete[j] > toDelete[i])
toDelete[j]--;
}
}
delete toDelete;
}
void qsort()
{
quickSort(this, 0, size() - 1);
}
bool compareAltB(T a, T b)
{
if (a < b)
return true;
return false;
}
bool compareAgtB(T a, T b)
{
if (a > b)
return true;
return false;
}
T pop_back(){
_iSize--;
return _mElems[_iSize];
}
T pop_first(){
T value = _mElems[0];
for (unsigned int i = 0; i < _iSize - 1; i++)
_mElems[i] = _mElems[i + 1];
_iSize--;
return value;
}
bool has_value(T val)
{
for (unsigned int i = 0; i < _iSize; i++)
if (_mElems[i] == val)
return true;
return false;
}
void erase(unsigned int _index) {
if (_index >= _iSize)
return;
//memory loss? do like resize operation
//for (unsigned int i = _index; i < _iSize - 1; i++)
// _mElems[i] = _mElems[i + 1];
T* newElems = new T[_mSize];
for (unsigned int i = 0; i < _index; i++)
newElems[i] = _mElems[i];
for (unsigned int i = _index+1; i < _iSize; i++)
newElems[i-1] = _mElems[i];
delete[]_mElems;
_mElems = newElems;
_iSize--;
}
unsigned int size() const
{
return _iSize;
}
void clear()
{
_iSize = 0;
_mSize = _startsize;
delete[]_mElems;
_mElems = new T[_mSize];
}
T& at(unsigned int x) const
{
if (x >= _iSize)
{
//assert(!"Value out of range!");
x = _iSize - 1;
}
return (_mElems[x]);
}
T& operator[](unsigned int x){
if (x >= _iSize)
{
//assert(!"Value out of range!");
x = _iSize-1;
}
return (_mElems[x]);
}
GCP_Vector& operator=(GCP_Vector const &rhs)
{
if (this != &rhs)
{
clear();
for (size_t i = 0; i < rhs.size(); i++)
{
push_back(rhs.at(i));
}
}
return *this;
}
void eat(GCP_Vector *rhs)
{
clear();
int size = rhs->size();
for (int i = 0; i < size; i++)
{
push_back(rhs->pop_first());
}
}
void swap(unsigned int i, unsigned int j)
{
if (i >= _iSize || j >= _iSize)
return;
T x = _mElems[i];
_mElems[i] = _mElems[j];
_mElems[j] = x;
}
};
typedef GCP_Vector<int> GCP_VectorInt;
typedef GCP_Vector<double> GCP_VectorDouble;
}
#endif