Main Page   Namespace List   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-2011 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.45 $       $Date: 2010/12/16 04:08:38 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Automatically-adjusting single-dim array template.
00019  * 
00020  * LICENSE:
00021  *   UIUC Open Source License
00022  *   http://www.ks.uiuc.edu/Research/vmd/plugins/pluginlicense.html
00023  *
00024  ***************************************************************************/
00025 #ifndef RESIZEARRAY_TEMPLATE_H
00026 #define RESIZEARRAY_TEMPLATE_H
00027 
00028 #include <string.h>
00029 
00039 template<class T>
00040 class ResizeArray {
00041 private:
00042   T *allocate(size_t n) { return new T[n]; }
00043   void deallocate(T *p) { delete [] p; }
00044 
00045   T *data;      
00046   int sz;       
00047   int currSize; 
00048 
00049 public:
00054   ResizeArray(int s = 3) {
00055     currSize = 0;
00056     sz = (s > 0 ? s : 10);
00057     data = allocate(sz); 
00058   }
00059 
00060   ~ResizeArray() {
00061     deallocate(data);
00062   }
00063   
00064   int num(void) const { return currSize; } 
00065   T& operator[](int N) { return data[N]; } 
00066   T const& operator[](int N) const { return data[N]; } 
00067 
00069   void append(const T& val) {
00070     if (currSize == sz) {    // extend size of array if necessary
00071       int newsize = (int)((float)sz * 1.3f);
00072 
00073       // guarantee minimum required size increase, since the scaled value
00074       // may truncate back to the original size value when the initial number
00075       // of elements is very small.
00076       if (newsize == sz)
00077         newsize++;
00078 
00079       // shallow copy the data to a newly allocated block since we can't
00080       // do something better like realloc()
00081       T *newdata = allocate(newsize); 
00082       memcpy(newdata, data, currSize * sizeof(T));
00083       deallocate(data); 
00084     
00085       // save new values
00086       data = newdata;
00087       sz = newsize;
00088     }
00089     data[currSize++] = val;
00090   }
00091 
00093   void remove(int n) {
00094     if (n < 0 || n >= currSize) return;
00095     for (int i=n; i<currSize-1; i++)
00096       data[i] = data[i+1];
00097     currSize--;
00098   }
00099 
00101   T& pop() {
00102     currSize--;
00103     return data[currSize]; 
00104   }
00105 
00107   void clear() {
00108     currSize = 0;
00109   }
00110 
00112   void truncatelastn(int N) {
00113     currSize -= N;
00114     if (currSize < 0) 
00115       currSize=0;
00116   }
00117 
00120   int find(const T& val) {
00121     int i;
00122   
00123     for(i=0; i < currSize; i++) {
00124       if(data[i] == val) 
00125         return i;
00126     }
00127   
00128     return -1;
00129   }
00130 };
00131 
00132 #endif
00133 

Generated on Sat May 18 01:47:41 2013 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002