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

Generated on Thu Jan 24 16:50:57 2008 for WvStreams by  doxygen 1.5.4