-
Notifications
You must be signed in to change notification settings - Fork 0
/
CArray.hpp
234 lines (206 loc) · 3.87 KB
/
CArray.hpp
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
#ifndef CArrayHPP
#define CArrayHPP
#include <iostream>
#include <stdio.h>
#include "CArrayException.h"
template <typename TData>
arr::CArray<TData>::CArray()
: array(0) , arr_size(0u) , capacity(0u)
{
}
template <typename TData>
arr::CArray<TData>::CArray(
const CArray& _rhs
) : array(0) , arr_size(_rhs.arr_size) , capacity(_rhs.capacity)
{
array = new TData[_rhs.capacity];
memcpy(array, _rhs.array, _rhs.arr_size * sizeof(TData));
}
template <typename TData>
arr::CArray<TData>::~CArray()
{
if (array)
{
delete[] array;
array = 0;
}
}
template <typename TData>
arr::CArray<TData>& arr::CArray<TData>::operator=(
const CArray& _rhs
)
{
if (this != &_rhs)
{
TData* source = array;
array = new TData[_rhs.capacity];
memcpy(array, _rhs.array, _rhs.arr_size * sizeof(TData));
delete source;
arr_size = _rhs.arr_size;
capacity = _rhs.capacity;
}
return *this;
}
template <typename TData>
void arr::CArray<TData>::print() const
{
std::cout << "Elements: |";
for (unsigned int i = 0; i < arr_size; i++)
{
std::cout << array[i] << "|";
}
std::cout << std::endl;
}
template <typename TData>
void arr::CArray<TData>::erase(
unsigned int _index
)
{
if (_index >= arr_size)
{
throw CArrayException("Attempt to erase out of range element");
}
if (_index < arr_size - 1)
{
memcpy(&array[_index], &array[_index + 1], (arr_size - 1 - _index) * sizeof(TData));
}
arr_size--;
}
template <typename TData>
void arr::CArray<TData>::insert(
unsigned int _index, const TData& _value
)
{
if (_index > arr_size)
{
throw CArrayException("Attempt to insert element out of range array");
}
if (capacity == arr_size)
{
realloc();
}
memcpy(&array[_index + 1], &array[_index], (arr_size - _index) * sizeof(TData));
array[_index] = _value;
arr_size++;
}
template <typename TData>
void arr::CArray<TData>::push_back(
const TData& _value
)
{
if (arr_size == capacity)
{
realloc();
}
array[arr_size] = _value;
arr_size++;
}
template <typename TData>
void arr::CArray<TData>::clear()
{
arr_size = 0u;
delete[] array;
array = 0;
}
template <typename TData>
void arr::CArray<TData>::sort()
{
quicksort(0, (int)(arr_size - 1));
}
template <typename TData>
void arr::CArray<TData>::eraseIf(
const AbstractPredicate<TData>& _predicate
)
{
unsigned int i = 0;
while (i < arr_size)
{
if (_predicate(array[i]))
{
erase(i);
}
else
{
i++;
}
}
}
template <typename TData>
unsigned int arr::CArray<TData>::size() const
{
return arr_size;
}
template <typename TData>
const TData& arr::CArray<TData>::operator[] (
unsigned int _index
) const
{
return getElement(_index);
}
template <typename TData>
TData& arr::CArray<TData>::operator[] (
unsigned int _index
)
{
return getElement(_index);
}
template <typename TData>
void arr::CArray<TData>::quicksort(
int _left, int _right
)
{
int i = _left;
int j = _right;
const TData mid = array[ (i + j) / 2 ];
while (i <= j)
{
while (array[i] < mid)
{
i++;
}
while (array[j] > mid)
{
j--;
}
if (i <= j)
{
const TData tmp = array[j];
array[j] = array[i];
array[i] = tmp;
i++;
j--;
}
}
if (_left < j)
{
quicksort(_left, j);
}
if (_right > i)
{
quicksort(i, _right);
}
}
template <typename TData>
void arr::CArray<TData>::realloc()
{
capacity = (capacity == 0) ? (1u) : (capacity * 2);
TData* buff = new TData[capacity];
if (array)
{
memcpy(buff, array, size() * sizeof(TData));
delete[] array;
}
array = buff;
}
template <typename TData>
TData& arr::CArray<TData>::getElement(
unsigned int _index
) const
{
if (arr_size == 0 || _index > arr_size - 1)
{
throw CArrayException("Attempt to access on out of range index");
}
return array[_index];
}
#endif