other-licenses/7zstub/src/Common/Vector.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial