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