ptrvec.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003     FILE: ptrvec.h
00004 
00005     DESCRIPTION:  An variable length array of pointers
00006 
00007     CREATED BY: Dan Silva
00008 
00009     HISTORY:
00010 
00011  *> Copyright (c) 1994, All Rights Reserved.
00012  **********************************************************************/
00013 
00014 #pragma once
00015 #include "maxheap.h"
00016 
00030 class PtrVector: public MaxHeapOperators {
00031     protected:
00032         int size;
00033         int nused;
00034         void** data;
00038         PtrVector() { size = nused = 0; data = NULL; }
00040         UtilExport ~PtrVector();
00045         UtilExport PtrVector(const PtrVector& v);
00050         UtilExport PtrVector&   operator=(const PtrVector& v);
00058         UtilExport void append(void * ptr , int extra);
00069         UtilExport void insertAt(void * ptr , int at, int extra);
00076         UtilExport void* remove(int i);
00080         UtilExport void* removeLast();
00085         void* operator[](int i) const { return data[i]; }       
00090         void*& operator[](int i) { return data[i]; }        
00091     public:
00096         UtilExport void reshape(int i);  // sets capacity
00102         UtilExport void setLength(int i);  // sets length, capacity if necessary
00105         UtilExport void clear();    // deletes the ptr array, but not the objects
00109         void shrink() { reshape(nused); }
00113         int length() const { return nused; }
00117         int capacity() const { return size; }
00118     };
00119 
00126 template <class T> class PtrVec: public PtrVector
00127 {
00128 public: 
00132     PtrVec():PtrVector() {}
00136     T* operator[](int i) const { return (T*)PtrVector::operator[](i); }     
00140     T*& operator[](int i) { return (T*&)PtrVector::operator[](i); }             
00144     PtrVec<T>& operator=(const PtrVec<T>& v) { return (PtrVec<T>&)PtrVector::operator=(v); }
00153     void append(T *ptr, int extra = 10) { PtrVector::append(ptr,extra); }   
00165     void insertAt(T* ptr, int at, int extra=10) { PtrVector::insertAt(ptr,at,extra); }   
00172     T* remove(int i) { return (T *)PtrVector::remove(i); }      
00177     T* removeLast() { return (T *)PtrVector::removeLast(); }        
00179     void deleteAll();  //  deletes all the objects      
00180     };
00181 
00182 template <class T>  void PtrVec<T>::deleteAll()
00183 {
00184     while (length()) {
00185         T* p = removeLast();
00186         delete p;
00187         p = NULL;
00188     }
00189 }
00190