00001 /* 00002 * Worldvisions Weaver Software: 00003 * Copyright (C) 1997-2002 Net Integration Technologies, Inc. 00004 * 00005 * Provides a dynamic array data structure. 00006 */ 00007 #include "wvvector.h" 00008 #include <assert.h> 00009 00010 00011 WvVectorBase::WvVectorBase(bool _auto_free) 00012 { 00013 xseq = NULL; 00014 xcount = xslots = 0; 00015 auto_free = _auto_free; 00016 } 00017 00018 00019 int WvVectorBase::growcapacity(int minslots) 00020 { 00021 int newslots = xslots != 0 || minslots == 0 ? 00022 xslots : MINALLOC; 00023 while (newslots < minslots) 00024 newslots *= 2; 00025 return newslots; 00026 } 00027 00028 00029 int WvVectorBase::shrinkcapacity(int maxslots) 00030 { 00031 maxslots *= 2; 00032 int newslots = xslots; 00033 while (newslots > maxslots) 00034 newslots /= 2; 00035 return newslots; 00036 } 00037 00038 00039 void WvVectorBase::remove(int slot) 00040 { 00041 xcount--; 00042 moveelems(xseq + slot, xseq + slot + 1, xcount - slot); 00043 setcapacity(shrinkcapacity(xcount)); 00044 } 00045 00046 00047 void WvVectorBase::insert(int slot, void *elem) 00048 { 00049 setcapacity(growcapacity(xcount + 1)); 00050 moveelems(xseq + slot + 1, xseq + slot, xcount - slot); 00051 xseq[slot] = elem; 00052 xcount++; 00053 } 00054 00055 00056 void WvVectorBase::append(void *elem) 00057 { 00058 setcapacity(growcapacity(xcount + 1)); 00059 xseq[xcount] = elem; 00060 xcount++; 00061 } 00062 00063 00064 void WvVectorBase::setcapacity(int newslots) 00065 { 00066 if (newslots == xslots) 00067 return; 00068 00069 assert(newslots >= xcount); 00070 if (newslots < xcount) 00071 xcount = newslots; 00072 void **oldseq = xseq; 00073 xslots = newslots; 00074 if (newslots != 0) 00075 { 00076 xseq = new void *[newslots]; 00077 moveelems(xseq, oldseq, xcount); 00078 } 00079 else 00080 xseq = NULL; 00081 delete[] oldseq; 00082 }