Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

ResizeArray.h

Go to the documentation of this file.
00001 
00007 /*
00008    ResizeArray template
00009    Uses simple contingous array allocation in a hidden manner
00010    so that array object can have items added without limit
00011    Suffers from memory fragmentation during resizing
00012    Fast access, safe and efficient passing of encapsulated array thru
00013    function arguments.
00014 */
00015 
00016 #ifndef RESIZEARRAY_H
00017 #define RESIZEARRAY_H
00018 
00019 #include "ResizeArrayRaw.h"
00020 
00021 // Need this juju to use templated friend below
00022 template <class Type> class ResizeArrayIter;
00023 template <class Type> class ResizeArrayRaw;
00024 
00025 template <class Elem> class ResizeArray {
00026   friend class ResizeArrayIter<Elem>;
00027 
00028   protected:
00029     ResizeArrayRaw<Elem> *rep;
00030 
00031   public:
00032     // STL style iterators
00033     typedef Elem* iterator;
00034     iterator begin(void) { return rep->array; }
00035     iterator end(void) { return rep->array + rep->arraySize; }
00036     typedef const Elem* const_iterator;
00037     const_iterator const_begin(void) const { return rep->array; }
00038     const_iterator const_end(void) const { return rep->array + rep->arraySize; }
00039 
00040     // Various Constructors
00041     ResizeArray(void) {
00042       rep = new ResizeArrayRaw<Elem>();
00043       rep->resize(0);
00044       rep->refCount = 1;
00045     }
00046 
00047     // Constructor make ResizeArray of predefined size
00048     ResizeArray(int s) {
00049       rep = new ResizeArrayRaw<Elem>();
00050       rep->resize(s);
00051       rep->refCount = 1;
00052     }
00053 
00054     // Contructor makes ResizeArray which points to same ResizeArrayRaw
00055     ResizeArray(ResizeArray<Elem> &ra) {
00056       rep = ra.rep;
00057       rep->refCount++;
00058     }
00059 
00060     // Constructor makes a copy of ResizeArrayRaw
00061     ResizeArray(const ResizeArray<Elem>* ra) {
00062       rep = new ResizeArrayRaw<Elem>(*(ra->rep));
00063       rep->refCount = 1;
00064     }
00065 
00066     // Constructor to take-in pre-existing array
00067     ResizeArray(Elem * * array, int arraySize, int allocSize=0) {
00068       rep = new ResizeArrayRaw<Elem>(array, arraySize, allocSize);
00069       rep->refCount = 1;
00070     }
00071 
00072     virtual ~ResizeArray(void) {
00073       if (!--rep->refCount) delete rep;
00074     }
00075 
00076     // We copy reference to ResizeArrayRaw
00077     ResizeArray<Elem> & operator= (ResizeArray<Elem> &ra) {
00078       if (rep != NULL && !(--rep->refCount) )
00079         delete rep;
00080       rep = ra.rep;
00081       rep->refCount++;
00082       return (*this);
00083     }
00084 
00085     // Set the initial allocation size to s with growth factor as growthFactor
00086     void setParams(int s, float growthFactor) {      
00087       rep->setResizeParams(s, growthFactor);
00088     }
00089 
00090 
00091     // If array is expanded - new elements are default constructed
00092     // if array is reduced, removed elements have ~Elem() run
00093     void resize(int i) { rep->resize(i); }
00094 
00095     // Set all elements to a given value (like 0).
00096     void setall(const Elem &elem) {
00097       iterator i = begin();
00098       iterator e = end();
00099       for ( ; i != e; ++i ) *i = elem;
00100     }
00101   
00102     // Add element to end of array
00103     int add (const Elem &elem) {
00104       int end=rep->size();
00105       rep->ins(elem, end);
00106       return(end);
00107     }
00108   
00109     // delete num elements from current index
00110     int del(int index, int num=1) {
00111       return(rep->del(index,num));
00112     }
00113 
00114     // insert element at index
00115     int insert (const Elem& elem, int index) {
00116       rep->ins(elem,index);
00117       return (index);
00118     }
00119 
00120     // array member access (can be lvalue) that grows array.
00121     inline Elem & item(int i) {
00122       i = ( i < 0 ? 0 : i );
00123       if ((i+1) > size())
00124           resize(i+1);
00125       return rep->array[i];
00126     }
00127 
00128     // array member access (can be lvalue) no checks.
00129     inline Elem & operator[](int index) { return rep->array[index]; }
00130     inline const Elem & operator[](int index) const { return rep->array[index]; }
00131 
00132     // returns size of ResizeArray
00133     inline int size(void) const { return rep->size(); }
00134 
00135     // reduce storage size
00136     void reduce(void) { rep->reduce(); }
00137 
00138     inline int find(const Elem &e) const { return rep->find(e); }
00139 
00140         // Difference with resize(0): 
00141         // This function will free the space occupied by rep,
00142         // while resize(0) will not.    
00143         void clear() { 
00144                 if (!--rep->refCount){
00145                         delete rep;
00146                         rep = new ResizeArrayRaw<Elem>();
00147                         rep->resize(0);
00148                         rep->refCount = 1; 
00149                 }
00150         }
00151 };
00152 
00153 #endif

Generated on Sun Feb 12 04:07:56 2012 for NAMD by  doxygen 1.3.9.1