MyVector.cpp revision baa3858d3f5d128a5c8466b700098109edcad5f2
1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Common/MyVector.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include <string.h>
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "MyVector.h"
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
9baa3858d3f5d128a5c8466b700098109edcad5f2repo syncCBaseRecordVector::~CBaseRecordVector() { ClearAndFree(); }
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
11baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::ClearAndFree()
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Clear();
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  delete []((unsigned char *)_items);
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _capacity = 0;
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _size = 0;
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _items = 0;
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
20baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::Clear() { DeleteFrom(0); }
21baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::DeleteBack() { Delete(_size - 1); }
22baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::DeleteFrom(int index) { Delete(index, _size - index); }
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
24baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::ReserveOnePosition()
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (_size != _capacity)
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return;
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  unsigned delta = 1;
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (_capacity >= 64)
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    delta = (unsigned)_capacity / 4;
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  else if (_capacity >= 8)
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    delta = 8;
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Reserve(_capacity + (int)delta);
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
36baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::Reserve(int newCapacity)
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  // if (newCapacity <= _capacity)
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (newCapacity == _capacity)
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return;
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if ((unsigned)newCapacity >= ((unsigned)1 << (sizeof(unsigned) * 8 - 1)))
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    throw 1052353;
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  size_t newSize = (size_t)(unsigned)newCapacity * _itemSize;
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (newSize / _itemSize != (size_t)(unsigned)newCapacity)
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    throw 1052354;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  unsigned char *p = NULL;
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (newSize > 0)
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    p = new unsigned char[newSize];
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (p == 0)
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      throw 1052355;
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    int numRecordsToMove = (_size < newCapacity ? _size : newCapacity);
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    memcpy(p, _items, _itemSize * numRecordsToMove);
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  delete [](unsigned char *)_items;
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _items = p;
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _capacity = newCapacity;
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
60baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::ReserveDown()
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  Reserve(_size);
63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
65baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::MoveItems(int destIndex, int srcIndex)
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  memmove(((unsigned char *)_items) + destIndex * _itemSize,
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    ((unsigned char  *)_items) + srcIndex * _itemSize,
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _itemSize * (_size - srcIndex));
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
72baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::InsertOneItem(int index)
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ReserveOnePosition();
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  MoveItems(index + 1, index);
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  _size++;
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
79baa3858d3f5d128a5c8466b700098109edcad5f2repo syncvoid CBaseRecordVector::Delete(int index, int num)
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  TestIndexAndCorrectNum(index, num);
82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (num > 0)
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    MoveItems(index, index + num);
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    _size -= num;
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
88