wvvector.cc

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 WvVectorBase::comparison_type_t WvVectorBase::innercomparator;
00011 
00012 WvVectorBase::WvVectorBase(int slots)
00013     : xseq(NULL), xcount(0), xslots(0)
00014 {
00015     innercomparator = NULL;
00016     set_capacity(slots);
00017 }
00018 
00019 void WvVectorBase::remove(int slot)
00020 {
00021     --xcount;
00022     if (xcount - slot)
00023         memmove(xseq + slot, xseq + slot + 1,
00024                 (xcount - slot) * sizeof(WvLink *));
00025 }
00026 
00027 void WvVectorBase::insert(int slot, WvLink *elem)
00028 {
00029     if (++xcount > xslots)
00030     {
00031         xslots *= 2;
00032         set_capacity(xslots);
00033     }
00034     memmove(xseq + slot + 1, xseq + slot,
00035             (xcount - slot - 1) * sizeof(WvLink *));
00036     xseq[slot] = elem;
00037 }
00038 
00039 void WvVectorBase::append(WvLink *elem)
00040 {
00041     if (++xcount > xslots)
00042     {
00043         xslots *= 2;
00044         set_capacity(xslots);
00045     }
00046     xseq[xcount - 1] = elem;
00047 }
00048 
00049 void WvVectorBase::set_capacity(int newslots)
00050 {
00051     // Ensure we don't eliminate data when we shrink
00052     if (newslots < xcount)
00053         newslots = xcount;
00054 
00055     // Free the memory if we don't want any.
00056     if (newslots <= 0)
00057     {
00058         xslots = 0;
00059         free(xseq);
00060         xseq = NULL;
00061         return;
00062     }
00063 
00064     // Allocate memory, if we want it
00065     xslots = newslots;
00066     void *newseq = realloc(xseq, xslots * sizeof(WvLink *));
00067     assert(newseq != NULL || xslots == 0);
00068     if (newseq != NULL || xslots == 0)
00069         xseq = static_cast<WvLink **>(newseq);
00070 }
00071 
00072 WvLink *WvVectorBase::IterBase::find(const void *data)
00073 {
00074     for (rewind(); next(); )
00075     {
00076         if (link->data == data)
00077             break;
00078     }
00079     return link;
00080 }
00081 
00082 WvLink *WvVectorBase::IterBase::find_next(const void *data)
00083 {
00084     if (link)
00085     {
00086         if (link->data == data)
00087             return link;
00088 
00089         for (; next(); )
00090         {
00091             if (link->data == data)
00092                 break;
00093         }
00094     }
00095     return link;
00096 }

Generated on Wed Jul 12 17:53:22 2006 for WvStreams by  doxygen 1.4.7