-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDynArray.h
451 lines (397 loc) · 8.88 KB
/
DynArray.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Author : XuBenHao
// Version : 1.0.0
// Mail : xbh370970843@163.com
// Copyright : XuBenHao 2020 - 2030
#ifndef ALLIB_DATASTRUCT_ARRAY_DYNARRAY_H
#define ALLIB_DATASTRUCT_ARRAY_DYNARRAY_H
#include "..\..\stdafx.h"
#include "..\..\Algorithm\Sort\Sort.h"
// svn
namespace AlLib
{
namespace DataStruct
{
namespace Array
{
// 突破固定容量数组限制
// 容量可以动态增加和减少的数组
// 适合作为管理动态元素集合的容器
template<typename T>
class DynArray
{
public:
DynArray();
DynArray(int nInitialSize_, const T& nInitialValue_);
virtual ~DynArray();
void Add(const T& value_);
void AddRange(const DynArray& arrItems_);
void Insert(int nIndex_, const T& value_);
void DeleteByIndex(int nIndex_ = -1);
void DeleteByValue(const T& value_);
void DeleteAll();
bool SetValue(int nIndex_, const T& value_);
bool GetValue(int nIndex_, T& value_) const;
int GetSize() const;
int GetCapacity() const;
int Find(std::function<bool(const T&)> fun_) const;
T& operator[](int nIndex_);
const T& operator[](int nIndex_) const;
DynArray(const DynArray<T>& arrElements_);
DynArray<T>& operator=(const DynArray<T>& arrElements_);
DynArray Sort(std::function<int(const T&, const T&)> fun_ = [](const T& nT1_, const T& nT2_)->int
{
if (nT1_ < nT2_)
{
return -1;
}
else if (nT1_ > nT2_)
{
return 1;
}
else
{
return 0;
}
}) const;
void Sort(std::function<int(const T&, const T&)> fun_ = [](const T& nT1_, const T& nT2_)->int
{
if (nT1_ < nT2_)
{
return -1;
}
else if (nT1_ > nT2_)
{
return 1;
}
else
{
return 0;
}
});
void Reset();
private:
void Check(int nSize_);
void Shrink();
private:
T* m_pSource;// 当T为引用类型时,sizeof无法获得其真实大小
int m_nSize;
int m_nCapacity;
std::allocator<T> m_alloc;
};
template<typename T>
DynArray<T>::DynArray(const DynArray<T>& arrElements_)
: m_pSource(nullptr), m_nSize(0), m_nCapacity(0)
{
m_pSource = m_alloc.allocate(arrElements_.m_nCapacity);
if (m_pSource == nullptr)
{
throw "out of memory";
}
else
{
m_nSize = arrElements_.m_nSize;
m_nCapacity = arrElements_.m_nCapacity;
std::uninitialized_copy_n(arrElements_.m_pSource, arrElements_.m_nSize, m_pSource);
}
}
template<typename T>
DynArray<T>& DynArray<T>::operator=(const DynArray<T>& arrElements_)
{
if (&arrElements_ == this)
{
return *this;
}
this->~DynArray();
m_pSource = m_alloc.allocate(arrElements_.m_nCapacity);
if (m_pSource == nullptr)
{
throw "out of memory";
}
else
{
m_nSize = arrElements_.m_nSize;
m_nCapacity = arrElements_.m_nCapacity;
std::uninitialized_copy_n(arrElements_.m_pSource, arrElements_.m_nSize, m_pSource);
}
return *this;
}
template<typename T>
int DynArray<T>::Find(std::function<bool(const T&)> fun_) const
{
int _nPosIndex = -1;
for (int _i = 0; _i < m_nSize; _i++)
{
if (fun_(*(m_pSource + _i)))
{
_nPosIndex = _i;
break;
}
}
return _nPosIndex;
}
template<typename T>
DynArray<T>::DynArray()
:m_pSource(nullptr), m_nSize(0), m_nCapacity(0)
{
m_pSource = m_alloc.allocate(100);
if (m_pSource == nullptr)
{
throw "out of memory";
}
else
{
m_nSize = 0;
m_nCapacity = 100;
}
}
template<typename T>
DynArray<T>::DynArray(int nInitialSize_, const T& nInitialValue_)
:m_pSource(nullptr), m_nSize(0), m_nCapacity(0)
{
m_pSource = m_alloc.allocate(nInitialSize_ * 2);
if (m_pSource == nullptr)
{
throw "out of memory";
}
else
{
m_nSize = nInitialSize_;
m_nCapacity = nInitialSize_ * 2;
std::uninitialized_fill_n(m_pSource, m_nSize, nInitialValue_);
}
}
template<typename T>
void DynArray<T>::Reset()
{
this->~DynArray();
m_pSource = m_alloc.allocate(100);
if (m_pSource == nullptr)
{
throw "out of memory";
}
else
{
m_nSize = 0;
m_nCapacity = 100;
}
}
template<typename T>
DynArray<T>::~DynArray()
{
if (m_pSource == nullptr)
{
return;
}
T* _pEnd = m_pSource + m_nSize;
while (_pEnd != m_pSource)
{
m_alloc.destroy(--_pEnd);
}
// 内存释放
m_alloc.deallocate(m_pSource, m_nCapacity);
m_pSource = nullptr;
m_nSize = 0;
m_nCapacity = 0;
}
template<typename T>
void DynArray<T>::Add(const T& value_)
{
Check(m_nSize + 1);
T* _pEnd = m_pSource + m_nSize;
m_alloc.construct(_pEnd, value_);
m_nSize++;
}
template<typename T>
void DynArray<T>::AddRange(const DynArray& arrItems_)
{
Check(m_nSize + arrItems_.GetSize());
for (int _i = 0; _i < arrItems_.m_nSize; _i++)
{
m_alloc.construct(m_pSource + m_nSize + _i, *(arrItems_.m_pSource + _i));
}
m_nSize += arrItems_.m_nSize;
}
template<typename T>
void DynArray<T>::Check(int nSize_)
{
if (m_nCapacity >= nSize_)
{
return;
}
int _nSize = m_nSize;
int _nCapacity = nSize_ * 2;
T* _pSource = m_alloc.allocate(_nCapacity);
if (_pSource == nullptr)
{
throw "out of memory";
}
else
{
std::uninitialized_copy_n(m_pSource, m_nSize, _pSource);
this->~DynArray();
m_pSource = _pSource;
m_nSize = _nSize;
m_nCapacity = _nCapacity;
}
}
template<typename T>
void DynArray<T>::Insert(int nIndex_, const T& value_)
{
if (nIndex_ > m_nSize)
{
throw "Insert position error";
}
Check(m_nSize + 1);
m_alloc.construct(m_pSource + m_nSize, value_);
for (int _i = m_nSize - 1; _i >= nIndex_; _i--)
{
*(m_pSource + _i + 1) = *(m_pSource + _i);
}
*(m_pSource + nIndex_) = value_;
m_nSize++;
}
template<typename T>
void DynArray<T>::DeleteByIndex(int nIndex_)
{
if (nIndex_ < 0
|| nIndex_ >= m_nSize)
{
return;
}
// 前移
for (int _i = nIndex_ + 1; _i < m_nSize; _i++)
{
*(m_pSource + _i - 1) = *(m_pSource + _i);
}
m_alloc.destroy(m_pSource + m_nSize - 1);
m_nSize--;
if (m_nSize <= m_nCapacity / 4)
{
Shrink();
}
}
template<typename T>
void DynArray<T>::DeleteByValue(const T& value_)
{
int _nIndex = Find([value_](const T& nT_)->bool
{
if (nT_ == value_)
{
return true;
}
else
{
return false;
}
});
if (_nIndex == -1)
{
return;
}
DeleteByIndex(_nIndex);
}
template<typename T>
void DynArray<T>::Shrink()
{
int _nSize = m_nSize;
int _nCapacity = (m_nCapacity / 2) > 100 ? (m_nCapacity / 2) : 100;
T* _pSource = m_alloc.allocate(_nCapacity);
if (_pSource == nullptr)
{
throw "out of memory";
}
else
{
std::uninitialized_copy_n(m_pSource, m_nSize, _pSource);
this->~DynArray();
m_pSource = _pSource;
m_nSize = _nSize;
m_nCapacity = _nCapacity;
}
}
template<typename T>
void DynArray<T>::DeleteAll()
{
Reset();
}
template<typename T>
bool DynArray<T>::SetValue(int nIndex_, const T& value_)
{
if (nIndex_ < 0
|| nIndex_ >= m_nSize)
{
return false;
}
*(m_pSource + nIndex_) = value_;
return true;
}
template<typename T>
bool DynArray<T>::GetValue(int nIndex_, T& value_) const
{
if (nIndex_ < 0
|| nIndex_ >= m_nSize)
{
return false;
}
value_ = *(m_pSource + nIndex_);
return true;
}
template<typename T>
int DynArray<T>::GetSize() const
{
return m_nSize;
}
template<typename T>
int DynArray<T>::GetCapacity() const
{
return m_nCapacity;
}
template<typename T>
T& DynArray<T>::operator[](int nIndex_)
{
if (nIndex_ < 0
|| nIndex_ >= m_nSize)
{
throw "index is error";
}
return *(m_pSource + nIndex_);
}
template<typename T>
const T& DynArray<T>::operator[](int nIndex_) const
{
if (nIndex_ < 0
|| nIndex_ >= m_nSize)
{
throw "index is error";
}
return *(m_pSource + nIndex_);
}
template<typename T>
typename DynArray<T> DynArray<T>::Sort(std::function<int(const T&, const T&)> fun_) const
{
DynArray _arr = *this;
if (m_nSize <= 1)
{
return _arr;
}
Algorithm::Sort::Helper::QuickSort<T>(_arr.m_pSource,
_arr.m_pSource + m_nSize,
fun_)
return _arr;
}
template<typename T>
void DynArray<T>::Sort(std::function<int(const T&, const T&)> fun_)
{
if (m_nSize <= 1)
{
return;
}
Algorithm::Sort::Helper::QuickSort<T>(m_pSource,
m_pSource + m_nSize,
fun_);
return;
}
}
}
}
#endif