|
1 // Common/Vector.cpp |
|
2 |
|
3 #include "StdAfx.h" |
|
4 |
|
5 #include <string.h> |
|
6 |
|
7 #include "Vector.h" |
|
8 |
|
9 CBaseRecordVector::~CBaseRecordVector() |
|
10 { delete []((unsigned char *)_items); } |
|
11 void CBaseRecordVector::Clear() |
|
12 { DeleteFrom(0); } |
|
13 void CBaseRecordVector::DeleteBack() |
|
14 { Delete(_size - 1); } |
|
15 void CBaseRecordVector::DeleteFrom(int index) |
|
16 { Delete(index, _size - index); } |
|
17 |
|
18 void CBaseRecordVector::ReserveOnePosition() |
|
19 { |
|
20 if(_size != _capacity) |
|
21 return; |
|
22 int delta; |
|
23 if (_capacity > 64) |
|
24 delta = _capacity / 2; |
|
25 else if (_capacity > 8) |
|
26 delta = 8; |
|
27 else |
|
28 delta = 4; |
|
29 Reserve(_capacity + delta); |
|
30 } |
|
31 |
|
32 void CBaseRecordVector::Reserve(int newCapacity) |
|
33 { |
|
34 if(newCapacity <= _capacity) |
|
35 return; |
|
36 /* |
|
37 #ifndef _DEBUG |
|
38 static const unsigned int kMaxVectorSize = 0xF0000000; |
|
39 if(newCapacity < _size || |
|
40 ((unsigned int )newCapacity * (unsigned int )_itemSize) > kMaxVectorSize) |
|
41 throw 1052354; |
|
42 #endif |
|
43 */ |
|
44 unsigned char *p = new unsigned char[newCapacity * _itemSize]; |
|
45 int numRecordsToMove = _capacity; |
|
46 memmove(p, _items, _itemSize * numRecordsToMove); |
|
47 delete [](unsigned char *)_items; |
|
48 _items = p; |
|
49 _capacity = newCapacity; |
|
50 } |
|
51 |
|
52 void CBaseRecordVector::MoveItems(int destIndex, int srcIndex) |
|
53 { |
|
54 memmove(((unsigned char *)_items) + destIndex * _itemSize, |
|
55 ((unsigned char *)_items) + srcIndex * _itemSize, |
|
56 _itemSize * (_size - srcIndex)); |
|
57 } |
|
58 |
|
59 void CBaseRecordVector::InsertOneItem(int index) |
|
60 { |
|
61 ReserveOnePosition(); |
|
62 MoveItems(index + 1, index); |
|
63 _size++; |
|
64 } |
|
65 |
|
66 void CBaseRecordVector::Delete(int index, int num) |
|
67 { |
|
68 TestIndexAndCorrectNum(index, num); |
|
69 if (num > 0) |
|
70 { |
|
71 MoveItems(index, index + num); |
|
72 _size -= num; |
|
73 } |
|
74 } |