1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*
* Provides a dynamic array data structure.
*/
#include "wvvector.h"
#include <assert.h>
WvVectorBase::WvVectorBase(bool _auto_free)
{
xseq = NULL;
xcount = xslots = 0;
auto_free = _auto_free;
}
int WvVectorBase::growcapacity(int minslots)
{
int newslots = xslots != 0 || minslots == 0 ?
xslots : MINALLOC;
while (newslots < minslots)
newslots *= 2;
return newslots;
}
int WvVectorBase::shrinkcapacity(int maxslots)
{
maxslots *= 2;
int newslots = xslots;
while (newslots > maxslots)
newslots /= 2;
return newslots;
}
void WvVectorBase::remove(int slot)
{
xcount--;
moveelems(xseq + slot, xseq + slot + 1, xcount - slot);
setcapacity(shrinkcapacity(xcount));
}
void WvVectorBase::insert(int slot, void *elem)
{
setcapacity(growcapacity(xcount + 1));
moveelems(xseq + slot + 1, xseq + slot, xcount - slot);
xseq[slot] = elem;
xcount++;
}
void WvVectorBase::append(void *elem)
{
setcapacity(growcapacity(xcount + 1));
xseq[xcount] = elem;
xcount++;
}
void WvVectorBase::setcapacity(int newslots)
{
if (newslots == xslots)
return;
assert(newslots >= xcount);
if (newslots < xcount)
xcount = newslots;
void **oldseq = xseq;
xslots = newslots;
if (newslots != 0)
{
xseq = new void *[newslots];
moveelems(xseq, oldseq, xcount);
}
else
xseq = NULL;
deletev oldseq;
}
|