Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

wvvector.h

00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  * 
00005  * Provides an auto-resizing array data structure.
00006  */
00007 #ifndef __WVVECTOR_H
00008 #define __WVVECTOR_H
00009 
00010 #include "wvxplc.h"
00011 #include "wvlink.h"
00012 #include <string.h>
00013 
00018 class WvVectorBase
00019 {
00020 protected:
00021     static const int MINALLOC = 4;
00024     void **xseq; 
00025     int xcount; 
00026     int xslots; 
00027     bool auto_free; 
00030     WvVectorBase(bool _auto_free);
00031 
00033     int growcapacity(int minslots);
00034     
00036     int shrinkcapacity(int maxslots);
00037     
00039     void moveelems(void *dst, void *src, int nelems)
00040         { memmove(dst, src, nelems * sizeof(void *)); }
00041 
00043     void remove(int slot);
00044     
00046     void insert(int slot, void *elem);
00047 
00049     void append(void *elem);
00050     
00051 public:
00053     int count() const
00054         { return xcount; }
00055 
00057     bool isempty() const
00058         { return xcount == 0; }
00059 
00061     int capacity() const
00062         { return xslots; }
00063     
00070     void setcapacity(int newslots);
00071 
00073     void compact()
00074         { setcapacity(count()); }
00075 };
00076 
00077 
00083 template<class T>
00084 class WvVector : public WvVectorBase
00085 {
00086 public:
00088     WvVector(bool _auto_free) : WvVectorBase(_auto_free)
00089         { }
00090 
00092     ~WvVector()
00093         { zap(); }
00094 
00096     T *operator[] (int slot)
00097         { return ptr()[slot]; }
00098 
00100     void zap()
00101     { 
00102         // guard against potential side-effects
00103         T **oldarray = ptr();
00104         int oldcount = xcount;
00105         xcount = 0;
00106         xslots = 0;
00107         xseq = NULL;
00108         if (auto_free)
00109         {
00110             while (oldcount > 0)
00111                 delete oldarray[--oldcount];
00112         }
00113         deletev oldarray;
00114     }
00115 
00116     void remove(int slot, bool never_delete = false)
00117     {
00118         T *obj = (*this)[slot];
00119         WvVectorBase::remove(slot);
00120         if (auto_free && !never_delete)
00121             delete obj;
00122     }
00123     
00125     void remove_last()
00126         { if (xcount) { remove(xcount-1); } }
00127         
00128     T *last()
00129         { return xcount ? (*this)[xcount-1] : NULL; }
00130     
00131     void insert(int slot, T *elem)
00132         { WvVectorBase::insert(slot, elem); }
00133 
00134     void append(T *elem)
00135         { WvVectorBase::append(elem); }
00136     
00137     // FIXME: I'd rather not give public access to this, since it's dangerous!
00138     T **ptr() 
00139         { return reinterpret_cast<T **>(xseq); }
00140     
00141     
00143     class Iter
00144     {
00145         WvVector<T> *list;
00146         int count;
00147         
00148     protected:
00150         Iter(WvVector<T> *_list) : list(_list)
00151             { count = -1; }
00152 
00153     public:
00154         Iter(WvVector<T> &_list) : list(&_list)
00155             { count = -1; }
00156         
00157         void rewind()
00158             { count = -1; }
00159         bool next()
00160             { count++; return cur(); }
00161         bool cur()
00162             { return list && count >= 0 && count < list->count(); }
00163         
00164         T *ptr() const
00165             { return (*list)[count]; }
00166         
00167         WvIterStuff(T);
00168     };
00169 };
00170 
00171 #endif // __WVVECTOR_H

Generated on Sun Jul 10 16:27:06 2005 for WvStreams by  doxygen 1.4.0