annotate src/win32/7zip/7z/CPP/Common/MyVector.cpp @ 1:f9f4f1b99eed

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