Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

ResizeArray.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: ResizeArray.h,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.41 $       $Date: 2008/03/27 19:36:45 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  * Automatically-adjusting single-dim array template.
00019  *
00020  ***************************************************************************/
00021 #ifndef RESIZEARRAY_TEMPLATE_H
00022 #define RESIZEARRAY_TEMPLATE_H
00023 
00024 #include <string.h>
00025 
00035 template<class T>
00036 class ResizeArray {
00037 private:
00038   T *allocate(size_t n) { return new T[n]; }
00039   void deallocate(T *p) { delete [] p; }
00040 
00041   T *data;      
00042   int sz;       
00043   int currSize; 
00044 
00045 public:
00050   ResizeArray(int s = 3) {
00051     currSize = 0;
00052     sz = (s > 0 ? s : 10);
00053     data = allocate(sz); 
00054   }
00055 
00056   ~ResizeArray() {
00057     deallocate(data);
00058   }
00059   
00060   int num(void) const { return currSize; } 
00061   T& operator[](int N) { return data[N]; } 
00062   T const& operator[](int N) const { return data[N]; } 
00063 
00065   void append(const T& val) {
00066     if (currSize == sz) {    // extend size of array if necessary
00067       int newsize = (int)((float)sz * 1.3f);
00068 
00069       // guarantee minimum required size increase, since the scaled value
00070       // may truncate back to the original size value when the initial number
00071       // of elements is very small.
00072       if (newsize == sz)
00073         newsize++;
00074 
00075       // shallow copy the data to a newly allocated block since we can't
00076       // do something better like realloc()
00077       T *newdata = allocate(newsize); 
00078       memcpy(newdata, data, currSize * sizeof(T));
00079       deallocate(data); 
00080     
00081       // save new values
00082       data = newdata;
00083       sz = newsize;
00084     }
00085     data[currSize++] = val;
00086   }
00087 
00089   void remove(int n) {
00090     if (n < 0 || n >= currSize) return;
00091     for (int i=n; i<currSize-1; i++)
00092       data[i] = data[i+1];
00093     currSize--;
00094   }
00095 
00097   void clear() {
00098     currSize = 0;
00099   }
00100 
00102   void truncatelastn(int N) {
00103     currSize -= N;
00104     if (currSize < 0) 
00105       currSize=0;
00106   }
00107 
00110   int find(const T& val) {
00111     int i;
00112   
00113     for(i=0; i < currSize; i++) {
00114       if(data[i] == val) 
00115         return i;
00116     }
00117   
00118     return -1;
00119   }
00120 };
00121 
00122 #endif
00123 

Generated on Fri Aug 29 01:27:18 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002