blob: def2a581c1236037a89412f115dec3c97d1541e2 (
plain)
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
|
// Common/MyVector.cpp
#include "StdAfx.h"
#include <string.h>
#include "MyVector.h"
CBaseRecordVector::~CBaseRecordVector() { Free(); }
void CBaseRecordVector::Free()
{
delete []((unsigned char *)_items);
_capacity = 0;
_size = 0;
_items = 0;
}
void CBaseRecordVector::Clear() { DeleteFrom(0); }
void CBaseRecordVector::DeleteBack() { Delete(_size - 1); }
void CBaseRecordVector::DeleteFrom(int index) { Delete(index, _size - index); }
void CBaseRecordVector::ReserveOnePosition()
{
if (_size != _capacity)
return;
int delta;
if (_capacity > 64)
delta = _capacity / 2;
else if (_capacity > 8)
delta = 8;
else
delta = 4;
Reserve(_capacity + delta);
}
void CBaseRecordVector::Reserve(int newCapacity)
{
if (newCapacity <= _capacity)
return;
if ((unsigned)newCapacity >= ((unsigned)1 << (sizeof(unsigned) * 8 - 1)))
throw 1052353;
size_t newSize = (size_t)(unsigned)newCapacity * _itemSize;
if (newSize / _itemSize != (size_t)(unsigned)newCapacity)
throw 1052354;
unsigned char *p = new unsigned char[newSize];
if (p == 0)
throw 1052355;
int numRecordsToMove = _capacity;
memmove(p, _items, _itemSize * numRecordsToMove);
delete [](unsigned char *)_items;
_items = p;
_capacity = newCapacity;
}
void CBaseRecordVector::MoveItems(int destIndex, int srcIndex)
{
memmove(((unsigned char *)_items) + destIndex * _itemSize,
((unsigned char *)_items) + srcIndex * _itemSize,
_itemSize * (_size - srcIndex));
}
void CBaseRecordVector::InsertOneItem(int index)
{
ReserveOnePosition();
MoveItems(index + 1, index);
_size++;
}
void CBaseRecordVector::Delete(int index, int num)
{
TestIndexAndCorrectNum(index, num);
if (num > 0)
{
MoveItems(index, index + num);
_size -= num;
}
}
|