diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/Common/MyVector.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,90 @@
     1.4 +// Common/MyVector.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include <string.h>
     1.9 +
    1.10 +#include "MyVector.h"
    1.11 +
    1.12 +CBaseRecordVector::~CBaseRecordVector() { ClearAndFree(); }
    1.13 +
    1.14 +void CBaseRecordVector::ClearAndFree()
    1.15 +{
    1.16 +  Clear();
    1.17 +  delete []((unsigned char *)_items);
    1.18 +  _capacity = 0;
    1.19 +  _size = 0;
    1.20 +  _items = 0;
    1.21 +}
    1.22 +
    1.23 +void CBaseRecordVector::Clear() { DeleteFrom(0); }
    1.24 +void CBaseRecordVector::DeleteBack() { Delete(_size - 1); }
    1.25 +void CBaseRecordVector::DeleteFrom(int index)
    1.26 +{
    1.27 +	Delete(index, _size - index);
    1.28 +}
    1.29 +
    1.30 +void CBaseRecordVector::ReserveOnePosition()
    1.31 +{
    1.32 +  if (_size != _capacity)
    1.33 +    return;
    1.34 +  int delta = 1;
    1.35 +  if (_capacity >= 64)
    1.36 +    delta = _capacity / 4;
    1.37 +  else if (_capacity >= 8)
    1.38 +    delta = 8;
    1.39 +  Reserve(_capacity + delta);
    1.40 +}
    1.41 +
    1.42 +void CBaseRecordVector::Reserve(int newCapacity)
    1.43 +{
    1.44 +  // if (newCapacity <= _capacity)
    1.45 +  if (newCapacity == _capacity)
    1.46 +    return;
    1.47 +  if ((unsigned)newCapacity >= ((unsigned)1 << (sizeof(unsigned) * 8 - 1)))
    1.48 +    throw 1052353;
    1.49 +  size_t newSize = (size_t)(unsigned)newCapacity * _itemSize;
    1.50 +  if (newSize / _itemSize != (size_t)(unsigned)newCapacity)
    1.51 +    throw 1052354;
    1.52 +  unsigned char *p = NULL;
    1.53 +  if (newSize > 0)
    1.54 +  {
    1.55 +    p = new unsigned char[newSize];
    1.56 +    if (p == 0)
    1.57 +      throw 1052355;
    1.58 +    int numRecordsToMove = (_size < newCapacity ? _size : newCapacity);
    1.59 +    memcpy(p, _items, _itemSize * numRecordsToMove);
    1.60 +  }
    1.61 +  delete [](unsigned char *)_items;
    1.62 +  _items = p;
    1.63 +  _capacity = newCapacity;
    1.64 +}
    1.65 +
    1.66 +void CBaseRecordVector::ReserveDown()
    1.67 +{
    1.68 +  Reserve(_size);
    1.69 +}
    1.70 +
    1.71 +void CBaseRecordVector::MoveItems(int destIndex, int srcIndex)
    1.72 +{
    1.73 +  memmove(((unsigned char *)_items) + destIndex * _itemSize,
    1.74 +    ((unsigned char  *)_items) + srcIndex * _itemSize,
    1.75 +    _itemSize * (_size - srcIndex));
    1.76 +}
    1.77 +
    1.78 +void CBaseRecordVector::InsertOneItem(int index)
    1.79 +{
    1.80 +  ReserveOnePosition();
    1.81 +  MoveItems(index + 1, index);
    1.82 +  _size++;
    1.83 +}
    1.84 +
    1.85 +void CBaseRecordVector::Delete(int index, int num)
    1.86 +{
    1.87 +  TestIndexAndCorrectNum(index, num);
    1.88 +  if (num > 0)
    1.89 +  {
    1.90 +    MoveItems(index, index + num);
    1.91 +    _size -= num;
    1.92 +  }
    1.93 +}