00001
00002
00003
00004
00005 #include "StdAfx.h"
00006
00007 #include <string.h>
00008
00009 #include "Vector.h"
00010
00011 CBaseRecordVector::~CBaseRecordVector()
00012 { delete []((unsigned char *)_items); }
00013 void CBaseRecordVector::Clear()
00014 { DeleteFrom(0); }
00015 void CBaseRecordVector::DeleteBack()
00016 { Delete(_size - 1); }
00017 void CBaseRecordVector::DeleteFrom(int index)
00018 { Delete(index, _size - index); }
00019
00020 void CBaseRecordVector::ReserveOnePosition()
00021 {
00022 if(_size != _capacity)
00023 return;
00024 int delta;
00025 if (_capacity > 64)
00026 delta = _capacity / 2;
00027 else if (_capacity > 8)
00028 delta = 8;
00029 else
00030 delta = 4;
00031 Reserve(_capacity + delta);
00032 }
00033
00034 void CBaseRecordVector::Reserve(int newCapacity)
00035 {
00036 if(newCapacity <= _capacity)
00037 return;
00038
00039 unsigned char *p = new unsigned char[newCapacity * _itemSize];
00040 int numRecordsToMove = _capacity;
00041 memmove(p, _items, _itemSize * numRecordsToMove);
00042 delete [](unsigned char *)_items;
00043 _items = p;
00044 _capacity = newCapacity;
00045 }
00046
00047 void CBaseRecordVector::MoveItems(int destIndex, int srcIndex)
00048 {
00049 memmove(((unsigned char *)_items) + destIndex * _itemSize,
00050 ((unsigned char *)_items) + srcIndex * _itemSize,
00051 _itemSize * (_size - srcIndex));
00052 }
00053
00054 void CBaseRecordVector::InsertOneItem(int index)
00055 {
00056 ReserveOnePosition();
00057 MoveItems(index + 1, index);
00058 _size++;
00059 }
00060
00061 void CBaseRecordVector::Delete(int index, int num)
00062 {
00063 TestIndexAndCorrectNum(index, num);
00064 if (num > 0)
00065 {
00066 MoveItems(index, index + num);
00067 _size -= num;
00068 }
00069 }