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